1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const
|
13 | path = require("path"),
|
14 | util = require("util"),
|
15 | lodash = require("lodash"),
|
16 | configSchema = require("../../conf/config-schema"),
|
17 | BuiltInEnvironments = require("../../conf/environments"),
|
18 | BuiltInRules = require("../rules"),
|
19 | ConfigOps = require("./config-ops");
|
20 |
|
21 | const ajv = require("./ajv")();
|
22 | const ruleValidators = new WeakMap();
|
23 | const noop = Function.prototype;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | let validateSchema;
|
29 |
|
30 |
|
31 | const deprecationWarningMessages = {
|
32 | ESLINT_LEGACY_ECMAFEATURES: "The 'ecmaFeatures' config file property is deprecated, and has no effect."
|
33 | };
|
34 | const severityMap = {
|
35 | error: 2,
|
36 | warn: 1,
|
37 | off: 0
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function getRuleOptionsSchema(rule) {
|
46 | if (!rule) {
|
47 | return null;
|
48 | }
|
49 |
|
50 | const schema = rule.schema || rule.meta && rule.meta.schema;
|
51 |
|
52 |
|
53 | if (Array.isArray(schema)) {
|
54 | if (schema.length) {
|
55 | return {
|
56 | type: "array",
|
57 | items: schema,
|
58 | minItems: 0,
|
59 | maxItems: schema.length
|
60 | };
|
61 | }
|
62 | return {
|
63 | type: "array",
|
64 | minItems: 0,
|
65 | maxItems: 0
|
66 | };
|
67 |
|
68 | }
|
69 |
|
70 |
|
71 | return schema || null;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function validateRuleSeverity(options) {
|
80 | const severity = Array.isArray(options) ? options[0] : options;
|
81 | const normSeverity = typeof severity === "string" ? severityMap[severity.toLowerCase()] : severity;
|
82 |
|
83 | if (normSeverity === 0 || normSeverity === 1 || normSeverity === 2) {
|
84 | return normSeverity;
|
85 | }
|
86 |
|
87 | throw new Error(`\tSeverity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '${util.inspect(severity).replace(/'/gu, "\"").replace(/\n/gu, "")}').\n`);
|
88 |
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function validateRuleSchema(rule, localOptions) {
|
98 | if (!ruleValidators.has(rule)) {
|
99 | const schema = getRuleOptionsSchema(rule);
|
100 |
|
101 | if (schema) {
|
102 | ruleValidators.set(rule, ajv.compile(schema));
|
103 | }
|
104 | }
|
105 |
|
106 | const validateRule = ruleValidators.get(rule);
|
107 |
|
108 | if (validateRule) {
|
109 | validateRule(localOptions);
|
110 | if (validateRule.errors) {
|
111 | throw new Error(validateRule.errors.map(
|
112 | error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
|
113 | ).join(""));
|
114 | }
|
115 | }
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function validateRuleOptions(rule, ruleId, options, source = null) {
|
128 | try {
|
129 | const severity = validateRuleSeverity(options);
|
130 |
|
131 | if (severity !== 0) {
|
132 | validateRuleSchema(rule, Array.isArray(options) ? options.slice(1) : []);
|
133 | }
|
134 | } catch (err) {
|
135 | const enhancedMessage = `Configuration for rule "${ruleId}" is invalid:\n${err.message}`;
|
136 |
|
137 | if (typeof source === "string") {
|
138 | throw new Error(`${source}:\n\t${enhancedMessage}`);
|
139 | } else {
|
140 | throw new Error(enhancedMessage);
|
141 | }
|
142 | }
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function validateEnvironment(
|
153 | environment,
|
154 | source,
|
155 | getAdditionalEnv = noop
|
156 | ) {
|
157 |
|
158 |
|
159 | if (!environment) {
|
160 | return;
|
161 | }
|
162 |
|
163 | Object.keys(environment).forEach(id => {
|
164 | const env = getAdditionalEnv(id) || BuiltInEnvironments.get(id) || null;
|
165 |
|
166 | if (!env) {
|
167 | const message = `${source}:\n\tEnvironment key "${id}" is unknown\n`;
|
168 |
|
169 | throw new Error(message);
|
170 | }
|
171 | });
|
172 | }
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | function validateRules(
|
182 | rulesConfig,
|
183 | source,
|
184 | getAdditionalRule = noop
|
185 | ) {
|
186 | if (!rulesConfig) {
|
187 | return;
|
188 | }
|
189 |
|
190 | Object.keys(rulesConfig).forEach(id => {
|
191 | const rule = getAdditionalRule(id) || BuiltInRules.get(id) || null;
|
192 |
|
193 | validateRuleOptions(rule, id, rulesConfig[id], source);
|
194 | });
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | function validateGlobals(globalsConfig, source = null) {
|
204 | if (!globalsConfig) {
|
205 | return;
|
206 | }
|
207 |
|
208 | Object.entries(globalsConfig)
|
209 | .forEach(([configuredGlobal, configuredValue]) => {
|
210 | try {
|
211 | ConfigOps.normalizeConfigGlobal(configuredValue);
|
212 | } catch (err) {
|
213 | throw new Error(`ESLint configuration of global '${configuredGlobal}' in ${source} is invalid:\n${err.message}`);
|
214 | }
|
215 | });
|
216 | }
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | function validateProcessor(processorName, source, getProcessor) {
|
226 | if (processorName && !getProcessor(processorName)) {
|
227 | throw new Error(`ESLint configuration of processor in '${source}' is invalid: '${processorName}' was not found.`);
|
228 | }
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | function formatErrors(errors) {
|
237 | return errors.map(error => {
|
238 | if (error.keyword === "additionalProperties") {
|
239 | const formattedPropertyPath = error.dataPath.length ? `${error.dataPath.slice(1)}.${error.params.additionalProperty}` : error.params.additionalProperty;
|
240 |
|
241 | return `Unexpected top-level property "${formattedPropertyPath}"`;
|
242 | }
|
243 | if (error.keyword === "type") {
|
244 | const formattedField = error.dataPath.slice(1);
|
245 | const formattedExpectedType = Array.isArray(error.schema) ? error.schema.join("/") : error.schema;
|
246 | const formattedValue = JSON.stringify(error.data);
|
247 |
|
248 | return `Property "${formattedField}" is the wrong type (expected ${formattedExpectedType} but got \`${formattedValue}\`)`;
|
249 | }
|
250 |
|
251 | const field = error.dataPath[0] === "." ? error.dataPath.slice(1) : error.dataPath;
|
252 |
|
253 | return `"${field}" ${error.message}. Value: ${JSON.stringify(error.data)}`;
|
254 | }).map(message => `\t- ${message}.\n`).join("");
|
255 | }
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | const emitDeprecationWarning = lodash.memoize((source, errorCode) => {
|
266 | const rel = path.relative(process.cwd(), source);
|
267 | const message = deprecationWarningMessages[errorCode];
|
268 |
|
269 | process.emitWarning(
|
270 | `${message} (found in "${rel}")`,
|
271 | "DeprecationWarning",
|
272 | errorCode
|
273 | );
|
274 | });
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | function validateConfigSchema(config, source = null) {
|
283 | validateSchema = validateSchema || ajv.compile(configSchema);
|
284 |
|
285 | if (!validateSchema(config)) {
|
286 | throw new Error(`ESLint configuration in ${source} is invalid:\n${formatErrors(validateSchema.errors)}`);
|
287 | }
|
288 |
|
289 | if (Object.hasOwnProperty.call(config, "ecmaFeatures")) {
|
290 | emitDeprecationWarning(source, "ESLINT_LEGACY_ECMAFEATURES");
|
291 | }
|
292 | }
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | function validate(config, source, getAdditionalRule, getAdditionalEnv) {
|
303 | validateConfigSchema(config, source);
|
304 | validateRules(config.rules, source, getAdditionalRule);
|
305 | validateEnvironment(config.env, source, getAdditionalEnv);
|
306 | validateGlobals(config.globals, source);
|
307 |
|
308 | for (const override of config.overrides || []) {
|
309 | validateRules(override.rules, source, getAdditionalRule);
|
310 | validateEnvironment(override.env, source, getAdditionalEnv);
|
311 | validateGlobals(config.globals, source);
|
312 | }
|
313 | }
|
314 |
|
315 | const validated = new WeakSet();
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | function validateConfigArray(configArray) {
|
323 | const getPluginEnv = Map.prototype.get.bind(configArray.pluginEnvironments);
|
324 | const getPluginProcessor = Map.prototype.get.bind(configArray.pluginProcessors);
|
325 | const getPluginRule = Map.prototype.get.bind(configArray.pluginRules);
|
326 |
|
327 |
|
328 | for (const element of configArray) {
|
329 | if (validated.has(element)) {
|
330 | continue;
|
331 | }
|
332 | validated.add(element);
|
333 |
|
334 | validateEnvironment(element.env, element.name, getPluginEnv);
|
335 | validateGlobals(element.globals, element.name);
|
336 | validateProcessor(element.processor, element.name, getPluginProcessor);
|
337 | validateRules(element.rules, element.name, getPluginRule);
|
338 | }
|
339 | }
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | module.exports = {
|
346 | getRuleOptionsSchema,
|
347 | validate,
|
348 | validateConfigArray,
|
349 | validateConfigSchema,
|
350 | validateRuleOptions
|
351 | };
|