source: imaps-frontend/node_modules/eslint/lib/config/rule-validator.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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