]>
Commit | Line | Data |
---|---|---|
609c276f TL |
1 | /** |
2 | * @fileoverview Rule Validator | |
3 | * @author Nicholas C. Zakas | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //----------------------------------------------------------------------------- | |
9 | // Requirements | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | const ajv = require("../shared/ajv")(); | |
8f9d1d4d DC |
13 | const { |
14 | parseRuleId, | |
15 | getRuleFromConfig, | |
16 | getRuleOptionsSchema | |
17 | } = require("./flat-config-helpers"); | |
34eeec05 | 18 | const ruleReplacements = require("../../conf/replacements.json"); |
609c276f TL |
19 | |
20 | //----------------------------------------------------------------------------- | |
21 | // Helpers | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | /** | |
34eeec05 TL |
25 | * Throws a helpful error when a rule cannot be found. |
26 | * @param {Object} ruleId The rule identifier. | |
27 | * @param {string} ruleId.pluginName The ID of the rule to find. | |
28 | * @param {string} ruleId.ruleName The ID of the rule to find. | |
609c276f TL |
29 | * @param {Object} config The config to search in. |
30 | * @throws {TypeError} For missing plugin or rule. | |
34eeec05 | 31 | * @returns {void} |
609c276f | 32 | */ |
34eeec05 TL |
33 | function throwRuleNotFoundError({ pluginName, ruleName }, config) { |
34 | ||
35 | const ruleId = pluginName === "@" ? ruleName : `${pluginName}/${ruleName}`; | |
609c276f TL |
36 | |
37 | const errorMessageHeader = `Key "rules": Key "${ruleId}"`; | |
38 | let errorMessage = `${errorMessageHeader}: Could not find plugin "${pluginName}".`; | |
39 | ||
40 | // if the plugin exists then we need to check if the rule exists | |
41 | if (config.plugins && config.plugins[pluginName]) { | |
34eeec05 | 42 | const replacementRuleName = ruleReplacements.rules[ruleName]; |
609c276f | 43 | |
34eeec05 | 44 | if (pluginName === "@" && replacementRuleName) { |
609c276f | 45 | |
34eeec05 TL |
46 | errorMessage = `${errorMessageHeader}: Rule "${ruleName}" was removed and replaced by "${replacementRuleName}".`; |
47 | ||
48 | } else { | |
609c276f | 49 | |
34eeec05 | 50 | errorMessage = `${errorMessageHeader}: Could not find "${ruleName}" in plugin "${pluginName}".`; |
609c276f | 51 | |
34eeec05 TL |
52 | // otherwise, let's see if we can find the rule name elsewhere |
53 | for (const [otherPluginName, otherPlugin] of Object.entries(config.plugins)) { | |
54 | if (otherPlugin.rules && otherPlugin.rules[ruleName]) { | |
55 | errorMessage += ` Did you mean "${otherPluginName}/${ruleName}"?`; | |
56 | break; | |
57 | } | |
609c276f | 58 | } |
34eeec05 | 59 | |
609c276f TL |
60 | } |
61 | ||
62 | // falls through to throw error | |
63 | } | |
64 | ||
65 | throw new TypeError(errorMessage); | |
66 | } | |
67 | ||
609c276f TL |
68 | //----------------------------------------------------------------------------- |
69 | // Exports | |
70 | //----------------------------------------------------------------------------- | |
71 | ||
72 | /** | |
73 | * Implements validation functionality for the rules portion of a config. | |
74 | */ | |
75 | class RuleValidator { | |
76 | ||
77 | /** | |
78 | * Creates a new instance. | |
79 | */ | |
80 | constructor() { | |
81 | ||
82 | /** | |
83 | * A collection of compiled validators for rules that have already | |
84 | * been validated. | |
85 | * @type {WeakMap} | |
86 | */ | |
87 | this.validators = new WeakMap(); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Validates all of the rule configurations in a config against each | |
92 | * rule's schema. | |
93 | * @param {Object} config The full config to validate. This object must | |
94 | * contain both the rules section and the plugins section. | |
95 | * @returns {void} | |
96 | * @throws {Error} If a rule's configuration does not match its schema. | |
97 | */ | |
98 | validate(config) { | |
99 | ||
100 | if (!config.rules) { | |
101 | return; | |
102 | } | |
103 | ||
104 | for (const [ruleId, ruleOptions] of Object.entries(config.rules)) { | |
105 | ||
106 | // check for edge case | |
107 | if (ruleId === "__proto__") { | |
108 | continue; | |
109 | } | |
110 | ||
111 | /* | |
112 | * If a rule is disabled, we don't do any validation. This allows | |
113 | * users to safely set any value to 0 or "off" without worrying | |
114 | * that it will cause a validation error. | |
115 | * | |
116 | * Note: ruleOptions is always an array at this point because | |
117 | * this validation occurs after FlatConfigArray has merged and | |
118 | * normalized values. | |
119 | */ | |
120 | if (ruleOptions[0] === 0) { | |
121 | continue; | |
122 | } | |
123 | ||
34eeec05 TL |
124 | const rule = getRuleFromConfig(ruleId, config); |
125 | ||
126 | if (!rule) { | |
127 | throwRuleNotFoundError(parseRuleId(ruleId), config); | |
128 | } | |
609c276f TL |
129 | |
130 | // Precompile and cache validator the first time | |
131 | if (!this.validators.has(rule)) { | |
132 | const schema = getRuleOptionsSchema(rule); | |
133 | ||
134 | if (schema) { | |
135 | this.validators.set(rule, ajv.compile(schema)); | |
136 | } | |
137 | } | |
138 | ||
139 | const validateRule = this.validators.get(rule); | |
140 | ||
141 | if (validateRule) { | |
142 | ||
143 | validateRule(ruleOptions.slice(1)); | |
144 | ||
145 | if (validateRule.errors) { | |
146 | throw new Error(`Key "rules": Key "${ruleId}": ${ | |
147 | validateRule.errors.map( | |
148 | error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n` | |
149 | ).join("") | |
150 | }`); | |
151 | } | |
152 | } | |
153 | } | |
154 | } | |
155 | } | |
156 | ||
157 | exports.RuleValidator = RuleValidator; |