Skip to content

Commit be7ea2e

Browse files
authored
Refactor rule loader for rule types (#59)
* Bump version and increase branch coverage requirements * Update standard rules to have a ruleType of standard * Update array rules to have a ruleType of array * Update keywords rule to have a ruleType of standard. Also fix unit tests so it imports the current source file! * Closes #56 - Update config validation to static rules Update npm package json lint to call validate when rule is being processed * Update changeling with details of 2.9.0 release * Remove unused var and add missing semicolon * Add max-statements override for ESLint * Remove destructuring since node 4 and 5 don't support it
1 parent 1b0a91f commit be7ea2e

File tree

157 files changed

+851
-268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+851
-268
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1212
### Removed
1313

1414

15+
## [2.9.0] - 2017-08-29
16+
### Changed
17+
- Update all rules to export the type of rule they are. Current valid values are "standard" and "array". The rules loader has been updated to references the ruleType export rather than trying to maintain a separate list of array style rules. This change closes [issue #56](https://github.com/tclindner/npm-package-json-lint/issues/56) and should prevent the issue discussed in [issue #53](https://github.com/tclindner/npm-package-json-lint/issues/53) from occurring again.
18+
1519
## [2.8.2] - 2017-08-23
1620
### Fixed
1721
- Rule loader so it recognized [prefer-property-order](https://github.com/tclindner/npm-package-json-lint/wiki/prefer-property-order) as an array type rule.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "2.8.2",
3+
"version": "2.9.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",
@@ -31,7 +31,7 @@
3131
"eslint": "eslint . --format=node_modules/eslint-formatter-pretty",
3232
"lint": "npm run eslint",
3333
"test": "mocha tests/unit --recursive",
34-
"coverage": "nyc --extension .js --check-coverage --lines 99 --branches 97 --functions 96 npm test"
34+
"coverage": "nyc --extension .js --check-coverage --lines 99 --branches 98 --functions 96 npm test"
3535
},
3636
"dependencies": {
3737
"chalk": "^2.1.0",

src/Config.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/* eslint class-methods-use-this: 'off' */
44

55
const fs = require('fs');
6-
const inArray = require('in-array');
76
const Parser = require('./Parser');
87
const path = require('path');
98
const userHome = require('user-home');
@@ -40,8 +39,6 @@ class Config {
4039
get() {
4140
const userConfig = this._getUserConfig();
4241

43-
this._validateConfig(userConfig);
44-
4542
let extendsConfig = {};
4643

4744
if (userConfig.hasOwnProperty('extends')) {
@@ -125,8 +122,6 @@ class Config {
125122

126123
const configObj = this._getExtendsConfigModule(adjustedModuleName);
127124

128-
this._validateConfig(configObj);
129-
130125
return configObj.rules;
131126
}
132127

@@ -192,50 +187,51 @@ class Config {
192187
}
193188

194189
/**
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
198195
*/
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]}".`);
202207
}
203208

204209
return true;
205210
}
206211

207212
/**
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
211218
*/
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}".`);
227222
}
228223

229224
return true;
230225
}
231226

232227
/**
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
236232
*/
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');
239235
}
240236

241237
}

src/NpmPackageJsonLint.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

3-
/* eslint class-methods-use-this: 'off' */
3+
/* eslint class-methods-use-this: 'off', max-statements: 'off' */
44

55
const Config = require('./Config');
6-
const inArray = require('in-array');
76
const Rules = require('./Rules');
87

98
class NpmPackageJsonLint {
@@ -17,12 +16,6 @@ class NpmPackageJsonLint {
1716
constructor(packageJsonData, config, options) {
1817
this.packageJsonData = packageJsonData;
1918
this.ignoreWarnings = options.ignoreWarnings;
20-
this.arrayRuleTypes = [
21-
'valid-values',
22-
'no-restricted-dependencies',
23-
'no-restricted-pre-release-dependencies',
24-
'property-order'
25-
];
2619
this.errors = [];
2720
this.warnings = [];
2821

@@ -41,7 +34,8 @@ class NpmPackageJsonLint {
4134
for (const rule in configObj) {
4235
const ruleModule = this.rules.get(rule);
4336

44-
if (inArray(this.arrayRuleTypes, ruleModule.ruleType)) {
37+
if (ruleModule.ruleType === 'array') {
38+
Config.isArrayRuleConfigValid(rule, configObj[rule]);
4539
const errorWarningOffSetting = typeof configObj[rule] === 'string' && configObj[rule] === 'off' ? configObj[rule] : configObj[rule][0];
4640
const ruleConfigArray = configObj[rule][1];
4741

@@ -50,6 +44,7 @@ class NpmPackageJsonLint {
5044
}
5145

5246
} else {
47+
Config.isStandardRuleConfigValid(rule, configObj[rule]);
5348
const errorWarningOffSetting = configObj[rule];
5449

5550
if (errorWarningOffSetting !== 'off') {

src/rules/bin-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const LintIssue = require('./../LintIssue');
66
const lintId = 'bin-type';
77
const nodeName = 'bin';
88
const message = 'Type should be either a string or an Object';
9-
const ruleType = 'type';
9+
const ruleType = 'standard';
1010

1111
const lint = function(packageJsonData, lintType) {
1212
if (!isString(packageJsonData, nodeName) && !isObject(packageJsonData, nodeName)) {

src/rules/bundledDependencies-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const LintIssue = require('./../LintIssue');
55
const lintId = 'bundledDependencies-type';
66
const nodeName = 'bundledDependencies';
77
const message = 'Type should be an Object';
8-
const ruleType = 'type';
8+
const ruleType = 'standard';
99

1010
const lint = function(packageJsonData, lintType) {
1111
if (!isObject(packageJsonData, nodeName)) {

src/rules/config-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const LintIssue = require('./../LintIssue');
55
const lintId = 'config-type';
66
const nodeName = 'config';
77
const message = 'Type should be an Object';
8-
const ruleType = 'type';
8+
const ruleType = 'standard';
99

1010
const lint = function(packageJsonData, lintType) {
1111
if (!isObject(packageJsonData, nodeName)) {

src/rules/cpu-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const LintIssue = require('./../LintIssue');
55
const lintId = 'os-type';
66
const nodeName = 'os';
77
const message = 'Type should be an array';
8-
const ruleType = 'type';
8+
const ruleType = 'standard';
99

1010
const lint = function(packageJsonData, lintType) {
1111
if (!isArray(packageJsonData, nodeName)) {

src/rules/dependencies-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const LintIssue = require('./../LintIssue');
55
const lintId = 'dependencies-type';
66
const nodeName = 'dependencies';
77
const message = 'Type should be an Object';
8-
const ruleType = 'type';
8+
const ruleType = 'standard';
99

1010
const lint = function(packageJsonData, lintType) {
1111
if (!isObject(packageJsonData, nodeName)) {

src/rules/description-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const isString = require('./../validators/type').isString;
55
const lintId = 'description-type';
66
const nodeName = 'description';
77
const message = 'Type should be a string';
8-
const ruleType = 'type';
8+
const ruleType = 'standard';
99

1010
const lint = function(packageJsonData, lintType) {
1111
if (!isString(packageJsonData, nodeName)) {

0 commit comments

Comments
 (0)