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.
Files changed (78) hide show
  1. package/README.md +4 -9
  2. package/conf/rule-type-list.json +2 -2
  3. package/lib/cli-engine/file-enumerator.js +6 -4
  4. package/lib/config/default-config.js +1 -4
  5. package/lib/config/flat-config-array.js +63 -9
  6. package/lib/config/flat-config-schema.js +4 -18
  7. package/lib/eslint/eslint-helpers.js +2 -2
  8. package/lib/linter/linter.js +5 -30
  9. package/lib/rule-tester/flat-rule-tester.js +1 -1
  10. package/lib/rule-tester/rule-tester.js +1 -1
  11. package/lib/rules/camelcase.js +3 -2
  12. package/lib/rules/consistent-this.js +4 -2
  13. package/lib/rules/global-require.js +3 -1
  14. package/lib/rules/handle-callback-err.js +2 -1
  15. package/lib/rules/id-blacklist.js +3 -2
  16. package/lib/rules/id-denylist.js +3 -2
  17. package/lib/rules/id-match.js +3 -2
  18. package/lib/rules/logical-assignment-operators.js +3 -3
  19. package/lib/rules/multiline-comment-style.js +42 -3
  20. package/lib/rules/no-alert.js +3 -1
  21. package/lib/rules/no-catch-shadow.js +3 -1
  22. package/lib/rules/no-console.js +3 -2
  23. package/lib/rules/no-constant-binary-expression.js +4 -2
  24. package/lib/rules/no-constant-condition.js +3 -2
  25. package/lib/rules/no-control-regex.js +1 -1
  26. package/lib/rules/no-else-return.js +13 -12
  27. package/lib/rules/no-eval.js +4 -4
  28. package/lib/rules/no-extend-native.js +3 -2
  29. package/lib/rules/no-extra-boolean-cast.js +1 -1
  30. package/lib/rules/no-extra-parens.js +1 -1
  31. package/lib/rules/no-global-assign.js +3 -2
  32. package/lib/rules/no-implicit-globals.js +3 -2
  33. package/lib/rules/no-implied-eval.js +5 -4
  34. package/lib/rules/no-import-assign.js +4 -2
  35. package/lib/rules/no-invalid-regexp.js +1 -1
  36. package/lib/rules/no-invalid-this.js +2 -2
  37. package/lib/rules/no-label-var.js +2 -1
  38. package/lib/rules/no-lone-blocks.js +3 -2
  39. package/lib/rules/no-loop-func.js +3 -1
  40. package/lib/rules/no-misleading-character-class.js +12 -41
  41. package/lib/rules/no-native-reassign.js +3 -2
  42. package/lib/rules/no-new-func.js +7 -6
  43. package/lib/rules/no-new-native-nonconstructor.js +8 -6
  44. package/lib/rules/no-new-object.js +4 -1
  45. package/lib/rules/no-new-symbol.js +8 -6
  46. package/lib/rules/no-obj-calls.js +8 -6
  47. package/lib/rules/no-promise-executor-return.js +3 -2
  48. package/lib/rules/no-redeclare.js +3 -3
  49. package/lib/rules/no-regex-spaces.js +4 -2
  50. package/lib/rules/no-restricted-globals.js +4 -2
  51. package/lib/rules/no-setter-return.js +3 -2
  52. package/lib/rules/no-shadow.js +3 -2
  53. package/lib/rules/no-undef-init.js +1 -1
  54. package/lib/rules/no-undef.js +3 -2
  55. package/lib/rules/no-undefined.js +4 -2
  56. package/lib/rules/no-unmodified-loop-condition.js +2 -2
  57. package/lib/rules/no-unused-vars.js +1 -1
  58. package/lib/rules/no-use-before-define.js +3 -2
  59. package/lib/rules/no-useless-backreference.js +9 -7
  60. package/lib/rules/object-shorthand.js +3 -2
  61. package/lib/rules/prefer-arrow-callback.js +1 -1
  62. package/lib/rules/prefer-exponentiation-operator.js +5 -5
  63. package/lib/rules/prefer-named-capture-group.js +8 -8
  64. package/lib/rules/prefer-object-has-own.js +4 -2
  65. package/lib/rules/prefer-object-spread.js +12 -12
  66. package/lib/rules/prefer-regex-literals.js +26 -27
  67. package/lib/rules/prefer-rest-params.js +5 -2
  68. package/lib/rules/radix.js +11 -11
  69. package/lib/rules/require-atomic-updates.js +2 -2
  70. package/lib/rules/require-unicode-regexp.js +63 -7
  71. package/lib/rules/symbol-description.js +7 -5
  72. package/lib/rules/utils/regular-expressions.js +42 -0
  73. package/lib/rules/valid-typeof.js +3 -3
  74. package/lib/rules/wrap-iife.js +1 -1
  75. package/lib/source-code/source-code.js +52 -1
  76. package/lib/source-code/token-store/index.js +1 -1
  77. package/lib/source-code/token-store/utils.js +14 -4
  78. package/package.json +7 -7
@@ -104,6 +104,7 @@ module.exports = {
104
104
 
105
105
  create(context) {
106
106
  const mode = context.options[0] || MODE_ALWAYS;
107
+ const sourceCode = context.getSourceCode();
107
108
 
108
109
  /**
109
110
  * Checks the arguments of a given CallExpression node and reports it if it
@@ -131,7 +132,6 @@ module.exports = {
131
132
  {
132
133
  messageId: "addRadixParameter10",
133
134
  fix(fixer) {
134
- const sourceCode = context.getSourceCode();
135
135
  const tokens = sourceCode.getTokens(node);
136
136
  const lastToken = tokens[tokens.length - 1]; // Parenthesis.
137
137
  const secondToLastToken = tokens[tokens.length - 2]; // May or may not be a comma.
@@ -162,18 +162,18 @@ module.exports = {
162
162
  }
163
163
 
164
164
  return {
165
- "Program:exit"() {
166
- const scope = context.getScope();
165
+ "Program:exit"(node) {
166
+ const scope = sourceCode.getScope(node);
167
167
  let variable;
168
168
 
169
169
  // Check `parseInt()`
170
170
  variable = astUtils.getVariableByName(scope, "parseInt");
171
171
  if (variable && !isShadowed(variable)) {
172
172
  variable.references.forEach(reference => {
173
- const node = reference.identifier;
173
+ const idNode = reference.identifier;
174
174
 
175
- if (astUtils.isCallee(node)) {
176
- checkArguments(node.parent);
175
+ if (astUtils.isCallee(idNode)) {
176
+ checkArguments(idNode.parent);
177
177
  }
178
178
  });
179
179
  }
@@ -182,12 +182,12 @@ module.exports = {
182
182
  variable = astUtils.getVariableByName(scope, "Number");
183
183
  if (variable && !isShadowed(variable)) {
184
184
  variable.references.forEach(reference => {
185
- const node = reference.identifier.parent;
186
- const maybeCallee = node.parent.type === "ChainExpression"
187
- ? node.parent
188
- : node;
185
+ const parentNode = reference.identifier.parent;
186
+ const maybeCallee = parentNode.parent.type === "ChainExpression"
187
+ ? parentNode.parent
188
+ : parentNode;
189
189
 
190
- if (isParseIntMethod(node) && astUtils.isCallee(maybeCallee)) {
190
+ if (isParseIntMethod(parentNode) && astUtils.isCallee(maybeCallee)) {
191
191
  checkArguments(maybeCallee.parent);
192
192
  }
193
193
  });
@@ -204,8 +204,8 @@ module.exports = {
204
204
  let stack = null;
205
205
 
206
206
  return {
207
- onCodePathStart(codePath) {
208
- const scope = context.getScope();
207
+ onCodePathStart(codePath, node) {
208
+ const scope = sourceCode.getScope(node);
209
209
  const shouldVerify =
210
210
  scope.type === "function" &&
211
211
  (scope.block.async || scope.block.generator);
@@ -14,7 +14,9 @@ const {
14
14
  CONSTRUCT,
15
15
  ReferenceTracker,
16
16
  getStringIfConstant
17
- } = require("eslint-utils");
17
+ } = require("@eslint-community/eslint-utils");
18
+ const astUtils = require("./utils/ast-utils.js");
19
+ const { isValidWithUnicodeFlag } = require("./utils/regular-expressions");
18
20
 
19
21
  //------------------------------------------------------------------------------
20
22
  // Rule Definition
@@ -31,7 +33,10 @@ module.exports = {
31
33
  url: "https://eslint.org/docs/rules/require-unicode-regexp"
32
34
  },
33
35
 
36
+ hasSuggestions: true,
37
+
34
38
  messages: {
39
+ addUFlag: "Add the 'u' flag.",
35
40
  requireUFlag: "Use the 'u' flag."
36
41
  },
37
42
 
@@ -39,28 +44,79 @@ module.exports = {
39
44
  },
40
45
 
41
46
  create(context) {
47
+
48
+ const sourceCode = context.getSourceCode();
49
+
42
50
  return {
43
51
  "Literal[regex]"(node) {
44
52
  const flags = node.regex.flags || "";
45
53
 
46
54
  if (!flags.includes("u")) {
47
- context.report({ node, messageId: "requireUFlag" });
55
+ context.report({
56
+ messageId: "requireUFlag",
57
+ node,
58
+ suggest: isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern)
59
+ ? [
60
+ {
61
+ fix(fixer) {
62
+ return fixer.insertTextAfter(node, "u");
63
+ },
64
+ messageId: "addUFlag"
65
+ }
66
+ ]
67
+ : null
68
+ });
48
69
  }
49
70
  },
50
71
 
51
- Program() {
52
- const scope = context.getScope();
72
+ Program(node) {
73
+ const scope = sourceCode.getScope(node);
53
74
  const tracker = new ReferenceTracker(scope);
54
75
  const trackMap = {
55
76
  RegExp: { [CALL]: true, [CONSTRUCT]: true }
56
77
  };
57
78
 
58
- for (const { node } of tracker.iterateGlobalReferences(trackMap)) {
59
- const flagsNode = node.arguments[1];
79
+ for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
80
+ const [patternNode, flagsNode] = refNode.arguments;
81
+ const pattern = getStringIfConstant(patternNode, scope);
60
82
  const flags = getStringIfConstant(flagsNode, scope);
61
83
 
62
84
  if (!flagsNode || (typeof flags === "string" && !flags.includes("u"))) {
63
- context.report({ node, messageId: "requireUFlag" });
85
+ context.report({
86
+ messageId: "requireUFlag",
87
+ node: refNode,
88
+ suggest: typeof pattern === "string" && isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, pattern)
89
+ ? [
90
+ {
91
+ fix(fixer) {
92
+ if (flagsNode) {
93
+ if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") {
94
+ const flagsNodeText = sourceCode.getText(flagsNode);
95
+
96
+ return fixer.replaceText(flagsNode, [
97
+ flagsNodeText.slice(0, flagsNodeText.length - 1),
98
+ flagsNodeText.slice(flagsNodeText.length - 1)
99
+ ].join("u"));
100
+ }
101
+
102
+ // We intentionally don't suggest concatenating + "u" to non-literals
103
+ return null;
104
+ }
105
+
106
+ const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis
107
+
108
+ return fixer.insertTextAfter(
109
+ penultimateToken,
110
+ astUtils.isCommaToken(penultimateToken)
111
+ ? ' "u",'
112
+ : ', "u"'
113
+ );
114
+ },
115
+ messageId: "addUFlag"
116
+ }
117
+ ]
118
+ : null
119
+ });
64
120
  }
65
121
  }
66
122
  }
@@ -35,6 +35,8 @@ module.exports = {
35
35
 
36
36
  create(context) {
37
37
 
38
+ const sourceCode = context.getSourceCode();
39
+
38
40
  /**
39
41
  * Reports if node does not conform the rule in case rule is set to
40
42
  * report missing description
@@ -51,16 +53,16 @@ module.exports = {
51
53
  }
52
54
 
53
55
  return {
54
- "Program:exit"() {
55
- const scope = context.getScope();
56
+ "Program:exit"(node) {
57
+ const scope = sourceCode.getScope(node);
56
58
  const variable = astUtils.getVariableByName(scope, "Symbol");
57
59
 
58
60
  if (variable && variable.defs.length === 0) {
59
61
  variable.references.forEach(reference => {
60
- const node = reference.identifier;
62
+ const idNode = reference.identifier;
61
63
 
62
- if (astUtils.isCallee(node)) {
63
- checkArgument(node.parent);
64
+ if (astUtils.isCallee(idNode)) {
65
+ checkArgument(idNode.parent);
64
66
  }
65
67
  });
66
68
  }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @fileoverview Common utils for regular expressions.
3
+ * @author Josh Goldberg
4
+ * @author Toru Nagashima
5
+ */
6
+
7
+ "use strict";
8
+
9
+ const { RegExpValidator } = require("@eslint-community/regexpp");
10
+
11
+ const REGEXPP_LATEST_ECMA_VERSION = 2022;
12
+
13
+ /**
14
+ * Checks if the given regular expression pattern would be valid with the `u` flag.
15
+ * @param {number} ecmaVersion ECMAScript version to parse in.
16
+ * @param {string} pattern The regular expression pattern to verify.
17
+ * @returns {boolean} `true` if the pattern would be valid with the `u` flag.
18
+ * `false` if the pattern would be invalid with the `u` flag or the configured
19
+ * ecmaVersion doesn't support the `u` flag.
20
+ */
21
+ function isValidWithUnicodeFlag(ecmaVersion, pattern) {
22
+ if (ecmaVersion <= 5) { // ecmaVersion <= 5 doesn't support the 'u' flag
23
+ return false;
24
+ }
25
+
26
+ const validator = new RegExpValidator({
27
+ ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION)
28
+ });
29
+
30
+ try {
31
+ validator.validatePattern(pattern, void 0, void 0, /* uFlag = */ true);
32
+ } catch {
33
+ return false;
34
+ }
35
+
36
+ return true;
37
+ }
38
+
39
+ module.exports = {
40
+ isValidWithUnicodeFlag,
41
+ REGEXPP_LATEST_ECMA_VERSION
42
+ };
@@ -44,7 +44,7 @@ module.exports = {
44
44
 
45
45
  const VALID_TYPES = new Set(["symbol", "undefined", "object", "boolean", "number", "string", "function", "bigint"]),
46
46
  OPERATORS = new Set(["==", "===", "!=", "!=="]);
47
-
47
+ const sourceCode = context.getSourceCode();
48
48
  const requireStringLiterals = context.options[0] && context.options[0].requireStringLiterals;
49
49
 
50
50
  let globalScope;
@@ -77,8 +77,8 @@ module.exports = {
77
77
 
78
78
  return {
79
79
 
80
- Program() {
81
- globalScope = context.getScope();
80
+ Program(node) {
81
+ globalScope = sourceCode.getScope(node);
82
82
  },
83
83
 
84
84
  UnaryExpression(node) {
@@ -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
  //----------------------------------------------------------------------
16
16
  // Helpers
@@ -9,7 +9,7 @@
9
9
  //------------------------------------------------------------------------------
10
10
 
11
11
  const
12
- { isCommentToken } = require("eslint-utils"),
12
+ { isCommentToken } = require("@eslint-community/eslint-utils"),
13
13
  TokenStore = require("./token-store"),
14
14
  astUtils = require("../shared/ast-utils"),
15
15
  Traverser = require("../shared/traverser");
@@ -143,6 +143,8 @@ function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
143
143
  // Public Interface
144
144
  //------------------------------------------------------------------------------
145
145
 
146
+ const caches = Symbol("caches");
147
+
146
148
  /**
147
149
  * Represents parsed source code.
148
150
  */
@@ -175,6 +177,13 @@ class SourceCode extends TokenStore {
175
177
  validate(ast);
176
178
  super(ast.tokens, ast.comments);
177
179
 
180
+ /**
181
+ * General purpose caching for the class.
182
+ */
183
+ this[caches] = new Map([
184
+ ["scopes", new WeakMap()]
185
+ ]);
186
+
178
187
  /**
179
188
  * The flag to indicate that the source code has Unicode BOM.
180
189
  * @type {boolean}
@@ -588,6 +597,48 @@ class SourceCode extends TokenStore {
588
597
 
589
598
  return positionIndex;
590
599
  }
600
+
601
+ /**
602
+ * Gets the scope for the given node
603
+ * @param {ASTNode} currentNode The node to get the scope of
604
+ * @returns {eslint-scope.Scope} The scope information for this node
605
+ * @throws {TypeError} If the `currentNode` argument is missing.
606
+ */
607
+ getScope(currentNode) {
608
+
609
+ if (!currentNode) {
610
+ throw new TypeError("Missing required argument: node.");
611
+ }
612
+
613
+ // check cache first
614
+ const cache = this[caches].get("scopes");
615
+ const cachedScope = cache.get(currentNode);
616
+
617
+ if (cachedScope) {
618
+ return cachedScope;
619
+ }
620
+
621
+ // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
622
+ const inner = currentNode.type !== "Program";
623
+
624
+ for (let node = currentNode; node; node = node.parent) {
625
+ const scope = this.scopeManager.acquire(node, inner);
626
+
627
+ if (scope) {
628
+ if (scope.type === "function-expression-name") {
629
+ cache.set(currentNode, scope.childScopes[0]);
630
+ return scope.childScopes[0];
631
+ }
632
+
633
+ cache.set(currentNode, scope);
634
+ return scope;
635
+ }
636
+ }
637
+
638
+ cache.set(currentNode, this.scopeManager.scopes[0]);
639
+ return this.scopeManager.scopes[0];
640
+ }
641
+
591
642
  }
592
643
 
593
644
  module.exports = SourceCode;
@@ -9,7 +9,7 @@
9
9
  //------------------------------------------------------------------------------
10
10
 
11
11
  const assert = require("assert");
12
- const { isCommentToken } = require("eslint-utils");
12
+ const { isCommentToken } = require("@eslint-community/eslint-utils");
13
13
  const cursors = require("./cursors");
14
14
  const ForwardTokenCursor = require("./forward-token-cursor");
15
15
  const PaddedTokenCursor = require("./padded-token-cursor");
@@ -49,13 +49,18 @@ exports.getFirstIndex = function getFirstIndex(tokens, indexMap, startLoc) {
49
49
  }
50
50
  if ((startLoc - 1) in indexMap) {
51
51
  const index = indexMap[startLoc - 1];
52
- const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
52
+ const token = tokens[index];
53
+
54
+ // If the mapped index is out of bounds, the returned cursor index will point after the end of the tokens array.
55
+ if (!token) {
56
+ return tokens.length;
57
+ }
53
58
 
54
59
  /*
55
60
  * For the map of "comment's location -> token's index", it points the next token of a comment.
56
61
  * In that case, +1 is unnecessary.
57
62
  */
58
- if (token && token.range[0] >= startLoc) {
63
+ if (token.range[0] >= startLoc) {
59
64
  return index;
60
65
  }
61
66
  return index + 1;
@@ -77,13 +82,18 @@ exports.getLastIndex = function getLastIndex(tokens, indexMap, endLoc) {
77
82
  }
78
83
  if ((endLoc - 1) in indexMap) {
79
84
  const index = indexMap[endLoc - 1];
80
- const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
85
+ const token = tokens[index];
86
+
87
+ // If the mapped index is out of bounds, the returned cursor index will point before the end of the tokens array.
88
+ if (!token) {
89
+ return tokens.length - 1;
90
+ }
81
91
 
82
92
  /*
83
93
  * For the map of "comment's location -> token's index", it points the next token of a comment.
84
94
  * In that case, -1 is necessary.
85
95
  */
86
- if (token && token.range[1] > endLoc) {
96
+ if (token.range[1] > endLoc) {
87
97
  return index - 1;
88
98
  }
89
99
  return index;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint",
3
- "version": "8.35.0",
3
+ "version": "8.37.0",
4
4
  "author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
5
5
  "description": "An AST-based pattern checker for JavaScript.",
6
6
  "bin": {
@@ -60,8 +60,10 @@
60
60
  "homepage": "https://eslint.org",
61
61
  "bugs": "https://github.com/eslint/eslint/issues/",
62
62
  "dependencies": {
63
- "@eslint/eslintrc": "^2.0.0",
64
- "@eslint/js": "8.35.0",
63
+ "@eslint-community/eslint-utils": "^4.2.0",
64
+ "@eslint-community/regexpp": "^4.4.0",
65
+ "@eslint/eslintrc": "^2.0.2",
66
+ "@eslint/js": "8.37.0",
65
67
  "@humanwhocodes/config-array": "^0.11.8",
66
68
  "@humanwhocodes/module-importer": "^1.0.1",
67
69
  "@nodelib/fs.walk": "^1.2.8",
@@ -72,9 +74,8 @@
72
74
  "doctrine": "^3.0.0",
73
75
  "escape-string-regexp": "^4.0.0",
74
76
  "eslint-scope": "^7.1.1",
75
- "eslint-utils": "^3.0.0",
76
- "eslint-visitor-keys": "^3.3.0",
77
- "espree": "^9.4.0",
77
+ "eslint-visitor-keys": "^3.4.0",
78
+ "espree": "^9.5.1",
78
79
  "esquery": "^1.4.2",
79
80
  "esutils": "^2.0.2",
80
81
  "fast-deep-equal": "^3.1.3",
@@ -96,7 +97,6 @@
96
97
  "minimatch": "^3.1.2",
97
98
  "natural-compare": "^1.4.0",
98
99
  "optionator": "^0.9.1",
99
- "regexpp": "^3.2.0",
100
100
  "strip-ansi": "^6.0.1",
101
101
  "strip-json-comments": "^3.1.0",
102
102
  "text-table": "^0.2.0"