massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / config / rule-validator.js
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")();
13
14 //-----------------------------------------------------------------------------
15 // Helpers
16 //-----------------------------------------------------------------------------
17
18 /**
19  * Finds a rule with the given ID in the given config.
20  * @param {string} ruleId The ID of the rule to find.
21  * @param {Object} config The config to search in.
22  * @returns {{create: Function, schema: (Array|null)}} THe rule object.
23  */
24 function findRuleDefinition(ruleId, config) {
25     const ruleIdParts = ruleId.split("/");
26     let pluginName, ruleName;
27
28     // built-in rule
29     if (ruleIdParts.length === 1) {
30         pluginName = "@";
31         ruleName = ruleIdParts[0];
32     } else {
33         ruleName = ruleIdParts.pop();
34         pluginName = ruleIdParts.join("/");
35     }
36
37     if (!config.plugins || !config.plugins[pluginName]) {
38         throw new TypeError(`Key "rules": Key "${ruleId}": Could not find plugin "${pluginName}".`);
39     }
40
41     if (!config.plugins[pluginName].rules || !config.plugins[pluginName].rules[ruleName]) {
42         throw new TypeError(`Key "rules": Key "${ruleId}": Could not find "${ruleName}" in plugin "${pluginName}".`);
43     }
44
45     return config.plugins[pluginName].rules[ruleName];
46
47 }
48
49 /**
50  * Gets a complete options schema for a rule.
51  * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
52  * @returns {Object} JSON Schema for the rule's options.
53  */
54 function getRuleOptionsSchema(rule) {
55
56     if (!rule) {
57         return null;
58     }
59
60     const schema = rule.schema || rule.meta && rule.meta.schema;
61
62     if (Array.isArray(schema)) {
63         if (schema.length) {
64             return {
65                 type: "array",
66                 items: schema,
67                 minItems: 0,
68                 maxItems: schema.length
69             };
70         }
71         return {
72             type: "array",
73             minItems: 0,
74             maxItems: 0
75         };
76
77     }
78
79     // Given a full schema, leave it alone
80     return schema || null;
81 }
82
83 //-----------------------------------------------------------------------------
84 // Exports
85 //-----------------------------------------------------------------------------
86
87 /**
88  * Implements validation functionality for the rules portion of a config.
89  */
90 class RuleValidator {
91
92     /**
93      * Creates a new instance.
94      */
95     constructor() {
96
97         /**
98          * A collection of compiled validators for rules that have already
99          * been validated.
100          * @type {WeakMap}
101          * @property validators
102          */
103         this.validators = new WeakMap();
104     }
105
106     /**
107      * Validates all of the rule configurations in a config against each
108      * rule's schema.
109      * @param {Object} config The full config to validate. This object must
110      *      contain both the rules section and the plugins section.
111      * @returns {void}
112      * @throws {Error} If a rule's configuration does not match its schema.
113      */
114     validate(config) {
115
116         if (!config.rules) {
117             return;
118         }
119
120         for (const [ruleId, ruleOptions] of Object.entries(config.rules)) {
121
122             // check for edge case
123             if (ruleId === "__proto__") {
124                 continue;
125             }
126
127             /*
128              * If a rule is disabled, we don't do any validation. This allows
129              * users to safely set any value to 0 or "off" without worrying
130              * that it will cause a validation error.
131              *
132              * Note: ruleOptions is always an array at this point because
133              * this validation occurs after FlatConfigArray has merged and
134              * normalized values.
135              */
136             if (ruleOptions[0] === 0) {
137                 continue;
138             }
139
140             const rule = findRuleDefinition(ruleId, config);
141
142             // Precompile and cache validator the first time
143             if (!this.validators.has(rule)) {
144                 const schema = getRuleOptionsSchema(rule);
145
146                 if (schema) {
147                     this.validators.set(rule, ajv.compile(schema));
148                 }
149             }
150
151             const validateRule = this.validators.get(rule);
152
153             if (validateRule) {
154
155                 validateRule(ruleOptions.slice(1));
156
157                 if (validateRule.errors) {
158                     throw new Error(`Key "rules": Key "${ruleId}": ${
159                         validateRule.errors.map(
160                             error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
161                         ).join("")
162                     }`);
163                 }
164             }
165         }
166     }
167 }
168
169 exports.RuleValidator = RuleValidator;