-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
281 lines (218 loc) · 8.15 KB
/
Copy pathconfig.js
File metadata and controls
281 lines (218 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @fileoverview The `Config` class
* @author Nicholas C. Zakas
*/
"use strict";
//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------
const { RuleValidator } = require("./rule-validator");
const { flatConfigSchema, hasMethod } = require("./flat-config-schema");
const { ObjectSchema } = require("@eslint/config-array");
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
const ruleValidator = new RuleValidator();
const severities = new Map([
[0, 0],
[1, 1],
[2, 2],
["off", 0],
["warn", 1],
["error", 2]
]);
/**
* Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
* @param {string} identifier The identifier to parse.
* @returns {{objectName: string, pluginName: string}} The parts of the plugin
* name.
*/
function splitPluginIdentifier(identifier) {
const parts = identifier.split("/");
return {
objectName: parts.pop(),
pluginName: parts.join("/")
};
}
/**
* Returns the name of an object in the config by reading its `meta` key.
* @param {Object} object The object to check.
* @returns {string?} The name of the object if found or `null` if there
* is no name.
*/
function getObjectId(object) {
// first check old-style name
let name = object.name;
if (!name) {
if (!object.meta) {
return null;
}
name = object.meta.name;
if (!name) {
return null;
}
}
// now check for old-style version
let version = object.version;
if (!version) {
version = object.meta && object.meta.version;
}
// if there's a version then append that
if (version) {
return `${name}@${version}`;
}
return name;
}
/**
* Converts a languageOptions object to a JSON representation.
* @param {Record<string, any>} languageOptions The options to create a JSON
* representation of.
* @param {string} objectKey The key of the object being converted.
* @returns {Record<string, any>} The JSON representation of the languageOptions.
* @throws {TypeError} If a function is found in the languageOptions.
*/
function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
const result = {};
for (const [key, value] of Object.entries(languageOptions)) {
if (value) {
if (typeof value === "object") {
const name = getObjectId(value);
if (name && hasMethod(value)) {
result[key] = name;
} else {
result[key] = languageOptionsToJSON(value, key);
}
continue;
}
if (typeof value === "function") {
throw new TypeError(`Cannot serialize key "${key}" in ${objectKey}: Function values are not supported.`);
}
}
result[key] = value;
}
return result;
}
/**
* Normalizes the rules configuration. Ensure that each rule config is
* an array and that the severity is a number. This function modifies the
* rulesConfig.
* @param {Record<string, any>} rulesConfig The rules configuration to normalize.
* @returns {void}
*/
function normalizeRulesConfig(rulesConfig) {
for (const [ruleId, ruleConfig] of Object.entries(rulesConfig)) {
// ensure rule config is an array
if (!Array.isArray(ruleConfig)) {
rulesConfig[ruleId] = [ruleConfig];
}
// normalize severity
rulesConfig[ruleId][0] = severities.get(rulesConfig[ruleId][0]);
}
}
//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------
/**
* Represents a normalized configuration object.
*/
class Config {
/**
* The name to use for the language when serializing to JSON.
* @type {string|undefined}
*/
#languageName;
/**
* The name to use for the processor when serializing to JSON.
* @type {string|undefined}
*/
#processorName;
/**
* Creates a new instance.
* @param {Object} config The configuration object.
*/
constructor(config) {
const { plugins, language, languageOptions, processor, ...otherKeys } = config;
// Validate config object
const schema = new ObjectSchema(flatConfigSchema);
schema.validate(config);
// first, copy all the other keys over
Object.assign(this, otherKeys);
// ensure that a language is specified
if (!language) {
throw new TypeError("Key 'language' is required.");
}
// copy the rest over
this.plugins = plugins;
this.language = language;
// Check language value
const { pluginName: languagePluginName, objectName: localLanguageName } = splitPluginIdentifier(language);
this.#languageName = language;
if (!plugins || !plugins[languagePluginName] || !plugins[languagePluginName].languages || !plugins[languagePluginName].languages[localLanguageName]) {
throw new TypeError(`Key "language": Could not find "${localLanguageName}" in plugin "${languagePluginName}".`);
}
this.language = plugins[languagePluginName].languages[localLanguageName];
if (this.language.defaultLanguageOptions ?? languageOptions) {
this.languageOptions = flatConfigSchema.languageOptions.merge(
this.language.defaultLanguageOptions,
languageOptions
);
} else {
this.languageOptions = {};
}
// Validate language options
try {
this.language.validateLanguageOptions(this.languageOptions);
} catch (error) {
throw new TypeError(`Key "languageOptions": ${error.message}`, { cause: error });
}
// Check processor value
if (processor) {
this.processor = processor;
if (typeof processor === "string") {
const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor);
this.#processorName = processor;
if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) {
throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`);
}
this.processor = plugins[pluginName].processors[localProcessorName];
} else if (typeof processor === "object") {
this.#processorName = getObjectId(processor);
this.processor = processor;
} else {
throw new TypeError("Key 'processor' must be a string or an object.");
}
}
// Process the rules
if (this.rules) {
normalizeRulesConfig(this.rules);
ruleValidator.validate(this);
}
}
/**
* Converts the configuration to a JSON representation.
* @returns {Record<string, any>} The JSON representation of the configuration.
* @throws {Error} If the configuration cannot be serialized.
*/
toJSON() {
if (this.processor && !this.#processorName) {
throw new Error("Could not serialize processor object (missing 'meta' object).");
}
if (!this.#languageName) {
throw new Error("Could not serialize language object (missing 'meta' object).");
}
return {
...this,
plugins: Object.entries(this.plugins).map(([namespace, plugin]) => {
const pluginId = getObjectId(plugin);
if (!pluginId) {
return namespace;
}
return `${namespace}:${pluginId}`;
}),
language: this.#languageName,
languageOptions: languageOptionsToJSON(this.languageOptions),
processor: this.#processorName
};
}
}
module.exports = { Config };