Skip to content
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

Improved autofix of vue/order-in-components rule to understand "Nullish Coalescing". #1183

Merged
merged 1 commit into from Jun 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/rules/order-in-components.js
Expand Up @@ -64,7 +64,7 @@ function isComma (node) {
return node.type === 'Punctuator' && node.value === ','
}

const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**']
const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**'/* es2016 */]
const BITWISE_OPERATORS = ['&', '|', '^', '~', '<<', '>>', '>>>']
const COMPARISON_OPERATORS = ['==', '!=', '===', '!==', '>', '>=', '<', '<=']
const RELATIONAL_OPERATORS = ['in', 'instanceof']
Expand All @@ -74,7 +74,7 @@ const ALL_BINARY_OPERATORS = [].concat(
COMPARISON_OPERATORS,
RELATIONAL_OPERATORS
)
const LOGICAL_OPERATORS = ['&&', '||']
const LOGICAL_OPERATORS = ['&&', '||', '??'/* es2020 */]

/*
* Result `true` if the node is sure that there are no side effects
Expand All @@ -94,17 +94,15 @@ const LOGICAL_OPERATORS = ['&&', '||']
*/
function isNotSideEffectsNode (node, visitorKeys) {
let result = true
const noSideEffectsNodes = new Set()
let skipNode = false
traverseNodes(node, {
visitorKeys,
enterNode (node, parent) {
if (!result) {
enterNode (node) {
if (!result || skipNode) {
return
}

if (
// parent has no side effects
noSideEffectsNodes.has(parent) ||
// no side effects node
node.type === 'FunctionExpression' ||
node.type === 'Identifier' ||
Expand All @@ -113,7 +111,7 @@ function isNotSideEffectsNode (node, visitorKeys) {
node.type === 'ArrowFunctionExpression' ||
node.type === 'TemplateElement'
) {
noSideEffectsNodes.add(node)
skipNode = node
} else if (
node.type !== 'Property' &&
node.type !== 'ObjectExpression' &&
Expand All @@ -131,7 +129,11 @@ function isNotSideEffectsNode (node, visitorKeys) {
result = false
}
},
leaveNode () {}
leaveNode (node) {
if (skipNode === node) {
skipNode = null
}
}
})

return result
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/order-in-components.js
Expand Up @@ -10,7 +10,7 @@ const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester()

const parserOptions = {
ecmaVersion: 2018,
ecmaVersion: 2020,
sourceType: 'module'
}

Expand Down Expand Up @@ -751,6 +751,7 @@ ruleTester.run('order-in-components', rule, {
testConditional: a ? b : c,
testYield: function* () {},
testTemplate: \`a:\${a},b:\${b},c:\${c}.\`,
testNullish: a ?? b,
name: 'burger',
};
`,
Expand All @@ -768,11 +769,12 @@ ruleTester.run('order-in-components', rule, {
testConditional: a ? b : c,
testYield: function* () {},
testTemplate: \`a:\${a},b:\${b},c:\${c}.\`,
testNullish: a ?? b,
};
`,
errors: [{
message: 'The "name" property should be above the "data" property on line 3.',
line: 13
line: 14
}]
}
]
Expand Down