eslint 6.5.0 → 6.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +115 -0
- package/README.md +10 -9
- package/conf/config-schema.js +1 -0
- package/conf/default-cli-options.js +1 -1
- package/lib/cli-engine/cascading-config-array-factory.js +40 -14
- package/lib/cli-engine/cli-engine.js +49 -21
- package/lib/cli-engine/config-array/config-array.js +13 -4
- package/lib/cli-engine/config-array/config-dependency.js +2 -0
- package/lib/cli-engine/config-array/extracted-config.js +27 -0
- package/lib/cli-engine/config-array/ignore-pattern.js +231 -0
- package/lib/cli-engine/config-array/index.js +2 -0
- package/lib/cli-engine/config-array/override-tester.js +2 -0
- package/lib/cli-engine/config-array-factory.js +120 -2
- package/lib/cli-engine/file-enumerator.js +51 -30
- package/lib/cli-engine/formatters/html.js +1 -0
- package/lib/init/autoconfig.js +1 -11
- package/lib/init/config-file.js +0 -1
- package/lib/init/config-initializer.js +4 -4
- package/lib/init/config-rule.js +1 -5
- package/lib/init/npm-utils.js +0 -5
- package/lib/linter/code-path-analysis/code-path-analyzer.js +24 -38
- package/lib/linter/code-path-analysis/code-path-segment.js +17 -25
- package/lib/linter/code-path-analysis/code-path-state.js +40 -81
- package/lib/linter/code-path-analysis/code-path.js +10 -11
- package/lib/linter/code-path-analysis/debug-helpers.js +8 -12
- package/lib/linter/code-path-analysis/fork-context.js +23 -34
- package/lib/linter/code-path-analysis/id-generator.js +2 -2
- package/lib/linter/linter.js +121 -95
- package/lib/linter/node-event-generator.js +3 -2
- package/lib/linter/report-translator.js +73 -7
- package/lib/rule-tester/rule-tester.js +46 -14
- package/lib/rules/accessor-pairs.js +8 -8
- package/lib/rules/array-bracket-newline.js +12 -15
- package/lib/rules/array-bracket-spacing.js +12 -12
- package/lib/rules/array-callback-return.js +6 -11
- package/lib/rules/array-element-newline.js +5 -8
- package/lib/rules/arrow-parens.js +0 -1
- package/lib/rules/block-scoped-var.js +3 -3
- package/lib/rules/block-spacing.js +4 -4
- package/lib/rules/camelcase.js +19 -6
- package/lib/rules/capitalized-comments.js +0 -7
- package/lib/rules/class-methods-use-this.js +3 -3
- package/lib/rules/comma-dangle.js +20 -25
- package/lib/rules/comma-spacing.js +1 -1
- package/lib/rules/computed-property-spacing.js +14 -14
- package/lib/rules/consistent-return.js +4 -5
- package/lib/rules/consistent-this.js +5 -5
- package/lib/rules/constructor-super.js +14 -16
- package/lib/rules/curly.js +12 -9
- package/lib/rules/default-param-last.js +1 -0
- package/lib/rules/dot-location.js +11 -12
- package/lib/rules/func-names.js +6 -6
- package/lib/rules/function-call-argument-newline.js +8 -6
- package/lib/rules/generator-star-spacing.js +4 -9
- package/lib/rules/getter-return.js +4 -7
- package/lib/rules/grouped-accessor-pairs.js +224 -0
- package/lib/rules/indent.js +13 -2
- package/lib/rules/index.js +5 -0
- package/lib/rules/init-declarations.js +2 -2
- package/lib/rules/jsx-quotes.js +1 -1
- package/lib/rules/keyword-spacing.js +32 -56
- package/lib/rules/lines-around-directive.js +1 -1
- package/lib/rules/max-len.js +0 -5
- package/lib/rules/max-statements-per-line.js +3 -7
- package/lib/rules/multiline-comment-style.js +237 -106
- package/lib/rules/multiline-ternary.js +3 -3
- package/lib/rules/newline-after-var.js +6 -7
- package/lib/rules/newline-before-return.js +8 -9
- package/lib/rules/newline-per-chained-call.js +2 -4
- package/lib/rules/no-class-assign.js +2 -2
- package/lib/rules/no-compare-neg-zero.js +1 -2
- package/lib/rules/no-cond-assign.js +14 -4
- package/lib/rules/no-confusing-arrow.js +2 -2
- package/lib/rules/no-console.js +4 -8
- package/lib/rules/no-const-assign.js +1 -1
- package/lib/rules/no-constructor-return.js +62 -0
- package/lib/rules/no-dupe-args.js +1 -1
- package/lib/rules/no-dupe-class-members.js +3 -4
- package/lib/rules/no-dupe-else-if.js +122 -0
- package/lib/rules/no-dupe-keys.js +6 -5
- package/lib/rules/no-duplicate-imports.js +14 -18
- package/lib/rules/no-else-return.js +0 -8
- package/lib/rules/no-empty-function.js +2 -4
- package/lib/rules/no-eval.js +10 -18
- package/lib/rules/no-ex-assign.js +1 -1
- package/lib/rules/no-extra-bind.js +5 -12
- package/lib/rules/no-extra-boolean-cast.js +0 -2
- package/lib/rules/no-extra-label.js +4 -9
- package/lib/rules/no-extra-parens.js +17 -15
- package/lib/rules/no-extra-semi.js +5 -6
- package/lib/rules/no-fallthrough.js +6 -6
- package/lib/rules/no-func-assign.js +3 -3
- package/lib/rules/no-global-assign.js +4 -4
- package/lib/rules/no-implicit-coercion.js +10 -10
- package/lib/rules/no-implicit-globals.js +90 -8
- package/lib/rules/no-implied-eval.js +0 -1
- package/lib/rules/no-inline-comments.js +25 -11
- package/lib/rules/no-invalid-this.js +17 -5
- package/lib/rules/no-labels.js +3 -6
- package/lib/rules/no-lone-blocks.js +1 -1
- package/lib/rules/no-loop-func.js +6 -11
- package/lib/rules/no-magic-numbers.js +6 -6
- package/lib/rules/no-misleading-character-class.js +14 -7
- package/lib/rules/no-mixed-operators.js +13 -22
- package/lib/rules/no-mixed-requires.js +0 -1
- package/lib/rules/no-multi-spaces.js +1 -1
- package/lib/rules/no-native-reassign.js +4 -4
- package/lib/rules/no-octal-escape.js +1 -1
- package/lib/rules/no-param-reassign.js +28 -7
- package/lib/rules/no-redeclare.js +1 -1
- package/lib/rules/no-regex-spaces.js +0 -1
- package/lib/rules/no-restricted-imports.js +11 -11
- package/lib/rules/no-self-assign.js +12 -13
- package/lib/rules/no-sequences.js +3 -3
- package/lib/rules/no-setter-return.js +227 -0
- package/lib/rules/no-shadow.js +1 -4
- package/lib/rules/no-tabs.js +8 -2
- package/lib/rules/no-this-before-super.js +12 -13
- package/lib/rules/no-trailing-spaces.js +19 -7
- package/lib/rules/no-underscore-dangle.js +23 -4
- package/lib/rules/no-unmodified-loop-condition.js +16 -29
- package/lib/rules/no-unneeded-ternary.js +3 -3
- package/lib/rules/no-unreachable.js +7 -7
- package/lib/rules/no-unsafe-finally.js +4 -7
- package/lib/rules/no-unsafe-negation.js +32 -9
- package/lib/rules/no-unused-expressions.js +11 -7
- package/lib/rules/no-unused-labels.js +3 -6
- package/lib/rules/no-unused-vars.js +22 -29
- package/lib/rules/no-use-before-define.js +10 -15
- package/lib/rules/no-useless-call.js +4 -4
- package/lib/rules/no-useless-computed-key.js +60 -33
- package/lib/rules/no-useless-concat.js +4 -4
- package/lib/rules/no-useless-constructor.js +14 -22
- package/lib/rules/no-useless-escape.js +29 -8
- package/lib/rules/no-useless-rename.js +15 -7
- package/lib/rules/no-useless-return.js +8 -15
- package/lib/rules/no-var.js +12 -25
- package/lib/rules/no-warning-comments.js +0 -1
- package/lib/rules/no-whitespace-before-property.js +3 -3
- package/lib/rules/object-curly-newline.js +7 -10
- package/lib/rules/object-curly-spacing.js +21 -22
- package/lib/rules/object-shorthand.js +1 -1
- package/lib/rules/one-var-declaration-per-line.js +2 -2
- package/lib/rules/operator-assignment.js +33 -3
- package/lib/rules/padded-blocks.js +1 -1
- package/lib/rules/padding-line-between-statements.js +0 -16
- package/lib/rules/prefer-arrow-callback.js +6 -6
- package/lib/rules/prefer-const.js +27 -28
- package/lib/rules/prefer-destructuring.js +1 -7
- package/lib/rules/prefer-exponentiation-operator.js +189 -0
- package/lib/rules/prefer-named-capture-group.js +0 -1
- package/lib/rules/prefer-numeric-literals.js +32 -4
- package/lib/rules/prefer-object-spread.js +7 -7
- package/lib/rules/prefer-rest-params.js +3 -6
- package/lib/rules/prefer-spread.js +4 -4
- package/lib/rules/prefer-template.js +5 -6
- package/lib/rules/quote-props.js +1 -1
- package/lib/rules/quotes.js +5 -6
- package/lib/rules/radix.js +5 -10
- package/lib/rules/require-await.js +10 -5
- package/lib/rules/require-yield.js +2 -2
- package/lib/rules/rest-spread-spacing.js +1 -1
- package/lib/rules/semi.js +6 -3
- package/lib/rules/sort-imports.js +3 -4
- package/lib/rules/sort-keys.js +1 -3
- package/lib/rules/space-before-blocks.js +1 -2
- package/lib/rules/space-in-parens.js +4 -4
- package/lib/rules/space-infix-ops.js +6 -6
- package/lib/rules/spaced-comment.js +20 -22
- package/lib/rules/strict.js +2 -4
- package/lib/rules/symbol-description.js +1 -2
- package/lib/rules/template-curly-spacing.js +2 -2
- package/lib/rules/use-isnan.js +40 -3
- package/lib/rules/utils/ast-utils.js +84 -85
- package/lib/rules/utils/fix-tracker.js +0 -6
- package/lib/rules/utils/lazy-loading-rule-map.js +0 -1
- package/lib/rules/vars-on-top.js +11 -11
- package/lib/shared/config-ops.js +2 -2
- package/lib/shared/runtime-info.js +8 -8
- package/lib/shared/traverser.js +2 -0
- package/lib/shared/types.js +9 -0
- package/lib/source-code/source-code.js +62 -17
- package/lib/source-code/token-store/backward-token-comment-cursor.js +5 -5
- package/lib/source-code/token-store/backward-token-cursor.js +5 -5
- package/lib/source-code/token-store/cursors.js +17 -19
- package/lib/source-code/token-store/decorative-cursor.js +1 -1
- package/lib/source-code/token-store/filter-cursor.js +2 -2
- package/lib/source-code/token-store/forward-token-comment-cursor.js +5 -5
- package/lib/source-code/token-store/forward-token-cursor.js +5 -5
- package/lib/source-code/token-store/index.js +86 -92
- package/lib/source-code/token-store/limit-cursor.js +2 -2
- package/lib/source-code/token-store/padded-token-cursor.js +7 -7
- package/lib/source-code/token-store/skip-cursor.js +2 -2
- package/lib/source-code/token-store/utils.js +9 -13
- package/package.json +9 -7
- package/lib/cli-engine/ignored-paths.js +0 -362
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
/**
|
12
12
|
* Checks whether or not a given variable declarator has the initializer.
|
13
|
-
* @param {ASTNode} node
|
13
|
+
* @param {ASTNode} node A VariableDeclarator node to check.
|
14
14
|
* @returns {boolean} `true` if the node has the initializer.
|
15
15
|
*/
|
16
16
|
function isInitialized(node) {
|
@@ -19,7 +19,7 @@ function isInitialized(node) {
|
|
19
19
|
|
20
20
|
/**
|
21
21
|
* Checks whether or not a given code path segment is unreachable.
|
22
|
-
* @param {CodePathSegment} segment
|
22
|
+
* @param {CodePathSegment} segment A CodePathSegment to check.
|
23
23
|
* @returns {boolean} `true` if the segment is unreachable.
|
24
24
|
*/
|
25
25
|
function isUnreachable(segment) {
|
@@ -57,7 +57,7 @@ class ConsecutiveRange {
|
|
57
57
|
|
58
58
|
/**
|
59
59
|
* Checks whether the given node is inside of this range.
|
60
|
-
* @param {ASTNode|Token} node
|
60
|
+
* @param {ASTNode|Token} node The node to check.
|
61
61
|
* @returns {boolean} `true` if the node is inside of this range.
|
62
62
|
*/
|
63
63
|
contains(node) {
|
@@ -69,7 +69,7 @@ class ConsecutiveRange {
|
|
69
69
|
|
70
70
|
/**
|
71
71
|
* Checks whether the given node is consecutive to this range.
|
72
|
-
* @param {ASTNode} node
|
72
|
+
* @param {ASTNode} node The node to check.
|
73
73
|
* @returns {boolean} `true` if the node is consecutive to this range.
|
74
74
|
*/
|
75
75
|
isConsecutive(node) {
|
@@ -78,7 +78,7 @@ class ConsecutiveRange {
|
|
78
78
|
|
79
79
|
/**
|
80
80
|
* Merges the given node to this range.
|
81
|
-
* @param {ASTNode} node
|
81
|
+
* @param {ASTNode} node The node to merge.
|
82
82
|
* @returns {void}
|
83
83
|
*/
|
84
84
|
merge(node) {
|
@@ -87,7 +87,7 @@ class ConsecutiveRange {
|
|
87
87
|
|
88
88
|
/**
|
89
89
|
* Resets this range by the given node or null.
|
90
|
-
* @param {ASTNode|null} node
|
90
|
+
* @param {ASTNode|null} node The node to reset, or null.
|
91
91
|
* @returns {void}
|
92
92
|
*/
|
93
93
|
reset(node) {
|
@@ -120,7 +120,7 @@ module.exports = {
|
|
120
120
|
|
121
121
|
/**
|
122
122
|
* Reports a given node if it's unreachable.
|
123
|
-
* @param {ASTNode} node
|
123
|
+
* @param {ASTNode} node A statement node to report.
|
124
124
|
* @returns {void}
|
125
125
|
*/
|
126
126
|
function reportIfUnreachable(node) {
|
@@ -35,8 +35,7 @@ module.exports = {
|
|
35
35
|
|
36
36
|
/**
|
37
37
|
* Checks if the node is the finalizer of a TryStatement
|
38
|
-
*
|
39
|
-
* @param {ASTNode} node - node to check.
|
38
|
+
* @param {ASTNode} node node to check.
|
40
39
|
* @returns {boolean} - true if the node is the finalizer of a TryStatement
|
41
40
|
*/
|
42
41
|
function isFinallyBlock(node) {
|
@@ -45,9 +44,8 @@ module.exports = {
|
|
45
44
|
|
46
45
|
/**
|
47
46
|
* Climbs up the tree if the node is not a sentinel node
|
48
|
-
*
|
49
|
-
* @param {
|
50
|
-
* @param {string} label - label of the break or continue statement
|
47
|
+
* @param {ASTNode} node node to check.
|
48
|
+
* @param {string} label label of the break or continue statement
|
51
49
|
* @returns {boolean} - return whether the node is a finally block or a sentinel node
|
52
50
|
*/
|
53
51
|
function isInFinallyBlock(node, label) {
|
@@ -82,8 +80,7 @@ module.exports = {
|
|
82
80
|
|
83
81
|
/**
|
84
82
|
* Checks whether the possibly-unsafe statement is inside a finally block.
|
85
|
-
*
|
86
|
-
* @param {ASTNode} node - node to check.
|
83
|
+
* @param {ASTNode} node node to check.
|
87
84
|
* @returns {void}
|
88
85
|
*/
|
89
86
|
function check(node) {
|
@@ -16,19 +16,26 @@ const astUtils = require("./utils/ast-utils");
|
|
16
16
|
//------------------------------------------------------------------------------
|
17
17
|
|
18
18
|
/**
|
19
|
-
* Checks whether the given operator is
|
20
|
-
*
|
21
|
-
* @
|
22
|
-
* @returns {boolean} `true` if the operator is a relational operator.
|
19
|
+
* Checks whether the given operator is `in` or `instanceof`
|
20
|
+
* @param {string} op The operator type to check.
|
21
|
+
* @returns {boolean} `true` if the operator is `in` or `instanceof`
|
23
22
|
*/
|
24
|
-
function
|
23
|
+
function isInOrInstanceOfOperator(op) {
|
25
24
|
return op === "in" || op === "instanceof";
|
26
25
|
}
|
27
26
|
|
27
|
+
/**
|
28
|
+
* Checks whether the given operator is an ordering relational operator or not.
|
29
|
+
* @param {string} op The operator type to check.
|
30
|
+
* @returns {boolean} `true` if the operator is an ordering relational operator.
|
31
|
+
*/
|
32
|
+
function isOrderingRelationalOperator(op) {
|
33
|
+
return op === "<" || op === ">" || op === ">=" || op === "<=";
|
34
|
+
}
|
35
|
+
|
28
36
|
/**
|
29
37
|
* Checks whether the given node is a logical negation expression or not.
|
30
|
-
*
|
31
|
-
* @param {ASTNode} node - The node to check.
|
38
|
+
* @param {ASTNode} node The node to check.
|
32
39
|
* @returns {boolean} `true` if the node is a logical negation expression.
|
33
40
|
*/
|
34
41
|
function isNegation(node) {
|
@@ -50,7 +57,18 @@ module.exports = {
|
|
50
57
|
url: "https://eslint.org/docs/rules/no-unsafe-negation"
|
51
58
|
},
|
52
59
|
|
53
|
-
schema: [
|
60
|
+
schema: [
|
61
|
+
{
|
62
|
+
type: "object",
|
63
|
+
properties: {
|
64
|
+
enforceForOrderingRelations: {
|
65
|
+
type: "boolean",
|
66
|
+
default: false
|
67
|
+
}
|
68
|
+
},
|
69
|
+
additionalProperties: false
|
70
|
+
}
|
71
|
+
],
|
54
72
|
fixable: null,
|
55
73
|
messages: {
|
56
74
|
unexpected: "Unexpected negating the left operand of '{{operator}}' operator."
|
@@ -59,10 +77,15 @@ module.exports = {
|
|
59
77
|
|
60
78
|
create(context) {
|
61
79
|
const sourceCode = context.getSourceCode();
|
80
|
+
const options = context.options[0] || {};
|
81
|
+
const enforceForOrderingRelations = options.enforceForOrderingRelations === true;
|
62
82
|
|
63
83
|
return {
|
64
84
|
BinaryExpression(node) {
|
65
|
-
|
85
|
+
const orderingRelationRuleApplies = enforceForOrderingRelations && isOrderingRelationalOperator(node.operator);
|
86
|
+
|
87
|
+
if (
|
88
|
+
(isInOrInstanceOfOperator(node.operator) || orderingRelationRuleApplies) &&
|
66
89
|
isNegation(node.left) &&
|
67
90
|
!astUtils.isParenthesised(sourceCode, node.left)
|
68
91
|
) {
|
@@ -47,8 +47,9 @@ module.exports = {
|
|
47
47
|
allowTernary = config.allowTernary || false,
|
48
48
|
allowTaggedTemplates = config.allowTaggedTemplates || false;
|
49
49
|
|
50
|
+
// eslint-disable-next-line jsdoc/require-description
|
50
51
|
/**
|
51
|
-
* @param {ASTNode} node
|
52
|
+
* @param {ASTNode} node any node
|
52
53
|
* @returns {boolean} whether the given node structurally represents a directive
|
53
54
|
*/
|
54
55
|
function looksLikeDirective(node) {
|
@@ -56,9 +57,10 @@ module.exports = {
|
|
56
57
|
node.expression.type === "Literal" && typeof node.expression.value === "string";
|
57
58
|
}
|
58
59
|
|
60
|
+
// eslint-disable-next-line jsdoc/require-description
|
59
61
|
/**
|
60
|
-
* @param {Function} predicate
|
61
|
-
* @param {a[]} list
|
62
|
+
* @param {Function} predicate ([a] -> Boolean) the function used to make the determination
|
63
|
+
* @param {a[]} list the input list
|
62
64
|
* @returns {a[]} the leading sequence of members in the given list that pass the given predicate
|
63
65
|
*/
|
64
66
|
function takeWhile(predicate, list) {
|
@@ -70,17 +72,19 @@ module.exports = {
|
|
70
72
|
return list.slice();
|
71
73
|
}
|
72
74
|
|
75
|
+
// eslint-disable-next-line jsdoc/require-description
|
73
76
|
/**
|
74
|
-
* @param {ASTNode} node
|
77
|
+
* @param {ASTNode} node a Program or BlockStatement node
|
75
78
|
* @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
|
76
79
|
*/
|
77
80
|
function directives(node) {
|
78
81
|
return takeWhile(looksLikeDirective, node.body);
|
79
82
|
}
|
80
83
|
|
84
|
+
// eslint-disable-next-line jsdoc/require-description
|
81
85
|
/**
|
82
|
-
* @param {ASTNode} node
|
83
|
-
* @param {ASTNode[]} ancestors
|
86
|
+
* @param {ASTNode} node any node
|
87
|
+
* @param {ASTNode[]} ancestors the given node's ancestors
|
84
88
|
* @returns {boolean} whether the given node is considered a directive in its current position
|
85
89
|
*/
|
86
90
|
function isDirective(node, ancestors) {
|
@@ -94,7 +98,7 @@ module.exports = {
|
|
94
98
|
|
95
99
|
/**
|
96
100
|
* Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
|
97
|
-
* @param {ASTNode} node
|
101
|
+
* @param {ASTNode} node any node
|
98
102
|
* @returns {boolean} whether the given node is a valid expression
|
99
103
|
*/
|
100
104
|
function isValidExpression(node) {
|
@@ -35,8 +35,7 @@ module.exports = {
|
|
35
35
|
|
36
36
|
/**
|
37
37
|
* Adds a scope info to the stack.
|
38
|
-
*
|
39
|
-
* @param {ASTNode} node - A node to add. This is a LabeledStatement.
|
38
|
+
* @param {ASTNode} node A node to add. This is a LabeledStatement.
|
40
39
|
* @returns {void}
|
41
40
|
*/
|
42
41
|
function enterLabeledScope(node) {
|
@@ -50,8 +49,7 @@ module.exports = {
|
|
50
49
|
/**
|
51
50
|
* Removes the top of the stack.
|
52
51
|
* At the same time, this reports the label if it's never used.
|
53
|
-
*
|
54
|
-
* @param {ASTNode} node - A node to report. This is a LabeledStatement.
|
52
|
+
* @param {ASTNode} node A node to report. This is a LabeledStatement.
|
55
53
|
* @returns {void}
|
56
54
|
*/
|
57
55
|
function exitLabeledScope(node) {
|
@@ -81,8 +79,7 @@ module.exports = {
|
|
81
79
|
|
82
80
|
/**
|
83
81
|
* Marks the label of a given node as used.
|
84
|
-
*
|
85
|
-
* @param {ASTNode} node - A node to mark. This is a BreakStatement or
|
82
|
+
* @param {ASTNode} node A node to mark. This is a BreakStatement or
|
86
83
|
* ContinueStatement.
|
87
84
|
* @returns {void}
|
88
85
|
*/
|
@@ -103,7 +103,7 @@ module.exports = {
|
|
103
103
|
/**
|
104
104
|
* Generate the warning message about the variable being
|
105
105
|
* defined and unused, including the ignore pattern if configured.
|
106
|
-
* @param {Variable} unusedVar
|
106
|
+
* @param {Variable} unusedVar eslint-scope variable object.
|
107
107
|
* @returns {string} The warning message to be used with this unused variable.
|
108
108
|
*/
|
109
109
|
function getDefinedMessage(unusedVar) {
|
@@ -146,7 +146,7 @@ module.exports = {
|
|
146
146
|
|
147
147
|
/**
|
148
148
|
* Determines if a given variable is being exported from a module.
|
149
|
-
* @param {Variable} variable
|
149
|
+
* @param {Variable} variable eslint-scope variable object.
|
150
150
|
* @returns {boolean} True if the variable is exported, false if not.
|
151
151
|
* @private
|
152
152
|
*/
|
@@ -172,7 +172,7 @@ module.exports = {
|
|
172
172
|
|
173
173
|
/**
|
174
174
|
* Determines if a variable has a sibling rest property
|
175
|
-
* @param {Variable} variable
|
175
|
+
* @param {Variable} variable eslint-scope variable object.
|
176
176
|
* @returns {boolean} True if the variable is exported, false if not.
|
177
177
|
* @private
|
178
178
|
*/
|
@@ -195,7 +195,7 @@ module.exports = {
|
|
195
195
|
|
196
196
|
/**
|
197
197
|
* Determines if a reference is a read operation.
|
198
|
-
* @param {Reference} ref
|
198
|
+
* @param {Reference} ref An eslint-scope Reference
|
199
199
|
* @returns {boolean} whether the given reference represents a read operation
|
200
200
|
* @private
|
201
201
|
*/
|
@@ -205,8 +205,8 @@ module.exports = {
|
|
205
205
|
|
206
206
|
/**
|
207
207
|
* Determine if an identifier is referencing an enclosing function name.
|
208
|
-
* @param {Reference} ref
|
209
|
-
* @param {ASTNode[]} nodes
|
208
|
+
* @param {Reference} ref The reference to check.
|
209
|
+
* @param {ASTNode[]} nodes The candidate function nodes.
|
210
210
|
* @returns {boolean} True if it's a self-reference, false if not.
|
211
211
|
* @private
|
212
212
|
*/
|
@@ -226,7 +226,7 @@ module.exports = {
|
|
226
226
|
|
227
227
|
/**
|
228
228
|
* Gets a list of function definitions for a specified variable.
|
229
|
-
* @param {Variable} variable
|
229
|
+
* @param {Variable} variable eslint-scope variable object.
|
230
230
|
* @returns {ASTNode[]} Function nodes.
|
231
231
|
* @private
|
232
232
|
*/
|
@@ -252,9 +252,8 @@ module.exports = {
|
|
252
252
|
|
253
253
|
/**
|
254
254
|
* Checks the position of given nodes.
|
255
|
-
*
|
256
|
-
* @param {ASTNode}
|
257
|
-
* @param {ASTNode} outer - A node which is expected as outside.
|
255
|
+
* @param {ASTNode} inner A node which is expected as inside.
|
256
|
+
* @param {ASTNode} outer A node which is expected as outside.
|
258
257
|
* @returns {boolean} `true` if the `inner` node exists in the `outer` node.
|
259
258
|
* @private
|
260
259
|
*/
|
@@ -275,9 +274,8 @@ module.exports = {
|
|
275
274
|
* - The reference is inside of a loop.
|
276
275
|
* - The reference is inside of a function scope which is different from
|
277
276
|
* the declaration.
|
278
|
-
*
|
279
|
-
* @param {
|
280
|
-
* @param {ASTNode} prevRhsNode - The previous RHS node.
|
277
|
+
* @param {eslint-scope.Reference} ref A reference to check.
|
278
|
+
* @param {ASTNode} prevRhsNode The previous RHS node.
|
281
279
|
* @returns {ASTNode|null} The RHS node or null.
|
282
280
|
* @private
|
283
281
|
*/
|
@@ -310,9 +308,8 @@ module.exports = {
|
|
310
308
|
/**
|
311
309
|
* Checks whether a given function node is stored to somewhere or not.
|
312
310
|
* If the function node is stored, the function can be used later.
|
313
|
-
*
|
314
|
-
* @param {ASTNode}
|
315
|
-
* @param {ASTNode} rhsNode - The RHS node of the previous assignment.
|
311
|
+
* @param {ASTNode} funcNode A function node to check.
|
312
|
+
* @param {ASTNode} rhsNode The RHS node of the previous assignment.
|
316
313
|
* @returns {boolean} `true` if under the following conditions:
|
317
314
|
* - the funcNode is assigned to a variable.
|
318
315
|
* - the funcNode is bound as an argument of a function call.
|
@@ -367,9 +364,8 @@ module.exports = {
|
|
367
364
|
* - the function is bound as an argument of a function call.
|
368
365
|
*
|
369
366
|
* If a reference exists in a function which can be used later, the reference is read when the function is called.
|
370
|
-
*
|
371
|
-
* @param {ASTNode}
|
372
|
-
* @param {ASTNode} rhsNode - The RHS node of the previous assignment.
|
367
|
+
* @param {ASTNode} id An Identifier node to check.
|
368
|
+
* @param {ASTNode} rhsNode The RHS node of the previous assignment.
|
373
369
|
* @returns {boolean} `true` if the `id` node exists inside of a function node which can be used later.
|
374
370
|
* @private
|
375
371
|
*/
|
@@ -385,9 +381,8 @@ module.exports = {
|
|
385
381
|
|
386
382
|
/**
|
387
383
|
* Checks whether a given reference is a read to update itself or not.
|
388
|
-
*
|
389
|
-
* @param {
|
390
|
-
* @param {ASTNode} rhsNode - The RHS node of the previous assignment.
|
384
|
+
* @param {eslint-scope.Reference} ref A reference to check.
|
385
|
+
* @param {ASTNode} rhsNode The RHS node of the previous assignment.
|
391
386
|
* @returns {boolean} The reference is a read to update itself.
|
392
387
|
* @private
|
393
388
|
*/
|
@@ -416,8 +411,7 @@ module.exports = {
|
|
416
411
|
|
417
412
|
/**
|
418
413
|
* Determine if an identifier is used either in for-in loops.
|
419
|
-
*
|
420
|
-
* @param {Reference} ref - The reference to check.
|
414
|
+
* @param {Reference} ref The reference to check.
|
421
415
|
* @returns {boolean} whether reference is used in the for-in loops
|
422
416
|
* @private
|
423
417
|
*/
|
@@ -453,7 +447,7 @@ module.exports = {
|
|
453
447
|
|
454
448
|
/**
|
455
449
|
* Determines if the variable is used.
|
456
|
-
* @param {Variable} variable
|
450
|
+
* @param {Variable} variable The variable to check.
|
457
451
|
* @returns {boolean} True if the variable is used
|
458
452
|
* @private
|
459
453
|
*/
|
@@ -481,8 +475,7 @@ module.exports = {
|
|
481
475
|
|
482
476
|
/**
|
483
477
|
* Checks whether the given variable is after the last used parameter.
|
484
|
-
*
|
485
|
-
* @param {eslint-scope.Variable} variable - The variable to check.
|
478
|
+
* @param {eslint-scope.Variable} variable The variable to check.
|
486
479
|
* @returns {boolean} `true` if the variable is defined after the last
|
487
480
|
* used parameter.
|
488
481
|
*/
|
@@ -497,8 +490,8 @@ module.exports = {
|
|
497
490
|
|
498
491
|
/**
|
499
492
|
* Gets an array of variables without read references.
|
500
|
-
* @param {Scope} scope
|
501
|
-
* @param {Variable[]} unusedVars
|
493
|
+
* @param {Scope} scope an eslint-scope Scope object.
|
494
|
+
* @param {Variable[]} unusedVars an array that saving result.
|
502
495
|
* @returns {Variable[]} unused variables of the scope and descendant scopes.
|
503
496
|
* @private
|
504
497
|
*/
|
@@ -14,8 +14,7 @@ const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u;
|
|
14
14
|
|
15
15
|
/**
|
16
16
|
* Parses a given value as options.
|
17
|
-
*
|
18
|
-
* @param {any} options - A value to parse.
|
17
|
+
* @param {any} options A value to parse.
|
19
18
|
* @returns {Object} The parsed options.
|
20
19
|
*/
|
21
20
|
function parseOptions(options) {
|
@@ -36,8 +35,7 @@ function parseOptions(options) {
|
|
36
35
|
|
37
36
|
/**
|
38
37
|
* Checks whether or not a given variable is a function declaration.
|
39
|
-
*
|
40
|
-
* @param {eslint-scope.Variable} variable - A variable to check.
|
38
|
+
* @param {eslint-scope.Variable} variable A variable to check.
|
41
39
|
* @returns {boolean} `true` if the variable is a function declaration.
|
42
40
|
*/
|
43
41
|
function isFunction(variable) {
|
@@ -46,9 +44,8 @@ function isFunction(variable) {
|
|
46
44
|
|
47
45
|
/**
|
48
46
|
* Checks whether or not a given variable is a class declaration in an upper function scope.
|
49
|
-
*
|
50
|
-
* @param {eslint-scope.
|
51
|
-
* @param {eslint-scope.Reference} reference - A reference to check.
|
47
|
+
* @param {eslint-scope.Variable} variable A variable to check.
|
48
|
+
* @param {eslint-scope.Reference} reference A reference to check.
|
52
49
|
* @returns {boolean} `true` if the variable is a class declaration.
|
53
50
|
*/
|
54
51
|
function isOuterClass(variable, reference) {
|
@@ -60,8 +57,8 @@ function isOuterClass(variable, reference) {
|
|
60
57
|
|
61
58
|
/**
|
62
59
|
* Checks whether or not a given variable is a variable declaration in an upper function scope.
|
63
|
-
* @param {eslint-scope.Variable} variable
|
64
|
-
* @param {eslint-scope.Reference} reference
|
60
|
+
* @param {eslint-scope.Variable} variable A variable to check.
|
61
|
+
* @param {eslint-scope.Reference} reference A reference to check.
|
65
62
|
* @returns {boolean} `true` if the variable is a variable declaration.
|
66
63
|
*/
|
67
64
|
function isOuterVariable(variable, reference) {
|
@@ -73,9 +70,8 @@ function isOuterVariable(variable, reference) {
|
|
73
70
|
|
74
71
|
/**
|
75
72
|
* Checks whether or not a given location is inside of the range of a given node.
|
76
|
-
*
|
77
|
-
* @param {
|
78
|
-
* @param {number} location - A location to check.
|
73
|
+
* @param {ASTNode} node An node to check.
|
74
|
+
* @param {number} location A location to check.
|
79
75
|
* @returns {boolean} `true` if the location is inside of the range of the node.
|
80
76
|
*/
|
81
77
|
function isInRange(node, location) {
|
@@ -92,9 +88,8 @@ function isInRange(node, location) {
|
|
92
88
|
* var {a = a} = obj
|
93
89
|
* for (var a in a) {}
|
94
90
|
* for (var a of a) {}
|
95
|
-
*
|
96
|
-
* @param {
|
97
|
-
* @param {Reference} reference - A reference to check.
|
91
|
+
* @param {Variable} variable A variable to check.
|
92
|
+
* @param {Reference} reference A reference to check.
|
98
93
|
* @returns {boolean} `true` if the reference is inside of the initializers.
|
99
94
|
*/
|
100
95
|
function isInInitializer(variable, reference) {
|
@@ -13,7 +13,7 @@ const astUtils = require("./utils/ast-utils");
|
|
13
13
|
|
14
14
|
/**
|
15
15
|
* Checks whether or not a node is a `.call()`/`.apply()`.
|
16
|
-
* @param {ASTNode} node
|
16
|
+
* @param {ASTNode} node A CallExpression node to check.
|
17
17
|
* @returns {boolean} Whether or not the node is a `.call()`/`.apply()`.
|
18
18
|
*/
|
19
19
|
function isCallOrNonVariadicApply(node) {
|
@@ -31,9 +31,9 @@ function isCallOrNonVariadicApply(node) {
|
|
31
31
|
|
32
32
|
/**
|
33
33
|
* Checks whether or not `thisArg` is not changed by `.call()`/`.apply()`.
|
34
|
-
* @param {ASTNode|null} expectedThis
|
35
|
-
* @param {ASTNode} thisArg
|
36
|
-
* @param {SourceCode} sourceCode
|
34
|
+
* @param {ASTNode|null} expectedThis The node that is the owner of the applied function.
|
35
|
+
* @param {ASTNode} thisArg The node that is given to the first argument of the `.call()`/`.apply()`.
|
36
|
+
* @param {SourceCode} sourceCode The ESLint source code object.
|
37
37
|
* @returns {boolean} Whether or not `thisArg` is not changed by `.call()`/`.apply()`.
|
38
38
|
*/
|
39
39
|
function isValidThisArg(expectedThis, thisArg, sourceCode) {
|
@@ -8,6 +8,7 @@
|
|
8
8
|
// Requirements
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
+
const lodash = require("lodash");
|
11
12
|
const astUtils = require("./utils/ast-utils");
|
12
13
|
|
13
14
|
//------------------------------------------------------------------------------
|
@@ -21,57 +22,83 @@ module.exports = {
|
|
21
22
|
type: "suggestion",
|
22
23
|
|
23
24
|
docs: {
|
24
|
-
description: "disallow unnecessary computed property keys in
|
25
|
+
description: "disallow unnecessary computed property keys in objects and classes",
|
25
26
|
category: "ECMAScript 6",
|
26
27
|
recommended: false,
|
27
28
|
url: "https://eslint.org/docs/rules/no-useless-computed-key"
|
28
29
|
},
|
29
30
|
|
30
|
-
schema: [
|
31
|
+
schema: [{
|
32
|
+
type: "object",
|
33
|
+
properties: {
|
34
|
+
enforceForClassMembers: {
|
35
|
+
type: "boolean",
|
36
|
+
default: false
|
37
|
+
}
|
38
|
+
},
|
39
|
+
additionalProperties: false
|
40
|
+
}],
|
31
41
|
fixable: "code"
|
32
42
|
},
|
33
43
|
create(context) {
|
34
44
|
const sourceCode = context.getSourceCode();
|
45
|
+
const enforceForClassMembers = context.options[0] && context.options[0].enforceForClassMembers;
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Reports a given node if it violated this rule.
|
49
|
+
* @param {ASTNode} node The node to check.
|
50
|
+
* @returns {void}
|
51
|
+
*/
|
52
|
+
function check(node) {
|
53
|
+
if (!node.computed) {
|
54
|
+
return;
|
55
|
+
}
|
35
56
|
|
36
|
-
|
37
|
-
|
38
|
-
if (!node.computed) {
|
39
|
-
return;
|
40
|
-
}
|
41
|
-
|
42
|
-
const key = node.key,
|
43
|
-
nodeType = typeof key.value;
|
57
|
+
const key = node.key,
|
58
|
+
nodeType = typeof key.value;
|
44
59
|
|
45
|
-
|
46
|
-
context.report({
|
47
|
-
node,
|
48
|
-
message: MESSAGE_UNNECESSARY_COMPUTED,
|
49
|
-
data: { property: sourceCode.getText(key) },
|
50
|
-
fix(fixer) {
|
51
|
-
const leftSquareBracket = sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken);
|
52
|
-
const rightSquareBracket = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken);
|
53
|
-
const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);
|
60
|
+
let allowedKey;
|
54
61
|
|
55
|
-
|
56
|
-
|
62
|
+
if (node.type === "MethodDefinition") {
|
63
|
+
allowedKey = node.static ? "prototype" : "constructor";
|
64
|
+
} else {
|
65
|
+
allowedKey = "__proto__";
|
66
|
+
}
|
57
67
|
|
58
|
-
|
59
|
-
|
60
|
-
|
68
|
+
if (key.type === "Literal" && (nodeType === "string" || nodeType === "number") && key.value !== allowedKey) {
|
69
|
+
context.report({
|
70
|
+
node,
|
71
|
+
message: MESSAGE_UNNECESSARY_COMPUTED,
|
72
|
+
data: { property: sourceCode.getText(key) },
|
73
|
+
fix(fixer) {
|
74
|
+
const leftSquareBracket = sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken);
|
75
|
+
const rightSquareBracket = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken);
|
76
|
+
const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);
|
77
|
+
|
78
|
+
if (tokensBetween.slice(0, -1).some((token, index) =>
|
79
|
+
sourceCode.getText().slice(token.range[1], tokensBetween[index + 1].range[0]).trim())) {
|
80
|
+
|
81
|
+
// If there are comments between the brackets and the property name, don't do a fix.
|
82
|
+
return null;
|
83
|
+
}
|
61
84
|
|
62
|
-
|
85
|
+
const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket);
|
63
86
|
|
64
|
-
|
65
|
-
|
66
|
-
|
87
|
+
// Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} })
|
88
|
+
const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] &&
|
89
|
+
!astUtils.canTokensBeAdjacent(tokenBeforeLeftBracket, sourceCode.getFirstToken(key));
|
67
90
|
|
68
|
-
|
91
|
+
const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw;
|
69
92
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
93
|
+
return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey);
|
94
|
+
}
|
95
|
+
});
|
74
96
|
}
|
97
|
+
}
|
98
|
+
|
99
|
+
return {
|
100
|
+
Property: check,
|
101
|
+
MethodDefinition: enforceForClassMembers ? check : lodash.noop
|
75
102
|
};
|
76
103
|
}
|
77
104
|
};
|
@@ -16,7 +16,7 @@ const astUtils = require("./utils/ast-utils");
|
|
16
16
|
|
17
17
|
/**
|
18
18
|
* Checks whether or not a given node is a concatenation.
|
19
|
-
* @param {ASTNode} node
|
19
|
+
* @param {ASTNode} node A node to check.
|
20
20
|
* @returns {boolean} `true` if the node is a concatenation.
|
21
21
|
*/
|
22
22
|
function isConcatenation(node) {
|
@@ -25,7 +25,7 @@ function isConcatenation(node) {
|
|
25
25
|
|
26
26
|
/**
|
27
27
|
* Checks if the given token is a `+` token or not.
|
28
|
-
* @param {Token} token
|
28
|
+
* @param {Token} token The token to check.
|
29
29
|
* @returns {boolean} `true` if the token is a `+` token.
|
30
30
|
*/
|
31
31
|
function isConcatOperatorToken(token) {
|
@@ -34,7 +34,7 @@ function isConcatOperatorToken(token) {
|
|
34
34
|
|
35
35
|
/**
|
36
36
|
* Get's the right most node on the left side of a BinaryExpression with + operator.
|
37
|
-
* @param {ASTNode} node
|
37
|
+
* @param {ASTNode} node A BinaryExpression node to check.
|
38
38
|
* @returns {ASTNode} node
|
39
39
|
*/
|
40
40
|
function getLeft(node) {
|
@@ -48,7 +48,7 @@ function getLeft(node) {
|
|
48
48
|
|
49
49
|
/**
|
50
50
|
* Get's the left most node on the right side of a BinaryExpression with + operator.
|
51
|
-
* @param {ASTNode} node
|
51
|
+
* @param {ASTNode} node A BinaryExpression node to check.
|
52
52
|
* @returns {ASTNode} node
|
53
53
|
*/
|
54
54
|
function getRight(node) {
|