eslint 8.35.0 → 8.37.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/README.md +4 -9
- package/conf/rule-type-list.json +2 -2
- package/lib/cli-engine/file-enumerator.js +6 -4
- package/lib/config/default-config.js +1 -4
- package/lib/config/flat-config-array.js +63 -9
- package/lib/config/flat-config-schema.js +4 -18
- package/lib/eslint/eslint-helpers.js +2 -2
- package/lib/linter/linter.js +5 -30
- package/lib/rule-tester/flat-rule-tester.js +1 -1
- package/lib/rule-tester/rule-tester.js +1 -1
- package/lib/rules/camelcase.js +3 -2
- package/lib/rules/consistent-this.js +4 -2
- package/lib/rules/global-require.js +3 -1
- package/lib/rules/handle-callback-err.js +2 -1
- package/lib/rules/id-blacklist.js +3 -2
- package/lib/rules/id-denylist.js +3 -2
- package/lib/rules/id-match.js +3 -2
- package/lib/rules/logical-assignment-operators.js +3 -3
- package/lib/rules/multiline-comment-style.js +42 -3
- package/lib/rules/no-alert.js +3 -1
- package/lib/rules/no-catch-shadow.js +3 -1
- package/lib/rules/no-console.js +3 -2
- package/lib/rules/no-constant-binary-expression.js +4 -2
- package/lib/rules/no-constant-condition.js +3 -2
- package/lib/rules/no-control-regex.js +1 -1
- package/lib/rules/no-else-return.js +13 -12
- package/lib/rules/no-eval.js +4 -4
- package/lib/rules/no-extend-native.js +3 -2
- package/lib/rules/no-extra-boolean-cast.js +1 -1
- package/lib/rules/no-extra-parens.js +1 -1
- package/lib/rules/no-global-assign.js +3 -2
- package/lib/rules/no-implicit-globals.js +3 -2
- package/lib/rules/no-implied-eval.js +5 -4
- package/lib/rules/no-import-assign.js +4 -2
- package/lib/rules/no-invalid-regexp.js +1 -1
- package/lib/rules/no-invalid-this.js +2 -2
- package/lib/rules/no-label-var.js +2 -1
- package/lib/rules/no-lone-blocks.js +3 -2
- package/lib/rules/no-loop-func.js +3 -1
- package/lib/rules/no-misleading-character-class.js +12 -41
- package/lib/rules/no-native-reassign.js +3 -2
- package/lib/rules/no-new-func.js +7 -6
- package/lib/rules/no-new-native-nonconstructor.js +8 -6
- package/lib/rules/no-new-object.js +4 -1
- package/lib/rules/no-new-symbol.js +8 -6
- package/lib/rules/no-obj-calls.js +8 -6
- package/lib/rules/no-promise-executor-return.js +3 -2
- package/lib/rules/no-redeclare.js +3 -3
- package/lib/rules/no-regex-spaces.js +4 -2
- package/lib/rules/no-restricted-globals.js +4 -2
- package/lib/rules/no-setter-return.js +3 -2
- package/lib/rules/no-shadow.js +3 -2
- package/lib/rules/no-undef-init.js +1 -1
- package/lib/rules/no-undef.js +3 -2
- package/lib/rules/no-undefined.js +4 -2
- package/lib/rules/no-unmodified-loop-condition.js +2 -2
- package/lib/rules/no-unused-vars.js +1 -1
- package/lib/rules/no-use-before-define.js +3 -2
- package/lib/rules/no-useless-backreference.js +9 -7
- package/lib/rules/object-shorthand.js +3 -2
- package/lib/rules/prefer-arrow-callback.js +1 -1
- package/lib/rules/prefer-exponentiation-operator.js +5 -5
- package/lib/rules/prefer-named-capture-group.js +8 -8
- package/lib/rules/prefer-object-has-own.js +4 -2
- package/lib/rules/prefer-object-spread.js +12 -12
- package/lib/rules/prefer-regex-literals.js +26 -27
- package/lib/rules/prefer-rest-params.js +5 -2
- package/lib/rules/radix.js +11 -11
- package/lib/rules/require-atomic-updates.js +2 -2
- package/lib/rules/require-unicode-regexp.js +63 -7
- package/lib/rules/symbol-description.js +7 -5
- package/lib/rules/utils/regular-expressions.js +42 -0
- package/lib/rules/valid-typeof.js +3 -3
- package/lib/rules/wrap-iife.js +1 -1
- package/lib/source-code/source-code.js +52 -1
- package/lib/source-code/token-store/index.js +1 -1
- package/lib/source-code/token-store/utils.js +14 -4
- package/package.json +7 -7
@@ -453,10 +453,12 @@ module.exports = {
|
|
453
453
|
},
|
454
454
|
|
455
455
|
create(context) {
|
456
|
+
const sourceCode = context.getSourceCode();
|
457
|
+
|
456
458
|
return {
|
457
459
|
LogicalExpression(node) {
|
458
460
|
const { operator, left } = node;
|
459
|
-
const scope =
|
461
|
+
const scope = sourceCode.getScope(node);
|
460
462
|
|
461
463
|
if ((operator === "&&" || operator === "||") && isConstant(scope, left, true)) {
|
462
464
|
context.report({ node: left, messageId: "constantShortCircuit", data: { property: "truthiness", operator } });
|
@@ -465,7 +467,7 @@ module.exports = {
|
|
465
467
|
}
|
466
468
|
},
|
467
469
|
BinaryExpression(node) {
|
468
|
-
const scope =
|
470
|
+
const scope = sourceCode.getScope(node);
|
469
471
|
const { right, left, operator } = node;
|
470
472
|
const rightConstantOperand = findBinaryExpressionConstantOperand(scope, left, right, operator);
|
471
473
|
const leftConstantOperand = findBinaryExpressionConstantOperand(scope, right, left, operator);
|
@@ -48,6 +48,7 @@ module.exports = {
|
|
48
48
|
const options = context.options[0] || {},
|
49
49
|
checkLoops = options.checkLoops !== false,
|
50
50
|
loopSetStack = [];
|
51
|
+
const sourceCode = context.getSourceCode();
|
51
52
|
|
52
53
|
let loopsInCurrentScope = new Set();
|
53
54
|
|
@@ -62,7 +63,7 @@ module.exports = {
|
|
62
63
|
* @private
|
63
64
|
*/
|
64
65
|
function trackConstantConditionLoop(node) {
|
65
|
-
if (node.test && isConstant(
|
66
|
+
if (node.test && isConstant(sourceCode.getScope(node), node.test, true)) {
|
66
67
|
loopsInCurrentScope.add(node);
|
67
68
|
}
|
68
69
|
}
|
@@ -87,7 +88,7 @@ module.exports = {
|
|
87
88
|
* @private
|
88
89
|
*/
|
89
90
|
function reportIfConstant(node) {
|
90
|
-
if (node.test && isConstant(
|
91
|
+
if (node.test && isConstant(sourceCode.getScope(node), node.test, true)) {
|
91
92
|
context.report({ node: node.test, messageId: "unexpected" });
|
92
93
|
}
|
93
94
|
}
|
@@ -47,6 +47,8 @@ module.exports = {
|
|
47
47
|
|
48
48
|
create(context) {
|
49
49
|
|
50
|
+
const sourceCode = context.getSourceCode();
|
51
|
+
|
50
52
|
//--------------------------------------------------------------------------
|
51
53
|
// Helpers
|
52
54
|
//--------------------------------------------------------------------------
|
@@ -169,25 +171,24 @@ module.exports = {
|
|
169
171
|
|
170
172
|
/**
|
171
173
|
* Display the context report if rule is violated
|
172
|
-
* @param {Node}
|
174
|
+
* @param {Node} elseNode The 'else' node
|
173
175
|
* @returns {void}
|
174
176
|
*/
|
175
|
-
function displayReport(
|
176
|
-
const currentScope =
|
177
|
+
function displayReport(elseNode) {
|
178
|
+
const currentScope = sourceCode.getScope(elseNode.parent);
|
177
179
|
|
178
180
|
context.report({
|
179
|
-
node,
|
181
|
+
node: elseNode,
|
180
182
|
messageId: "unexpected",
|
181
183
|
fix(fixer) {
|
182
184
|
|
183
|
-
if (!isSafeFromNameCollisions(
|
185
|
+
if (!isSafeFromNameCollisions(elseNode, currentScope)) {
|
184
186
|
return null;
|
185
187
|
}
|
186
188
|
|
187
|
-
const
|
188
|
-
const startToken = sourceCode.getFirstToken(node);
|
189
|
+
const startToken = sourceCode.getFirstToken(elseNode);
|
189
190
|
const elseToken = sourceCode.getTokenBefore(startToken);
|
190
|
-
const source = sourceCode.getText(
|
191
|
+
const source = sourceCode.getText(elseNode);
|
191
192
|
const lastIfToken = sourceCode.getTokenBefore(elseToken);
|
192
193
|
let fixedSource, firstTokenOfElseBlock;
|
193
194
|
|
@@ -203,14 +204,14 @@ module.exports = {
|
|
203
204
|
* safe to remove the else keyword, because ASI will not add a semicolon
|
204
205
|
* after the if block
|
205
206
|
*/
|
206
|
-
const ifBlockMaybeUnsafe =
|
207
|
+
const ifBlockMaybeUnsafe = elseNode.parent.consequent.type !== "BlockStatement" && lastIfToken.value !== ";";
|
207
208
|
const elseBlockUnsafe = /^[([/+`-]/u.test(firstTokenOfElseBlock.value);
|
208
209
|
|
209
210
|
if (ifBlockMaybeUnsafe && elseBlockUnsafe) {
|
210
211
|
return null;
|
211
212
|
}
|
212
213
|
|
213
|
-
const endToken = sourceCode.getLastToken(
|
214
|
+
const endToken = sourceCode.getLastToken(elseNode);
|
214
215
|
const lastTokenOfElseBlock = sourceCode.getTokenBefore(endToken);
|
215
216
|
|
216
217
|
if (lastTokenOfElseBlock.value !== ";") {
|
@@ -244,8 +245,8 @@ module.exports = {
|
|
244
245
|
* Also, to avoid name collisions between two else blocks.
|
245
246
|
*/
|
246
247
|
return new FixTracker(fixer, sourceCode)
|
247
|
-
.retainEnclosingFunction(
|
248
|
-
.replaceTextRange([elseToken.range[0],
|
248
|
+
.retainEnclosingFunction(elseNode)
|
249
|
+
.replaceTextRange([elseToken.range[0], elseNode.range[1]], fixedSource);
|
249
250
|
}
|
250
251
|
});
|
251
252
|
}
|
package/lib/rules/no-eval.js
CHANGED
@@ -84,7 +84,7 @@ module.exports = {
|
|
84
84
|
* @returns {void}
|
85
85
|
*/
|
86
86
|
function enterThisScope(node) {
|
87
|
-
const strict =
|
87
|
+
const strict = sourceCode.getScope(node).isStrict;
|
88
88
|
|
89
89
|
funcInfo = {
|
90
90
|
upper: funcInfo,
|
@@ -221,7 +221,7 @@ module.exports = {
|
|
221
221
|
},
|
222
222
|
|
223
223
|
Program(node) {
|
224
|
-
const scope =
|
224
|
+
const scope = sourceCode.getScope(node),
|
225
225
|
features = context.parserOptions.ecmaFeatures || {},
|
226
226
|
strict =
|
227
227
|
scope.isStrict ||
|
@@ -239,8 +239,8 @@ module.exports = {
|
|
239
239
|
};
|
240
240
|
},
|
241
241
|
|
242
|
-
"Program:exit"() {
|
243
|
-
const globalScope =
|
242
|
+
"Program:exit"(node) {
|
243
|
+
const globalScope = sourceCode.getScope(node);
|
244
244
|
|
245
245
|
exitThisScope();
|
246
246
|
reportAccessingEval(globalScope);
|
@@ -51,6 +51,7 @@ module.exports = {
|
|
51
51
|
create(context) {
|
52
52
|
|
53
53
|
const config = context.options[0] || {};
|
54
|
+
const sourceCode = context.getSourceCode();
|
54
55
|
const exceptions = new Set(config.exceptions || []);
|
55
56
|
const modifiedBuiltins = new Set(
|
56
57
|
Object.keys(globals.builtin)
|
@@ -159,8 +160,8 @@ module.exports = {
|
|
159
160
|
|
160
161
|
return {
|
161
162
|
|
162
|
-
"Program:exit"() {
|
163
|
-
const globalScope =
|
163
|
+
"Program:exit"(node) {
|
164
|
+
const globalScope = sourceCode.getScope(node);
|
164
165
|
|
165
166
|
modifiedBuiltins.forEach(builtin => {
|
166
167
|
const builtinVar = globalScope.set.get(builtin);
|
@@ -10,7 +10,7 @@
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const astUtils = require("./utils/ast-utils");
|
13
|
-
const eslintUtils = require("eslint-utils");
|
13
|
+
const eslintUtils = require("@eslint-community/eslint-utils");
|
14
14
|
|
15
15
|
const precedence = astUtils.getPrecedence;
|
16
16
|
|
@@ -8,7 +8,7 @@
|
|
8
8
|
// Rule Definition
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
-
const { isParenthesized: isParenthesizedRaw } = require("eslint-utils");
|
11
|
+
const { isParenthesized: isParenthesizedRaw } = require("@eslint-community/eslint-utils");
|
12
12
|
const astUtils = require("./utils/ast-utils.js");
|
13
13
|
|
14
14
|
/** @type {import('../shared/types').Rule} */
|
@@ -41,6 +41,7 @@ module.exports = {
|
|
41
41
|
|
42
42
|
create(context) {
|
43
43
|
const config = context.options[0];
|
44
|
+
const sourceCode = context.getSourceCode();
|
44
45
|
const exceptions = (config && config.exceptions) || [];
|
45
46
|
|
46
47
|
/**
|
@@ -84,8 +85,8 @@ module.exports = {
|
|
84
85
|
}
|
85
86
|
|
86
87
|
return {
|
87
|
-
Program() {
|
88
|
-
const globalScope =
|
88
|
+
Program(node) {
|
89
|
+
const globalScope = sourceCode.getScope(node);
|
89
90
|
|
90
91
|
globalScope.variables.forEach(checkVariable);
|
91
92
|
}
|
@@ -43,6 +43,7 @@ module.exports = {
|
|
43
43
|
create(context) {
|
44
44
|
|
45
45
|
const checkLexicalBindings = context.options[0] && context.options[0].lexicalBindings === true;
|
46
|
+
const sourceCode = context.getSourceCode();
|
46
47
|
|
47
48
|
/**
|
48
49
|
* Reports the node.
|
@@ -62,8 +63,8 @@ module.exports = {
|
|
62
63
|
}
|
63
64
|
|
64
65
|
return {
|
65
|
-
Program() {
|
66
|
-
const scope =
|
66
|
+
Program(node) {
|
67
|
+
const scope = sourceCode.getScope(node);
|
67
68
|
|
68
69
|
scope.variables.forEach(variable => {
|
69
70
|
|
@@ -10,7 +10,7 @@
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const astUtils = require("./utils/ast-utils");
|
13
|
-
const { getStaticValue } = require("eslint-utils");
|
13
|
+
const { getStaticValue } = require("@eslint-community/eslint-utils");
|
14
14
|
|
15
15
|
//------------------------------------------------------------------------------
|
16
16
|
// Rule Definition
|
@@ -37,6 +37,7 @@ module.exports = {
|
|
37
37
|
create(context) {
|
38
38
|
const GLOBAL_CANDIDATES = Object.freeze(["global", "window", "globalThis"]);
|
39
39
|
const EVAL_LIKE_FUNC_PATTERN = /^(?:set(?:Interval|Timeout)|execScript)$/u;
|
40
|
+
const sourceCode = context.getSourceCode();
|
40
41
|
|
41
42
|
/**
|
42
43
|
* Checks whether a node is evaluated as a string or not.
|
@@ -66,7 +67,7 @@ module.exports = {
|
|
66
67
|
|
67
68
|
if (firstArgument) {
|
68
69
|
|
69
|
-
const staticValue = getStaticValue(firstArgument,
|
70
|
+
const staticValue = getStaticValue(firstArgument, sourceCode.getScope(node));
|
70
71
|
const isStaticString = staticValue && typeof staticValue.value === "string";
|
71
72
|
const isString = isStaticString || isEvaluatedString(firstArgument);
|
72
73
|
|
@@ -117,8 +118,8 @@ module.exports = {
|
|
117
118
|
reportImpliedEvalCallExpression(node);
|
118
119
|
}
|
119
120
|
},
|
120
|
-
"Program:exit"() {
|
121
|
-
const globalScope =
|
121
|
+
"Program:exit"(node) {
|
122
|
+
const globalScope = sourceCode.getScope(node);
|
122
123
|
|
123
124
|
GLOBAL_CANDIDATES
|
124
125
|
.map(candidate => astUtils.getVariableByName(globalScope, candidate))
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Helpers
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const { findVariable } = require("eslint-utils");
|
12
|
+
const { findVariable } = require("@eslint-community/eslint-utils");
|
13
13
|
const astUtils = require("./utils/ast-utils");
|
14
14
|
|
15
15
|
const WellKnownMutationFunctions = {
|
@@ -194,9 +194,11 @@ module.exports = {
|
|
194
194
|
},
|
195
195
|
|
196
196
|
create(context) {
|
197
|
+
const sourceCode = context.getSourceCode();
|
198
|
+
|
197
199
|
return {
|
198
200
|
ImportDeclaration(node) {
|
199
|
-
const scope =
|
201
|
+
const scope = sourceCode.getScope(node);
|
200
202
|
|
201
203
|
for (const variable of context.getDeclaredVariables(node)) {
|
202
204
|
const shouldCheckMembers = variable.defs.some(
|
@@ -8,7 +8,7 @@
|
|
8
8
|
// Requirements
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
-
const RegExpValidator = require("regexpp").RegExpValidator;
|
11
|
+
const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator;
|
12
12
|
const validator = new RegExpValidator();
|
13
13
|
const validFlags = /[dgimsuy]/gu;
|
14
14
|
const undefined1 = void 0;
|
@@ -95,7 +95,7 @@ module.exports = {
|
|
95
95
|
}
|
96
96
|
|
97
97
|
if (codePath.origin === "program") {
|
98
|
-
const scope =
|
98
|
+
const scope = sourceCode.getScope(node);
|
99
99
|
const features = context.parserOptions.ecmaFeatures || {};
|
100
100
|
|
101
101
|
// `this` at the top level of scripts always refers to the global object
|
@@ -120,7 +120,7 @@ module.exports = {
|
|
120
120
|
* always valid, so we can set `init: true` right away.
|
121
121
|
*/
|
122
122
|
stack.push({
|
123
|
-
init: !
|
123
|
+
init: !sourceCode.getScope(node).isStrict,
|
124
124
|
node,
|
125
125
|
valid: true
|
126
126
|
});
|
@@ -34,6 +34,7 @@ module.exports = {
|
|
34
34
|
},
|
35
35
|
|
36
36
|
create(context) {
|
37
|
+
const sourceCode = context.getSourceCode();
|
37
38
|
|
38
39
|
//--------------------------------------------------------------------------
|
39
40
|
// Helpers
|
@@ -59,7 +60,7 @@ module.exports = {
|
|
59
60
|
LabeledStatement(node) {
|
60
61
|
|
61
62
|
// Fetch the innermost scope.
|
62
|
-
const scope =
|
63
|
+
const scope = sourceCode.getScope(node);
|
63
64
|
|
64
65
|
/*
|
65
66
|
* Recursively find the identifier walking up the scope, starting
|
@@ -33,6 +33,7 @@ module.exports = {
|
|
33
33
|
// A stack of lone blocks to be checked for block-level bindings
|
34
34
|
const loneBlocks = [];
|
35
35
|
let ruleDef;
|
36
|
+
const sourceCode = context.getSourceCode();
|
36
37
|
|
37
38
|
/**
|
38
39
|
* Reports a node as invalid.
|
@@ -120,8 +121,8 @@ module.exports = {
|
|
120
121
|
}
|
121
122
|
};
|
122
123
|
|
123
|
-
ruleDef.FunctionDeclaration = function() {
|
124
|
-
if (
|
124
|
+
ruleDef.FunctionDeclaration = function(node) {
|
125
|
+
if (sourceCode.getScope(node).isStrict) {
|
125
126
|
markLoneBlock();
|
126
127
|
}
|
127
128
|
};
|
@@ -168,6 +168,8 @@ module.exports = {
|
|
168
168
|
|
169
169
|
create(context) {
|
170
170
|
|
171
|
+
const sourceCode = context.getSourceCode();
|
172
|
+
|
171
173
|
/**
|
172
174
|
* Reports functions which match the following condition:
|
173
175
|
*
|
@@ -183,7 +185,7 @@ module.exports = {
|
|
183
185
|
return;
|
184
186
|
}
|
185
187
|
|
186
|
-
const references =
|
188
|
+
const references = sourceCode.getScope(node).through;
|
187
189
|
const unsafeRefs = references.filter(r => !isSafe(loopNode, r)).map(r => r.identifier.name);
|
188
190
|
|
189
191
|
if (unsafeRefs.length > 0) {
|
@@ -3,17 +3,16 @@
|
|
3
3
|
*/
|
4
4
|
"use strict";
|
5
5
|
|
6
|
-
const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("eslint-utils");
|
7
|
-
const {
|
6
|
+
const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils");
|
7
|
+
const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp");
|
8
8
|
const { isCombiningCharacter, isEmojiModifier, isRegionalIndicatorSymbol, isSurrogatePair } = require("./utils/unicode");
|
9
9
|
const astUtils = require("./utils/ast-utils.js");
|
10
|
+
const { isValidWithUnicodeFlag } = require("./utils/regular-expressions");
|
10
11
|
|
11
12
|
//------------------------------------------------------------------------------
|
12
13
|
// Helpers
|
13
14
|
//------------------------------------------------------------------------------
|
14
15
|
|
15
|
-
const REGEXPP_LATEST_ECMA_VERSION = 2022;
|
16
|
-
|
17
16
|
/**
|
18
17
|
* Iterate character sequences of a given nodes.
|
19
18
|
*
|
@@ -185,46 +184,18 @@ module.exports = {
|
|
185
184
|
}
|
186
185
|
}
|
187
186
|
|
188
|
-
/**
|
189
|
-
* Checks if the given regular expression pattern would be valid with the `u` flag.
|
190
|
-
* @param {string} pattern The regular expression pattern to verify.
|
191
|
-
* @returns {boolean} `true` if the pattern would be valid with the `u` flag.
|
192
|
-
* `false` if the pattern would be invalid with the `u` flag or the configured
|
193
|
-
* ecmaVersion doesn't support the `u` flag.
|
194
|
-
*/
|
195
|
-
function isValidWithUnicodeFlag(pattern) {
|
196
|
-
const { ecmaVersion } = context.languageOptions;
|
197
|
-
|
198
|
-
// ecmaVersion <= 5 doesn't support the 'u' flag
|
199
|
-
if (ecmaVersion <= 5) {
|
200
|
-
return false;
|
201
|
-
}
|
202
|
-
|
203
|
-
const validator = new RegExpValidator({
|
204
|
-
ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION)
|
205
|
-
});
|
206
|
-
|
207
|
-
try {
|
208
|
-
validator.validatePattern(pattern, void 0, void 0, /* uFlag = */ true);
|
209
|
-
} catch {
|
210
|
-
return false;
|
211
|
-
}
|
212
|
-
|
213
|
-
return true;
|
214
|
-
}
|
215
|
-
|
216
187
|
return {
|
217
188
|
"Literal[regex]"(node) {
|
218
189
|
verify(node, node.regex.pattern, node.regex.flags, fixer => {
|
219
|
-
if (!isValidWithUnicodeFlag(node.regex.pattern)) {
|
190
|
+
if (!isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern)) {
|
220
191
|
return null;
|
221
192
|
}
|
222
193
|
|
223
194
|
return fixer.insertTextAfter(node, "u");
|
224
195
|
});
|
225
196
|
},
|
226
|
-
"Program"() {
|
227
|
-
const scope =
|
197
|
+
"Program"(node) {
|
198
|
+
const scope = sourceCode.getScope(node);
|
228
199
|
const tracker = new ReferenceTracker(scope);
|
229
200
|
|
230
201
|
/*
|
@@ -232,22 +203,22 @@ module.exports = {
|
|
232
203
|
* E.g., `new RegExp()`, `RegExp()`, `new window.RegExp()`,
|
233
204
|
* `const {RegExp: a} = window; new a()`, etc...
|
234
205
|
*/
|
235
|
-
for (const { node } of tracker.iterateGlobalReferences({
|
206
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences({
|
236
207
|
RegExp: { [CALL]: true, [CONSTRUCT]: true }
|
237
208
|
})) {
|
238
|
-
const [patternNode, flagsNode] =
|
209
|
+
const [patternNode, flagsNode] = refNode.arguments;
|
239
210
|
const pattern = getStringIfConstant(patternNode, scope);
|
240
211
|
const flags = getStringIfConstant(flagsNode, scope);
|
241
212
|
|
242
213
|
if (typeof pattern === "string") {
|
243
|
-
verify(
|
214
|
+
verify(refNode, pattern, flags || "", fixer => {
|
244
215
|
|
245
|
-
if (!isValidWithUnicodeFlag(pattern)) {
|
216
|
+
if (!isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, pattern)) {
|
246
217
|
return null;
|
247
218
|
}
|
248
219
|
|
249
|
-
if (
|
250
|
-
const penultimateToken = sourceCode.getLastToken(
|
220
|
+
if (refNode.arguments.length === 1) {
|
221
|
+
const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis
|
251
222
|
|
252
223
|
return fixer.insertTextAfter(
|
253
224
|
penultimateToken,
|
@@ -47,6 +47,7 @@ module.exports = {
|
|
47
47
|
create(context) {
|
48
48
|
const config = context.options[0];
|
49
49
|
const exceptions = (config && config.exceptions) || [];
|
50
|
+
const sourceCode = context.getSourceCode();
|
50
51
|
|
51
52
|
/**
|
52
53
|
* Reports write references.
|
@@ -87,8 +88,8 @@ module.exports = {
|
|
87
88
|
}
|
88
89
|
|
89
90
|
return {
|
90
|
-
Program() {
|
91
|
-
const globalScope =
|
91
|
+
Program(node) {
|
92
|
+
const globalScope = sourceCode.getScope(node);
|
92
93
|
|
93
94
|
globalScope.variables.forEach(checkVariable);
|
94
95
|
}
|
package/lib/rules/no-new-func.js
CHANGED
@@ -40,27 +40,28 @@ module.exports = {
|
|
40
40
|
},
|
41
41
|
|
42
42
|
create(context) {
|
43
|
+
const sourceCode = context.getSourceCode();
|
43
44
|
|
44
45
|
return {
|
45
|
-
"Program:exit"() {
|
46
|
-
const globalScope =
|
46
|
+
"Program:exit"(node) {
|
47
|
+
const globalScope = sourceCode.getScope(node);
|
47
48
|
const variable = globalScope.set.get("Function");
|
48
49
|
|
49
50
|
if (variable && variable.defs.length === 0) {
|
50
51
|
variable.references.forEach(ref => {
|
51
|
-
const
|
52
|
-
const { parent } =
|
52
|
+
const idNode = ref.identifier;
|
53
|
+
const { parent } = idNode;
|
53
54
|
let evalNode;
|
54
55
|
|
55
56
|
if (parent) {
|
56
|
-
if (
|
57
|
+
if (idNode === parent.callee && (
|
57
58
|
parent.type === "NewExpression" ||
|
58
59
|
parent.type === "CallExpression"
|
59
60
|
)) {
|
60
61
|
evalNode = parent;
|
61
62
|
} else if (
|
62
63
|
parent.type === "MemberExpression" &&
|
63
|
-
|
64
|
+
idNode === parent.object &&
|
64
65
|
callMethods.has(astUtils.getStaticPropertyName(parent))
|
65
66
|
) {
|
66
67
|
const maybeCallee = parent.parent.type === "ChainExpression" ? parent.parent : parent;
|
@@ -35,21 +35,23 @@ module.exports = {
|
|
35
35
|
|
36
36
|
create(context) {
|
37
37
|
|
38
|
+
const sourceCode = context.getSourceCode();
|
39
|
+
|
38
40
|
return {
|
39
|
-
"Program:exit"() {
|
40
|
-
const globalScope =
|
41
|
+
"Program:exit"(node) {
|
42
|
+
const globalScope = sourceCode.getScope(node);
|
41
43
|
|
42
44
|
for (const nonConstructorName of nonConstructorGlobalFunctionNames) {
|
43
45
|
const variable = globalScope.set.get(nonConstructorName);
|
44
46
|
|
45
47
|
if (variable && variable.defs.length === 0) {
|
46
48
|
variable.references.forEach(ref => {
|
47
|
-
const
|
48
|
-
const parent =
|
49
|
+
const idNode = ref.identifier;
|
50
|
+
const parent = idNode.parent;
|
49
51
|
|
50
|
-
if (parent && parent.type === "NewExpression" && parent.callee ===
|
52
|
+
if (parent && parent.type === "NewExpression" && parent.callee === idNode) {
|
51
53
|
context.report({
|
52
|
-
node,
|
54
|
+
node: idNode,
|
53
55
|
messageId: "noNewNonconstructor",
|
54
56
|
data: { name: nonConstructorName }
|
55
57
|
});
|
@@ -34,10 +34,13 @@ module.exports = {
|
|
34
34
|
},
|
35
35
|
|
36
36
|
create(context) {
|
37
|
+
|
38
|
+
const sourceCode = context.getSourceCode();
|
39
|
+
|
37
40
|
return {
|
38
41
|
NewExpression(node) {
|
39
42
|
const variable = astUtils.getVariableByName(
|
40
|
-
|
43
|
+
sourceCode.getScope(node),
|
41
44
|
node.callee.name
|
42
45
|
);
|
43
46
|
|
@@ -29,19 +29,21 @@ module.exports = {
|
|
29
29
|
|
30
30
|
create(context) {
|
31
31
|
|
32
|
+
const sourceCode = context.getSourceCode();
|
33
|
+
|
32
34
|
return {
|
33
|
-
"Program:exit"() {
|
34
|
-
const globalScope =
|
35
|
+
"Program:exit"(node) {
|
36
|
+
const globalScope = sourceCode.getScope(node);
|
35
37
|
const variable = globalScope.set.get("Symbol");
|
36
38
|
|
37
39
|
if (variable && variable.defs.length === 0) {
|
38
40
|
variable.references.forEach(ref => {
|
39
|
-
const
|
40
|
-
const parent =
|
41
|
+
const idNode = ref.identifier;
|
42
|
+
const parent = idNode.parent;
|
41
43
|
|
42
|
-
if (parent && parent.type === "NewExpression" && parent.callee ===
|
44
|
+
if (parent && parent.type === "NewExpression" && parent.callee === idNode) {
|
43
45
|
context.report({
|
44
|
-
node,
|
46
|
+
node: idNode,
|
45
47
|
messageId: "noNewSymbol"
|
46
48
|
});
|
47
49
|
}
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const { CALL, CONSTRUCT, ReferenceTracker } = require("eslint-utils");
|
12
|
+
const { CALL, CONSTRUCT, ReferenceTracker } = require("@eslint-community/eslint-utils");
|
13
13
|
const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
|
14
14
|
|
15
15
|
//------------------------------------------------------------------------------
|
@@ -58,9 +58,11 @@ module.exports = {
|
|
58
58
|
|
59
59
|
create(context) {
|
60
60
|
|
61
|
+
const sourceCode = context.getSourceCode();
|
62
|
+
|
61
63
|
return {
|
62
|
-
Program() {
|
63
|
-
const scope =
|
64
|
+
Program(node) {
|
65
|
+
const scope = sourceCode.getScope(node);
|
64
66
|
const tracker = new ReferenceTracker(scope);
|
65
67
|
const traceMap = {};
|
66
68
|
|
@@ -71,12 +73,12 @@ module.exports = {
|
|
71
73
|
};
|
72
74
|
}
|
73
75
|
|
74
|
-
for (const { node, path } of tracker.iterateGlobalReferences(traceMap)) {
|
75
|
-
const name = getReportNodeName(
|
76
|
+
for (const { node: refNode, path } of tracker.iterateGlobalReferences(traceMap)) {
|
77
|
+
const name = getReportNodeName(refNode.callee);
|
76
78
|
const ref = path[0];
|
77
79
|
const messageId = name === ref ? "unexpectedCall" : "unexpectedRefCall";
|
78
80
|
|
79
|
-
context.report({ node, messageId, data: { name, ref } });
|
81
|
+
context.report({ node: refNode, messageId, data: { name, ref } });
|
80
82
|
}
|
81
83
|
}
|
82
84
|
};
|