Skip to content

Commit 05ebc78

Browse files
authored
Fix an issue with how config overrides get applied (#143)
* Fix an issue with how config overrides get applied Please see #123 for discussion with @simison. * Update getFileList.test.js
1 parent 09d248e commit 05ebc78

File tree

11 files changed

+160
-28
lines changed

11 files changed

+160
-28
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Configurable linter for package.json files.",
55
"keywords": [
66
"lint",
@@ -51,15 +51,15 @@
5151
},
5252
"devDependencies": {
5353
"eslint": "^6.5.1",
54-
"eslint-config-tc": "^8.0.1",
54+
"eslint-config-tc": "^8.1.0",
5555
"eslint-formatter-pretty": "^2.1.1",
5656
"eslint-plugin-import": "^2.18.2",
57-
"eslint-plugin-jest": "^22.17.0",
57+
"eslint-plugin-jest": "^22.19.0",
5858
"eslint-plugin-prettier": "^3.1.1",
5959
"figures": "^3.0.0",
6060
"jest": "^24.9.0",
6161
"npm-package-json-lint-config-default": "^2.0.0",
62-
"npm-package-json-lint-config-tc": "^2.2.0",
62+
"npm-package-json-lint-config-tc": "^3.0.0",
6363
"prettier": "^1.18.2"
6464
},
6565
"engines": {

src/Config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ class Config {
3131
this.configFile = configFile;
3232
this.configBaseDirectory = configBaseDirectory;
3333
this.rules = rules;
34-
this.explorer = cosmiconfig('npmpackagejsonlint', {
35-
transform: cosmicConfigTransformer.transform(cwd, configBaseDirectory)
36-
});
3734
}
3835

3936
/**
@@ -54,10 +51,14 @@ class Config {
5451
debug(`User passed config is undefined.`);
5552
if (this.configFile) {
5653
debug(`Config file specified, loading it.`);
57-
config = this.explorer.loadSync(this.configFile);
54+
config = cosmiconfig('npmpackagejsonlint', {
55+
transform: cosmicConfigTransformer.transform(this.cwd, this.configBaseDirectory, this.configFile)
56+
}).loadSync(this.configFile);
5857
} else {
5958
debug(`Config file wasn't specified, searching for config.`);
60-
config = this.explorer.searchSync(filePathToSearch);
59+
config = cosmiconfig('npmpackagejsonlint', {
60+
transform: cosmicConfigTransformer.transform(this.cwd, this.configBaseDirectory, filePathToSearch)
61+
}).searchSync(filePathToSearch);
6162
}
6263
} else {
6364
debug(`User passed config is set, using it.`);
@@ -78,6 +79,8 @@ class Config {
7879
}
7980

8081
debug(`Overrides applied for ${filePath}`);
82+
debug('Final Config');
83+
debug(config);
8184

8285
configValidator.validateRules(config, 'cli', this.rules);
8386

src/config/cosmicConfigTransformer.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
const path = require('path');
2+
const debug = require('debug')('npm-package-json-lint:cosmicConfigTransformer');
23
const applyExtendsIfSpecified = require('./applyExtendsIfSpecified');
34
const applyOverrides = require('./applyOverrides');
45

5-
const transform = (cwd, configBaseDirectory) => {
6+
const transform = (cwd, configBaseDirectory, filePathBeingLinted) => {
7+
debug(`cwd: ${cwd}`);
8+
debug(`configBaseDirectory`);
9+
debug(configBaseDirectory);
10+
611
return cosmiconfigResult => {
12+
debug(`cosmiconfigResult`);
13+
debug(cosmiconfigResult);
14+
715
if (!cosmiconfigResult) {
816
return null;
917
}
1018

1119
const {config, filepath} = cosmiconfigResult;
1220

21+
debug(`cosmiconfigResult.config`);
22+
debug(config);
23+
debug(`cosmiconfigResult.filepath`);
24+
debug(filepath);
25+
1326
/* eslint-disable no-unused-vars */
1427
const configDir = configBaseDirectory || path.dirname(filepath || '');
1528
const npmPackageJsonLintConfig = {...config};
1629

17-
const configAfterExtends = applyExtendsIfSpecified(npmPackageJsonLintConfig, filepath);
18-
const configAfterOverrides = applyOverrides(cwd, filepath, configAfterExtends.rules, configAfterExtends.overrides);
30+
const configAfterExtends = applyExtendsIfSpecified(npmPackageJsonLintConfig, filePathBeingLinted);
31+
const configAfterOverrides = applyOverrides(
32+
cwd,
33+
filePathBeingLinted,
34+
configAfterExtends.rules,
35+
configAfterExtends.overrides
36+
);
1937

2038
return configAfterOverrides;
2139
};

src/utils/getFileList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const getFileList = (patterns, cwd) => {
4343
files.push(filePath);
4444
});
4545

46-
debug('files');
46+
debug('Final file list from `getFileList`');
4747
debug(files);
4848

4949
return files;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": "npm-package-json-lint-config-default",
3+
"overrides": [
4+
{
5+
"patterns": ["packages/packageOne/**/package.json"],
6+
"rules": {
7+
"license-type": "warning"
8+
}
9+
},
10+
{
11+
"patterns": ["packages/packageTwo/**/package.json"],
12+
"rules": {
13+
"license-type": "warning",
14+
"valid-values-author": ["error", ["TC"]]
15+
}
16+
}
17+
]
18+
}

test/fixtures/monorepo/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "npm-package-json-lint-errors",
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+
"license": false
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "npm-package-json-lint-errors",
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+
"license": false
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "npm-package-json-lint-errors",
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+
"license": false
18+
}

test/integration/cli.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,41 @@ Totals
290290
4 errors
291291
4 warnings
292292
0 files ignored
293+
`;
294+
295+
expect(cli.stdout.toString()).toStrictEqual(expected);
296+
expect(cli.stderr.toString()).toStrictEqual('');
297+
expect(cli.status).toStrictEqual(twoLintErrorsDetected);
298+
});
299+
});
300+
301+
describe('when the cli is run against a monorepo with overrides', () => {
302+
test('each file results and totals will be output', () => {
303+
const cli = spawnSync('../../../src/cli.js', [`**/package.json`], {
304+
env,
305+
cwd: './test/fixtures/monorepo'
306+
});
307+
const expected = `
308+
./package.json
309+
${figures.cross} license-type - node: license - Type should be a string
310+
1 error
311+
0 warnings
312+
313+
./packages/packageOne/package.json
314+
${figures.warning} license-type - node: license - Type should be a string
315+
0 errors
316+
1 warning
317+
318+
./packages/packageTwo/package.json
319+
${figures.warning} license-type - node: license - Type should be a string
320+
${figures.cross} valid-values-author - node: author - Invalid value for author
321+
1 error
322+
1 warning
323+
324+
Totals
325+
2 errors
326+
2 warnings
327+
0 files ignored
293328
`;
294329

295330
expect(cli.stdout.toString()).toStrictEqual(expected);

test/unit/config/cosmicConfigTransformer.test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ describe('cosmicConfigTransformer Unit Tests', () => {
1313
test('null should be returned', () => {
1414
const cwd = 'cwd';
1515
const configBaseDirectory = 'configBaseDirectory';
16+
const filePathBeingLinted = 'myLintedFilePath';
1617

17-
const transformer = cosmicConfigTransformer.transform(cwd, configBaseDirectory);
18+
const transformer = cosmicConfigTransformer.transform(cwd, configBaseDirectory, filePathBeingLinted);
1819
const actual = transformer(null);
1920
expect(actual).toBeNull();
2021
});
@@ -31,8 +32,9 @@ describe('cosmicConfigTransformer Unit Tests', () => {
3132

3233
const cwd = 'cwd';
3334
const configBaseDirectory = 'configBaseDirectory';
35+
const filePathBeingLinted = 'myLintedFilePath';
3436

35-
const transformer = cosmicConfigTransformer.transform(cwd, configBaseDirectory);
37+
const transformer = cosmicConfigTransformer.transform(cwd, configBaseDirectory, filePathBeingLinted);
3638
const cosmiconfigResult = {
3739
config: {
3840
property: 'value'
@@ -44,9 +46,9 @@ describe('cosmicConfigTransformer Unit Tests', () => {
4446

4547
expect(path.dirname).toHaveBeenCalledTimes(0);
4648
expect(applyExtendsIfSpecified).toHaveBeenCalledTimes(1);
47-
expect(applyExtendsIfSpecified).toHaveBeenCalledWith(cosmiconfigResult.config, cosmiconfigResult.filepath);
49+
expect(applyExtendsIfSpecified).toHaveBeenCalledWith(cosmiconfigResult.config, filePathBeingLinted);
4850
expect(applyOverrides).toHaveBeenCalledTimes(1);
49-
expect(applyOverrides).toHaveBeenCalledWith(cwd, cosmiconfigResult.filepath, 'rules', 'overrides');
51+
expect(applyOverrides).toHaveBeenCalledWith(cwd, filePathBeingLinted, 'rules', 'overrides');
5052
});
5153
});
5254

@@ -61,8 +63,9 @@ describe('cosmicConfigTransformer Unit Tests', () => {
6163

6264
const cwd = 'cwd';
6365
const configBaseDirectory = null;
66+
const filePathBeingLinted = 'myLintedFilePath';
6467

65-
const transformer = cosmicConfigTransformer.transform(cwd, configBaseDirectory);
68+
const transformer = cosmicConfigTransformer.transform(cwd, configBaseDirectory, filePathBeingLinted);
6669
const cosmiconfigResult = {
6770
config: {
6871
property: 'value'
@@ -75,9 +78,9 @@ describe('cosmicConfigTransformer Unit Tests', () => {
7578
expect(path.dirname).toHaveBeenCalledTimes(1);
7679
expect(path.dirname).toHaveBeenCalledWith(cosmiconfigResult.filepath);
7780
expect(applyExtendsIfSpecified).toHaveBeenCalledTimes(1);
78-
expect(applyExtendsIfSpecified).toHaveBeenCalledWith(cosmiconfigResult.config, cosmiconfigResult.filepath);
81+
expect(applyExtendsIfSpecified).toHaveBeenCalledWith(cosmiconfigResult.config, filePathBeingLinted);
7982
expect(applyOverrides).toHaveBeenCalledTimes(1);
80-
expect(applyOverrides).toHaveBeenCalledWith(cwd, cosmiconfigResult.filepath, 'rules', 'overrides');
83+
expect(applyOverrides).toHaveBeenCalledWith(cwd, filePathBeingLinted, 'rules', 'overrides');
8184
});
8285
});
8386
});

0 commit comments

Comments
 (0)