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