Skip to content

fix(stylistic): apply javascript formatting conventions #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ import globals from 'globals'
import pluginJs from '@eslint/js'
import eslintPlugin from 'eslint-plugin-eslint-plugin'
import mochaPlugin from 'eslint-plugin-mocha'
import stylistic from '@stylistic/eslint-plugin'

export default [
pluginJs.configs.recommended,
eslintPlugin.configs['flat/recommended'],
mochaPlugin.configs.recommended,
{ignores: ['test-project/']},
stylistic.configs.recommended,
{ ignores: ['test-project/'] },
{
languageOptions: {
globals: globals.node
globals: globals.node,
},
rules: {
'no-redeclare': 'off',
'@stylistic/arrow-parens': ['error', 'always'],
'@stylistic/indent': ['error', 2],
'@stylistic/comma-dangle': ['error', 'always-multiline'],
'@stylistic/quotes': ['error', 'single'],
'@stylistic/semi': ['error', 'never'],
'eslint-plugin/require-meta-docs-url':
['error', { 'pattern': 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/{{name}}.md' }],
['error', { pattern: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/{{name}}.md' }],
'eslint-plugin/require-meta-docs-description': 'error',
'eslint-plugin/meta-property-ordering': 'error',
'eslint-plugin/test-case-property-ordering': 'error',
'mocha/no-mocha-arrows': 'off',
'mocha/no-setup-in-describe': 'off'
}
'mocha/no-setup-in-describe': 'off',
},
},
]
16 changes: 8 additions & 8 deletions lib/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const plugin = {
},
}

const commonGlobals =
Object.assign({
const commonGlobals
= Object.assign({
cy: false,
Cypress: false,
expect: false,
Expand All @@ -33,20 +33,20 @@ Object.assign(plugin.configs, {
globals: {
name: 'cypress/globals',
plugins: {
cypress: plugin
cypress: plugin,
},
languageOptions: {
globals:
commonGlobals,
}
}
},
},
})

Object.assign(plugin.configs, {
recommended: {
name: 'cypress/recommended',
plugins: {
cypress: plugin
cypress: plugin,
},
rules: {
'cypress/no-assigning-return-values': 'error',
Expand All @@ -57,8 +57,8 @@ Object.assign(plugin.configs, {
languageOptions: {
globals:
commonGlobals,
}
}
},
},
})

module.exports = plugin
42 changes: 21 additions & 21 deletions lib/rules/assertion-before-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ module.exports = {
unexpected: 'Make an assertion on the page state before taking a screenshot',
},
},
create (context) {
create(context) {
return {
CallExpression (node) {
CallExpression(node) {
if (isCallingCyScreenshot(node) && !isPreviousAnAssertion(node)) {
context.report({ node, messageId: 'unexpected' })
}
Expand All @@ -39,12 +39,12 @@ module.exports = {
},
}

function isRootCypress (node) {
function isRootCypress(node) {
while (node.type === 'CallExpression') {
if (node.callee.type !== 'MemberExpression') return false

if (node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy') {
if (node.callee.object.type === 'Identifier'
&& node.callee.object.name === 'cy') {
return true
}

Expand All @@ -54,26 +54,26 @@ function isRootCypress (node) {
return false
}

function getPreviousInChain (node) {
return node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'CallExpression' &&
node.callee.object.callee.type === 'MemberExpression' &&
node.callee.object.callee.property.type === 'Identifier' &&
node.callee.object.callee.property.name
function getPreviousInChain(node) {
return node.type === 'CallExpression'
&& node.callee.type === 'MemberExpression'
&& node.callee.object.type === 'CallExpression'
&& node.callee.object.callee.type === 'MemberExpression'
&& node.callee.object.callee.property.type === 'Identifier'
&& node.callee.object.callee.property.name
}

function getCallExpressionCypressCommand (node) {
return isRootCypress(node) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name
function getCallExpressionCypressCommand(node) {
return isRootCypress(node)
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name
}

function isCallingCyScreenshot (node) {
function isCallingCyScreenshot(node) {
return getCallExpressionCypressCommand(node) === 'screenshot'
}

function getPreviousCypressCommand (node) {
function getPreviousCypressCommand(node) {
const previousInChain = getPreviousInChain(node)

if (previousInChain) {
Expand All @@ -97,15 +97,15 @@ function getPreviousCypressCommand (node) {

const previousStatement = body[index - 1]

if (previousStatement.type !== 'ExpressionStatement' ||
previousStatement.expression.type !== 'CallExpression') {
if (previousStatement.type !== 'ExpressionStatement'
|| previousStatement.expression.type !== 'CallExpression') {
return null
}

return getCallExpressionCypressCommand(previousStatement.expression)
}

function isPreviousAnAssertion (node) {
function isPreviousAnAssertion(node) {
const previousCypressCommand = getPreviousCypressCommand(node)

return assertionCommands.indexOf(previousCypressCommand) >= 0
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/no-assigning-return-values.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

// safely get nested object property
function get (obj, propertyString = '') {
function get(obj, propertyString = '') {
const properties = propertyString.split('.')

for (let i = 0; i < properties.length; i++) {
Expand Down Expand Up @@ -29,9 +29,9 @@ module.exports = {
unexpected: 'Do not assign the return value of a Cypress command',
},
},
create (context) {
create(context) {
return {
VariableDeclaration (node) {
VariableDeclaration(node) {
if (node.declarations.some(isCypressCommandDeclaration)) {
context.report({ node, messageId: 'unexpected' })
}
Expand All @@ -47,7 +47,7 @@ const allowedCommands = {
stub: true,
}

function isCypressCommandDeclaration (declarator) {
function isCypressCommandDeclaration(declarator) {
let object = get(declarator, 'init.callee.object')

if (!object) return
Expand Down
20 changes: 10 additions & 10 deletions lib/rules/no-async-before.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ module.exports = {
},
},

create (context) {
function isBeforeBlock (callExpressionNode) {
create(context) {
function isBeforeBlock(callExpressionNode) {
const { type, name } = callExpressionNode.callee

return type === 'Identifier'
&& name === 'before' || name === 'beforeEach'
&& (name === 'before' || name === 'beforeEach')
}

function isBeforeAsync (node) {
function isBeforeAsync(node) {
return node.arguments
&& node.arguments.length >= 2
&& node.arguments[1].async === true
&& node.arguments.length >= 2
&& node.arguments[1].async === true
}
const sourceCode = context.sourceCode ?? context.getSourceCode()

return {
Identifier (node) {
Identifier(node) {
if (node.name === 'cy' || node.name === 'Cypress') {
const ancestors = sourceCode.getAncestors
? sourceCode.getAncestors(node)
: context.getAncestors()
const asyncTestBlocks = ancestors
.filter((n) => n.type === 'CallExpression')
.filter(isBeforeBlock)
.filter(isBeforeAsync)
.filter((n) => n.type === 'CallExpression')
.filter(isBeforeBlock)
.filter(isBeforeAsync)

if (asyncTestBlocks.length >= 1) {
asyncTestBlocks.forEach((node) => {
Expand Down
20 changes: 10 additions & 10 deletions lib/rules/no-async-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ module.exports = {
},
},

create (context) {
function isTestBlock (callExpressionNode) {
create(context) {
function isTestBlock(callExpressionNode) {
const { type, name } = callExpressionNode.callee

return type === 'Identifier'
&& name === 'it' || name === 'test'
&& (name === 'it' || name === 'test')
}

function isTestAsync (node) {
function isTestAsync(node) {
return node.arguments
&& node.arguments.length >= 2
&& node.arguments[1].async === true
&& node.arguments.length >= 2
&& node.arguments[1].async === true
}
const sourceCode = context.sourceCode ?? context.getSourceCode()

return {
Identifier (node) {
Identifier(node) {
if (node.name === 'cy' || node.name === 'Cypress') {
const ancestors = sourceCode.getAncestors
? sourceCode.getAncestors(node)
: context.getAncestors()
const asyncTestBlocks = ancestors
.filter((n) => n.type === 'CallExpression')
.filter(isTestBlock)
.filter(isTestAsync)
.filter((n) => n.type === 'CallExpression')
.filter(isTestBlock)
.filter(isTestAsync)

if (asyncTestBlocks.length >= 1) {
asyncTestBlocks.forEach((node) => {
Expand Down
64 changes: 32 additions & 32 deletions lib/rules/no-chained-get.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
'use strict'

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
Expand All @@ -23,65 +23,65 @@ module.exports = {
create(context) {
const isRootCypress = (node) => {
if (
node.type !== 'CallExpression' ||
node.callee.type !== 'MemberExpression'
node.type !== 'CallExpression'
|| node.callee.type !== 'MemberExpression'
) {
return false;
return false
}

if (
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy'
node.callee.object.type === 'Identifier'
&& node.callee.object.name === 'cy'
) {
return true;
return true
}

return isRootCypress(node.callee.object);
};
return isRootCypress(node.callee.object)
}

const hasChainedGet = (node) => {
// Check if this node is a get() call
const isGetCall =
node.callee &&
node.callee.type === 'MemberExpression' &&
node.callee.property &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'get';
const isGetCall
= node.callee
&& node.callee.type === 'MemberExpression'
&& node.callee.property
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'get'

if (!isGetCall) {
return false;
return false
}

const obj = node.callee.object;
const obj = node.callee.object

if (obj.type === 'CallExpression') {
const objCallee = obj.callee;
const objCallee = obj.callee

if (
objCallee &&
objCallee.type === 'MemberExpression' &&
objCallee.property &&
objCallee.property.type === 'Identifier' &&
objCallee.property.name === 'get'
objCallee
&& objCallee.type === 'MemberExpression'
&& objCallee.property
&& objCallee.property.type === 'Identifier'
&& objCallee.property.name === 'get'
) {
return true;
return true
}

return hasChainedGet(obj);
return hasChainedGet(obj)
}

return false;
};
return false
}

return {
CallExpression(node) {
if (isRootCypress(node) && hasChainedGet(node)) {
context.report({
node,
messageId: 'unexpected',
});
})
}
},
};
}
},
};
}
Loading