eslint 8.36.0 → 8.38.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.
Files changed (88) hide show
  1. package/README.md +0 -5
  2. package/lib/config/default-config.js +1 -4
  3. package/lib/config/flat-config-array.js +11 -12
  4. package/lib/config/flat-config-schema.js +4 -18
  5. package/lib/linter/linter.js +7 -48
  6. package/lib/rule-tester/flat-rule-tester.js +1 -1
  7. package/lib/rule-tester/rule-tester.js +1 -1
  8. package/lib/rules/block-scoped-var.js +2 -1
  9. package/lib/rules/camelcase.js +5 -4
  10. package/lib/rules/consistent-this.js +4 -2
  11. package/lib/rules/func-names.js +1 -1
  12. package/lib/rules/global-require.js +4 -2
  13. package/lib/rules/handle-callback-err.js +2 -1
  14. package/lib/rules/id-blacklist.js +3 -2
  15. package/lib/rules/id-denylist.js +3 -2
  16. package/lib/rules/id-match.js +3 -2
  17. package/lib/rules/logical-assignment-operators.js +3 -3
  18. package/lib/rules/new-parens.js +5 -14
  19. package/lib/rules/no-alert.js +3 -1
  20. package/lib/rules/no-catch-shadow.js +3 -1
  21. package/lib/rules/no-class-assign.js +3 -1
  22. package/lib/rules/no-console.js +3 -2
  23. package/lib/rules/no-const-assign.js +3 -1
  24. package/lib/rules/no-constant-binary-expression.js +4 -2
  25. package/lib/rules/no-constant-condition.js +3 -2
  26. package/lib/rules/no-div-regex.js +1 -1
  27. package/lib/rules/no-dupe-args.js +3 -1
  28. package/lib/rules/no-else-return.js +13 -12
  29. package/lib/rules/no-eval.js +4 -4
  30. package/lib/rules/no-ex-assign.js +3 -1
  31. package/lib/rules/no-extend-native.js +3 -2
  32. package/lib/rules/no-func-assign.js +3 -1
  33. package/lib/rules/no-global-assign.js +3 -2
  34. package/lib/rules/no-implicit-globals.js +3 -2
  35. package/lib/rules/no-implied-eval.js +4 -3
  36. package/lib/rules/no-import-assign.js +4 -2
  37. package/lib/rules/no-invalid-this.js +2 -2
  38. package/lib/rules/no-label-var.js +2 -1
  39. package/lib/rules/no-lone-blocks.js +8 -6
  40. package/lib/rules/no-lonely-if.js +1 -1
  41. package/lib/rules/no-loop-func.js +3 -1
  42. package/lib/rules/no-misleading-character-class.js +11 -40
  43. package/lib/rules/no-native-reassign.js +3 -2
  44. package/lib/rules/no-new-func.js +7 -6
  45. package/lib/rules/no-new-native-nonconstructor.js +8 -6
  46. package/lib/rules/no-new-object.js +4 -1
  47. package/lib/rules/no-new-symbol.js +8 -6
  48. package/lib/rules/no-obj-calls.js +7 -5
  49. package/lib/rules/no-param-reassign.js +2 -1
  50. package/lib/rules/no-promise-executor-return.js +2 -1
  51. package/lib/rules/no-redeclare.js +3 -3
  52. package/lib/rules/no-regex-spaces.js +3 -1
  53. package/lib/rules/no-restricted-exports.js +2 -1
  54. package/lib/rules/no-restricted-globals.js +4 -2
  55. package/lib/rules/no-setter-return.js +2 -1
  56. package/lib/rules/no-shadow-restricted-names.js +2 -1
  57. package/lib/rules/no-shadow.js +3 -2
  58. package/lib/rules/no-undef-init.js +1 -1
  59. package/lib/rules/no-undef.js +3 -2
  60. package/lib/rules/no-undefined.js +4 -2
  61. package/lib/rules/no-underscore-dangle.js +2 -1
  62. package/lib/rules/no-unmodified-loop-condition.js +2 -2
  63. package/lib/rules/no-unused-expressions.js +3 -2
  64. package/lib/rules/no-unused-vars.js +2 -2
  65. package/lib/rules/no-use-before-define.js +3 -2
  66. package/lib/rules/no-useless-backreference.js +7 -5
  67. package/lib/rules/no-var.js +2 -2
  68. package/lib/rules/object-shorthand.js +3 -2
  69. package/lib/rules/prefer-arrow-callback.js +2 -2
  70. package/lib/rules/prefer-const.js +1 -1
  71. package/lib/rules/prefer-exponentiation-operator.js +4 -4
  72. package/lib/rules/prefer-named-capture-group.js +6 -6
  73. package/lib/rules/prefer-object-has-own.js +4 -2
  74. package/lib/rules/prefer-object-spread.js +11 -11
  75. package/lib/rules/prefer-promise-reject-errors.js +2 -1
  76. package/lib/rules/prefer-regex-literals.js +24 -25
  77. package/lib/rules/prefer-rest-params.js +5 -2
  78. package/lib/rules/radix.js +11 -11
  79. package/lib/rules/require-atomic-updates.js +2 -2
  80. package/lib/rules/require-unicode-regexp.js +66 -6
  81. package/lib/rules/symbol-description.js +7 -5
  82. package/lib/rules/utils/regular-expressions.js +42 -0
  83. package/lib/rules/valid-typeof.js +4 -4
  84. package/lib/rules/wrap-regex.js +1 -1
  85. package/lib/rules/yoda.js +1 -1
  86. package/lib/source-code/source-code.js +93 -0
  87. package/lib/source-code/token-store/utils.js +14 -4
  88. package/package.json +5 -5
@@ -129,7 +129,7 @@ module.exports = {
129
129
  * @private
130
130
  */
131
131
  function checkForBlock(node) {
132
- const scope = context.getScope();
132
+ const scope = sourceCode.getScope(node);
133
133
 
134
134
  /*
135
135
  * In ES5, some node type such as `BlockStatement` doesn't have that scope.
@@ -141,8 +141,8 @@ module.exports = {
141
141
  }
142
142
 
143
143
  return {
144
- Program() {
145
- const scope = context.getScope();
144
+ Program(node) {
145
+ const scope = sourceCode.getScope(node);
146
146
 
147
147
  findVariablesInScope(scope);
148
148
 
@@ -54,6 +54,8 @@ module.exports = {
54
54
 
55
55
  create(context) {
56
56
 
57
+ const sourceCode = context.getSourceCode();
58
+
57
59
  /**
58
60
  * Validate regular expression
59
61
  * @param {ASTNode} nodeToReport Node to report.
@@ -149,7 +151,7 @@ module.exports = {
149
151
  * @private
150
152
  */
151
153
  function checkFunction(node) {
152
- const scope = context.getScope();
154
+ const scope = sourceCode.getScope(node);
153
155
  const regExpVar = astUtils.getVariableByName(scope, "RegExp");
154
156
  const shadowed = regExpVar && regExpVar.defs.length > 0;
155
157
  const patternNode = node.arguments[0];
@@ -99,6 +99,7 @@ module.exports = {
99
99
 
100
100
  const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
101
101
  const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
102
+ const sourceCode = context.getSourceCode();
102
103
 
103
104
  /**
104
105
  * Checks and reports given exported name.
@@ -176,7 +177,7 @@ module.exports = {
176
177
  if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") {
177
178
  checkExportedName(declaration.id);
178
179
  } else if (declaration.type === "VariableDeclaration") {
179
- context.getDeclaredVariables(declaration)
180
+ sourceCode.getDeclaredVariables(declaration)
180
181
  .map(v => v.defs.find(d => d.parent === declaration))
181
182
  .map(d => d.name) // Identifier nodes
182
183
  .forEach(checkExportedName);
@@ -50,6 +50,8 @@ module.exports = {
50
50
 
51
51
  create(context) {
52
52
 
53
+ const sourceCode = context.getSourceCode();
54
+
53
55
  // If no globals are restricted, we don't need to do anything
54
56
  if (context.options.length === 0) {
55
57
  return {};
@@ -99,8 +101,8 @@ module.exports = {
99
101
  }
100
102
 
101
103
  return {
102
- Program() {
103
- const scope = context.getScope();
104
+ Program(node) {
105
+ const scope = sourceCode.getScope(node);
104
106
 
105
107
  // Report variables declared elsewhere (ex: variables defined as "global" by eslint)
106
108
  scope.variables.forEach(variable => {
@@ -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(context.getScope());
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 context.getDeclaredVariables(node)) {
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,
@@ -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 = context.getScope();
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 = context.getScope(),
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);
@@ -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"(/* node */) {
60
- const globalScope = context.getScope();
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 = context.getScope();
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
- context.getDeclaredVariables(node).forEach(variable => {
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;
@@ -340,8 +340,8 @@ module.exports = {
340
340
  }
341
341
 
342
342
  return {
343
- "Program:exit"() {
344
- const queue = [context.getScope()];
343
+ "Program:exit"(node) {
344
+ const queue = [sourceCode.getScope(node)];
345
345
 
346
346
  groupMap = new Map();
347
347
 
@@ -70,7 +70,8 @@ module.exports = {
70
70
  allowShortCircuit = config.allowShortCircuit || false,
71
71
  allowTernary = config.allowTernary || false,
72
72
  allowTaggedTemplates = config.allowTaggedTemplates || false,
73
- enforceForJSX = config.enforceForJSX || false;
73
+ enforceForJSX = config.enforceForJSX || false,
74
+ sourceCode = context.getSourceCode();
74
75
 
75
76
  /**
76
77
  * Has AST suggesting a directive.
@@ -180,7 +181,7 @@ module.exports = {
180
181
 
181
182
  return {
182
183
  ExpressionStatement(node) {
183
- if (Checker.isDisallowed(node.expression) && !isDirective(node, context.getAncestors())) {
184
+ if (Checker.isDisallowed(node.expression) && !isDirective(node, sourceCode.getAncestors(node))) {
184
185
  context.report({ node, messageId: "unusedExpression" });
185
186
  }
186
187
  }
@@ -555,7 +555,7 @@ module.exports = {
555
555
  */
556
556
  function isAfterLastUsedArg(variable) {
557
557
  const def = variable.defs[0];
558
- const params = context.getDeclaredVariables(def.node);
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(context.getScope(), []);
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(context.getScope());
343
+ Program(node) {
344
+ checkReferencesInScope(sourceCode.getScope(node));
344
345
  }
345
346
  };
346
347
  }
@@ -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 = context.getScope(),
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] = node.arguments,
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(node, pattern, flags || "");
188
+ checkRegex(refNode, pattern, flags || "");
187
189
  }
188
190
  }
189
191
  }
@@ -210,7 +210,7 @@ module.exports = {
210
210
  if (!declarator.init) {
211
211
  return false;
212
212
  }
213
- const variables = context.getDeclaredVariables(declarator);
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 = context.getDeclaredVariables(node);
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
- context.getScope().variables.filter(variable => variable.name === "arguments").forEach(variable => {
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 = context.getDeclaredVariables(node)[0];
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(context.getScope());
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(...context.getDeclaredVariables(node));
496
+ variables.push(...sourceCode.getDeclaredVariables(node));
497
497
  }
498
498
  }
499
499
  };
@@ -172,8 +172,8 @@ module.exports = {
172
172
  }
173
173
 
174
174
  return {
175
- Program() {
176
- const scope = context.getScope();
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(node);
184
+ for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
185
+ report(refNode);
186
186
  }
187
187
  }
188
188
  };
@@ -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 = context.getScope();
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(node.arguments[0]);
166
- const flags = getStringIfConstant(node.arguments[1]);
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, node, node.arguments[0], flags && flags.includes("u"));
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 = context.getScope();
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;
@@ -265,8 +265,8 @@ module.exports = {
265
265
  const sourceCode = context.getSourceCode();
266
266
 
267
267
  return {
268
- Program() {
269
- const scope = context.getScope();
268
+ Program(node) {
269
+ const scope = sourceCode.getScope(node);
270
270
  const tracker = new ReferenceTracker(scope);
271
271
  const trackMap = {
272
272
  Object: {
@@ -275,22 +275,22 @@ module.exports = {
275
275
  };
276
276
 
277
277
  // Iterate all calls of `Object.assign` (only of the global variable `Object`).
278
- for (const { node } of tracker.iterateGlobalReferences(trackMap)) {
278
+ for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
279
279
  if (
280
- node.arguments.length >= 1 &&
281
- node.arguments[0].type === "ObjectExpression" &&
282
- !hasArraySpread(node) &&
280
+ refNode.arguments.length >= 1 &&
281
+ refNode.arguments[0].type === "ObjectExpression" &&
282
+ !hasArraySpread(refNode) &&
283
283
  !(
284
- node.arguments.length > 1 &&
285
- hasArgumentsWithAccessors(node)
284
+ refNode.arguments.length > 1 &&
285
+ hasArgumentsWithAccessors(refNode)
286
286
  )
287
287
  ) {
288
- const messageId = node.arguments.length === 1
288
+ const messageId = refNode.arguments.length === 1
289
289
  ? "useLiteralMessage"
290
290
  : "useSpreadMessage";
291
- const fix = defineFixer(node, sourceCode);
291
+ const fix = defineFixer(refNode, sourceCode);
292
292
 
293
- context.report({ node, messageId, fix });
293
+ context.report({ node: refNode, messageId, fix });
294
294
  }
295
295
  }
296
296
  }
@@ -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
- context.getDeclaredVariables(node.arguments[0])
104
+ sourceCode.getDeclaredVariables(node.arguments[0])
104
105
 
105
106
  /*
106
107
  * Find the first variable that matches the second parameter's name.
@@ -13,13 +13,12 @@ const astUtils = require("./utils/ast-utils");
13
13
  const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("@eslint-community/eslint-utils");
14
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 = context.getScope();
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 = context.getScope();
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(node)) {
390
- const regexNode = node.arguments[0];
387
+ for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) {
388
+ if (disallowRedundantWrapping && isUnnecessarilyWrappedRegexLiteral(refNode)) {
389
+ const regexNode = refNode.arguments[0];
391
390
 
392
- if (node.arguments.length === 2) {
391
+ if (refNode.arguments.length === 2) {
393
392
  const suggests = [];
394
393
 
395
- const argFlags = getStringValue(node.arguments[1]) || "";
394
+ const argFlags = getStringValue(refNode.arguments[1]) || "";
396
395
 
397
- if (canFixTo(node, regexNode.regex.pattern, argFlags)) {
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(node, regexNode.regex.pattern, mergedFlags)
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(node, getSafeOutput(node, `/${pattern}/${flags}`));
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(node, regexNode.regex.pattern, regexNode.regex.flags)) {
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
- node,
448
- getSafeOutput(node, output)
446
+ refNode,
447
+ getSafeOutput(refNode, output)
449
448
  );
450
449
  }
451
450
  }))
452
451
  });
453
452
  }
454
- } else if (hasOnlyStaticStringArguments(node)) {
455
- let regexContent = getStringValue(node.arguments[0]);
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 (node.arguments[1]) {
460
- flags = getStringValue(node.arguments[1]);
458
+ if (refNode.arguments[1]) {
459
+ flags = getStringValue(refNode.arguments[1]);
461
460
  }
462
461
 
463
- if (!canFixTo(node, regexContent, flags)) {
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(node, getSafeOutput(node, newRegExpValue));
501
+ return fixer.replaceText(refNode, getSafeOutput(refNode, newRegExpValue));
503
502
  }
504
503
  }]
505
504
  });