1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const path = require("path"),
|
13 | ajv = require("../util/ajv"),
|
14 | lodash = require("lodash"),
|
15 | configSchema = require("../../conf/config-schema.js"),
|
16 | util = require("util");
|
17 |
|
18 | const ruleValidators = new WeakMap();
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | let validateSchema;
|
24 |
|
25 |
|
26 | const deprecationWarningMessages = {
|
27 | ESLINT_LEGACY_ECMAFEATURES: "The 'ecmaFeatures' config file property is deprecated, and has no effect.",
|
28 | ESLINT_LEGACY_OBJECT_REST_SPREAD: "The 'parserOptions.ecmaFeatures.experimentalObjectRestSpread' option is deprecated. Use 'parserOptions.ecmaVersion' instead."
|
29 | };
|
30 | const severityMap = {
|
31 | error: 2,
|
32 | warn: 1,
|
33 | off: 0
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function getRuleOptionsSchema(rule) {
|
42 | const schema = rule.schema || rule.meta && rule.meta.schema;
|
43 |
|
44 |
|
45 | if (Array.isArray(schema)) {
|
46 | if (schema.length) {
|
47 | return {
|
48 | type: "array",
|
49 | items: schema,
|
50 | minItems: 0,
|
51 | maxItems: schema.length
|
52 | };
|
53 | }
|
54 | return {
|
55 | type: "array",
|
56 | minItems: 0,
|
57 | maxItems: 0
|
58 | };
|
59 |
|
60 | }
|
61 |
|
62 |
|
63 | return schema || null;
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function validateRuleSeverity(options) {
|
72 | const severity = Array.isArray(options) ? options[0] : options;
|
73 | const normSeverity = typeof severity === "string" ? severityMap[severity.toLowerCase()] : severity;
|
74 |
|
75 | if (normSeverity === 0 || normSeverity === 1 || normSeverity === 2) {
|
76 | return normSeverity;
|
77 | }
|
78 |
|
79 | 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`);
|
80 |
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function validateRuleSchema(rule, localOptions) {
|
90 | if (!ruleValidators.has(rule)) {
|
91 | const schema = getRuleOptionsSchema(rule);
|
92 |
|
93 | if (schema) {
|
94 | ruleValidators.set(rule, ajv.compile(schema));
|
95 | }
|
96 | }
|
97 |
|
98 | const validateRule = ruleValidators.get(rule);
|
99 |
|
100 | if (validateRule) {
|
101 | validateRule(localOptions);
|
102 | if (validateRule.errors) {
|
103 | throw new Error(validateRule.errors.map(
|
104 | error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
|
105 | ).join(""));
|
106 | }
|
107 | }
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function validateRuleOptions(rule, ruleId, options, source = null) {
|
120 | if (!rule) {
|
121 | return;
|
122 | }
|
123 | try {
|
124 | const severity = validateRuleSeverity(options);
|
125 |
|
126 | if (severity !== 0) {
|
127 | validateRuleSchema(rule, Array.isArray(options) ? options.slice(1) : []);
|
128 | }
|
129 | } catch (err) {
|
130 | const enhancedMessage = `Configuration for rule "${ruleId}" is invalid:\n${err.message}`;
|
131 |
|
132 | if (typeof source === "string") {
|
133 | throw new Error(`${source}:\n\t${enhancedMessage}`);
|
134 | } else {
|
135 | throw new Error(enhancedMessage);
|
136 | }
|
137 | }
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | function validateEnvironment(environment, envContext, source = null) {
|
148 |
|
149 |
|
150 | if (!environment) {
|
151 | return;
|
152 | }
|
153 |
|
154 | Object.keys(environment).forEach(env => {
|
155 | if (!envContext.get(env)) {
|
156 | const message = `${source}:\n\tEnvironment key "${env}" is unknown\n`;
|
157 |
|
158 | throw new Error(message);
|
159 | }
|
160 | });
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function validateRules(rulesConfig, ruleMapper, source = null) {
|
171 | if (!rulesConfig) {
|
172 | return;
|
173 | }
|
174 |
|
175 | Object.keys(rulesConfig).forEach(id => {
|
176 | validateRuleOptions(ruleMapper(id), id, rulesConfig[id], source);
|
177 | });
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | function formatErrors(errors) {
|
186 | return errors.map(error => {
|
187 | if (error.keyword === "additionalProperties") {
|
188 | const formattedPropertyPath = error.dataPath.length ? `${error.dataPath.slice(1)}.${error.params.additionalProperty}` : error.params.additionalProperty;
|
189 |
|
190 | return `Unexpected top-level property "${formattedPropertyPath}"`;
|
191 | }
|
192 | if (error.keyword === "type") {
|
193 | const formattedField = error.dataPath.slice(1);
|
194 | const formattedExpectedType = Array.isArray(error.schema) ? error.schema.join("/") : error.schema;
|
195 | const formattedValue = JSON.stringify(error.data);
|
196 |
|
197 | return `Property "${formattedField}" is the wrong type (expected ${formattedExpectedType} but got \`${formattedValue}\`)`;
|
198 | }
|
199 |
|
200 | const field = error.dataPath[0] === "." ? error.dataPath.slice(1) : error.dataPath;
|
201 |
|
202 | return `"${field}" ${error.message}. Value: ${JSON.stringify(error.data)}`;
|
203 | }).map(message => `\t- ${message}.\n`).join("");
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | const emitDeprecationWarning = lodash.memoize((source, errorCode) => {
|
215 | const rel = path.relative(process.cwd(), source);
|
216 | const message = deprecationWarningMessages[errorCode];
|
217 |
|
218 | process.emitWarning(
|
219 | `${message} (found in "${rel}")`,
|
220 | "DeprecationWarning",
|
221 | errorCode
|
222 | );
|
223 | });
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | function validateConfigSchema(config, source = null) {
|
232 | validateSchema = validateSchema || ajv.compile(configSchema);
|
233 |
|
234 | if (!validateSchema(config)) {
|
235 | throw new Error(`ESLint configuration in ${source} is invalid:\n${formatErrors(validateSchema.errors)}`);
|
236 | }
|
237 |
|
238 | if (Object.hasOwnProperty.call(config, "ecmaFeatures")) {
|
239 | emitDeprecationWarning(source, "ESLINT_LEGACY_ECMAFEATURES");
|
240 | }
|
241 |
|
242 | if (
|
243 | (config.parser || "espree") === "espree" &&
|
244 | config.parserOptions &&
|
245 | config.parserOptions.ecmaFeatures &&
|
246 | config.parserOptions.ecmaFeatures.experimentalObjectRestSpread
|
247 | ) {
|
248 | emitDeprecationWarning(source, "ESLINT_LEGACY_OBJECT_REST_SPREAD");
|
249 | }
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function validate(config, ruleMapper, envContext, source = null) {
|
261 | validateConfigSchema(config, source);
|
262 | validateRules(config.rules, ruleMapper, source);
|
263 | validateEnvironment(config.env, envContext, source);
|
264 |
|
265 | for (const override of config.overrides || []) {
|
266 | validateRules(override.rules, ruleMapper, source);
|
267 | validateEnvironment(override.env, envContext, source);
|
268 | }
|
269 | }
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | module.exports = {
|
276 | getRuleOptionsSchema,
|
277 | validate,
|
278 | validateRuleOptions
|
279 | };
|