diff --git a/lib/worker-helpers.js b/lib/worker-helpers.js index 9a8f5368..a6b80e42 100644 --- a/lib/worker-helpers.js +++ b/lib/worker-helpers.js @@ -128,16 +128,21 @@ function getESLintInstance(fileDir, config, projectPath) { } function getConfigPath(fileDir) { - const configFile = (0, _atomLinter.findCached)(fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']); + const configFile = (0, _atomLinter.findCached)(fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc', 'package.json']); if (configFile) { + if (_path2.default.basename(configFile) === 'package.json') { + // eslint-disable-next-line import/no-dynamic-require + if (require(configFile).eslintConfig) { + return configFile; + } + // If we are here, we found a package.json without an eslint config + // in a dir without any other eslint config files + // (because 'package.json' is last in the call to findCached) + // So, keep looking from the parent directory + return getConfigPath(_path2.default.resolve(_path2.default.dirname(configFile), '..')); + } return configFile; } - - const packagePath = (0, _atomLinter.findCached)(fileDir, 'package.json'); - // eslint-disable-next-line import/no-dynamic-require - if (packagePath && Boolean(require(packagePath).eslintConfig)) { - return packagePath; - } return null; } diff --git a/spec/fixtures/configs/package-json/nested/foo.js b/spec/fixtures/configs/package-json/nested/foo.js new file mode 100644 index 00000000..be92fda5 --- /dev/null +++ b/spec/fixtures/configs/package-json/nested/foo.js @@ -0,0 +1 @@ +var foo = 42; diff --git a/spec/fixtures/configs/package-json/nested/package.json b/spec/fixtures/configs/package-json/nested/package.json new file mode 100644 index 00000000..f68a5ea2 --- /dev/null +++ b/spec/fixtures/configs/package-json/nested/package.json @@ -0,0 +1,8 @@ +{ + "name": "test-fixture", + "version": "0.0.1", + "description": "", + "main": "foo.js", + "author": "", + "license": "" +} diff --git a/spec/worker-helpers-spec.js b/spec/worker-helpers-spec.js index 70812734..94a9f0d0 100644 --- a/spec/worker-helpers-spec.js +++ b/spec/worker-helpers-spec.js @@ -141,6 +141,16 @@ describe('Worker Helpers', () => { const expectedPath = Path.join(fileDir, '.eslintrc.json') expect(Helpers.getConfigPath(fileDir)).toBe(expectedPath) }) + it('finds package.json with an eslintConfig property', () => { + const fileDir = getFixturesPath(Path.join('configs', 'package-json')) + const expectedPath = Path.join(fileDir, 'package.json') + expect(Helpers.getConfigPath(fileDir)).toBe(expectedPath) + }) + it('ignores package.json with no eslintConfig property', () => { + const fileDir = getFixturesPath(Path.join('configs', 'package-json', 'nested')) + const expectedPath = getFixturesPath(Path.join('configs', 'package-json', 'package.json')) + expect(Helpers.getConfigPath(fileDir)).toBe(expectedPath) + }) }) describe('getRelativePath', () => { diff --git a/src/worker-helpers.js b/src/worker-helpers.js index 975cda1f..e8dec11c 100644 --- a/src/worker-helpers.js +++ b/src/worker-helpers.js @@ -104,17 +104,22 @@ export function getESLintInstance(fileDir, config, projectPath) { export function getConfigPath(fileDir) { const configFile = findCached(fileDir, [ - '.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc' + '.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc', 'package.json' ]) if (configFile) { + if (Path.basename(configFile) === 'package.json') { + // eslint-disable-next-line import/no-dynamic-require + if (require(configFile).eslintConfig) { + return configFile + } + // If we are here, we found a package.json without an eslint config + // in a dir without any other eslint config files + // (because 'package.json' is last in the call to findCached) + // So, keep looking from the parent directory + return getConfigPath(Path.resolve(Path.dirname(configFile), '..')) + } return configFile } - - const packagePath = findCached(fileDir, 'package.json') - // eslint-disable-next-line import/no-dynamic-require - if (packagePath && Boolean(require(packagePath).eslintConfig)) { - return packagePath - } return null }