|
3 | 3 | /* eslint class-methods-use-this: 'off' */
|
4 | 4 |
|
5 | 5 | const fs = require('fs');
|
6 |
| -const inArray = require('in-array'); |
7 | 6 | const Parser = require('./Parser');
|
8 | 7 | const path = require('path');
|
9 | 8 | const userHome = require('user-home');
|
@@ -40,8 +39,6 @@ class Config {
|
40 | 39 | get() {
|
41 | 40 | const userConfig = this._getUserConfig();
|
42 | 41 |
|
43 |
| - this._validateConfig(userConfig); |
44 |
| - |
45 | 42 | let extendsConfig = {};
|
46 | 43 |
|
47 | 44 | if (userConfig.hasOwnProperty('extends')) {
|
@@ -125,8 +122,6 @@ class Config {
|
125 | 122 |
|
126 | 123 | const configObj = this._getExtendsConfigModule(adjustedModuleName);
|
127 | 124 |
|
128 |
| - this._validateConfig(configObj); |
129 |
| - |
130 | 125 | return configObj.rules;
|
131 | 126 | }
|
132 | 127 |
|
@@ -192,50 +187,51 @@ class Config {
|
192 | 187 | }
|
193 | 188 |
|
194 | 189 | /**
|
195 |
| - * Validates config object |
196 |
| - * @param {Object} rcFileObj Object version of .npmpackagejsonlintrc file |
197 |
| - * @return {boolean} True if validate config is successful |
| 190 | + * Validates array rule config |
| 191 | + * @param {String} ruleName Name of the rule |
| 192 | + * @param {Array} ruleConfig Array rule |
| 193 | + * @return {Boolean} True if config is valid, false if not |
| 194 | + * @static |
198 | 195 | */
|
199 |
| - _validateConfig(rcFileObj) { |
200 |
| - if (rcFileObj.hasOwnProperty('rules')) { |
201 |
| - this._validateRulesConfig(rcFileObj.rules); |
| 196 | + static isArrayRuleConfigValid(ruleName, ruleConfig) { |
| 197 | + if (typeof ruleConfig === 'string' && ruleConfig === 'off') { |
| 198 | + return true; |
| 199 | + } else if (typeof ruleConfig === 'string' && ruleConfig !== 'off') { |
| 200 | + throw new Error(`${ruleName} - is an array type rule. It must be set to "off" if an array is not supplied.`); |
| 201 | + } else if (typeof ruleConfig[0] !== 'string' || this._isSeverityInvalid(ruleConfig[0])) { |
| 202 | + throw new Error(`${ruleName} - first key must be set to "error", "warning", or "off". Currently set to "${ruleConfig[0]}".`); |
| 203 | + } |
| 204 | + |
| 205 | + if (!Array.isArray(ruleConfig[1])) { |
| 206 | + throw new Error(`${ruleName} - second key must be set an array. Currently set to "${ruleConfig[1]}".`); |
202 | 207 | }
|
203 | 208 |
|
204 | 209 | return true;
|
205 | 210 | }
|
206 | 211 |
|
207 | 212 | /**
|
208 |
| - * Validates rules object |
209 |
| - * @param {Object} rulesObj Object version of .npmpackagejsonlintrc file |
210 |
| - * @return {boolean} True if validate config is successful |
| 213 | + * Validates standard rule config |
| 214 | + * @param {String} ruleName Name of the rule |
| 215 | + * @param {Object} ruleConfig Value for standard rule config |
| 216 | + * @return {Boolean} True if config is valid, false if not |
| 217 | + * @static |
211 | 218 | */
|
212 |
| - _validateRulesConfig(rulesObj) { |
213 |
| - for (const rule in rulesObj) { |
214 |
| - const ruleConfig = rulesObj[rule]; |
215 |
| - |
216 |
| - if (Array.isArray(ruleConfig) && inArray(this.arrayRules, rule)) { |
217 |
| - if (typeof ruleConfig[0] !== 'string' || this._isRuleValid(ruleConfig[0])) { |
218 |
| - throw new Error(`${rule} - first key must be set to "error", "warning", or "off". Currently set to ${ruleConfig[0]}`); |
219 |
| - } |
220 |
| - |
221 |
| - if (!Array.isArray(ruleConfig[1])) { |
222 |
| - throw new Error(`${rule} - second key must be set an array. Currently set to ${ruleConfig[1]}`); |
223 |
| - } |
224 |
| - } else if (typeof ruleConfig !== 'string' || this._isRuleValid(ruleConfig)) { |
225 |
| - throw new Error(`${rule} - must be set to "error", "warning", or "off". Currently set to ${ruleConfig}`); |
226 |
| - } |
| 219 | + static isStandardRuleConfigValid(ruleName, ruleConfig) { |
| 220 | + if (this._isSeverityInvalid(ruleConfig)) { |
| 221 | + throw new Error(`${ruleName} - must be set to "error", "warning", or "off". Currently set to "${ruleConfig}".`); |
227 | 222 | }
|
228 | 223 |
|
229 | 224 | return true;
|
230 | 225 | }
|
231 | 226 |
|
232 | 227 | /**
|
233 |
| - * Validates the first key of an array type rule |
234 |
| - * @param {String} key Error type of the rule |
235 |
| - * @return {Boolean} True if the rule is valid. False if the rule is invalid. |
| 228 | + * Validates if the severity config is set correctly |
| 229 | + * @param {String} severity Severity the rule is set to |
| 230 | + * @return {Boolean} True if the severity is valid. False if the severity is invalid. |
| 231 | + * @static |
236 | 232 | */
|
237 |
| - _isRuleValid(key) { |
238 |
| - return typeof key === 'string' && key !== 'error' && key !== 'warning' && key !== 'off'; |
| 233 | + static _isSeverityInvalid(severity) { |
| 234 | + return typeof severity !== 'string' || (typeof severity === 'string' && severity !== 'error' && severity !== 'warning' && severity !== 'off'); |
239 | 235 | }
|
240 | 236 |
|
241 | 237 | }
|
|
0 commit comments