Skip to content

Commit 2ce8105

Browse files
authored
2.0.2 (#24)
* Hotfix for relative path shared config objects * Fix issue where an exception was thrown if the npmpackagejsonlintrc only contained an extends element
1 parent f2e36db commit 2ce8105

File tree

5 files changed

+95
-23
lines changed

5 files changed

+95
-23
lines changed

CHANGELOG.md

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

1212
### Removed
1313

14+
## [2.0.2] - 2017-02-18
15+
### Fixed
16+
- Issue .npmpackagejsonlintrc.json files that only have a extends value with no rules object
17+
1418
## [2.0.1] - 2017-02-18
1519
### Fixed
1620
- Issue with relative path local config extension modules. Now relative path modules are load relative to the current working directory of the active process.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",

src/Config.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class Config {
3737
extendsConfig = this._getExtendsConfig(passedConfig.extends);
3838
}
3939

40+
if (!passedConfig.hasOwnProperty('rules')) {
41+
return Object.assign({}, extendsConfig);
42+
}
43+
4044
return Object.assign({}, extendsConfig, passedConfig.rules);
4145
} else {
4246
throw new Error('No configuration passed');
@@ -84,7 +88,15 @@ class Config {
8488
* @return {Object} Configuration object
8589
*/
8690
_getExtendsConfig(moduleName) {
87-
const configObj = this._getExtendsConfigModule(moduleName);
91+
let adjustedModuleName;
92+
93+
if (moduleName.startsWith('./')) {
94+
adjustedModuleName = path.join(process.cwd(), moduleName);
95+
} else {
96+
adjustedModuleName = moduleName;
97+
}
98+
99+
const configObj = this._getExtendsConfigModule(adjustedModuleName);
88100

89101
this._validateConfig(configObj);
90102

@@ -98,7 +110,7 @@ class Config {
98110
*/
99111
_getExtendsConfigModule(moduleName) {
100112
/* istanbul ignore next */
101-
return require(path.join(process.cwd(), moduleName));
113+
return require(moduleName);
102114
}
103115

104116
/**
@@ -109,8 +121,6 @@ class Config {
109121
_validateConfig(rcFileObj) {
110122
if (rcFileObj.hasOwnProperty('rules')) {
111123
this._validateRulesConfig(rcFileObj.rules);
112-
} else {
113-
throw new Error('`rules` object missing in config');
114124
}
115125

116126
return true;

tests/unit/ConfigTest.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,58 @@ describe('Config Unit Tests', function() {
130130

131131
config._getExtendsConfigModule.restore();
132132
});
133+
134+
it('and the module name is relative path', function() {
135+
const moduleName = './index.js';
136+
const passedConfig = {
137+
extends: './index.js',
138+
rules: {
139+
'version-type': 'warning'
140+
}
141+
};
142+
const config = new Config(passedConfig);
143+
const extendsObj = {
144+
rules: {
145+
'version-type': 'warning'
146+
}
147+
};
148+
const stubbedGetExtendCfgModule = sinon.stub(config, '_getExtendsConfigModule').returns(extendsObj);
149+
const result = config.get(moduleName);
150+
const expectedObj = {
151+
'version-type': 'warning'
152+
};
153+
154+
spy.calledOnce.should.be.true();
155+
spy.firstCall.calledWithExactly(process.cwd(), moduleName);
156+
stubbedGetExtendCfgModule.calledWithExactly(path.join(process.cwd(), moduleName));
157+
result.should.eql(expectedObj);
158+
159+
config._getExtendsConfigModule.restore();
160+
});
161+
162+
it('and the module name is a node module', function() {
163+
const moduleName = 'npm-package-json-lint-config-tc';
164+
const passedConfig = {
165+
extends: 'npm-package-json-lint-config-tc'
166+
};
167+
const config = new Config(passedConfig);
168+
const extendsObj = {
169+
rules: {
170+
'version-type': 'warning'
171+
}
172+
};
173+
const stubbedGetExtendCfgModule = sinon.stub(config, '_getExtendsConfigModule').returns(extendsObj);
174+
const result = config.get();
175+
const expectedObj = {
176+
'version-type': 'warning'
177+
};
178+
179+
spy.called.should.be.false();
180+
stubbedGetExtendCfgModule.calledWithExactly(moduleName);
181+
result.should.eql(expectedObj);
182+
183+
config._getExtendsConfigModule.restore();
184+
});
133185
});
134186
});
135187

@@ -157,24 +209,6 @@ describe('Config Unit Tests', function() {
157209
});
158210
});
159211

160-
context('_validateConfig tests', function() {
161-
context('when a invalid config object is passed', function() {
162-
it('an error should be thrown', function() {
163-
const rcFileObj = {
164-
'require-author': 'error',
165-
'require-version': 'warning',
166-
'valid-values-author': ['error', ['Thomas', 'Lindner', 'Thomas Lindner']],
167-
'valid-values-private': ['warning', [true, false]]
168-
};
169-
const config = new Config(rcFileObj);
170-
171-
(function() {
172-
config._validateConfig(rcFileObj);
173-
}).should.throw('`rules` object missing in config');
174-
});
175-
});
176-
});
177-
178212
context('_validateRulesConfig tests', function() {
179213
context('when a valid npmpackagejsonlintrc file object is passed', function() {
180214
it('true should be returned', function() {

tests/unit/NpmPackageJsonLintTest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,30 @@ describe('NpmPackageJsonLint Unit Tests', function() {
125125
response.hasOwnProperty('warnings').should.be.false();
126126
});
127127
});
128+
129+
context('validate that errors and warnings are set', function() {
130+
it('one error and one warning expected', function() {
131+
const packageJsonData = {
132+
author: 'Caitlin Snow'
133+
};
134+
const config = {
135+
'valid-values-author': ['off', [
136+
'Barry Allen',
137+
'Iris West'
138+
]]
139+
};
140+
const options = {
141+
ignoreWarnings: true
142+
};
143+
const npmPackageJsonLint = new NpmPackageJsonLint(packageJsonData, config, options);
144+
const configStub = sinon.stub(npmPackageJsonLint, '_getConfig').returns(config);
145+
const response = npmPackageJsonLint.lint();
146+
const expectedErrorCount = 0;
147+
148+
response.errors.length.should.equal(expectedErrorCount);
149+
response.hasOwnProperty('warnings').should.be.false();
150+
});
151+
});
128152
});
129153

130154
describe('_getConfig method', function() {

0 commit comments

Comments
 (0)