eslint 5.2.0 → 5.6.0
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 +63 -0
- package/conf/eslint-recommended.js +4 -0
- package/lib/cli-engine.js +13 -14
- package/lib/code-path-analysis/code-path-analyzer.js +1 -1
- package/lib/config/config-initializer.js +7 -7
- package/lib/config.js +1 -1
- package/lib/formatters/codeframe.js +2 -2
- package/lib/ignored-paths.js +125 -37
- package/lib/linter.js +1 -1
- package/lib/rules/array-bracket-newline.js +1 -1
- package/lib/rules/array-bracket-spacing.js +1 -1
- package/lib/rules/array-callback-return.js +1 -1
- package/lib/rules/array-element-newline.js +1 -1
- package/lib/rules/arrow-body-style.js +1 -1
- package/lib/rules/arrow-parens.js +1 -1
- package/lib/rules/arrow-spacing.js +1 -1
- package/lib/rules/block-spacing.js +1 -1
- package/lib/rules/brace-style.js +1 -1
- package/lib/rules/camelcase.js +1 -1
- package/lib/rules/capitalized-comments.js +1 -1
- package/lib/rules/comma-dangle.js +1 -1
- package/lib/rules/comma-spacing.js +1 -1
- package/lib/rules/comma-style.js +2 -2
- package/lib/rules/complexity.js +3 -3
- package/lib/rules/computed-property-spacing.js +1 -1
- package/lib/rules/consistent-return.js +1 -1
- package/lib/rules/curly.js +1 -1
- package/lib/rules/dot-location.js +1 -1
- package/lib/rules/dot-notation.js +1 -1
- package/lib/rules/eqeqeq.js +1 -1
- package/lib/rules/for-direction.js +5 -2
- package/lib/rules/func-call-spacing.js +8 -4
- package/lib/rules/func-name-matching.js +13 -7
- package/lib/rules/func-names.js +82 -21
- package/lib/rules/func-style.js +8 -4
- package/lib/rules/function-paren-newline.js +12 -6
- package/lib/rules/generator-star-spacing.js +18 -9
- package/lib/rules/getter-return.js +8 -6
- package/lib/rules/indent-legacy.js +1 -1
- package/lib/rules/indent.js +43 -7
- package/lib/rules/jsx-quotes.js +1 -1
- package/lib/rules/key-spacing.js +2 -2
- package/lib/rules/keyword-spacing.js +1 -1
- package/lib/rules/line-comment-position.js +3 -3
- package/lib/rules/linebreak-style.js +1 -1
- package/lib/rules/lines-around-comment.js +1 -1
- package/lib/rules/lines-around-directive.js +1 -1
- package/lib/rules/lines-between-class-members.js +1 -1
- package/lib/rules/max-depth.js +2 -2
- package/lib/rules/max-len.js +1 -1
- package/lib/rules/max-lines-per-function.js +1 -1
- package/lib/rules/max-lines.js +2 -2
- package/lib/rules/max-nested-callbacks.js +2 -2
- package/lib/rules/max-params.js +5 -4
- package/lib/rules/max-statements-per-line.js +1 -1
- package/lib/rules/max-statements.js +3 -3
- package/lib/rules/multiline-comment-style.js +1 -1
- package/lib/rules/multiline-ternary.js +1 -1
- package/lib/rules/new-parens.js +1 -1
- package/lib/rules/newline-after-var.js +1 -1
- package/lib/rules/newline-per-chained-call.js +1 -1
- package/lib/rules/no-alert.js +1 -1
- package/lib/rules/no-async-promise-executor.js +33 -0
- package/lib/rules/no-catch-shadow.js +1 -1
- package/lib/rules/no-class-assign.js +1 -1
- package/lib/rules/no-cond-assign.js +1 -1
- package/lib/rules/no-confusing-arrow.js +1 -1
- package/lib/rules/no-console.js +1 -1
- package/lib/rules/no-const-assign.js +1 -1
- package/lib/rules/no-constant-condition.js +2 -0
- package/lib/rules/no-dupe-keys.js +1 -1
- package/lib/rules/no-else-return.js +1 -1
- package/lib/rules/no-empty-function.js +1 -1
- package/lib/rules/no-empty.js +1 -1
- package/lib/rules/no-eval.js +1 -1
- package/lib/rules/no-ex-assign.js +1 -1
- package/lib/rules/no-extend-native.js +1 -1
- package/lib/rules/no-extra-bind.js +1 -1
- package/lib/rules/no-extra-boolean-cast.js +1 -1
- package/lib/rules/no-extra-label.js +1 -1
- package/lib/rules/no-extra-parens.js +2 -2
- package/lib/rules/no-extra-semi.js +1 -1
- package/lib/rules/no-floating-decimal.js +1 -1
- package/lib/rules/no-func-assign.js +1 -1
- package/lib/rules/no-implicit-coercion.js +1 -1
- package/lib/rules/no-inline-comments.js +1 -1
- package/lib/rules/no-invalid-this.js +1 -1
- package/lib/rules/no-irregular-whitespace.js +1 -1
- package/lib/rules/no-label-var.js +1 -1
- package/lib/rules/no-labels.js +1 -1
- package/lib/rules/no-magic-numbers.js +7 -3
- package/lib/rules/no-misleading-character-class.js +189 -0
- package/lib/rules/no-mixed-operators.js +1 -1
- package/lib/rules/no-multi-spaces.js +1 -1
- package/lib/rules/no-multi-str.js +1 -1
- package/lib/rules/no-regex-spaces.js +1 -1
- package/lib/rules/no-restricted-globals.js +1 -1
- package/lib/rules/no-restricted-imports.js +1 -1
- package/lib/rules/no-restricted-modules.js +1 -1
- package/lib/rules/no-restricted-properties.js +1 -1
- package/lib/rules/no-return-assign.js +1 -1
- package/lib/rules/no-return-await.js +1 -1
- package/lib/rules/no-self-assign.js +1 -1
- package/lib/rules/no-sequences.js +1 -1
- package/lib/rules/no-shadow.js +1 -1
- package/lib/rules/no-this-before-super.js +1 -1
- package/lib/rules/no-throw-literal.js +1 -1
- package/lib/rules/no-trailing-spaces.js +1 -1
- package/lib/rules/no-undef-init.js +1 -1
- package/lib/rules/no-unexpected-multiline.js +1 -1
- package/lib/rules/no-unmodified-loop-condition.js +2 -3
- package/lib/rules/no-unneeded-ternary.js +1 -1
- package/lib/rules/no-unsafe-negation.js +1 -1
- package/lib/rules/no-unused-vars.js +1 -1
- package/lib/rules/no-useless-call.js +1 -1
- package/lib/rules/no-useless-computed-key.js +1 -1
- package/lib/rules/no-useless-concat.js +1 -1
- package/lib/rules/no-useless-escape.js +1 -1
- package/lib/rules/no-useless-return.js +2 -11
- package/lib/rules/no-var.js +1 -1
- package/lib/rules/no-warning-comments.js +1 -1
- package/lib/rules/no-whitespace-before-property.js +1 -1
- package/lib/rules/object-curly-newline.js +1 -1
- package/lib/rules/object-curly-spacing.js +1 -1
- package/lib/rules/object-shorthand.js +1 -1
- package/lib/rules/one-var.js +6 -6
- package/lib/rules/operator-assignment.js +1 -1
- package/lib/rules/operator-linebreak.js +1 -1
- package/lib/rules/padded-blocks.js +6 -6
- package/lib/rules/padding-line-between-statements.js +2 -2
- package/lib/rules/prefer-const.js +2 -11
- package/lib/rules/prefer-object-spread.js +9 -3
- package/lib/rules/prefer-promise-reject-errors.js +1 -1
- package/lib/rules/prefer-reflect.js +1 -1
- package/lib/rules/prefer-spread.js +1 -1
- package/lib/rules/prefer-template.js +1 -1
- package/lib/rules/quotes.js +1 -1
- package/lib/rules/radix.js +1 -1
- package/lib/rules/require-atomic-updates.js +239 -0
- package/lib/rules/require-await.js +1 -1
- package/lib/rules/require-unicode-regexp.js +65 -0
- package/lib/rules/semi-spacing.js +3 -3
- package/lib/rules/semi-style.js +1 -1
- package/lib/rules/semi.js +1 -1
- package/lib/rules/sort-keys.js +1 -1
- package/lib/rules/space-before-blocks.js +1 -1
- package/lib/rules/space-before-function-paren.js +1 -1
- package/lib/rules/space-in-parens.js +1 -1
- package/lib/rules/space-unary-ops.js +2 -2
- package/lib/rules/spaced-comment.js +1 -1
- package/lib/rules/strict.js +1 -1
- package/lib/rules/switch-colon-spacing.js +1 -1
- package/lib/rules/symbol-description.js +1 -1
- package/lib/rules/template-curly-spacing.js +1 -1
- package/lib/rules/valid-jsdoc.js +1 -1
- package/lib/rules/wrap-iife.js +1 -1
- package/lib/rules/yoda.js +1 -1
- package/lib/testers/rule-tester.js +7 -7
- package/lib/token-store/index.js +1 -1
- package/lib/{ast-utils.js → util/ast-utils.js} +0 -0
- package/lib/{file-finder.js → util/file-finder.js} +2 -2
- package/lib/util/fix-tracker.js +1 -1
- package/lib/util/{glob-util.js → glob-utils.js} +4 -4
- package/lib/util/lint-result-cache.js +2 -2
- package/lib/util/naming.js +2 -2
- package/lib/util/node-event-generator.js +3 -3
- package/lib/util/{npm-util.js → npm-utils.js} +0 -0
- package/lib/util/{path-util.js → path-utils.js} +1 -1
- package/lib/util/source-code-fixer.js +1 -1
- package/lib/util/{source-code-util.js → source-code-utils.js} +3 -3
- package/lib/util/source-code.js +1 -1
- package/lib/util/unicode/index.js +11 -0
- package/lib/util/unicode/is-combining-character.js +13 -0
- package/lib/util/unicode/is-emoji-modifier.js +13 -0
- package/lib/util/unicode/is-regional-indicator-symbol.js +13 -0
- package/lib/util/unicode/is-surrogate-pair.js +14 -0
- package/package.json +13 -14
- package/lib/rules/.eslintrc.yml +0 -4
@@ -17,7 +17,7 @@ const OPTIONS = {
|
|
17
17
|
//------------------------------------------------------------------------------
|
18
18
|
// Requirements
|
19
19
|
//------------------------------------------------------------------------------
|
20
|
-
const astUtils = require("../ast-utils");
|
20
|
+
const astUtils = require("../util/ast-utils");
|
21
21
|
|
22
22
|
//------------------------------------------------------------------------------
|
23
23
|
// Rule Definition
|
package/lib/rules/one-var.js
CHANGED
@@ -74,19 +74,19 @@ module.exports = {
|
|
74
74
|
options.let = { uninitialized: mode, initialized: mode };
|
75
75
|
options.const = { uninitialized: mode, initialized: mode };
|
76
76
|
} else if (typeof mode === "object") { // options configuration is an object
|
77
|
-
if (
|
77
|
+
if (Object.prototype.hasOwnProperty.call(mode, "separateRequires")) {
|
78
78
|
options.separateRequires = !!mode.separateRequires;
|
79
79
|
}
|
80
|
-
if (
|
80
|
+
if (Object.prototype.hasOwnProperty.call(mode, "var")) {
|
81
81
|
options.var = { uninitialized: mode.var, initialized: mode.var };
|
82
82
|
}
|
83
|
-
if (
|
83
|
+
if (Object.prototype.hasOwnProperty.call(mode, "let")) {
|
84
84
|
options.let = { uninitialized: mode.let, initialized: mode.let };
|
85
85
|
}
|
86
|
-
if (
|
86
|
+
if (Object.prototype.hasOwnProperty.call(mode, "const")) {
|
87
87
|
options.const = { uninitialized: mode.const, initialized: mode.const };
|
88
88
|
}
|
89
|
-
if (
|
89
|
+
if (Object.prototype.hasOwnProperty.call(mode, "uninitialized")) {
|
90
90
|
if (!options.var) {
|
91
91
|
options.var = {};
|
92
92
|
}
|
@@ -100,7 +100,7 @@ module.exports = {
|
|
100
100
|
options.let.uninitialized = mode.uninitialized;
|
101
101
|
options.const.uninitialized = mode.uninitialized;
|
102
102
|
}
|
103
|
-
if (
|
103
|
+
if (Object.prototype.hasOwnProperty.call(mode, "initialized")) {
|
104
104
|
if (!options.var) {
|
105
105
|
options.var = {};
|
106
106
|
}
|
@@ -8,7 +8,7 @@
|
|
8
8
|
// Requirements
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
-
const astUtils = require("../ast-utils");
|
11
|
+
const astUtils = require("../util/ast-utils");
|
12
12
|
|
13
13
|
//------------------------------------------------------------------------------
|
14
14
|
// Helpers
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Rule Definition
|
@@ -58,13 +58,13 @@ module.exports = {
|
|
58
58
|
options.switches = shouldHavePadding;
|
59
59
|
options.classes = shouldHavePadding;
|
60
60
|
} else {
|
61
|
-
if (
|
61
|
+
if (Object.prototype.hasOwnProperty.call(config, "blocks")) {
|
62
62
|
options.blocks = config.blocks === "always";
|
63
63
|
}
|
64
|
-
if (
|
64
|
+
if (Object.prototype.hasOwnProperty.call(config, "switches")) {
|
65
65
|
options.switches = config.switches === "always";
|
66
66
|
}
|
67
|
-
if (
|
67
|
+
if (Object.prototype.hasOwnProperty.call(config, "classes")) {
|
68
68
|
options.classes = config.classes === "always";
|
69
69
|
}
|
70
70
|
}
|
@@ -225,7 +225,7 @@ module.exports = {
|
|
225
225
|
|
226
226
|
const rule = {};
|
227
227
|
|
228
|
-
if (
|
228
|
+
if (Object.prototype.hasOwnProperty.call(options, "switches")) {
|
229
229
|
rule.SwitchStatement = function(node) {
|
230
230
|
if (node.cases.length === 0) {
|
231
231
|
return;
|
@@ -234,7 +234,7 @@ module.exports = {
|
|
234
234
|
};
|
235
235
|
}
|
236
236
|
|
237
|
-
if (
|
237
|
+
if (Object.prototype.hasOwnProperty.call(options, "blocks")) {
|
238
238
|
rule.BlockStatement = function(node) {
|
239
239
|
if (node.body.length === 0) {
|
240
240
|
return;
|
@@ -243,7 +243,7 @@ module.exports = {
|
|
243
243
|
};
|
244
244
|
}
|
245
245
|
|
246
|
-
if (
|
246
|
+
if (Object.prototype.hasOwnProperty.call(options, "classes")) {
|
247
247
|
rule.ClassBody = function(node) {
|
248
248
|
if (node.body.length === 0) {
|
249
249
|
return;
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Helpers
|
@@ -367,6 +367,7 @@ const StatementTypes = {
|
|
367
367
|
|
368
368
|
block: newNodeTypeTester("BlockStatement"),
|
369
369
|
empty: newNodeTypeTester("EmptyStatement"),
|
370
|
+
function: newNodeTypeTester("FunctionDeclaration"),
|
370
371
|
|
371
372
|
break: newKeywordTester("break"),
|
372
373
|
case: newKeywordTester("case"),
|
@@ -378,7 +379,6 @@ const StatementTypes = {
|
|
378
379
|
do: newKeywordTester("do"),
|
379
380
|
export: newKeywordTester("export"),
|
380
381
|
for: newKeywordTester("for"),
|
381
|
-
function: newKeywordTester("function"),
|
382
382
|
if: newKeywordTester("if"),
|
383
383
|
import: newKeywordTester("import"),
|
384
384
|
let: newKeywordTester("let"),
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
-
const astUtils = require("../ast-utils");
|
8
|
+
const astUtils = require("../util/ast-utils");
|
9
9
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
// Helpers
|
@@ -15,15 +15,6 @@ const PATTERN_TYPE = /^(?:.+?Pattern|RestElement|SpreadProperty|ExperimentalRest
|
|
15
15
|
const DECLARATION_HOST_TYPE = /^(?:Program|BlockStatement|SwitchCase)$/;
|
16
16
|
const DESTRUCTURING_HOST_TYPE = /^(?:VariableDeclarator|AssignmentExpression)$/;
|
17
17
|
|
18
|
-
/**
|
19
|
-
* Adds multiple items to the tail of an array.
|
20
|
-
*
|
21
|
-
* @param {any[]} array - A destination to add.
|
22
|
-
* @param {any[]} values - Items to be added.
|
23
|
-
* @returns {void}
|
24
|
-
*/
|
25
|
-
const pushAll = Function.apply.bind(Array.prototype.push);
|
26
|
-
|
27
18
|
/**
|
28
19
|
* Checks whether a given node is located at `ForStatement.init` or not.
|
29
20
|
*
|
@@ -364,7 +355,7 @@ module.exports = {
|
|
364
355
|
|
365
356
|
VariableDeclaration(node) {
|
366
357
|
if (node.kind === "let" && !isInitOfForStatement(node)) {
|
367
|
-
|
358
|
+
variables.push(...context.getDeclaredVariables(node));
|
368
359
|
}
|
369
360
|
}
|
370
361
|
};
|
@@ -12,7 +12,7 @@ const {
|
|
12
12
|
isOpeningParenToken,
|
13
13
|
isClosingParenToken,
|
14
14
|
isParenthesised
|
15
|
-
} = require("../ast-utils");
|
15
|
+
} = require("../util/ast-utils");
|
16
16
|
|
17
17
|
const ANY_SPACE = /\s/;
|
18
18
|
|
@@ -180,8 +180,14 @@ function defineFixer(node, sourceCode) {
|
|
180
180
|
for (const innerParen of innerParens) {
|
181
181
|
yield fixer.remove(innerParen);
|
182
182
|
}
|
183
|
-
|
184
|
-
|
183
|
+
const leftRange = [left.range[0], getEndWithSpaces(left, sourceCode)];
|
184
|
+
const rightRange = [
|
185
|
+
Math.max(getStartWithSpaces(right, sourceCode), leftRange[1]), // Ensure ranges don't overlap
|
186
|
+
right.range[1]
|
187
|
+
];
|
188
|
+
|
189
|
+
yield fixer.removeRange(leftRange);
|
190
|
+
yield fixer.removeRange(rightRange);
|
185
191
|
|
186
192
|
// Remove the comma of this argument if it's duplication.
|
187
193
|
if (
|
@@ -98,7 +98,7 @@ module.exports = {
|
|
98
98
|
CallExpression(node) {
|
99
99
|
const methodName = (node.callee.property || {}).name;
|
100
100
|
const isReflectCall = (node.callee.object || {}).name === "Reflect";
|
101
|
-
const hasReflectSubsitute =
|
101
|
+
const hasReflectSubsitute = Object.prototype.hasOwnProperty.call(reflectSubsitutes, methodName);
|
102
102
|
const userConfiguredException = exceptions.indexOf(methodName) !== -1;
|
103
103
|
|
104
104
|
if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Helpers
|
package/lib/rules/quotes.js
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Constants
|
package/lib/rules/radix.js
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Helpers
|
@@ -0,0 +1,239 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview disallow assignments that can lead to race conditions due to usage of `await` or `yield`
|
3
|
+
* @author Teddy Katz
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
const astUtils = require("../util/ast-utils");
|
8
|
+
|
9
|
+
//------------------------------------------------------------------------------
|
10
|
+
// Rule Definition
|
11
|
+
//------------------------------------------------------------------------------
|
12
|
+
|
13
|
+
module.exports = {
|
14
|
+
meta: {
|
15
|
+
docs: {
|
16
|
+
description: "disallow assignments that can lead to race conditions due to usage of `await` or `yield`",
|
17
|
+
category: "Possible Errors",
|
18
|
+
recommended: false,
|
19
|
+
url: "https://eslint.org/docs/rules/require-atomic-updates"
|
20
|
+
},
|
21
|
+
fixable: null,
|
22
|
+
schema: [],
|
23
|
+
messages: {
|
24
|
+
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`."
|
25
|
+
}
|
26
|
+
},
|
27
|
+
|
28
|
+
create(context) {
|
29
|
+
const sourceCode = context.getSourceCode();
|
30
|
+
const identifierToSurroundingFunctionMap = new WeakMap();
|
31
|
+
const expressionsByCodePathSegment = new Map();
|
32
|
+
|
33
|
+
//----------------------------------------------------------------------
|
34
|
+
// Helpers
|
35
|
+
//----------------------------------------------------------------------
|
36
|
+
|
37
|
+
const resolvedVariableCache = new WeakMap();
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Gets the variable scope around this variable reference
|
41
|
+
* @param {ASTNode} identifier An `Identifier` AST node
|
42
|
+
* @returns {Scope|null} An escope Scope
|
43
|
+
*/
|
44
|
+
function getScope(identifier) {
|
45
|
+
for (let currentNode = identifier; currentNode; currentNode = currentNode.parent) {
|
46
|
+
const scope = sourceCode.scopeManager.acquire(currentNode, true);
|
47
|
+
|
48
|
+
if (scope) {
|
49
|
+
return scope;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
return null;
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Resolves a given identifier to a given scope
|
57
|
+
* @param {ASTNode} identifier An `Identifier` AST node
|
58
|
+
* @param {Scope} scope An escope Scope
|
59
|
+
* @returns {Variable|null} An escope Variable corresponding to the given identifier
|
60
|
+
*/
|
61
|
+
function resolveVariableInScope(identifier, scope) {
|
62
|
+
return scope.variables.find(variable => variable.name === identifier.name) ||
|
63
|
+
(scope.upper ? resolveVariableInScope(identifier, scope.upper) : null);
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Resolves an identifier to a variable
|
68
|
+
* @param {ASTNode} identifier An identifier node
|
69
|
+
* @returns {Variable|null} The escope Variable that uses this identifier
|
70
|
+
*/
|
71
|
+
function resolveVariable(identifier) {
|
72
|
+
if (!resolvedVariableCache.has(identifier)) {
|
73
|
+
const surroundingScope = getScope(identifier);
|
74
|
+
|
75
|
+
if (surroundingScope) {
|
76
|
+
resolvedVariableCache.set(identifier, resolveVariableInScope(identifier, surroundingScope));
|
77
|
+
} else {
|
78
|
+
resolvedVariableCache.set(identifier, null);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
return resolvedVariableCache.get(identifier);
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Checks if an expression is a variable that can only be observed within the given function.
|
87
|
+
* @param {ASTNode} expression The expression to check
|
88
|
+
* @param {ASTNode} surroundingFunction The function node
|
89
|
+
* @returns {boolean} `true` if the expression is a variable which is local to the given function, and is never
|
90
|
+
* referenced in a closure.
|
91
|
+
*/
|
92
|
+
function isLocalVariableWithoutEscape(expression, surroundingFunction) {
|
93
|
+
if (expression.type !== "Identifier") {
|
94
|
+
return false;
|
95
|
+
}
|
96
|
+
|
97
|
+
const variable = resolveVariable(expression);
|
98
|
+
|
99
|
+
if (!variable) {
|
100
|
+
return false;
|
101
|
+
}
|
102
|
+
|
103
|
+
return variable.references.every(reference => identifierToSurroundingFunctionMap.get(reference.identifier) === surroundingFunction) &&
|
104
|
+
variable.defs.every(def => identifierToSurroundingFunctionMap.get(def.name) === surroundingFunction);
|
105
|
+
}
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Reports an AssignmentExpression node that has a non-atomic update
|
109
|
+
* @param {ASTNode} assignmentExpression The assignment that is potentially unsafe
|
110
|
+
* @returns {void}
|
111
|
+
*/
|
112
|
+
function reportAssignment(assignmentExpression) {
|
113
|
+
context.report({
|
114
|
+
node: assignmentExpression,
|
115
|
+
messageId: "nonAtomicUpdate",
|
116
|
+
data: {
|
117
|
+
value: sourceCode.getText(assignmentExpression.left)
|
118
|
+
}
|
119
|
+
});
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* If the control flow graph of a function enters an assignment expression, then does the
|
124
|
+
* both of the following steps in order (possibly with other steps in between) before exiting the
|
125
|
+
* assignment expression, then the assignment might be using an outdated value.
|
126
|
+
* 1. Enters a read of the variable or property assigned in the expression (not necessary if operator assignment is used)
|
127
|
+
* 2. Exits an `await` or `yield` expression
|
128
|
+
*
|
129
|
+
* This function checks for the outdated values and reports them.
|
130
|
+
* @param {CodePathSegment} codePathSegment The current code path segment to traverse
|
131
|
+
* @param {ASTNode} surroundingFunction The function node containing the code path segment
|
132
|
+
* @returns {void}
|
133
|
+
*/
|
134
|
+
function findOutdatedReads(
|
135
|
+
codePathSegment,
|
136
|
+
surroundingFunction,
|
137
|
+
{
|
138
|
+
seenSegments = new Set(),
|
139
|
+
openAssignmentsWithoutReads = new Set(),
|
140
|
+
openAssignmentsWithReads = new Set()
|
141
|
+
} = {}
|
142
|
+
) {
|
143
|
+
if (seenSegments.has(codePathSegment)) {
|
144
|
+
|
145
|
+
// An AssignmentExpression can't contain loops, so it's not necessary to reenter them with new state.
|
146
|
+
return;
|
147
|
+
}
|
148
|
+
|
149
|
+
expressionsByCodePathSegment.get(codePathSegment).forEach(({ entering, node }) => {
|
150
|
+
if (node.type === "AssignmentExpression") {
|
151
|
+
if (entering) {
|
152
|
+
(node.operator === "=" ? openAssignmentsWithoutReads : openAssignmentsWithReads).add(node);
|
153
|
+
} else {
|
154
|
+
openAssignmentsWithoutReads.delete(node);
|
155
|
+
openAssignmentsWithReads.delete(node);
|
156
|
+
}
|
157
|
+
} else if (!entering && (node.type === "AwaitExpression" || node.type === "YieldExpression")) {
|
158
|
+
[...openAssignmentsWithReads]
|
159
|
+
.filter(assignment => !isLocalVariableWithoutEscape(assignment.left, surroundingFunction))
|
160
|
+
.forEach(reportAssignment);
|
161
|
+
|
162
|
+
openAssignmentsWithReads.clear();
|
163
|
+
} else if (!entering && (node.type === "Identifier" || node.type === "MemberExpression")) {
|
164
|
+
[...openAssignmentsWithoutReads]
|
165
|
+
.filter(assignment => (
|
166
|
+
assignment.left !== node &&
|
167
|
+
assignment.left.type === node.type &&
|
168
|
+
astUtils.equalTokens(assignment.left, node, sourceCode)
|
169
|
+
))
|
170
|
+
.forEach(assignment => {
|
171
|
+
openAssignmentsWithoutReads.delete(assignment);
|
172
|
+
openAssignmentsWithReads.add(assignment);
|
173
|
+
});
|
174
|
+
}
|
175
|
+
});
|
176
|
+
|
177
|
+
codePathSegment.nextSegments.forEach(nextSegment => {
|
178
|
+
findOutdatedReads(
|
179
|
+
nextSegment,
|
180
|
+
surroundingFunction,
|
181
|
+
{
|
182
|
+
seenSegments: new Set(seenSegments).add(codePathSegment),
|
183
|
+
openAssignmentsWithoutReads: new Set(openAssignmentsWithoutReads),
|
184
|
+
openAssignmentsWithReads: new Set(openAssignmentsWithReads)
|
185
|
+
}
|
186
|
+
);
|
187
|
+
});
|
188
|
+
}
|
189
|
+
|
190
|
+
//----------------------------------------------------------------------
|
191
|
+
// Public
|
192
|
+
//----------------------------------------------------------------------
|
193
|
+
|
194
|
+
const currentCodePathSegmentStack = [];
|
195
|
+
let currentCodePathSegment = null;
|
196
|
+
const functionStack = [];
|
197
|
+
|
198
|
+
return {
|
199
|
+
onCodePathStart() {
|
200
|
+
currentCodePathSegmentStack.push(currentCodePathSegment);
|
201
|
+
},
|
202
|
+
|
203
|
+
onCodePathEnd(codePath, node) {
|
204
|
+
currentCodePathSegment = currentCodePathSegmentStack.pop();
|
205
|
+
|
206
|
+
if (astUtils.isFunction(node) && (node.async || node.generator)) {
|
207
|
+
findOutdatedReads(codePath.initialSegment, node);
|
208
|
+
}
|
209
|
+
},
|
210
|
+
|
211
|
+
onCodePathSegmentStart(segment) {
|
212
|
+
currentCodePathSegment = segment;
|
213
|
+
expressionsByCodePathSegment.set(segment, []);
|
214
|
+
},
|
215
|
+
|
216
|
+
"AssignmentExpression, Identifier, MemberExpression, AwaitExpression, YieldExpression"(node) {
|
217
|
+
expressionsByCodePathSegment.get(currentCodePathSegment).push({ entering: true, node });
|
218
|
+
},
|
219
|
+
|
220
|
+
"AssignmentExpression, Identifier, MemberExpression, AwaitExpression, YieldExpression:exit"(node) {
|
221
|
+
expressionsByCodePathSegment.get(currentCodePathSegment).push({ entering: false, node });
|
222
|
+
},
|
223
|
+
|
224
|
+
":function"(node) {
|
225
|
+
functionStack.push(node);
|
226
|
+
},
|
227
|
+
|
228
|
+
":function:exit"() {
|
229
|
+
functionStack.pop();
|
230
|
+
},
|
231
|
+
|
232
|
+
Identifier(node) {
|
233
|
+
if (functionStack.length) {
|
234
|
+
identifierToSurroundingFunctionMap.set(node, functionStack[functionStack.length - 1]);
|
235
|
+
}
|
236
|
+
}
|
237
|
+
};
|
238
|
+
}
|
239
|
+
};
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Helpers
|
@@ -0,0 +1,65 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Rule to enforce the use of `u` flag on RegExp.
|
3
|
+
* @author Toru Nagashima
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
//------------------------------------------------------------------------------
|
9
|
+
// Requirements
|
10
|
+
//------------------------------------------------------------------------------
|
11
|
+
|
12
|
+
const {
|
13
|
+
CALL,
|
14
|
+
CONSTRUCT,
|
15
|
+
ReferenceTracker,
|
16
|
+
getStringIfConstant
|
17
|
+
} = require("eslint-utils");
|
18
|
+
|
19
|
+
//------------------------------------------------------------------------------
|
20
|
+
// Rule Definition
|
21
|
+
//------------------------------------------------------------------------------
|
22
|
+
|
23
|
+
module.exports = {
|
24
|
+
meta: {
|
25
|
+
docs: {
|
26
|
+
description: "enforce the use of `u` flag on RegExp",
|
27
|
+
category: "Best Practices",
|
28
|
+
recommended: false,
|
29
|
+
url: "https://eslint.org/docs/rules/require-unicode-regexp"
|
30
|
+
},
|
31
|
+
messages: {
|
32
|
+
requireUFlag: "Use the 'u' flag."
|
33
|
+
},
|
34
|
+
schema: []
|
35
|
+
},
|
36
|
+
|
37
|
+
create(context) {
|
38
|
+
return {
|
39
|
+
"Literal[regex]"(node) {
|
40
|
+
const flags = node.regex.flags || "";
|
41
|
+
|
42
|
+
if (!flags.includes("u")) {
|
43
|
+
context.report({ node, messageId: "requireUFlag" });
|
44
|
+
}
|
45
|
+
},
|
46
|
+
|
47
|
+
Program() {
|
48
|
+
const scope = context.getScope();
|
49
|
+
const tracker = new ReferenceTracker(scope);
|
50
|
+
const trackMap = {
|
51
|
+
RegExp: { [CALL]: true, [CONSTRUCT]: true }
|
52
|
+
};
|
53
|
+
|
54
|
+
for (const { node } of tracker.iterateGlobalReferences(trackMap)) {
|
55
|
+
const flagsNode = node.arguments[1];
|
56
|
+
const flags = getStringIfConstant(flagsNode, scope);
|
57
|
+
|
58
|
+
if (!flagsNode || (typeof flags === "string" && !flags.includes("u"))) {
|
59
|
+
context.report({ node, messageId: "requireUFlag" });
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
};
|
64
|
+
}
|
65
|
+
};
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
-
const astUtils = require("../ast-utils");
|
8
|
+
const astUtils = require("../util/ast-utils");
|
9
9
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
// Rule Definition
|
@@ -46,10 +46,10 @@ module.exports = {
|
|
46
46
|
requireSpaceAfter = true;
|
47
47
|
|
48
48
|
if (typeof config === "object") {
|
49
|
-
if (
|
49
|
+
if (Object.prototype.hasOwnProperty.call(config, "before")) {
|
50
50
|
requireSpaceBefore = config.before;
|
51
51
|
}
|
52
|
-
if (
|
52
|
+
if (Object.prototype.hasOwnProperty.call(config, "after")) {
|
53
53
|
requireSpaceAfter = config.after;
|
54
54
|
}
|
55
55
|
}
|
package/lib/rules/semi-style.js
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Rule Definition
|
package/lib/rules/semi.js
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
11
|
const FixTracker = require("../util/fix-tracker");
|
12
|
-
const astUtils = require("../ast-utils");
|
12
|
+
const astUtils = require("../util/ast-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Rule Definition
|
package/lib/rules/sort-keys.js
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const astUtils = require("../ast-utils"),
|
12
|
+
const astUtils = require("../util/ast-utils"),
|
13
13
|
naturalCompare = require("natural-compare");
|
14
14
|
|
15
15
|
//------------------------------------------------------------------------------
|
@@ -8,7 +8,7 @@
|
|
8
8
|
// Requirements
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
-
const astUtils = require("../ast-utils");
|
11
|
+
const astUtils = require("../util/ast-utils");
|
12
12
|
|
13
13
|
//------------------------------------------------------------------------------
|
14
14
|
// Rule Definition
|