Skip to content

Commit 10348dd

Browse files
authored
Merge pull request #84 from tclindner/fix-79
Fix check for root node in the event it does not exist
2 parents 6e1c80d + 7920c02 commit 10348dd

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

CHANGELOG.md

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

1212
### Removed
1313

14+
15+
## [3.0.1] - 2018-05-14
16+
### Fixed
17+
- Addressed issue, from @ntwb, [#79](https://github.com/tclindner/npm-package-json-lint/issues/79).
18+
19+
1420
## [3.0.0] - 2018-05-09
1521
### Added
1622
- Added support for glob based package.json file detection. Addresses [#74](https://github.com/tclindner/npm-package-json-lint/issues/74) from @dnepro.

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": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Configurable linter for package.json files.",
55
"keywords": [
66
"lint",

src/Config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ class Config {
128128
config = ConfigFile.load(javaScriptConfigFilePath, this);
129129
}
130130

131-
if (config.hasOwnProperty('root') && !config.root) {
132-
const parentDir = path.resolve(directory, '../');
133-
const parentConfig = this.getProjectHierarchyConfig(parentDir);
131+
if (!config.hasOwnProperty('root') || !config.root) {
132+
const parentPackageJsonFile = path.resolve(directory, '../', 'package.json');
133+
const parentConfig = this.getProjectHierarchyConfig(parentPackageJsonFile);
134134

135135
// Merge base object
136136
const mergedConfig = Object.assign({}, parentConfig, config);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "npm-package-json-lint",
3+
"version": "0.1.0",
4+
"description": "CLI app for linting package.json files.",
5+
"keywords": [
6+
"lint"
7+
],
8+
"homepage": "https://github.com/tclindner/npm-package-json-lint",
9+
"author": "Thomas Lindner",
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/tclindner/npm-package-json-lint"
13+
},
14+
"devDependencies": {
15+
"mocha": "^2.4.5"
16+
},
17+
"npmPackageJsonLintConfig": {
18+
"root": true,
19+
"rules": {
20+
"require-author": "off",
21+
"version-format": "error"
22+
}
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "npm-package-json-lint",
3+
"version": "0.1.0",
4+
"description": "CLI app for linting package.json files.",
5+
"keywords": [
6+
"lint"
7+
],
8+
"homepage": "https://github.com/tclindner/npm-package-json-lint",
9+
"author": "Thomas Lindner",
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/tclindner/npm-package-json-lint"
13+
},
14+
"devDependencies": {
15+
"mocha": "^2.4.5"
16+
},
17+
"npmPackageJsonLintConfig": {
18+
"rules": {
19+
"require-author": "error"
20+
}
21+
}
22+
}

tests/unit/Config.test.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,28 @@ describe('Config Unit Tests', function() {
752752
ConfigFile.load.restore();
753753
});
754754

755+
it('and package.json prop exists and root is not set, the config object should returned', function() {
756+
const options = {
757+
configFile: '',
758+
cwd: process.cwd(),
759+
useConfigFiles: false,
760+
rules: {}
761+
};
762+
const config = new Config(options, linterContext);
763+
764+
const expectedConfigObj = {
765+
root: true,
766+
rules: {
767+
'require-author': 'error',
768+
'version-format': 'error'
769+
}
770+
};
771+
const filePath = './tests/fixtures/hierarchyWithoutRoot/subdirectory/package.json';
772+
const result = config.getProjectHierarchyConfig(filePath);
773+
774+
result.should.deep.equal(expectedConfigObj);
775+
});
776+
755777
it('and package.json prop exists and has no prop, the config object should returned', function() {
756778
const options = {
757779
configFile: '',
@@ -775,9 +797,10 @@ describe('Config Unit Tests', function() {
775797
}
776798
});
777799
stubLoadPkgJson.returns({root: true, rules: {}});
778-
stubLoad.returns({rules: {'require-name': 'error'}});
800+
stubLoad.returns({root: true, rules: {'require-name': 'error'}});
779801

780802
const expectedConfigObj = {
803+
root: true,
781804
rules: {
782805
'require-name': 'error'
783806
}
@@ -1009,7 +1032,8 @@ describe('Config Unit Tests', function() {
10091032
const stubLoadPkgJson = sinon.stub(ConfigFile, 'loadFromPackageJson');
10101033
const stubLoad = sinon.stub(ConfigFile, 'load');
10111034

1012-
stubDirname.returns('./npm-package-json-lint/');
1035+
stubDirname.onCall(0).returns('./npm-package-json-lint/');
1036+
stubDirname.onCall(1).returns('/home');
10131037
stubExists.returns(false);
10141038

10151039
const expectedConfigObj = {
@@ -1018,7 +1042,7 @@ describe('Config Unit Tests', function() {
10181042
const filePath = './package.json';
10191043
const result = config.getProjectHierarchyConfig(filePath);
10201044

1021-
stubDirname.calledOnce.should.be.true;
1045+
stubDirname.calledTwice.should.be.true;
10221046
stubDirname.firstCall.calledWithExactly(filePath).should.be.true;
10231047

10241048
stubExists.calledThrice.should.be.true;
@@ -1056,7 +1080,8 @@ describe('Config Unit Tests', function() {
10561080
const stubLoadPkgJson = sinon.stub(ConfigFile, 'loadFromPackageJson');
10571081
const stubLoad = sinon.stub(ConfigFile, 'load');
10581082

1059-
stubDirname.returns('./npm-package-json-lint/');
1083+
stubDirname.onCall(0).returns('./npm-package-json-lint/');
1084+
stubDirname.onCall(1).returns('/home');
10601085
stubExists.returns(true);
10611086
stubStats.returns({
10621087
isFile: function() {
@@ -1073,7 +1098,7 @@ describe('Config Unit Tests', function() {
10731098
const filePath = './package.json';
10741099
const result = config.getProjectHierarchyConfig(filePath);
10751100

1076-
stubDirname.calledOnce.should.be.true;
1101+
stubDirname.calledTwice.should.be.true;
10771102
stubDirname.firstCall.calledWithExactly(filePath).should.be.true;
10781103

10791104
stubExists.calledOnce.should.be.true;

0 commit comments

Comments
 (0)