UNPKG

8.77 kBJavaScriptView Raw
1/**
2 * @fileoverview Validates configs.
3 * @author Brandon Mills
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const ajv = require("../util/ajv"),
13 lodash = require("lodash"),
14 configSchema = require("../../conf/config-schema.js"),
15 util = require("util");
16
17const ruleValidators = new WeakMap();
18
19//------------------------------------------------------------------------------
20// Private
21//------------------------------------------------------------------------------
22let validateSchema;
23
24/**
25 * Gets a complete options schema for a rule.
26 * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
27 * @returns {Object} JSON Schema for the rule's options.
28 */
29function getRuleOptionsSchema(rule) {
30 const schema = rule.schema || rule.meta && rule.meta.schema;
31
32 // Given a tuple of schemas, insert warning level at the beginning
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 // Given a full schema, leave it alone
51 return schema || null;
52}
53
54/**
55 * Validates a rule's severity and returns the severity value. Throws an error if the severity is invalid.
56 * @param {options} options The given options for the rule.
57 * @returns {number|string} The rule's severity value
58 */
59function 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 * Validates the non-severity options passed to a rule, based on its schema.
71 * @param {{create: Function}} rule The rule to validate
72 * @param {array} localOptions The options for the rule, excluding severity
73 * @returns {void}
74 */
75function 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 * Validates a rule's options against its schema.
98 * @param {{create: Function}|null} rule The rule that the config is being validated for
99 * @param {string} ruleId The rule's unique name.
100 * @param {array|number} options The given options for the rule.
101 * @param {string|null} source The name of the configuration source to report in any errors. If null or undefined,
102 * no source is prepended to the message.
103 * @returns {void}
104 */
105function 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 * Validates an environment object
128 * @param {Object} environment The environment config object to validate.
129 * @param {string} source The name of the configuration source to report in any errors.
130 * @param {Environments} envContext Env context
131 * @returns {void}
132 */
133function validateEnvironment(environment, source, envContext) {
134
135 // not having an environment is ok
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 * Validates a rules config object
151 * @param {Object} rulesConfig The rules config object to validate.
152 * @param {string} source The name of the configuration source to report in any errors.
153 * @param {function(string): {create: Function}} ruleMapper A mapper function from strings to loaded rules
154 * @returns {void}
155 */
156function 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 * Formats an array of schema validation errors.
168 * @param {Array} errors An array of error messages to format.
169 * @returns {string} Formatted error message
170 */
171function 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 * Emits a deprecation warning containing a given filepath. A new deprecation warning is emitted
194 * for each unique file path, but repeated invocations with the same file path have no effect.
195 * No warnings are emitted if the `--no-deprecation` or `--no-warnings` Node runtime flags are active.
196 * @param {string} source The name of the configuration source to report the warning for.
197 * @returns {void}
198 */
199const emitEcmaFeaturesWarning = lodash.memoize(source => {
200
201 /*
202 * util.deprecate seems to be the only way to emit a warning in Node 4.x while respecting the --no-warnings flag.
203 * (In Node 6+, process.emitWarning could be used instead.)
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 * Validates the top level properties of the config object.
213 * @param {Object} config The config object to validate.
214 * @param {string} source The name of the configuration source to report in any errors.
215 * @returns {void}
216 */
217function 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 * Validates an entire config object.
231 * @param {Object} config The config object to validate.
232 * @param {string} source The name of the configuration source to report in any errors.
233 * @param {function(string): {create: Function}} ruleMapper A mapper function from rule IDs to defined rules
234 * @param {Environments} envContext The env context
235 * @returns {void}
236 */
237function 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// Public Interface
245//------------------------------------------------------------------------------
246
247module.exports = {
248 getRuleOptionsSchema,
249 validate,
250 validateRuleOptions
251};