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

Improve package.json eslintConfig support #848

Merged
merged 2 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/worker-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/configs/package-json/nested/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = 42;
8 changes: 8 additions & 0 deletions spec/fixtures/configs/package-json/nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-fixture",
"version": "0.0.1",
"description": "",
"main": "foo.js",
"author": "",
"license": ""
}
10 changes: 10 additions & 0 deletions spec/worker-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
19 changes: 12 additions & 7 deletions src/worker-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down