eslint 8.57.0 → 9.2.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 (156) hide show
  1. package/README.md +31 -28
  2. package/bin/eslint.js +4 -3
  3. package/conf/ecma-version.js +16 -0
  4. package/conf/globals.js +1 -0
  5. package/conf/rule-type-list.json +3 -1
  6. package/lib/api.js +7 -11
  7. package/lib/cli-engine/cli-engine.js +14 -3
  8. package/lib/cli-engine/formatters/formatters-meta.json +1 -29
  9. package/lib/cli-engine/lint-result-cache.js +2 -2
  10. package/lib/cli.js +115 -36
  11. package/lib/config/default-config.js +3 -0
  12. package/lib/config/flat-config-array.js +110 -24
  13. package/lib/config/flat-config-helpers.js +41 -20
  14. package/lib/config/flat-config-schema.js +1 -7
  15. package/lib/config/rule-validator.js +42 -6
  16. package/lib/eslint/eslint-helpers.js +116 -58
  17. package/lib/eslint/eslint.js +892 -377
  18. package/lib/eslint/index.js +2 -2
  19. package/lib/eslint/legacy-eslint.js +728 -0
  20. package/lib/linter/apply-disable-directives.js +59 -31
  21. package/lib/linter/code-path-analysis/code-path-analyzer.js +0 -1
  22. package/lib/linter/code-path-analysis/code-path.js +32 -30
  23. package/lib/linter/code-path-analysis/fork-context.js +1 -1
  24. package/lib/linter/config-comment-parser.js +8 -11
  25. package/lib/linter/index.js +1 -3
  26. package/lib/linter/interpolate.js +24 -2
  27. package/lib/linter/linter.js +428 -207
  28. package/lib/linter/report-translator.js +3 -3
  29. package/lib/linter/rules.js +6 -15
  30. package/lib/linter/source-code-fixer.js +1 -1
  31. package/lib/linter/timing.js +16 -8
  32. package/lib/options.js +35 -3
  33. package/lib/rule-tester/index.js +3 -1
  34. package/lib/rule-tester/rule-tester.js +424 -347
  35. package/lib/rules/array-bracket-newline.js +1 -1
  36. package/lib/rules/array-bracket-spacing.js +1 -1
  37. package/lib/rules/block-scoped-var.js +1 -1
  38. package/lib/rules/callback-return.js +2 -2
  39. package/lib/rules/camelcase.js +3 -5
  40. package/lib/rules/capitalized-comments.js +10 -7
  41. package/lib/rules/comma-dangle.js +1 -1
  42. package/lib/rules/comma-style.js +2 -2
  43. package/lib/rules/complexity.js +14 -1
  44. package/lib/rules/constructor-super.js +99 -100
  45. package/lib/rules/default-case.js +1 -1
  46. package/lib/rules/eol-last.js +2 -2
  47. package/lib/rules/function-paren-newline.js +2 -2
  48. package/lib/rules/indent-legacy.js +5 -5
  49. package/lib/rules/indent.js +5 -5
  50. package/lib/rules/index.js +1 -2
  51. package/lib/rules/key-spacing.js +2 -2
  52. package/lib/rules/line-comment-position.js +1 -1
  53. package/lib/rules/lines-around-directive.js +2 -2
  54. package/lib/rules/max-depth.js +1 -1
  55. package/lib/rules/max-len.js +3 -3
  56. package/lib/rules/max-lines.js +3 -3
  57. package/lib/rules/max-nested-callbacks.js +1 -1
  58. package/lib/rules/max-params.js +1 -1
  59. package/lib/rules/max-statements.js +1 -1
  60. package/lib/rules/multiline-comment-style.js +7 -7
  61. package/lib/rules/new-cap.js +1 -1
  62. package/lib/rules/newline-after-var.js +1 -1
  63. package/lib/rules/newline-before-return.js +1 -1
  64. package/lib/rules/no-case-declarations.js +13 -1
  65. package/lib/rules/no-constant-binary-expression.js +7 -8
  66. package/lib/rules/no-constant-condition.js +18 -7
  67. package/lib/rules/no-constructor-return.js +2 -2
  68. package/lib/rules/no-dupe-class-members.js +2 -2
  69. package/lib/rules/no-else-return.js +1 -1
  70. package/lib/rules/no-empty-function.js +2 -2
  71. package/lib/rules/no-empty-static-block.js +1 -1
  72. package/lib/rules/no-extend-native.js +1 -2
  73. package/lib/rules/no-extra-semi.js +1 -1
  74. package/lib/rules/no-fallthrough.js +41 -16
  75. package/lib/rules/no-implicit-coercion.js +66 -24
  76. package/lib/rules/no-inner-declarations.js +23 -2
  77. package/lib/rules/no-invalid-regexp.js +1 -1
  78. package/lib/rules/no-invalid-this.js +1 -1
  79. package/lib/rules/no-lone-blocks.js +3 -3
  80. package/lib/rules/no-loss-of-precision.js +1 -1
  81. package/lib/rules/no-misleading-character-class.js +225 -69
  82. package/lib/rules/no-mixed-spaces-and-tabs.js +1 -1
  83. package/lib/rules/no-multiple-empty-lines.js +1 -1
  84. package/lib/rules/no-new-native-nonconstructor.js +1 -1
  85. package/lib/rules/no-new-symbol.js +8 -1
  86. package/lib/rules/no-restricted-globals.js +1 -1
  87. package/lib/rules/no-restricted-imports.js +186 -40
  88. package/lib/rules/no-restricted-modules.js +2 -2
  89. package/lib/rules/no-return-await.js +1 -1
  90. package/lib/rules/no-sequences.js +1 -0
  91. package/lib/rules/no-this-before-super.js +45 -13
  92. package/lib/rules/no-trailing-spaces.js +2 -3
  93. package/lib/rules/no-unneeded-ternary.js +1 -1
  94. package/lib/rules/no-unsafe-optional-chaining.js +1 -1
  95. package/lib/rules/no-unused-private-class-members.js +1 -1
  96. package/lib/rules/no-unused-vars.js +197 -36
  97. package/lib/rules/no-useless-assignment.js +566 -0
  98. package/lib/rules/no-useless-backreference.js +1 -1
  99. package/lib/rules/no-useless-computed-key.js +2 -2
  100. package/lib/rules/no-useless-return.js +7 -2
  101. package/lib/rules/object-curly-spacing.js +3 -3
  102. package/lib/rules/object-property-newline.js +1 -1
  103. package/lib/rules/one-var.js +5 -5
  104. package/lib/rules/padded-blocks.js +7 -7
  105. package/lib/rules/prefer-arrow-callback.js +3 -3
  106. package/lib/rules/prefer-reflect.js +1 -1
  107. package/lib/rules/prefer-regex-literals.js +1 -1
  108. package/lib/rules/prefer-template.js +1 -1
  109. package/lib/rules/radix.js +2 -2
  110. package/lib/rules/semi-style.js +1 -1
  111. package/lib/rules/sort-imports.js +1 -1
  112. package/lib/rules/sort-keys.js +1 -1
  113. package/lib/rules/sort-vars.js +1 -1
  114. package/lib/rules/space-unary-ops.js +1 -1
  115. package/lib/rules/strict.js +1 -1
  116. package/lib/rules/use-isnan.js +101 -7
  117. package/lib/rules/utils/ast-utils.js +16 -7
  118. package/lib/rules/utils/char-source.js +240 -0
  119. package/lib/rules/utils/lazy-loading-rule-map.js +1 -1
  120. package/lib/rules/utils/unicode/index.js +9 -4
  121. package/lib/rules/yield-star-spacing.js +1 -1
  122. package/lib/shared/runtime-info.js +1 -0
  123. package/lib/shared/serialization.js +55 -0
  124. package/lib/shared/stats.js +30 -0
  125. package/lib/shared/string-utils.js +9 -11
  126. package/lib/shared/types.js +35 -1
  127. package/lib/source-code/index.js +3 -1
  128. package/lib/source-code/source-code.js +299 -85
  129. package/lib/source-code/token-store/backward-token-cursor.js +3 -3
  130. package/lib/source-code/token-store/cursors.js +4 -2
  131. package/lib/source-code/token-store/forward-token-comment-cursor.js +3 -3
  132. package/lib/source-code/token-store/forward-token-cursor.js +3 -3
  133. package/lib/source-code/token-store/index.js +2 -2
  134. package/lib/unsupported-api.js +3 -5
  135. package/messages/no-config-found.js +1 -1
  136. package/messages/plugin-conflict.js +1 -1
  137. package/messages/plugin-invalid.js +1 -1
  138. package/messages/plugin-missing.js +1 -1
  139. package/package.json +32 -29
  140. package/conf/config-schema.js +0 -93
  141. package/lib/cli-engine/formatters/checkstyle.js +0 -60
  142. package/lib/cli-engine/formatters/compact.js +0 -60
  143. package/lib/cli-engine/formatters/jslint-xml.js +0 -41
  144. package/lib/cli-engine/formatters/junit.js +0 -82
  145. package/lib/cli-engine/formatters/tap.js +0 -95
  146. package/lib/cli-engine/formatters/unix.js +0 -58
  147. package/lib/cli-engine/formatters/visualstudio.js +0 -63
  148. package/lib/cli-engine/xml-escape.js +0 -34
  149. package/lib/eslint/flat-eslint.js +0 -1155
  150. package/lib/rule-tester/flat-rule-tester.js +0 -1131
  151. package/lib/rules/require-jsdoc.js +0 -122
  152. package/lib/rules/utils/patterns/letters.js +0 -36
  153. package/lib/rules/valid-jsdoc.js +0 -516
  154. package/lib/shared/config-validator.js +0 -347
  155. package/lib/shared/deprecation-warnings.js +0 -58
  156. package/lib/shared/relative-module-resolver.js +0 -50
@@ -109,12 +109,12 @@ module.exports = {
109
109
  options.var = { uninitialized: mode.var, initialized: mode.var };
110
110
  options.let = { uninitialized: mode.let, initialized: mode.let };
111
111
  options.const = { uninitialized: mode.const, initialized: mode.const };
112
- if (Object.prototype.hasOwnProperty.call(mode, "uninitialized")) {
112
+ if (Object.hasOwn(mode, "uninitialized")) {
113
113
  options.var.uninitialized = mode.uninitialized;
114
114
  options.let.uninitialized = mode.uninitialized;
115
115
  options.const.uninitialized = mode.uninitialized;
116
116
  }
117
- if (Object.prototype.hasOwnProperty.call(mode, "initialized")) {
117
+ if (Object.hasOwn(mode, "initialized")) {
118
118
  options.var.initialized = mode.initialized;
119
119
  options.let.initialized = mode.initialized;
120
120
  options.const.initialized = mode.initialized;
@@ -216,11 +216,11 @@ module.exports = {
216
216
  let currentScope;
217
217
 
218
218
  if (statementType === "var") {
219
- currentScope = functionStack[functionStack.length - 1];
219
+ currentScope = functionStack.at(-1);
220
220
  } else if (statementType === "let") {
221
- currentScope = blockStack[blockStack.length - 1].let;
221
+ currentScope = blockStack.at(-1).let;
222
222
  } else if (statementType === "const") {
223
- currentScope = blockStack[blockStack.length - 1].const;
223
+ currentScope = blockStack.at(-1).const;
224
224
  }
225
225
  return currentScope;
226
226
  }
@@ -84,18 +84,18 @@ module.exports = {
84
84
  options.switches = shouldHavePadding;
85
85
  options.classes = shouldHavePadding;
86
86
  } else {
87
- if (Object.prototype.hasOwnProperty.call(typeOptions, "blocks")) {
87
+ if (Object.hasOwn(typeOptions, "blocks")) {
88
88
  options.blocks = typeOptions.blocks === "always";
89
89
  }
90
- if (Object.prototype.hasOwnProperty.call(typeOptions, "switches")) {
90
+ if (Object.hasOwn(typeOptions, "switches")) {
91
91
  options.switches = typeOptions.switches === "always";
92
92
  }
93
- if (Object.prototype.hasOwnProperty.call(typeOptions, "classes")) {
93
+ if (Object.hasOwn(typeOptions, "classes")) {
94
94
  options.classes = typeOptions.classes === "always";
95
95
  }
96
96
  }
97
97
 
98
- if (Object.prototype.hasOwnProperty.call(exceptOptions, "allowSingleLineBlocks")) {
98
+ if (Object.hasOwn(exceptOptions, "allowSingleLineBlocks")) {
99
99
  options.allowSingleLineBlocks = exceptOptions.allowSingleLineBlocks === true;
100
100
  }
101
101
 
@@ -277,7 +277,7 @@ module.exports = {
277
277
 
278
278
  const rule = {};
279
279
 
280
- if (Object.prototype.hasOwnProperty.call(options, "switches")) {
280
+ if (Object.hasOwn(options, "switches")) {
281
281
  rule.SwitchStatement = function(node) {
282
282
  if (node.cases.length === 0) {
283
283
  return;
@@ -286,7 +286,7 @@ module.exports = {
286
286
  };
287
287
  }
288
288
 
289
- if (Object.prototype.hasOwnProperty.call(options, "blocks")) {
289
+ if (Object.hasOwn(options, "blocks")) {
290
290
  rule.BlockStatement = function(node) {
291
291
  if (node.body.length === 0) {
292
292
  return;
@@ -296,7 +296,7 @@ module.exports = {
296
296
  rule.StaticBlock = rule.BlockStatement;
297
297
  }
298
298
 
299
- if (Object.prototype.hasOwnProperty.call(options, "classes")) {
299
+ if (Object.hasOwn(options, "classes")) {
300
300
  rule.ClassBody = function(node) {
301
301
  if (node.body.length === 0) {
302
302
  return;
@@ -220,7 +220,7 @@ module.exports = {
220
220
 
221
221
  // If there are below, it cannot replace with arrow functions merely.
222
222
  ThisExpression() {
223
- const info = stack[stack.length - 1];
223
+ const info = stack.at(-1);
224
224
 
225
225
  if (info) {
226
226
  info.this = true;
@@ -228,7 +228,7 @@ module.exports = {
228
228
  },
229
229
 
230
230
  Super() {
231
- const info = stack[stack.length - 1];
231
+ const info = stack.at(-1);
232
232
 
233
233
  if (info) {
234
234
  info.super = true;
@@ -236,7 +236,7 @@ module.exports = {
236
236
  },
237
237
 
238
238
  MetaProperty(node) {
239
- const info = stack[stack.length - 1];
239
+ const info = stack.at(-1);
240
240
 
241
241
  if (info && checkMetaProperty(node, "new", "target")) {
242
242
  info.meta = true;
@@ -105,7 +105,7 @@ module.exports = {
105
105
  CallExpression(node) {
106
106
  const methodName = (node.callee.property || {}).name;
107
107
  const isReflectCall = (node.callee.object || {}).name === "Reflect";
108
- const hasReflectSubstitute = Object.prototype.hasOwnProperty.call(reflectSubstitutes, methodName);
108
+ const hasReflectSubstitute = Object.hasOwn(reflectSubstitutes, methodName);
109
109
  const userConfiguredException = exceptions.includes(methodName);
110
110
 
111
111
  if (hasReflectSubstitute && !isReflectCall && !userConfiguredException) {
@@ -34,7 +34,7 @@ function isStringLiteral(node) {
34
34
  * @returns {boolean} True if the node is a regex literal.
35
35
  */
36
36
  function isRegexLiteral(node) {
37
- return node.type === "Literal" && Object.prototype.hasOwnProperty.call(node, "regex");
37
+ return node.type === "Literal" && Object.hasOwn(node, "regex");
38
38
  }
39
39
 
40
40
  const validPrecedingTokens = new Set([
@@ -113,7 +113,7 @@ function endsWithTemplateCurly(node) {
113
113
  return startsWithTemplateCurly(node.right);
114
114
  }
115
115
  if (node.type === "TemplateLiteral") {
116
- return node.expressions.length && node.quasis.length && node.quasis[node.quasis.length - 1].range[0] === node.quasis[node.quasis.length - 1].range[1];
116
+ return node.expressions.length && node.quasis.length && node.quasis.at(-1).range[0] === node.quasis.at(-1).range[1];
117
117
  }
118
118
  return node.type !== "Literal" || typeof node.value !== "string";
119
119
  }
@@ -133,8 +133,8 @@ module.exports = {
133
133
  messageId: "addRadixParameter10",
134
134
  fix(fixer) {
135
135
  const tokens = sourceCode.getTokens(node);
136
- const lastToken = tokens[tokens.length - 1]; // Parenthesis.
137
- const secondToLastToken = tokens[tokens.length - 2]; // May or may not be a comma.
136
+ const lastToken = tokens.at(-1); // Parenthesis.
137
+ const secondToLastToken = tokens.at(-2); // May or may not be a comma.
138
138
  const hasTrailingComma = secondToLastToken.type === "Punctuator" && secondToLastToken.value === ",";
139
139
 
140
140
  return fixer.insertTextBefore(lastToken, hasTrailingComma ? " 10," : ", 10");
@@ -65,7 +65,7 @@ function isLastChild(node) {
65
65
  }
66
66
  const nodeList = getChildren(node.parent);
67
67
 
68
- return nodeList !== null && nodeList[nodeList.length - 1] === node; // before `}` or etc.
68
+ return nodeList !== null && nodeList.at(-1) === node; // before `}` or etc.
69
69
  }
70
70
 
71
71
  /** @type {import('../shared/types').Rule} */
@@ -208,7 +208,7 @@ module.exports = {
208
208
  }
209
209
 
210
210
  return fixer.replaceTextRange(
211
- [importSpecifiers[0].range[0], importSpecifiers[importSpecifiers.length - 1].range[1]],
211
+ [importSpecifiers[0].range[0], importSpecifiers.at(-1).range[1]],
212
212
  importSpecifiers
213
213
 
214
214
  // Clone the importSpecifiers array to avoid mutating it
@@ -185,7 +185,7 @@ module.exports = {
185
185
  });
186
186
 
187
187
  // check blank line between the current node and the last token
188
- if (!isBlankLineBetweenNodes && (node.loc.start.line - tokens[tokens.length - 1].loc.end.line > 1)) {
188
+ if (!isBlankLineBetweenNodes && (node.loc.start.line - tokens.at(-1).loc.end.line > 1)) {
189
189
  isBlankLineBetweenNodes = true;
190
190
  }
191
191
 
@@ -66,7 +66,7 @@ module.exports = {
66
66
  return null;
67
67
  }
68
68
  return fixer.replaceTextRange(
69
- [idDeclarations[0].range[0], idDeclarations[idDeclarations.length - 1].range[1]],
69
+ [idDeclarations[0].range[0], idDeclarations.at(-1).range[1]],
70
70
  idDeclarations
71
71
 
72
72
  // Clone the idDeclarations array to avoid mutating it
@@ -87,7 +87,7 @@ module.exports = {
87
87
  * @returns {boolean} Whether or not an override has been provided for the operator
88
88
  */
89
89
  function overrideExistsForOperator(operator) {
90
- return options.overrides && Object.prototype.hasOwnProperty.call(options.overrides, operator);
90
+ return options.overrides && Object.hasOwn(options.overrides, operator);
91
91
  }
92
92
 
93
93
  /**
@@ -173,7 +173,7 @@ module.exports = {
173
173
  function enterFunctionInFunctionMode(node, useStrictDirectives) {
174
174
  const isInClass = classScopes.length > 0,
175
175
  isParentGlobal = scopes.length === 0 && classScopes.length === 0,
176
- isParentStrict = scopes.length > 0 && scopes[scopes.length - 1],
176
+ isParentStrict = scopes.length > 0 && scopes.at(-1),
177
177
  isStrict = useStrictDirectives.length > 0;
178
178
 
179
179
  if (isStrict) {
@@ -21,9 +21,17 @@ const astUtils = require("./utils/ast-utils");
21
21
  * @returns {boolean} `true` if the node is 'NaN' identifier.
22
22
  */
23
23
  function isNaNIdentifier(node) {
24
- return Boolean(node) && (
25
- astUtils.isSpecificId(node, "NaN") ||
26
- astUtils.isSpecificMemberAccess(node, "Number", "NaN")
24
+ if (!node) {
25
+ return false;
26
+ }
27
+
28
+ const nodeToCheck = node.type === "SequenceExpression"
29
+ ? node.expressions.at(-1)
30
+ : node;
31
+
32
+ return (
33
+ astUtils.isSpecificId(nodeToCheck, "NaN") ||
34
+ astUtils.isSpecificMemberAccess(nodeToCheck, "Number", "NaN")
27
35
  );
28
36
  }
29
37
 
@@ -34,6 +42,7 @@ function isNaNIdentifier(node) {
34
42
  /** @type {import('../shared/types').Rule} */
35
43
  module.exports = {
36
44
  meta: {
45
+ hasSuggestions: true,
37
46
  type: "problem",
38
47
 
39
48
  docs: {
@@ -63,7 +72,10 @@ module.exports = {
63
72
  comparisonWithNaN: "Use the isNaN function to compare with NaN.",
64
73
  switchNaN: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.",
65
74
  caseNaN: "'case NaN' can never match. Use Number.isNaN before the switch.",
66
- indexOfNaN: "Array prototype method '{{ methodName }}' cannot find NaN."
75
+ indexOfNaN: "Array prototype method '{{ methodName }}' cannot find NaN.",
76
+ replaceWithIsNaN: "Replace with Number.isNaN.",
77
+ replaceWithCastingAndIsNaN: "Replace with Number.isNaN and cast to a Number.",
78
+ replaceWithFindIndex: "Replace with Array.prototype.{{ methodName }}."
67
79
  }
68
80
  },
69
81
 
@@ -71,6 +83,35 @@ module.exports = {
71
83
 
72
84
  const enforceForSwitchCase = !context.options[0] || context.options[0].enforceForSwitchCase;
73
85
  const enforceForIndexOf = context.options[0] && context.options[0].enforceForIndexOf;
86
+ const sourceCode = context.sourceCode;
87
+
88
+ const fixableOperators = new Set(["==", "===", "!=", "!=="]);
89
+ const castableOperators = new Set(["==", "!="]);
90
+
91
+ /**
92
+ * Get a fixer for a binary expression that compares to NaN.
93
+ * @param {ASTNode} node The node to fix.
94
+ * @param {function(string): string} wrapValue A function that wraps the compared value with a fix.
95
+ * @returns {function(Fixer): Fix} The fixer function.
96
+ */
97
+ function getBinaryExpressionFixer(node, wrapValue) {
98
+ return fixer => {
99
+ const comparedValue = isNaNIdentifier(node.left) ? node.right : node.left;
100
+ const shouldWrap = comparedValue.type === "SequenceExpression";
101
+ const shouldNegate = node.operator[0] === "!";
102
+
103
+ const negation = shouldNegate ? "!" : "";
104
+ let comparedValueText = sourceCode.getText(comparedValue);
105
+
106
+ if (shouldWrap) {
107
+ comparedValueText = `(${comparedValueText})`;
108
+ }
109
+
110
+ const fixedValue = wrapValue(comparedValueText);
111
+
112
+ return fixer.replaceText(node, `${negation}${fixedValue}`);
113
+ };
114
+ }
74
115
 
75
116
  /**
76
117
  * Checks the given `BinaryExpression` node for `foo === NaN` and other comparisons.
@@ -82,7 +123,32 @@ module.exports = {
82
123
  /^(?:[<>]|[!=]=)=?$/u.test(node.operator) &&
83
124
  (isNaNIdentifier(node.left) || isNaNIdentifier(node.right))
84
125
  ) {
85
- context.report({ node, messageId: "comparisonWithNaN" });
126
+ const suggestedFixes = [];
127
+ const NaNNode = isNaNIdentifier(node.left) ? node.left : node.right;
128
+
129
+ const isSequenceExpression = NaNNode.type === "SequenceExpression";
130
+ const isSuggestable = fixableOperators.has(node.operator) && !isSequenceExpression;
131
+ const isCastable = castableOperators.has(node.operator);
132
+
133
+ if (isSuggestable) {
134
+ suggestedFixes.push({
135
+ messageId: "replaceWithIsNaN",
136
+ fix: getBinaryExpressionFixer(node, value => `Number.isNaN(${value})`)
137
+ });
138
+
139
+ if (isCastable) {
140
+ suggestedFixes.push({
141
+ messageId: "replaceWithCastingAndIsNaN",
142
+ fix: getBinaryExpressionFixer(node, value => `Number.isNaN(Number(${value}))`)
143
+ });
144
+ }
145
+ }
146
+
147
+ context.report({
148
+ node,
149
+ messageId: "comparisonWithNaN",
150
+ suggest: suggestedFixes
151
+ });
86
152
  }
87
153
  }
88
154
 
@@ -116,10 +182,38 @@ module.exports = {
116
182
 
117
183
  if (
118
184
  (methodName === "indexOf" || methodName === "lastIndexOf") &&
119
- node.arguments.length === 1 &&
185
+ node.arguments.length <= 2 &&
120
186
  isNaNIdentifier(node.arguments[0])
121
187
  ) {
122
- context.report({ node, messageId: "indexOfNaN", data: { methodName } });
188
+
189
+ /*
190
+ * To retain side effects, it's essential to address `NaN` beforehand, which
191
+ * is not possible with fixes like `arr.findIndex(Number.isNaN)`.
192
+ */
193
+ const isSuggestable = node.arguments[0].type !== "SequenceExpression" && !node.arguments[1];
194
+ const suggestedFixes = [];
195
+
196
+ if (isSuggestable) {
197
+ const shouldWrap = callee.computed;
198
+ const findIndexMethod = methodName === "indexOf" ? "findIndex" : "findLastIndex";
199
+ const propertyName = shouldWrap ? `"${findIndexMethod}"` : findIndexMethod;
200
+
201
+ suggestedFixes.push({
202
+ messageId: "replaceWithFindIndex",
203
+ data: { methodName: findIndexMethod },
204
+ fix: fixer => [
205
+ fixer.replaceText(callee.property, propertyName),
206
+ fixer.replaceText(node.arguments[0], "Number.isNaN")
207
+ ]
208
+ });
209
+ }
210
+
211
+ context.report({
212
+ node,
213
+ messageId: "indexOfNaN",
214
+ data: { methodName },
215
+ suggest: suggestedFixes
216
+ });
123
217
  }
124
218
  }
125
219
  }
@@ -19,6 +19,8 @@ const {
19
19
  lineBreakPattern,
20
20
  shebangPattern
21
21
  } = require("../../shared/ast-utils");
22
+ const globals = require("../../../conf/globals");
23
+ const { LATEST_ECMA_VERSION } = require("../../../conf/ecma-version");
22
24
 
23
25
  //------------------------------------------------------------------------------
24
26
  // Helpers
@@ -46,6 +48,12 @@ const OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN = /^(?:[^\\]|\\.)*\\(?:[1-9]|0[0
46
48
 
47
49
  const LOGICAL_ASSIGNMENT_OPERATORS = new Set(["&&=", "||=", "??="]);
48
50
 
51
+ /**
52
+ * All builtin global variables defined in the latest ECMAScript specification.
53
+ * @type {Record<string,boolean>} Key is the name of the variable. Value is `true` if the variable is considered writable, `false` otherwise.
54
+ */
55
+ const ECMASCRIPT_GLOBALS = globals[`es${LATEST_ECMA_VERSION}`];
56
+
49
57
  /**
50
58
  * Checks reference if is non initializer and writable.
51
59
  * @param {Reference} reference A reference to check.
@@ -969,7 +977,7 @@ function isConstant(scope, node, inBooleanPosition) {
969
977
  return false;
970
978
 
971
979
  case "SequenceExpression":
972
- return isConstant(scope, node.expressions[node.expressions.length - 1], inBooleanPosition);
980
+ return isConstant(scope, node.expressions.at(-1), inBooleanPosition);
973
981
  case "SpreadElement":
974
982
  return isConstant(scope, node.argument, inBooleanPosition);
975
983
  case "CallExpression":
@@ -1133,6 +1141,7 @@ module.exports = {
1133
1141
  LINEBREAK_MATCHER: lineBreakPattern,
1134
1142
  SHEBANG_MATCHER: shebangPattern,
1135
1143
  STATEMENT_LIST_PARENTS,
1144
+ ECMASCRIPT_GLOBALS,
1136
1145
 
1137
1146
  /**
1138
1147
  * Determines whether two adjacent tokens are on the same line.
@@ -1231,7 +1240,7 @@ module.exports = {
1231
1240
  * @private
1232
1241
  */
1233
1242
  isSurroundedBy(val, character) {
1234
- return val[0] === character && val[val.length - 1] === character;
1243
+ return val[0] === character && val.at(-1) === character;
1235
1244
  },
1236
1245
 
1237
1246
  /**
@@ -1909,8 +1918,8 @@ module.exports = {
1909
1918
  */
1910
1919
  getFunctionHeadLoc(node, sourceCode) {
1911
1920
  const parent = node.parent;
1912
- let start = null;
1913
- let end = null;
1921
+ let start;
1922
+ let end;
1914
1923
 
1915
1924
  if (parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") {
1916
1925
  start = parent.loc.start;
@@ -2055,7 +2064,7 @@ module.exports = {
2055
2064
  case "SequenceExpression": {
2056
2065
  const exprs = node.expressions;
2057
2066
 
2058
- return exprs.length !== 0 && module.exports.couldBeError(exprs[exprs.length - 1]);
2067
+ return exprs.length !== 0 && module.exports.couldBeError(exprs.at(-1));
2059
2068
  }
2060
2069
 
2061
2070
  case "LogicalExpression":
@@ -2119,9 +2128,9 @@ module.exports = {
2119
2128
 
2120
2129
  const comments = tokens.comments;
2121
2130
 
2122
- leftToken = tokens[tokens.length - 1];
2131
+ leftToken = tokens.at(-1);
2123
2132
  if (comments.length) {
2124
- const lastComment = comments[comments.length - 1];
2133
+ const lastComment = comments.at(-1);
2125
2134
 
2126
2135
  if (!leftToken || lastComment.range[0] > leftToken.range[0]) {
2127
2136
  leftToken = lastComment;