eslint 8.22.0 → 8.33.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 (80) hide show
  1. package/README.md +51 -45
  2. package/bin/eslint.js +2 -4
  3. package/conf/globals.js +6 -1
  4. package/conf/rule-type-list.json +2 -2
  5. package/lib/cli-engine/file-enumerator.js +4 -2
  6. package/lib/cli-engine/formatters/formatters-meta.json +46 -0
  7. package/lib/cli-engine/formatters/html.js +76 -51
  8. package/lib/cli.js +163 -40
  9. package/lib/config/default-config.js +2 -2
  10. package/lib/config/flat-config-array.js +1 -1
  11. package/lib/eslint/eslint-helpers.js +409 -87
  12. package/lib/eslint/eslint.js +5 -2
  13. package/lib/eslint/flat-eslint.js +113 -110
  14. package/lib/linter/code-path-analysis/code-path-segment.js +2 -2
  15. package/lib/linter/code-path-analysis/code-path-state.js +7 -7
  16. package/lib/linter/code-path-analysis/debug-helpers.js +3 -3
  17. package/lib/linter/code-path-analysis/id-generator.js +2 -2
  18. package/lib/linter/config-comment-parser.js +1 -2
  19. package/lib/linter/linter.js +17 -7
  20. package/lib/linter/timing.js +4 -4
  21. package/lib/options.js +293 -239
  22. package/lib/rule-tester/flat-rule-tester.js +13 -11
  23. package/lib/rule-tester/rule-tester.js +15 -11
  24. package/lib/rules/array-callback-return.js +2 -2
  25. package/lib/rules/comma-dangle.js +3 -3
  26. package/lib/rules/for-direction.js +1 -1
  27. package/lib/rules/func-name-matching.js +2 -2
  28. package/lib/rules/getter-return.js +14 -8
  29. package/lib/rules/global-require.js +2 -1
  30. package/lib/rules/id-length.js +43 -2
  31. package/lib/rules/indent-legacy.js +4 -4
  32. package/lib/rules/indent.js +23 -15
  33. package/lib/rules/index.js +3 -0
  34. package/lib/rules/key-spacing.js +50 -38
  35. package/lib/rules/lines-around-comment.js +2 -2
  36. package/lib/rules/logical-assignment-operators.js +474 -0
  37. package/lib/rules/multiline-ternary.js +2 -2
  38. package/lib/rules/new-cap.js +2 -2
  39. package/lib/rules/no-else-return.js +1 -1
  40. package/lib/rules/no-empty-static-block.js +47 -0
  41. package/lib/rules/no-empty.js +19 -2
  42. package/lib/rules/no-extra-boolean-cast.js +1 -1
  43. package/lib/rules/no-extra-parens.js +18 -3
  44. package/lib/rules/no-fallthrough.js +26 -5
  45. package/lib/rules/no-implicit-coercion.js +20 -1
  46. package/lib/rules/no-implicit-globals.js +5 -0
  47. package/lib/rules/no-invalid-regexp.js +40 -18
  48. package/lib/rules/no-labels.js +1 -1
  49. package/lib/rules/no-lone-blocks.js +1 -1
  50. package/lib/rules/no-loss-of-precision.js +2 -2
  51. package/lib/rules/no-magic-numbers.js +18 -1
  52. package/lib/rules/no-misleading-character-class.js +4 -4
  53. package/lib/rules/no-new-native-nonconstructor.js +64 -0
  54. package/lib/rules/no-obj-calls.js +1 -1
  55. package/lib/rules/no-restricted-exports.js +106 -10
  56. package/lib/rules/no-return-await.js +28 -1
  57. package/lib/rules/no-underscore-dangle.js +36 -11
  58. package/lib/rules/no-unneeded-ternary.js +1 -1
  59. package/lib/rules/no-use-before-define.js +1 -1
  60. package/lib/rules/no-useless-computed-key.js +1 -1
  61. package/lib/rules/no-var.js +2 -2
  62. package/lib/rules/no-warning-comments.js +24 -5
  63. package/lib/rules/padded-blocks.js +1 -1
  64. package/lib/rules/prefer-arrow-callback.js +4 -3
  65. package/lib/rules/prefer-const.js +13 -1
  66. package/lib/rules/prefer-named-capture-group.js +71 -6
  67. package/lib/rules/prefer-object-spread.js +1 -1
  68. package/lib/rules/prefer-regex-literals.js +147 -32
  69. package/lib/rules/prefer-rest-params.js +1 -1
  70. package/lib/rules/require-yield.js +0 -1
  71. package/lib/rules/strict.js +1 -1
  72. package/lib/rules/utils/ast-utils.js +10 -4
  73. package/lib/shared/directives.js +15 -0
  74. package/lib/shared/logging.js +1 -1
  75. package/lib/shared/runtime-info.js +1 -1
  76. package/lib/shared/traverser.js +1 -1
  77. package/lib/shared/types.js +15 -2
  78. package/lib/source-code/token-store/cursor.js +1 -1
  79. package/messages/print-config-with-directory-path.js +1 -1
  80. package/package.json +27 -27
package/lib/options.js CHANGED
@@ -63,261 +63,315 @@ const optionator = require("optionator");
63
63
  //------------------------------------------------------------------------------
64
64
 
65
65
  // exports "parse(args)", "generateHelp()", and "generateHelpForOption(optionName)"
66
- module.exports = optionator({
67
- prepend: "eslint [options] file.js [file.js] [dir]",
68
- defaults: {
69
- concatRepeatedArrays: true,
70
- mergeRepeatedObjects: true
71
- },
72
- options: [
73
- {
74
- heading: "Basic configuration"
75
- },
76
- {
66
+
67
+ /**
68
+ * Creates the CLI options for ESLint.
69
+ * @param {boolean} usingFlatConfig Indicates if flat config is being used.
70
+ * @returns {Object} The optionator instance.
71
+ */
72
+ module.exports = function(usingFlatConfig) {
73
+
74
+ let lookupFlag;
75
+
76
+ if (usingFlatConfig) {
77
+ lookupFlag = {
78
+ option: "config-lookup",
79
+ type: "Boolean",
80
+ default: "true",
81
+ description: "Disable look up for eslint.config.js"
82
+ };
83
+ } else {
84
+ lookupFlag = {
77
85
  option: "eslintrc",
78
86
  type: "Boolean",
79
87
  default: "true",
80
88
  description: "Disable use of configuration from .eslintrc.*"
81
- },
82
- {
83
- option: "config",
84
- alias: "c",
85
- type: "path::String",
86
- description: "Use this configuration, overriding .eslintrc.* config options if present"
87
- },
88
- {
89
+ };
90
+ }
91
+
92
+ let envFlag;
93
+
94
+ if (!usingFlatConfig) {
95
+ envFlag = {
89
96
  option: "env",
90
97
  type: "[String]",
91
98
  description: "Specify environments"
92
- },
93
- {
99
+ };
100
+ }
101
+
102
+ let extFlag;
103
+
104
+ if (!usingFlatConfig) {
105
+ extFlag = {
94
106
  option: "ext",
95
107
  type: "[String]",
96
108
  description: "Specify JavaScript file extensions"
97
- },
98
- {
99
- option: "global",
100
- type: "[String]",
101
- description: "Define global variables"
102
- },
103
- {
104
- option: "parser",
105
- type: "String",
106
- description: "Specify the parser to be used"
107
- },
108
- {
109
- option: "parser-options",
110
- type: "Object",
111
- description: "Specify parser options"
112
- },
113
- {
109
+ };
110
+ }
111
+
112
+ let resolvePluginsFlag;
113
+
114
+ if (!usingFlatConfig) {
115
+ resolvePluginsFlag = {
114
116
  option: "resolve-plugins-relative-to",
115
117
  type: "path::String",
116
118
  description: "A folder where plugins should be resolved from, CWD by default"
117
- },
118
- {
119
- heading: "Specifying rules and plugins"
120
- },
121
- {
122
- option: "plugin",
123
- type: "[String]",
124
- description: "Specify plugins"
125
- },
126
- {
127
- option: "rule",
128
- type: "Object",
129
- description: "Specify rules"
130
- },
131
- {
119
+ };
120
+ }
121
+
122
+ let rulesDirFlag;
123
+
124
+ if (!usingFlatConfig) {
125
+ rulesDirFlag = {
132
126
  option: "rulesdir",
133
127
  type: "[path::String]",
134
128
  description: "Load additional rules from this directory. Deprecated: Use rules from plugins"
135
- },
136
- {
137
- heading: "Fixing problems"
138
- },
139
- {
140
- option: "fix",
141
- type: "Boolean",
142
- default: false,
143
- description: "Automatically fix problems"
144
- },
145
- {
146
- option: "fix-dry-run",
147
- type: "Boolean",
148
- default: false,
149
- description: "Automatically fix problems without saving the changes to the file system"
150
- },
151
- {
152
- option: "fix-type",
153
- type: "Array",
154
- description: "Specify the types of fixes to apply (directive, problem, suggestion, layout)"
155
- },
156
- {
157
- heading: "Ignoring files"
158
- },
159
- {
129
+ };
130
+ }
131
+
132
+ let ignorePathFlag;
133
+
134
+ if (!usingFlatConfig) {
135
+ ignorePathFlag = {
160
136
  option: "ignore-path",
161
137
  type: "path::String",
162
138
  description: "Specify path of ignore file"
163
- },
164
- {
165
- option: "ignore",
166
- type: "Boolean",
167
- default: "true",
168
- description: "Disable use of ignore files and patterns"
169
- },
170
- {
171
- option: "ignore-pattern",
172
- type: "[String]",
173
- description: "Pattern of files to ignore (in addition to those in .eslintignore)",
174
- concatRepeatedArrays: [true, {
175
- oneValuePerFlag: true
176
- }]
177
- },
178
- {
179
- heading: "Using stdin"
180
- },
181
- {
182
- option: "stdin",
183
- type: "Boolean",
184
- default: "false",
185
- description: "Lint code provided on <STDIN>"
186
- },
187
- {
188
- option: "stdin-filename",
189
- type: "String",
190
- description: "Specify filename to process STDIN as"
191
- },
192
- {
193
- heading: "Handling warnings"
194
- },
195
- {
196
- option: "quiet",
197
- type: "Boolean",
198
- default: "false",
199
- description: "Report errors only"
200
- },
201
- {
202
- option: "max-warnings",
203
- type: "Int",
204
- default: "-1",
205
- description: "Number of warnings to trigger nonzero exit code"
206
- },
207
- {
208
- heading: "Output"
209
- },
210
- {
211
- option: "output-file",
212
- alias: "o",
213
- type: "path::String",
214
- description: "Specify file to write report to"
215
- },
216
- {
217
- option: "format",
218
- alias: "f",
219
- type: "String",
220
- default: "stylish",
221
- description: "Use a specific output format"
222
- },
223
- {
224
- option: "color",
225
- type: "Boolean",
226
- alias: "no-color",
227
- description: "Force enabling/disabling of color"
228
- },
229
- {
230
- heading: "Inline configuration comments"
231
- },
232
- {
233
- option: "inline-config",
234
- type: "Boolean",
235
- default: "true",
236
- description: "Prevent comments from changing config or rules"
237
- },
238
- {
239
- option: "report-unused-disable-directives",
240
- type: "Boolean",
241
- default: void 0,
242
- description: "Adds reported errors for unused eslint-disable directives"
243
- },
244
- {
245
- heading: "Caching"
246
- },
247
- {
248
- option: "cache",
249
- type: "Boolean",
250
- default: "false",
251
- description: "Only check changed files"
252
- },
253
- {
254
- option: "cache-file",
255
- type: "path::String",
256
- default: ".eslintcache",
257
- description: "Path to the cache file. Deprecated: use --cache-location"
258
- },
259
- {
260
- option: "cache-location",
261
- type: "path::String",
262
- description: "Path to the cache file or directory"
263
- },
264
- {
265
- option: "cache-strategy",
266
- dependsOn: ["cache"],
267
- type: "String",
268
- default: "metadata",
269
- enum: ["metadata", "content"],
270
- description: "Strategy to use for detecting changed files in the cache"
271
- },
272
- {
273
- heading: "Miscellaneous"
274
- },
275
- {
276
- option: "init",
277
- type: "Boolean",
278
- default: "false",
279
- description: "Run config initialization wizard"
280
- },
281
- {
282
- option: "env-info",
283
- type: "Boolean",
284
- default: "false",
285
- description: "Output execution environment information"
286
- },
287
- {
288
- option: "error-on-unmatched-pattern",
289
- type: "Boolean",
290
- default: "true",
291
- description: "Prevent errors when pattern is unmatched"
292
- },
293
- {
294
- option: "exit-on-fatal-error",
295
- type: "Boolean",
296
- default: "false",
297
- description: "Exit with exit code 2 in case of fatal error"
298
- },
299
- {
300
- option: "debug",
301
- type: "Boolean",
302
- default: false,
303
- description: "Output debugging information"
304
- },
305
- {
306
- option: "help",
307
- alias: "h",
308
- type: "Boolean",
309
- description: "Show help"
310
- },
311
- {
312
- option: "version",
313
- alias: "v",
314
- type: "Boolean",
315
- description: "Output the version number"
316
- },
317
- {
318
- option: "print-config",
319
- type: "path::String",
320
- description: "Print the configuration for the given file"
321
- }
322
- ]
323
- });
139
+ };
140
+ }
141
+
142
+ return optionator({
143
+ prepend: "eslint [options] file.js [file.js] [dir]",
144
+ defaults: {
145
+ concatRepeatedArrays: true,
146
+ mergeRepeatedObjects: true
147
+ },
148
+ options: [
149
+ {
150
+ heading: "Basic configuration"
151
+ },
152
+ lookupFlag,
153
+ {
154
+ option: "config",
155
+ alias: "c",
156
+ type: "path::String",
157
+ description: usingFlatConfig
158
+ ? "Use this configuration instead of eslint.config.js"
159
+ : "Use this configuration, overriding .eslintrc.* config options if present"
160
+ },
161
+ envFlag,
162
+ extFlag,
163
+ {
164
+ option: "global",
165
+ type: "[String]",
166
+ description: "Define global variables"
167
+ },
168
+ {
169
+ option: "parser",
170
+ type: "String",
171
+ description: "Specify the parser to be used"
172
+ },
173
+ {
174
+ option: "parser-options",
175
+ type: "Object",
176
+ description: "Specify parser options"
177
+ },
178
+ resolvePluginsFlag,
179
+ {
180
+ heading: "Specify Rules and Plugins"
181
+ },
182
+ {
183
+ option: "plugin",
184
+ type: "[String]",
185
+ description: "Specify plugins"
186
+ },
187
+ {
188
+ option: "rule",
189
+ type: "Object",
190
+ description: "Specify rules"
191
+ },
192
+ rulesDirFlag,
193
+ {
194
+ heading: "Fix Problems"
195
+ },
196
+ {
197
+ option: "fix",
198
+ type: "Boolean",
199
+ default: false,
200
+ description: "Automatically fix problems"
201
+ },
202
+ {
203
+ option: "fix-dry-run",
204
+ type: "Boolean",
205
+ default: false,
206
+ description: "Automatically fix problems without saving the changes to the file system"
207
+ },
208
+ {
209
+ option: "fix-type",
210
+ type: "Array",
211
+ description: "Specify the types of fixes to apply (directive, problem, suggestion, layout)"
212
+ },
213
+ {
214
+ heading: "Ignore Files"
215
+ },
216
+ ignorePathFlag,
217
+ {
218
+ option: "ignore",
219
+ type: "Boolean",
220
+ default: "true",
221
+ description: "Disable use of ignore files and patterns"
222
+ },
223
+ {
224
+ option: "ignore-pattern",
225
+ type: "[String]",
226
+ description: "Pattern of files to ignore (in addition to those in .eslintignore)",
227
+ concatRepeatedArrays: [true, {
228
+ oneValuePerFlag: true
229
+ }]
230
+ },
231
+ {
232
+ heading: "Use stdin"
233
+ },
234
+ {
235
+ option: "stdin",
236
+ type: "Boolean",
237
+ default: "false",
238
+ description: "Lint code provided on <STDIN>"
239
+ },
240
+ {
241
+ option: "stdin-filename",
242
+ type: "String",
243
+ description: "Specify filename to process STDIN as"
244
+ },
245
+ {
246
+ heading: "Handle Warnings"
247
+ },
248
+ {
249
+ option: "quiet",
250
+ type: "Boolean",
251
+ default: "false",
252
+ description: "Report errors only"
253
+ },
254
+ {
255
+ option: "max-warnings",
256
+ type: "Int",
257
+ default: "-1",
258
+ description: "Number of warnings to trigger nonzero exit code"
259
+ },
260
+ {
261
+ heading: "Output"
262
+ },
263
+ {
264
+ option: "output-file",
265
+ alias: "o",
266
+ type: "path::String",
267
+ description: "Specify file to write report to"
268
+ },
269
+ {
270
+ option: "format",
271
+ alias: "f",
272
+ type: "String",
273
+ default: "stylish",
274
+ description: "Use a specific output format"
275
+ },
276
+ {
277
+ option: "color",
278
+ type: "Boolean",
279
+ alias: "no-color",
280
+ description: "Force enabling/disabling of color"
281
+ },
282
+ {
283
+ heading: "Inline configuration comments"
284
+ },
285
+ {
286
+ option: "inline-config",
287
+ type: "Boolean",
288
+ default: "true",
289
+ description: "Prevent comments from changing config or rules"
290
+ },
291
+ {
292
+ option: "report-unused-disable-directives",
293
+ type: "Boolean",
294
+ default: void 0,
295
+ description: "Adds reported errors for unused eslint-disable directives"
296
+ },
297
+ {
298
+ heading: "Caching"
299
+ },
300
+ {
301
+ option: "cache",
302
+ type: "Boolean",
303
+ default: "false",
304
+ description: "Only check changed files"
305
+ },
306
+ {
307
+ option: "cache-file",
308
+ type: "path::String",
309
+ default: ".eslintcache",
310
+ description: "Path to the cache file. Deprecated: use --cache-location"
311
+ },
312
+ {
313
+ option: "cache-location",
314
+ type: "path::String",
315
+ description: "Path to the cache file or directory"
316
+ },
317
+ {
318
+ option: "cache-strategy",
319
+ dependsOn: ["cache"],
320
+ type: "String",
321
+ default: "metadata",
322
+ enum: ["metadata", "content"],
323
+ description: "Strategy to use for detecting changed files in the cache"
324
+ },
325
+ {
326
+ heading: "Miscellaneous"
327
+ },
328
+ {
329
+ option: "init",
330
+ type: "Boolean",
331
+ default: "false",
332
+ description: "Run config initialization wizard"
333
+ },
334
+ {
335
+ option: "env-info",
336
+ type: "Boolean",
337
+ default: "false",
338
+ description: "Output execution environment information"
339
+ },
340
+ {
341
+ option: "error-on-unmatched-pattern",
342
+ type: "Boolean",
343
+ default: "true",
344
+ description: "Prevent errors when pattern is unmatched"
345
+ },
346
+ {
347
+ option: "exit-on-fatal-error",
348
+ type: "Boolean",
349
+ default: "false",
350
+ description: "Exit with exit code 2 in case of fatal error"
351
+ },
352
+ {
353
+ option: "debug",
354
+ type: "Boolean",
355
+ default: false,
356
+ description: "Output debugging information"
357
+ },
358
+ {
359
+ option: "help",
360
+ alias: "h",
361
+ type: "Boolean",
362
+ description: "Show help"
363
+ },
364
+ {
365
+ option: "version",
366
+ alias: "v",
367
+ type: "Boolean",
368
+ description: "Output the version number"
369
+ },
370
+ {
371
+ option: "print-config",
372
+ type: "path::String",
373
+ description: "Print the configuration for the given file"
374
+ }
375
+ ].filter(value => !!value)
376
+ });
377
+ };
@@ -4,7 +4,7 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
- /* eslint-env mocha -- Mocha/Jest wrapper */
7
+ /* globals describe, it -- Mocha globals */
8
8
 
9
9
  //------------------------------------------------------------------------------
10
10
  // Requirements
@@ -430,7 +430,7 @@ class FlatRuleTester {
430
430
  if (typeof this[DESCRIBE] === "function" || typeof this[IT] === "function") {
431
431
  throw new Error(
432
432
  "Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" +
433
- "See https://eslint.org/docs/developer-guide/nodejs-api#customizing-ruletester for more."
433
+ "See https://eslint.org/docs/latest/integrate/nodejs-api#customizing-ruletester for more."
434
434
  );
435
435
  }
436
436
  if (typeof it === "function") {
@@ -619,15 +619,17 @@ class FlatRuleTester {
619
619
  plugins: {
620
620
  "rule-tester": {
621
621
  rules: {
622
- "validate-ast"() {
623
- return {
624
- Program(node) {
625
- beforeAST = cloneDeeplyExcludesParent(node);
626
- },
627
- "Program:exit"(node) {
628
- afterAST = node;
629
- }
630
- };
622
+ "validate-ast": {
623
+ create() {
624
+ return {
625
+ Program(node) {
626
+ beforeAST = cloneDeeplyExcludesParent(node);
627
+ },
628
+ "Program:exit"(node) {
629
+ afterAST = node;
630
+ }
631
+ };
632
+ }
631
633
  }
632
634
  }
633
635
  }
@@ -4,7 +4,7 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
- /* eslint-env mocha -- Mocha wrapper */
7
+ /* globals describe, it -- Mocha globals */
8
8
 
9
9
  /*
10
10
  * This is a wrapper around mocha to allow for DRY unittests for eslint
@@ -314,7 +314,7 @@ function emitLegacyRuleAPIWarning(ruleName) {
314
314
  if (!emitLegacyRuleAPIWarning[`warned-${ruleName}`]) {
315
315
  emitLegacyRuleAPIWarning[`warned-${ruleName}`] = true;
316
316
  process.emitWarning(
317
- `"${ruleName}" rule is using the deprecated function-style format and will stop working in ESLint v9. Please use object-style format: https://eslint.org/docs/developer-guide/working-with-rules`,
317
+ `"${ruleName}" rule is using the deprecated function-style format and will stop working in ESLint v9. Please use object-style format: https://eslint.org/docs/latest/extend/custom-rules`,
318
318
  "DeprecationWarning"
319
319
  );
320
320
  }
@@ -329,7 +329,7 @@ function emitMissingSchemaWarning(ruleName) {
329
329
  if (!emitMissingSchemaWarning[`warned-${ruleName}`]) {
330
330
  emitMissingSchemaWarning[`warned-${ruleName}`] = true;
331
331
  process.emitWarning(
332
- `"${ruleName}" rule has options but is missing the "meta.schema" property and will stop working in ESLint v9. Please add a schema: https://eslint.org/docs/developer-guide/working-with-rules#options-schemas`,
332
+ `"${ruleName}" rule has options but is missing the "meta.schema" property and will stop working in ESLint v9. Please add a schema: https://eslint.org/docs/latest/extend/custom-rules#options-schemas`,
333
333
  "DeprecationWarning"
334
334
  );
335
335
  }
@@ -493,7 +493,7 @@ class RuleTester {
493
493
  if (typeof this[DESCRIBE] === "function" || typeof this[IT] === "function") {
494
494
  throw new Error(
495
495
  "Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" +
496
- "See https://eslint.org/docs/developer-guide/nodejs-api#customizing-ruletester for more."
496
+ "See https://eslint.org/docs/latest/integrate/nodejs-api#customizing-ruletester for more."
497
497
  );
498
498
  }
499
499
  if (typeof it === "function") {
@@ -632,14 +632,18 @@ class RuleTester {
632
632
  * The goal is to check whether or not AST was modified when
633
633
  * running the rule under test.
634
634
  */
635
- linter.defineRule("rule-tester/validate-ast", () => ({
636
- Program(node) {
637
- beforeAST = cloneDeeplyExcludesParent(node);
638
- },
639
- "Program:exit"(node) {
640
- afterAST = node;
635
+ linter.defineRule("rule-tester/validate-ast", {
636
+ create() {
637
+ return {
638
+ Program(node) {
639
+ beforeAST = cloneDeeplyExcludesParent(node);
640
+ },
641
+ "Program:exit"(node) {
642
+ afterAST = node;
643
+ }
644
+ };
641
645
  }
642
- }));
646
+ });
643
647
 
644
648
  if (typeof config.parser === "string") {
645
649
  assert(path.isAbsolute(config.parser), "Parsers provided as strings to RuleTester must be absolute paths");