eslint 9.9.1 → 9.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -297,9 +297,9 @@ The following companies, organizations, and individuals support ESLint's ongoing
297
297
  <!--sponsorsstart-->
298
298
  <h3>Platinum Sponsors</h3>
299
299
  <p><a href="https://automattic.com"><img src="https://images.opencollective.com/automattic/d0ef3e1/logo.png" alt="Automattic" height="128"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="128"></a></p><h3>Gold Sponsors</h3>
300
- <p><a href="#"><img src="https://images.opencollective.com/guest-bf377e88/avatar.png" alt="Eli Schleifer" height="96"></a> <a href="https://opensource.siemens.com"><img src="https://avatars.githubusercontent.com/u/624020?v=4" alt="Siemens" height="96"></a></p><h3>Silver Sponsors</h3>
300
+ <p><a href="https://trunk.io/"><img src="https://images.opencollective.com/trunkio/fb92d60/avatar.png" alt="trunk.io" height="96"></a> <a href="https://opensource.siemens.com"><img src="https://avatars.githubusercontent.com/u/624020?v=4" alt="Siemens" height="96"></a></p><h3>Silver Sponsors</h3>
301
301
  <p><a href="https://www.jetbrains.com/"><img src="https://images.opencollective.com/jetbrains/fe76f99/logo.png" alt="JetBrains" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a> <a href="https://www.workleap.com"><img src="https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4" alt="Workleap" height="64"></a></p><h3>Bronze Sponsors</h3>
302
- <p><a href="https://www.notion.so"><img src="https://images.opencollective.com/notion/bf3b117/logo.png" alt="notion" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104?v=4" alt="Nx" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a> <a href="https://usenextbase.com"><img src="https://avatars.githubusercontent.com/u/145838380?v=4" alt="Nextbase Starter Kit" height="32"></a></p>
302
+ <p><a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104?v=4" alt="Nx" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a> <a href="https://usenextbase.com"><img src="https://avatars.githubusercontent.com/u/145838380?v=4" alt="Nextbase Starter Kit" height="32"></a></p>
303
303
  <!--sponsorsend-->
304
304
 
305
305
  <!--techsponsorsstart-->
@@ -0,0 +1,278 @@
1
+ /**
2
+ * @fileoverview The `Config` class
3
+ * @author Nicholas C. Zakas
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //-----------------------------------------------------------------------------
9
+ // Requirements
10
+ //-----------------------------------------------------------------------------
11
+
12
+ const { RuleValidator } = require("./rule-validator");
13
+ const { flatConfigSchema, hasMethod } = require("./flat-config-schema");
14
+ const { ObjectSchema } = require("@eslint/config-array");
15
+
16
+ //-----------------------------------------------------------------------------
17
+ // Helpers
18
+ //-----------------------------------------------------------------------------
19
+
20
+ const ruleValidator = new RuleValidator();
21
+
22
+ const severities = new Map([
23
+ [0, 0],
24
+ [1, 1],
25
+ [2, 2],
26
+ ["off", 0],
27
+ ["warn", 1],
28
+ ["error", 2]
29
+ ]);
30
+
31
+ /**
32
+ * Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
33
+ * @param {string} identifier The identifier to parse.
34
+ * @returns {{objectName: string, pluginName: string}} The parts of the plugin
35
+ * name.
36
+ */
37
+ function splitPluginIdentifier(identifier) {
38
+ const parts = identifier.split("/");
39
+
40
+ return {
41
+ objectName: parts.pop(),
42
+ pluginName: parts.join("/")
43
+ };
44
+ }
45
+
46
+ /**
47
+ * Returns the name of an object in the config by reading its `meta` key.
48
+ * @param {Object} object The object to check.
49
+ * @returns {string?} The name of the object if found or `null` if there
50
+ * is no name.
51
+ */
52
+ function getObjectId(object) {
53
+
54
+ // first check old-style name
55
+ let name = object.name;
56
+
57
+ if (!name) {
58
+
59
+ if (!object.meta) {
60
+ return null;
61
+ }
62
+
63
+ name = object.meta.name;
64
+
65
+ if (!name) {
66
+ return null;
67
+ }
68
+ }
69
+
70
+ // now check for old-style version
71
+ let version = object.version;
72
+
73
+ if (!version) {
74
+ version = object.meta && object.meta.version;
75
+ }
76
+
77
+ // if there's a version then append that
78
+ if (version) {
79
+ return `${name}@${version}`;
80
+ }
81
+
82
+ return name;
83
+ }
84
+
85
+ /**
86
+ * Converts a languageOptions object to a JSON representation.
87
+ * @param {Record<string, any>} languageOptions The options to create a JSON
88
+ * representation of.
89
+ * @param {string} objectKey The key of the object being converted.
90
+ * @returns {Record<string, any>} The JSON representation of the languageOptions.
91
+ * @throws {TypeError} If a function is found in the languageOptions.
92
+ */
93
+ function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
94
+
95
+ const result = {};
96
+
97
+ for (const [key, value] of Object.entries(languageOptions)) {
98
+ if (value) {
99
+ if (typeof value === "object") {
100
+ const name = getObjectId(value);
101
+
102
+ if (name && hasMethod(value)) {
103
+ result[key] = name;
104
+ } else {
105
+ result[key] = languageOptionsToJSON(value, key);
106
+ }
107
+ continue;
108
+ }
109
+
110
+ if (typeof value === "function") {
111
+ throw new TypeError(`Cannot serialize key "${key}" in ${objectKey}: Function values are not supported.`);
112
+ }
113
+
114
+ }
115
+
116
+ result[key] = value;
117
+ }
118
+
119
+ return result;
120
+ }
121
+
122
+ /**
123
+ * Normalizes the rules configuration. Ensure that each rule config is
124
+ * an array and that the severity is a number. This function modifies the
125
+ * rulesConfig.
126
+ * @param {Record<string, any>} rulesConfig The rules configuration to normalize.
127
+ * @returns {void}
128
+ */
129
+ function normalizeRulesConfig(rulesConfig) {
130
+
131
+ for (const [ruleId, ruleConfig] of Object.entries(rulesConfig)) {
132
+
133
+ // ensure rule config is an array
134
+ if (!Array.isArray(ruleConfig)) {
135
+ rulesConfig[ruleId] = [ruleConfig];
136
+ }
137
+
138
+ // normalize severity
139
+ rulesConfig[ruleId][0] = severities.get(rulesConfig[ruleId][0]);
140
+ }
141
+
142
+ }
143
+
144
+
145
+ //-----------------------------------------------------------------------------
146
+ // Exports
147
+ //-----------------------------------------------------------------------------
148
+
149
+ /**
150
+ * Represents a normalized configuration object.
151
+ */
152
+ class Config {
153
+
154
+ /**
155
+ * The name to use for the language when serializing to JSON.
156
+ * @type {string|undefined}
157
+ */
158
+ #languageName;
159
+
160
+ /**
161
+ * The name to use for the processor when serializing to JSON.
162
+ * @type {string|undefined}
163
+ */
164
+ #processorName;
165
+
166
+ /**
167
+ * Creates a new instance.
168
+ * @param {Object} config The configuration object.
169
+ */
170
+ constructor(config) {
171
+
172
+ const { plugins, language, languageOptions, processor, ...otherKeys } = config;
173
+
174
+ // Validate config object
175
+ const schema = new ObjectSchema(flatConfigSchema);
176
+
177
+ schema.validate(config);
178
+
179
+ // first, copy all the other keys over
180
+ Object.assign(this, otherKeys);
181
+
182
+ // ensure that a language is specified
183
+ if (!language) {
184
+ throw new TypeError("Key 'language' is required.");
185
+ }
186
+
187
+ // copy the rest over
188
+ this.plugins = plugins;
189
+ this.language = language;
190
+
191
+ if (languageOptions) {
192
+ this.languageOptions = languageOptions;
193
+ }
194
+
195
+ // Check language value
196
+ const { pluginName: languagePluginName, objectName: localLanguageName } = splitPluginIdentifier(language);
197
+
198
+ this.#languageName = language;
199
+
200
+ if (!plugins || !plugins[languagePluginName] || !plugins[languagePluginName].languages || !plugins[languagePluginName].languages[localLanguageName]) {
201
+ throw new TypeError(`Key "language": Could not find "${localLanguageName}" in plugin "${languagePluginName}".`);
202
+ }
203
+
204
+ this.language = plugins[languagePluginName].languages[localLanguageName];
205
+
206
+ // Validate language options
207
+ if (this.languageOptions) {
208
+ try {
209
+ this.language.validateLanguageOptions(this.languageOptions);
210
+ } catch (error) {
211
+ throw new TypeError(`Key "languageOptions": ${error.message}`, { cause: error });
212
+ }
213
+ }
214
+
215
+ // Check processor value
216
+ if (processor) {
217
+ this.processor = processor;
218
+
219
+ if (typeof processor === "string") {
220
+ const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor);
221
+
222
+ this.#processorName = processor;
223
+
224
+ if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) {
225
+ throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`);
226
+ }
227
+
228
+ this.processor = plugins[pluginName].processors[localProcessorName];
229
+ } else if (typeof processor === "object") {
230
+ this.#processorName = getObjectId(processor);
231
+ this.processor = processor;
232
+ } else {
233
+ throw new TypeError("Key 'processor' must be a string or an object.");
234
+ }
235
+ }
236
+
237
+ // Process the rules
238
+ if (this.rules) {
239
+ normalizeRulesConfig(this.rules);
240
+ ruleValidator.validate(this);
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Converts the configuration to a JSON representation.
246
+ * @returns {Record<string, any>} The JSON representation of the configuration.
247
+ * @throws {Error} If the configuration cannot be serialized.
248
+ */
249
+ toJSON() {
250
+
251
+ if (this.processor && !this.#processorName) {
252
+ throw new Error("Could not serialize processor object (missing 'meta' object).");
253
+ }
254
+
255
+ if (!this.#languageName) {
256
+ throw new Error("Could not serialize language object (missing 'meta' object).");
257
+ }
258
+
259
+ return {
260
+ ...this,
261
+ plugins: Object.entries(this.plugins).map(([namespace, plugin]) => {
262
+
263
+ const pluginId = getObjectId(plugin);
264
+
265
+ if (!pluginId) {
266
+ return namespace;
267
+ }
268
+
269
+ return `${namespace}:${pluginId}`;
270
+ }),
271
+ language: this.#languageName,
272
+ languageOptions: languageOptionsToJSON(this.languageOptions),
273
+ processor: this.#processorName
274
+ };
275
+ }
276
+ }
277
+
278
+ module.exports = { Config };
@@ -10,9 +10,9 @@
10
10
  //-----------------------------------------------------------------------------
11
11
 
12
12
  const { ConfigArray, ConfigArraySymbol } = require("@eslint/config-array");
13
- const { flatConfigSchema, hasMethod } = require("./flat-config-schema");
14
- const { RuleValidator } = require("./rule-validator");
13
+ const { flatConfigSchema } = require("./flat-config-schema");
15
14
  const { defaultConfig } = require("./default-config");
15
+ const { Config } = require("./config");
16
16
 
17
17
  //-----------------------------------------------------------------------------
18
18
  // Helpers
@@ -23,62 +23,6 @@ const { defaultConfig } = require("./default-config");
23
23
  */
24
24
  const META_FIELDS = new Set(["name"]);
25
25
 
26
- const ruleValidator = new RuleValidator();
27
-
28
- /**
29
- * Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
30
- * @param {string} identifier The identifier to parse.
31
- * @returns {{objectName: string, pluginName: string}} The parts of the plugin
32
- * name.
33
- */
34
- function splitPluginIdentifier(identifier) {
35
- const parts = identifier.split("/");
36
-
37
- return {
38
- objectName: parts.pop(),
39
- pluginName: parts.join("/")
40
- };
41
- }
42
-
43
- /**
44
- * Returns the name of an object in the config by reading its `meta` key.
45
- * @param {Object} object The object to check.
46
- * @returns {string?} The name of the object if found or `null` if there
47
- * is no name.
48
- */
49
- function getObjectId(object) {
50
-
51
- // first check old-style name
52
- let name = object.name;
53
-
54
- if (!name) {
55
-
56
- if (!object.meta) {
57
- return null;
58
- }
59
-
60
- name = object.meta.name;
61
-
62
- if (!name) {
63
- return null;
64
- }
65
- }
66
-
67
- // now check for old-style version
68
- let version = object.version;
69
-
70
- if (!version) {
71
- version = object.meta && object.meta.version;
72
- }
73
-
74
- // if there's a version then append that
75
- if (version) {
76
- return `${name}@${version}`;
77
- }
78
-
79
- return name;
80
- }
81
-
82
26
  /**
83
27
  * Wraps a config error with details about where the error occurred.
84
28
  * @param {Error} error The original error.
@@ -123,42 +67,6 @@ function wrapConfigErrorWithDetails(error, originalLength, baseLength) {
123
67
  );
124
68
  }
125
69
 
126
- /**
127
- * Converts a languageOptions object to a JSON representation.
128
- * @param {Record<string, any>} languageOptions The options to create a JSON
129
- * representation of.
130
- * @param {string} objectKey The key of the object being converted.
131
- * @returns {Record<string, any>} The JSON representation of the languageOptions.
132
- * @throws {TypeError} If a function is found in the languageOptions.
133
- */
134
- function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
135
-
136
- const result = {};
137
-
138
- for (const [key, value] of Object.entries(languageOptions)) {
139
- if (value) {
140
- if (typeof value === "object") {
141
- const name = getObjectId(value);
142
-
143
- if (name && hasMethod(value)) {
144
- result[key] = name;
145
- } else {
146
- result[key] = languageOptionsToJSON(value, key);
147
- }
148
- continue;
149
- }
150
-
151
- if (typeof value === "function") {
152
- throw new TypeError(`Cannot serialize key "${key}" in ${objectKey}: Function values are not supported.`);
153
- }
154
-
155
- }
156
-
157
- result[key] = value;
158
- }
159
-
160
- return result;
161
- }
162
70
 
163
71
  const originalBaseConfig = Symbol("originalBaseConfig");
164
72
  const originalLength = Symbol("originalLength");
@@ -305,116 +213,7 @@ class FlatConfigArray extends ConfigArray {
305
213
  * @throws {TypeError} If the config is invalid.
306
214
  */
307
215
  [ConfigArraySymbol.finalizeConfig](config) {
308
-
309
- const { plugins, language, languageOptions, processor } = config;
310
- let parserName, processorName, languageName;
311
- let invalidParser = false,
312
- invalidProcessor = false,
313
- invalidLanguage = false;
314
-
315
- // Check parser value
316
- if (languageOptions && languageOptions.parser) {
317
- const { parser } = languageOptions;
318
-
319
- if (typeof parser === "object") {
320
- parserName = getObjectId(parser);
321
-
322
- if (!parserName) {
323
- invalidParser = true;
324
- }
325
-
326
- } else {
327
- invalidParser = true;
328
- }
329
- }
330
-
331
- // Check language value
332
- if (language) {
333
- if (typeof language === "string") {
334
- const { pluginName, objectName: localLanguageName } = splitPluginIdentifier(language);
335
-
336
- languageName = language;
337
-
338
- if (!plugins || !plugins[pluginName] || !plugins[pluginName].languages || !plugins[pluginName].languages[localLanguageName]) {
339
- throw new TypeError(`Key "language": Could not find "${localLanguageName}" in plugin "${pluginName}".`);
340
- }
341
-
342
- config.language = plugins[pluginName].languages[localLanguageName];
343
- } else {
344
- invalidLanguage = true;
345
- }
346
-
347
- try {
348
- config.language.validateLanguageOptions(config.languageOptions);
349
- } catch (error) {
350
- throw new TypeError(`Key "languageOptions": ${error.message}`, { cause: error });
351
- }
352
- }
353
-
354
- // Check processor value
355
- if (processor) {
356
- if (typeof processor === "string") {
357
- const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor);
358
-
359
- processorName = processor;
360
-
361
- if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) {
362
- throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`);
363
- }
364
-
365
- config.processor = plugins[pluginName].processors[localProcessorName];
366
- } else if (typeof processor === "object") {
367
- processorName = getObjectId(processor);
368
-
369
- if (!processorName) {
370
- invalidProcessor = true;
371
- }
372
-
373
- } else {
374
- invalidProcessor = true;
375
- }
376
- }
377
-
378
- ruleValidator.validate(config);
379
-
380
- // apply special logic for serialization into JSON
381
- /* eslint-disable object-shorthand -- shorthand would change "this" value */
382
- Object.defineProperty(config, "toJSON", {
383
- value: function() {
384
-
385
- if (invalidParser) {
386
- throw new Error("Could not serialize parser object (missing 'meta' object).");
387
- }
388
-
389
- if (invalidProcessor) {
390
- throw new Error("Could not serialize processor object (missing 'meta' object).");
391
- }
392
-
393
- if (invalidLanguage) {
394
- throw new Error("Caching is not supported when language is an object.");
395
- }
396
-
397
- return {
398
- ...this,
399
- plugins: Object.entries(plugins).map(([namespace, plugin]) => {
400
-
401
- const pluginId = getObjectId(plugin);
402
-
403
- if (!pluginId) {
404
- return namespace;
405
- }
406
-
407
- return `${namespace}:${pluginId}`;
408
- }),
409
- language: languageName,
410
- languageOptions: languageOptionsToJSON(languageOptions),
411
- processor: processorName
412
- };
413
- }
414
- });
415
- /* eslint-enable object-shorthand -- ok to enable now */
416
-
417
- return config;
216
+ return new Config(config);
418
217
  }
419
218
  /* eslint-enable class-methods-use-this -- Desired as instance method */
420
219