eslint 8.34.0 → 8.39.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 +7 -18
- package/conf/rule-type-list.json +2 -2
- package/lib/cli-engine/cli-engine.js +2 -2
- package/lib/cli-engine/file-enumerator.js +8 -6
- package/lib/config/default-config.js +1 -4
- package/lib/config/flat-config-array.js +77 -17
- package/lib/config/flat-config-schema.js +4 -18
- package/lib/eslint/eslint-helpers.js +3 -3
- package/lib/linter/linter.js +4 -77
- package/lib/rule-tester/flat-rule-tester.js +1 -1
- package/lib/rule-tester/rule-tester.js +1 -1
- package/lib/rules/block-scoped-var.js +2 -1
- package/lib/rules/camelcase.js +5 -4
- package/lib/rules/consistent-this.js +4 -2
- package/lib/rules/func-names.js +1 -1
- package/lib/rules/global-require.js +4 -2
- 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/lines-around-comment.js +11 -0
- package/lib/rules/logical-assignment-operators.js +4 -4
- package/lib/rules/multiline-comment-style.js +42 -3
- package/lib/rules/new-parens.js +5 -14
- package/lib/rules/no-alert.js +3 -1
- package/lib/rules/no-catch-shadow.js +3 -1
- package/lib/rules/no-class-assign.js +3 -1
- package/lib/rules/no-console.js +3 -2
- package/lib/rules/no-const-assign.js +3 -1
- package/lib/rules/no-constant-binary-expression.js +36 -27
- package/lib/rules/no-constant-condition.js +3 -2
- package/lib/rules/no-control-regex.js +1 -1
- package/lib/rules/no-div-regex.js +1 -1
- package/lib/rules/no-dupe-args.js +3 -1
- package/lib/rules/no-else-return.js +13 -12
- package/lib/rules/no-eval.js +5 -5
- package/lib/rules/no-ex-assign.js +3 -1
- 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-func-assign.js +3 -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 +5 -3
- 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 +8 -6
- package/lib/rules/no-lonely-if.js +2 -3
- 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-param-reassign.js +2 -1
- 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-exports.js +2 -1
- package/lib/rules/no-restricted-globals.js +4 -2
- package/lib/rules/no-setter-return.js +3 -2
- package/lib/rules/no-shadow-restricted-names.js +2 -1
- 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-underscore-dangle.js +2 -1
- package/lib/rules/no-unmodified-loop-condition.js +2 -2
- package/lib/rules/no-unused-expressions.js +4 -5
- package/lib/rules/no-unused-vars.js +2 -2
- package/lib/rules/no-use-before-define.js +3 -2
- package/lib/rules/no-useless-backreference.js +9 -7
- package/lib/rules/no-useless-return.js +1 -1
- package/lib/rules/no-var.js +2 -2
- package/lib/rules/object-shorthand.js +3 -2
- package/lib/rules/prefer-arrow-callback.js +2 -2
- package/lib/rules/prefer-const.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 -13
- package/lib/rules/prefer-promise-reject-errors.js +2 -1
- 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 +67 -7
- package/lib/rules/symbol-description.js +7 -5
- package/lib/rules/utils/regular-expressions.js +42 -0
- package/lib/rules/valid-typeof.js +4 -4
- package/lib/rules/wrap-iife.js +1 -1
- package/lib/rules/wrap-regex.js +2 -3
- package/lib/rules/yoda.js +1 -1
- package/lib/source-code/source-code.js +137 -1
- package/lib/source-code/token-store/index.js +1 -1
- package/lib/source-code/token-store/utils.js +35 -20
- package/messages/no-config-found.js +1 -1
- package/package.json +13 -8
- package/conf/eslint-all.js +0 -31
- package/conf/eslint-recommended.js +0 -76
@@ -10,7 +10,7 @@
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const astUtils = require("./utils/ast-utils");
|
13
|
-
const { findVariable } = require("eslint-utils");
|
13
|
+
const { findVariable } = require("@eslint-community/eslint-utils");
|
14
14
|
|
15
15
|
//------------------------------------------------------------------------------
|
16
16
|
// Helpers
|
@@ -156,6 +156,7 @@ module.exports = {
|
|
156
156
|
|
157
157
|
create(context) {
|
158
158
|
let funcInfo = null;
|
159
|
+
const sourceCode = context.getSourceCode();
|
159
160
|
|
160
161
|
/**
|
161
162
|
* Creates and pushes to the stack a function info object for the given function node.
|
@@ -163,7 +164,7 @@ module.exports = {
|
|
163
164
|
* @returns {void}
|
164
165
|
*/
|
165
166
|
function enterFunction(node) {
|
166
|
-
const outerScope = getOuterScope(
|
167
|
+
const outerScope = getOuterScope(sourceCode.getScope(node));
|
167
168
|
|
168
169
|
funcInfo = {
|
169
170
|
upper: funcInfo,
|
@@ -43,10 +43,11 @@ module.exports = {
|
|
43
43
|
|
44
44
|
|
45
45
|
const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]);
|
46
|
+
const sourceCode = context.getSourceCode();
|
46
47
|
|
47
48
|
return {
|
48
49
|
"VariableDeclaration, :function, CatchClause"(node) {
|
49
|
-
for (const variable of
|
50
|
+
for (const variable of sourceCode.getDeclaredVariables(node)) {
|
50
51
|
if (variable.defs.length > 0 && RESTRICTED.has(variable.name) && !safelyShadowsUndefined(variable)) {
|
51
52
|
context.report({
|
52
53
|
node: variable.defs[0].name,
|
package/lib/rules/no-shadow.js
CHANGED
@@ -67,6 +67,7 @@ module.exports = {
|
|
67
67
|
allow: (context.options[0] && context.options[0].allow) || [],
|
68
68
|
ignoreOnInitialization: context.options[0] && context.options[0].ignoreOnInitialization
|
69
69
|
};
|
70
|
+
const sourceCode = context.getSourceCode();
|
70
71
|
|
71
72
|
/**
|
72
73
|
* Checks whether or not a given location is inside of the range of a given node.
|
@@ -318,8 +319,8 @@ module.exports = {
|
|
318
319
|
}
|
319
320
|
|
320
321
|
return {
|
321
|
-
"Program:exit"() {
|
322
|
-
const globalScope =
|
322
|
+
"Program:exit"(node) {
|
323
|
+
const globalScope = sourceCode.getScope(node);
|
323
324
|
const stack = globalScope.childScopes.slice();
|
324
325
|
|
325
326
|
while (stack.length) {
|
@@ -39,7 +39,7 @@ module.exports = {
|
|
39
39
|
VariableDeclarator(node) {
|
40
40
|
const name = sourceCode.getText(node.id),
|
41
41
|
init = node.init && node.init.name,
|
42
|
-
scope =
|
42
|
+
scope = sourceCode.getScope(node),
|
43
43
|
undefinedVar = astUtils.getVariableByName(scope, "undefined"),
|
44
44
|
shadowed = undefinedVar && undefinedVar.defs.length > 0,
|
45
45
|
lastToken = sourceCode.getLastToken(node);
|
package/lib/rules/no-undef.js
CHANGED
@@ -54,10 +54,11 @@ module.exports = {
|
|
54
54
|
create(context) {
|
55
55
|
const options = context.options[0];
|
56
56
|
const considerTypeOf = options && options.typeof === true || false;
|
57
|
+
const sourceCode = context.getSourceCode();
|
57
58
|
|
58
59
|
return {
|
59
|
-
"Program:exit"(
|
60
|
-
const globalScope =
|
60
|
+
"Program:exit"(node) {
|
61
|
+
const globalScope = sourceCode.getScope(node);
|
61
62
|
|
62
63
|
globalScope.through.forEach(ref => {
|
63
64
|
const identifier = ref.identifier;
|
@@ -28,6 +28,8 @@ module.exports = {
|
|
28
28
|
|
29
29
|
create(context) {
|
30
30
|
|
31
|
+
const sourceCode = context.getSourceCode();
|
32
|
+
|
31
33
|
/**
|
32
34
|
* Report an invalid "undefined" identifier node.
|
33
35
|
* @param {ASTNode} node The node to report.
|
@@ -66,8 +68,8 @@ module.exports = {
|
|
66
68
|
}
|
67
69
|
|
68
70
|
return {
|
69
|
-
"Program:exit"() {
|
70
|
-
const globalScope =
|
71
|
+
"Program:exit"(node) {
|
72
|
+
const globalScope = sourceCode.getScope(node);
|
71
73
|
|
72
74
|
const stack = [globalScope];
|
73
75
|
|
@@ -84,6 +84,7 @@ module.exports = {
|
|
84
84
|
const allowFunctionParams = typeof options.allowFunctionParams !== "undefined" ? options.allowFunctionParams : true;
|
85
85
|
const allowInArrayDestructuring = typeof options.allowInArrayDestructuring !== "undefined" ? options.allowInArrayDestructuring : true;
|
86
86
|
const allowInObjectDestructuring = typeof options.allowInObjectDestructuring !== "undefined" ? options.allowInObjectDestructuring : true;
|
87
|
+
const sourceCode = context.getSourceCode();
|
87
88
|
|
88
89
|
//-------------------------------------------------------------------------
|
89
90
|
// Helpers
|
@@ -213,7 +214,7 @@ module.exports = {
|
|
213
214
|
* @private
|
214
215
|
*/
|
215
216
|
function checkForDanglingUnderscoreInVariableExpression(node) {
|
216
|
-
|
217
|
+
sourceCode.getDeclaredVariables(node).forEach(variable => {
|
217
218
|
const definition = variable.defs.find(def => def.node === node);
|
218
219
|
const identifierNode = definition.name;
|
219
220
|
const identifier = identifierNode.name;
|
@@ -109,12 +109,11 @@ module.exports = {
|
|
109
109
|
/**
|
110
110
|
* Detect if a Node is a directive.
|
111
111
|
* @param {ASTNode} node any node
|
112
|
-
* @param {ASTNode[]} ancestors the given node's ancestors
|
113
112
|
* @returns {boolean} whether the given node is considered a directive in its current position
|
114
113
|
*/
|
115
|
-
function isDirective(node
|
116
|
-
const parent =
|
117
|
-
grandparent =
|
114
|
+
function isDirective(node) {
|
115
|
+
const parent = node.parent,
|
116
|
+
grandparent = parent.parent;
|
118
117
|
|
119
118
|
/**
|
120
119
|
* https://tc39.es/ecma262/#directive-prologue
|
@@ -180,7 +179,7 @@ module.exports = {
|
|
180
179
|
|
181
180
|
return {
|
182
181
|
ExpressionStatement(node) {
|
183
|
-
if (Checker.isDisallowed(node.expression) && !isDirective(node
|
182
|
+
if (Checker.isDisallowed(node.expression) && !isDirective(node)) {
|
184
183
|
context.report({ node, messageId: "unusedExpression" });
|
185
184
|
}
|
186
185
|
}
|
@@ -555,7 +555,7 @@ module.exports = {
|
|
555
555
|
*/
|
556
556
|
function isAfterLastUsedArg(variable) {
|
557
557
|
const def = variable.defs[0];
|
558
|
-
const params =
|
558
|
+
const params = sourceCode.getDeclaredVariables(def.node);
|
559
559
|
const posteriorParams = params.slice(params.indexOf(variable) + 1);
|
560
560
|
|
561
561
|
// If any used parameters occur after this parameter, do not report.
|
@@ -673,7 +673,7 @@ module.exports = {
|
|
673
673
|
|
674
674
|
return {
|
675
675
|
"Program:exit"(programNode) {
|
676
|
-
const unusedVars = collectUnusedVariables(
|
676
|
+
const unusedVars = collectUnusedVariables(sourceCode.getScope(programNode), []);
|
677
677
|
|
678
678
|
for (let i = 0, l = unusedVars.length; i < l; ++i) {
|
679
679
|
const unusedVar = unusedVars[i];
|
@@ -258,6 +258,7 @@ module.exports = {
|
|
258
258
|
|
259
259
|
create(context) {
|
260
260
|
const options = parseOptions(context.options[0]);
|
261
|
+
const sourceCode = context.getSourceCode();
|
261
262
|
|
262
263
|
/**
|
263
264
|
* Determines whether a given reference should be checked.
|
@@ -339,8 +340,8 @@ module.exports = {
|
|
339
340
|
}
|
340
341
|
|
341
342
|
return {
|
342
|
-
Program() {
|
343
|
-
checkReferencesInScope(
|
343
|
+
Program(node) {
|
344
|
+
checkReferencesInScope(sourceCode.getScope(node));
|
344
345
|
}
|
345
346
|
};
|
346
347
|
}
|
@@ -9,8 +9,8 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("eslint-utils");
|
13
|
-
const { RegExpParser, visitRegExpAST } = require("regexpp");
|
12
|
+
const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils");
|
13
|
+
const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp");
|
14
14
|
|
15
15
|
//------------------------------------------------------------------------------
|
16
16
|
// Helpers
|
@@ -82,6 +82,8 @@ module.exports = {
|
|
82
82
|
|
83
83
|
create(context) {
|
84
84
|
|
85
|
+
const sourceCode = context.getSourceCode();
|
86
|
+
|
85
87
|
/**
|
86
88
|
* Checks and reports useless backreferences in the given regular expression.
|
87
89
|
* @param {ASTNode} node Node that represents regular expression. A regex literal or RegExp constructor call.
|
@@ -167,8 +169,8 @@ module.exports = {
|
|
167
169
|
|
168
170
|
checkRegex(node, pattern, flags);
|
169
171
|
},
|
170
|
-
Program() {
|
171
|
-
const scope =
|
172
|
+
Program(node) {
|
173
|
+
const scope = sourceCode.getScope(node),
|
172
174
|
tracker = new ReferenceTracker(scope),
|
173
175
|
traceMap = {
|
174
176
|
RegExp: {
|
@@ -177,13 +179,13 @@ module.exports = {
|
|
177
179
|
}
|
178
180
|
};
|
179
181
|
|
180
|
-
for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
|
181
|
-
const [patternNode, flagsNode] =
|
182
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) {
|
183
|
+
const [patternNode, flagsNode] = refNode.arguments,
|
182
184
|
pattern = getStringIfConstant(patternNode, scope),
|
183
185
|
flags = getStringIfConstant(flagsNode, scope);
|
184
186
|
|
185
187
|
if (typeof pattern === "string") {
|
186
|
-
checkRegex(
|
188
|
+
checkRegex(refNode, pattern, flags || "");
|
187
189
|
}
|
188
190
|
}
|
189
191
|
}
|
package/lib/rules/no-var.js
CHANGED
@@ -210,7 +210,7 @@ module.exports = {
|
|
210
210
|
if (!declarator.init) {
|
211
211
|
return false;
|
212
212
|
}
|
213
|
-
const variables =
|
213
|
+
const variables = sourceCode.getDeclaredVariables(declarator);
|
214
214
|
|
215
215
|
return variables.some(hasReferenceInTDZ(declarator.init));
|
216
216
|
}
|
@@ -268,7 +268,7 @@ module.exports = {
|
|
268
268
|
* @returns {boolean} `true` if it can fix the node.
|
269
269
|
*/
|
270
270
|
function canFix(node) {
|
271
|
-
const variables =
|
271
|
+
const variables = sourceCode.getDeclaredVariables(node);
|
272
272
|
const scopeNode = getScopeNode(node);
|
273
273
|
|
274
274
|
if (node.parent.type === "SwitchCase" ||
|
@@ -354,11 +354,12 @@ module.exports = {
|
|
354
354
|
/**
|
355
355
|
* Enters a function. This creates a new lexical identifier scope, so a new Set of arrow functions is pushed onto the stack.
|
356
356
|
* Also, this marks all `arguments` identifiers so that they can be detected later.
|
357
|
+
* @param {ASTNode} node The node representing the function.
|
357
358
|
* @returns {void}
|
358
359
|
*/
|
359
|
-
function enterFunction() {
|
360
|
+
function enterFunction(node) {
|
360
361
|
lexicalScopeStack.unshift(new Set());
|
361
|
-
|
362
|
+
sourceCode.getScope(node).variables.filter(variable => variable.name === "arguments").forEach(variable => {
|
362
363
|
variable.references.map(ref => ref.identifier).forEach(identifier => argumentsIdentifiers.add(identifier));
|
363
364
|
});
|
364
365
|
}
|
@@ -263,14 +263,14 @@ module.exports = {
|
|
263
263
|
}
|
264
264
|
|
265
265
|
// Skip recursive functions.
|
266
|
-
const nameVar =
|
266
|
+
const nameVar = sourceCode.getDeclaredVariables(node)[0];
|
267
267
|
|
268
268
|
if (isFunctionName(nameVar) && nameVar.references.length > 0) {
|
269
269
|
return;
|
270
270
|
}
|
271
271
|
|
272
272
|
// Skip if it's using arguments.
|
273
|
-
const variable = getVariableOfArguments(
|
273
|
+
const variable = getVariableOfArguments(sourceCode.getScope(node));
|
274
274
|
|
275
275
|
if (variable && variable.references.length > 0) {
|
276
276
|
return;
|
@@ -493,7 +493,7 @@ module.exports = {
|
|
493
493
|
|
494
494
|
VariableDeclaration(node) {
|
495
495
|
if (node.kind === "let" && !isInitOfForStatement(node)) {
|
496
|
-
variables.push(...
|
496
|
+
variables.push(...sourceCode.getDeclaredVariables(node));
|
497
497
|
}
|
498
498
|
}
|
499
499
|
};
|
@@ -10,7 +10,7 @@
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const astUtils = require("./utils/ast-utils");
|
13
|
-
const { CALL, ReferenceTracker } = require("eslint-utils");
|
13
|
+
const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils");
|
14
14
|
|
15
15
|
//------------------------------------------------------------------------------
|
16
16
|
// Helpers
|
@@ -172,8 +172,8 @@ module.exports = {
|
|
172
172
|
}
|
173
173
|
|
174
174
|
return {
|
175
|
-
Program() {
|
176
|
-
const scope =
|
175
|
+
Program(node) {
|
176
|
+
const scope = sourceCode.getScope(node);
|
177
177
|
const tracker = new ReferenceTracker(scope);
|
178
178
|
const trackMap = {
|
179
179
|
Math: {
|
@@ -181,8 +181,8 @@ module.exports = {
|
|
181
181
|
}
|
182
182
|
};
|
183
183
|
|
184
|
-
for (const { node } of tracker.iterateGlobalReferences(trackMap)) {
|
185
|
-
report(
|
184
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
|
185
|
+
report(refNode);
|
186
186
|
}
|
187
187
|
}
|
188
188
|
};
|
@@ -14,8 +14,8 @@ const {
|
|
14
14
|
CONSTRUCT,
|
15
15
|
ReferenceTracker,
|
16
16
|
getStringIfConstant
|
17
|
-
} = require("eslint-utils");
|
18
|
-
const regexpp = require("regexpp");
|
17
|
+
} = require("@eslint-community/eslint-utils");
|
18
|
+
const regexpp = require("@eslint-community/regexpp");
|
19
19
|
|
20
20
|
//------------------------------------------------------------------------------
|
21
21
|
// Helpers
|
@@ -151,8 +151,8 @@ module.exports = {
|
|
151
151
|
checkRegex(node.regex.pattern, node, node, node.regex.flags.includes("u"));
|
152
152
|
}
|
153
153
|
},
|
154
|
-
Program() {
|
155
|
-
const scope =
|
154
|
+
Program(node) {
|
155
|
+
const scope = sourceCode.getScope(node);
|
156
156
|
const tracker = new ReferenceTracker(scope);
|
157
157
|
const traceMap = {
|
158
158
|
RegExp: {
|
@@ -161,12 +161,12 @@ module.exports = {
|
|
161
161
|
}
|
162
162
|
};
|
163
163
|
|
164
|
-
for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
|
165
|
-
const regex = getStringIfConstant(
|
166
|
-
const flags = getStringIfConstant(
|
164
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) {
|
165
|
+
const regex = getStringIfConstant(refNode.arguments[0]);
|
166
|
+
const flags = getStringIfConstant(refNode.arguments[1]);
|
167
167
|
|
168
168
|
if (regex) {
|
169
|
-
checkRegex(regex,
|
169
|
+
checkRegex(regex, refNode, refNode.arguments[0], flags && flags.includes("u"));
|
170
170
|
}
|
171
171
|
}
|
172
172
|
}
|
@@ -61,6 +61,9 @@ module.exports = {
|
|
61
61
|
fixable: "code"
|
62
62
|
},
|
63
63
|
create(context) {
|
64
|
+
|
65
|
+
const sourceCode = context.getSourceCode();
|
66
|
+
|
64
67
|
return {
|
65
68
|
CallExpression(node) {
|
66
69
|
if (!(node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression")) {
|
@@ -72,7 +75,7 @@ module.exports = {
|
|
72
75
|
const isObject = hasLeftHandObject(node.callee.object);
|
73
76
|
|
74
77
|
// check `Object` scope
|
75
|
-
const scope =
|
78
|
+
const scope = sourceCode.getScope(node);
|
76
79
|
const variable = astUtils.getVariableByName(scope, "Object");
|
77
80
|
|
78
81
|
if (
|
@@ -85,7 +88,6 @@ module.exports = {
|
|
85
88
|
node,
|
86
89
|
messageId: "useHasOwn",
|
87
90
|
fix(fixer) {
|
88
|
-
const sourceCode = context.getSourceCode();
|
89
91
|
|
90
92
|
if (sourceCode.getCommentsInside(node.callee).length > 0) {
|
91
93
|
return null;
|
@@ -1,12 +1,11 @@
|
|
1
1
|
/**
|
2
2
|
* @fileoverview Prefers object spread property over Object.assign
|
3
3
|
* @author Sharmila Jesupaul
|
4
|
-
* See LICENSE file in root directory for full license.
|
5
4
|
*/
|
6
5
|
|
7
6
|
"use strict";
|
8
7
|
|
9
|
-
const { CALL, ReferenceTracker } = require("eslint-utils");
|
8
|
+
const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils");
|
10
9
|
const {
|
11
10
|
isCommaToken,
|
12
11
|
isOpeningParenToken,
|
@@ -265,8 +264,8 @@ module.exports = {
|
|
265
264
|
const sourceCode = context.getSourceCode();
|
266
265
|
|
267
266
|
return {
|
268
|
-
Program() {
|
269
|
-
const scope =
|
267
|
+
Program(node) {
|
268
|
+
const scope = sourceCode.getScope(node);
|
270
269
|
const tracker = new ReferenceTracker(scope);
|
271
270
|
const trackMap = {
|
272
271
|
Object: {
|
@@ -275,22 +274,22 @@ module.exports = {
|
|
275
274
|
};
|
276
275
|
|
277
276
|
// Iterate all calls of `Object.assign` (only of the global variable `Object`).
|
278
|
-
for (const { node } of tracker.iterateGlobalReferences(trackMap)) {
|
277
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
|
279
278
|
if (
|
280
|
-
|
281
|
-
|
282
|
-
!hasArraySpread(
|
279
|
+
refNode.arguments.length >= 1 &&
|
280
|
+
refNode.arguments[0].type === "ObjectExpression" &&
|
281
|
+
!hasArraySpread(refNode) &&
|
283
282
|
!(
|
284
|
-
|
285
|
-
hasArgumentsWithAccessors(
|
283
|
+
refNode.arguments.length > 1 &&
|
284
|
+
hasArgumentsWithAccessors(refNode)
|
286
285
|
)
|
287
286
|
) {
|
288
|
-
const messageId =
|
287
|
+
const messageId = refNode.arguments.length === 1
|
289
288
|
? "useLiteralMessage"
|
290
289
|
: "useSpreadMessage";
|
291
|
-
const fix = defineFixer(
|
290
|
+
const fix = defineFixer(refNode, sourceCode);
|
292
291
|
|
293
|
-
context.report({ node, messageId, fix });
|
292
|
+
context.report({ node: refNode, messageId, fix });
|
294
293
|
}
|
295
294
|
}
|
296
295
|
}
|
@@ -41,6 +41,7 @@ module.exports = {
|
|
41
41
|
create(context) {
|
42
42
|
|
43
43
|
const ALLOW_EMPTY_REJECT = context.options.length && context.options[0].allowEmptyReject;
|
44
|
+
const sourceCode = context.getSourceCode();
|
44
45
|
|
45
46
|
//----------------------------------------------------------------------
|
46
47
|
// Helpers
|
@@ -100,7 +101,7 @@ module.exports = {
|
|
100
101
|
node.arguments.length && astUtils.isFunction(node.arguments[0]) &&
|
101
102
|
node.arguments[0].params.length > 1 && node.arguments[0].params[1].type === "Identifier"
|
102
103
|
) {
|
103
|
-
|
104
|
+
sourceCode.getDeclaredVariables(node.arguments[0])
|
104
105
|
|
105
106
|
/*
|
106
107
|
* Find the first variable that matches the second parameter's name.
|
@@ -10,16 +10,15 @@
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const astUtils = require("./utils/ast-utils");
|
13
|
-
const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("eslint-utils");
|
14
|
-
const { RegExpValidator, visitRegExpAST, RegExpParser } = require("regexpp");
|
13
|
+
const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("@eslint-community/eslint-utils");
|
14
|
+
const { RegExpValidator, visitRegExpAST, RegExpParser } = require("@eslint-community/regexpp");
|
15
15
|
const { canTokensBeAdjacent } = require("./utils/ast-utils");
|
16
|
+
const { REGEXPP_LATEST_ECMA_VERSION } = require("./utils/regular-expressions");
|
16
17
|
|
17
18
|
//------------------------------------------------------------------------------
|
18
19
|
// Helpers
|
19
20
|
//------------------------------------------------------------------------------
|
20
21
|
|
21
|
-
const REGEXPP_LATEST_ECMA_VERSION = 2022;
|
22
|
-
|
23
22
|
/**
|
24
23
|
* Determines whether the given node is a string literal.
|
25
24
|
* @param {ASTNode} node Node to check.
|
@@ -163,7 +162,7 @@ module.exports = {
|
|
163
162
|
* @returns {boolean} True if the identifier is a reference to a global variable.
|
164
163
|
*/
|
165
164
|
function isGlobalReference(node) {
|
166
|
-
const scope =
|
165
|
+
const scope = sourceCode.getScope(node);
|
167
166
|
const variable = findVariable(scope, node);
|
168
167
|
|
169
168
|
return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
|
@@ -375,8 +374,8 @@ module.exports = {
|
|
375
374
|
}
|
376
375
|
|
377
376
|
return {
|
378
|
-
Program() {
|
379
|
-
const scope =
|
377
|
+
Program(node) {
|
378
|
+
const scope = sourceCode.getScope(node);
|
380
379
|
const tracker = new ReferenceTracker(scope);
|
381
380
|
const traceMap = {
|
382
381
|
RegExp: {
|
@@ -385,16 +384,16 @@ module.exports = {
|
|
385
384
|
}
|
386
385
|
};
|
387
386
|
|
388
|
-
for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
|
389
|
-
if (disallowRedundantWrapping && isUnnecessarilyWrappedRegexLiteral(
|
390
|
-
const regexNode =
|
387
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) {
|
388
|
+
if (disallowRedundantWrapping && isUnnecessarilyWrappedRegexLiteral(refNode)) {
|
389
|
+
const regexNode = refNode.arguments[0];
|
391
390
|
|
392
|
-
if (
|
391
|
+
if (refNode.arguments.length === 2) {
|
393
392
|
const suggests = [];
|
394
393
|
|
395
|
-
const argFlags = getStringValue(
|
394
|
+
const argFlags = getStringValue(refNode.arguments[1]) || "";
|
396
395
|
|
397
|
-
if (canFixTo(
|
396
|
+
if (canFixTo(refNode, regexNode.regex.pattern, argFlags)) {
|
398
397
|
suggests.push({
|
399
398
|
messageId: "replaceWithLiteralAndFlags",
|
400
399
|
pattern: regexNode.regex.pattern,
|
@@ -407,7 +406,7 @@ module.exports = {
|
|
407
406
|
|
408
407
|
if (
|
409
408
|
!areFlagsEqual(mergedFlags, argFlags) &&
|
410
|
-
canFixTo(
|
409
|
+
canFixTo(refNode, regexNode.regex.pattern, mergedFlags)
|
411
410
|
) {
|
412
411
|
suggests.push({
|
413
412
|
messageId: "replaceWithIntendedLiteralAndFlags",
|
@@ -417,7 +416,7 @@ module.exports = {
|
|
417
416
|
}
|
418
417
|
|
419
418
|
context.report({
|
420
|
-
node,
|
419
|
+
node: refNode,
|
421
420
|
messageId: "unexpectedRedundantRegExpWithFlags",
|
422
421
|
suggest: suggests.map(({ flags, pattern, messageId }) => ({
|
423
422
|
messageId,
|
@@ -425,42 +424,42 @@ module.exports = {
|
|
425
424
|
flags
|
426
425
|
},
|
427
426
|
fix(fixer) {
|
428
|
-
return fixer.replaceText(
|
427
|
+
return fixer.replaceText(refNode, getSafeOutput(refNode, `/${pattern}/${flags}`));
|
429
428
|
}
|
430
429
|
}))
|
431
430
|
});
|
432
431
|
} else {
|
433
432
|
const outputs = [];
|
434
433
|
|
435
|
-
if (canFixTo(
|
434
|
+
if (canFixTo(refNode, regexNode.regex.pattern, regexNode.regex.flags)) {
|
436
435
|
outputs.push(sourceCode.getText(regexNode));
|
437
436
|
}
|
438
437
|
|
439
438
|
|
440
439
|
context.report({
|
441
|
-
node,
|
440
|
+
node: refNode,
|
442
441
|
messageId: "unexpectedRedundantRegExp",
|
443
442
|
suggest: outputs.map(output => ({
|
444
443
|
messageId: "replaceWithLiteral",
|
445
444
|
fix(fixer) {
|
446
445
|
return fixer.replaceText(
|
447
|
-
|
448
|
-
getSafeOutput(
|
446
|
+
refNode,
|
447
|
+
getSafeOutput(refNode, output)
|
449
448
|
);
|
450
449
|
}
|
451
450
|
}))
|
452
451
|
});
|
453
452
|
}
|
454
|
-
} else if (hasOnlyStaticStringArguments(
|
455
|
-
let regexContent = getStringValue(
|
453
|
+
} else if (hasOnlyStaticStringArguments(refNode)) {
|
454
|
+
let regexContent = getStringValue(refNode.arguments[0]);
|
456
455
|
let noFix = false;
|
457
456
|
let flags;
|
458
457
|
|
459
|
-
if (
|
460
|
-
flags = getStringValue(
|
458
|
+
if (refNode.arguments[1]) {
|
459
|
+
flags = getStringValue(refNode.arguments[1]);
|
461
460
|
}
|
462
461
|
|
463
|
-
if (!canFixTo(
|
462
|
+
if (!canFixTo(refNode, regexContent, flags)) {
|
464
463
|
noFix = true;
|
465
464
|
}
|
466
465
|
|
@@ -494,12 +493,12 @@ module.exports = {
|
|
494
493
|
const newRegExpValue = `/${regexContent || "(?:)"}/${flags || ""}`;
|
495
494
|
|
496
495
|
context.report({
|
497
|
-
node,
|
496
|
+
node: refNode,
|
498
497
|
messageId: "unexpectedRegExp",
|
499
498
|
suggest: noFix ? [] : [{
|
500
499
|
messageId: "replaceWithLiteral",
|
501
500
|
fix(fixer) {
|
502
|
-
return fixer.replaceText(
|
501
|
+
return fixer.replaceText(refNode, getSafeOutput(refNode, newRegExpValue));
|
503
502
|
}
|
504
503
|
}]
|
505
504
|
});
|