Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 90cb8de

Browse files
authored
Merge pull request #848 from AtomLinter/package-json-configs
Improve package.json eslintConfig support
2 parents 9fa229c + f7ef0a0 commit 90cb8de

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

lib/worker-helpers.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,21 @@ function getESLintInstance(fileDir, config, projectPath) {
128128
}
129129

130130
function getConfigPath(fileDir) {
131-
const configFile = (0, _atomLinter.findCached)(fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']);
131+
const configFile = (0, _atomLinter.findCached)(fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc', 'package.json']);
132132
if (configFile) {
133+
if (_path2.default.basename(configFile) === 'package.json') {
134+
// eslint-disable-next-line import/no-dynamic-require
135+
if (require(configFile).eslintConfig) {
136+
return configFile;
137+
}
138+
// If we are here, we found a package.json without an eslint config
139+
// in a dir without any other eslint config files
140+
// (because 'package.json' is last in the call to findCached)
141+
// So, keep looking from the parent directory
142+
return getConfigPath(_path2.default.resolve(_path2.default.dirname(configFile), '..'));
143+
}
133144
return configFile;
134145
}
135-
136-
const packagePath = (0, _atomLinter.findCached)(fileDir, 'package.json');
137-
// eslint-disable-next-line import/no-dynamic-require
138-
if (packagePath && Boolean(require(packagePath).eslintConfig)) {
139-
return packagePath;
140-
}
141146
return null;
142147
}
143148

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = 42;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "test-fixture",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "foo.js",
6+
"author": "",
7+
"license": ""
8+
}

spec/worker-helpers-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ describe('Worker Helpers', () => {
141141
const expectedPath = Path.join(fileDir, '.eslintrc.json')
142142
expect(Helpers.getConfigPath(fileDir)).toBe(expectedPath)
143143
})
144+
it('finds package.json with an eslintConfig property', () => {
145+
const fileDir = getFixturesPath(Path.join('configs', 'package-json'))
146+
const expectedPath = Path.join(fileDir, 'package.json')
147+
expect(Helpers.getConfigPath(fileDir)).toBe(expectedPath)
148+
})
149+
it('ignores package.json with no eslintConfig property', () => {
150+
const fileDir = getFixturesPath(Path.join('configs', 'package-json', 'nested'))
151+
const expectedPath = getFixturesPath(Path.join('configs', 'package-json', 'package.json'))
152+
expect(Helpers.getConfigPath(fileDir)).toBe(expectedPath)
153+
})
144154
})
145155

146156
describe('getRelativePath', () => {

src/worker-helpers.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,22 @@ export function getESLintInstance(fileDir, config, projectPath) {
104104
export function getConfigPath(fileDir) {
105105
const configFile =
106106
findCached(fileDir, [
107-
'.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc'
107+
'.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc', 'package.json'
108108
])
109109
if (configFile) {
110+
if (Path.basename(configFile) === 'package.json') {
111+
// eslint-disable-next-line import/no-dynamic-require
112+
if (require(configFile).eslintConfig) {
113+
return configFile
114+
}
115+
// If we are here, we found a package.json without an eslint config
116+
// in a dir without any other eslint config files
117+
// (because 'package.json' is last in the call to findCached)
118+
// So, keep looking from the parent directory
119+
return getConfigPath(Path.resolve(Path.dirname(configFile), '..'))
120+
}
110121
return configFile
111122
}
112-
113-
const packagePath = findCached(fileDir, 'package.json')
114-
// eslint-disable-next-line import/no-dynamic-require
115-
if (packagePath && Boolean(require(packagePath).eslintConfig)) {
116-
return packagePath
117-
}
118123
return null
119124
}
120125

0 commit comments

Comments
 (0)