From fccb50258653aab995c28e5591f226040f805f1b Mon Sep 17 00:00:00 2001 From: bcnichols3 Date: Wed, 20 Jun 2018 16:51:36 -0400 Subject: [PATCH 1/3] updating global linter as fallback --- package.json | 2 +- spec/worker-helpers-spec.js | 27 +++++++++++++++++++-------- src/worker-helpers.js | 35 ++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index cc68b31b..058d6330 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "title": "Global ESLint", "properties": { "useGlobalEslint": { - "title": "Use global ESLint installation", + "title": "Always use global ESLint installation", "description": "Make sure you have it in your $PATH", "type": "boolean", "default": false, diff --git a/spec/worker-helpers-spec.js b/spec/worker-helpers-spec.js index 84943d3a..799b5771 100644 --- a/spec/worker-helpers-spec.js +++ b/spec/worker-helpers-spec.js @@ -62,14 +62,25 @@ describe('Worker Helpers', () => { expect(foundEslint.type).toEqual('global') }) - it('falls back to the packaged eslint when no local eslint is found', () => { - const modulesDir = 'not/a/real/path' - const config = createConfig({ global: { useGlobalEslint: false } }) - const foundEslint = Helpers.findESLintDirectory(modulesDir, config) - const expectedBundledPath = Path.join(__dirname, '..', 'node_modules', 'eslint') - expect(foundEslint.path).toEqual(expectedBundledPath) - expect(foundEslint.type).toEqual('bundled fallback') - }) + it('falls back to the global eslint when no local eslint is found', () => { + const modulesDir = 'not/a/real/path' + const config = createConfig({ global: { useGlobalEslint: false, globalNodePath } }) + const foundEslint = Helpers.findESLintDirectory(modulesDir, config) + const expectedEslintPath = process.platform === 'win32' + ? Path.join(globalNodePath, 'node_modules', 'eslint') + : Path.join(globalNodePath, 'lib', 'node_modules', 'eslint') + expect(foundEslint.path).toEqual(expectedEslintPath) + expect(foundEslint.type).toEqual('global') + }) + + // it('falls back to the packaged eslint when no local or global eslint is found', () => { + // const modulesDir = 'not/a/real/path' + // const config = createConfig({ global: { useGlobalEslint: false } }) + // const foundEslint = Helpers.findESLintDirectory(modulesDir, config) + // const expectedBundledPath = Path.join(__dirname, '..', 'node_modules', 'eslint') + // expect(foundEslint.path).toEqual(expectedBundledPath) + // expect(foundEslint.type).toEqual('bundled fallback') + // }) }) describe('getESLintInstance && getESLintFromDirectory', () => { diff --git a/src/worker-helpers.js b/src/worker-helpers.js index ba6492f2..8a2ae164 100644 --- a/src/worker-helpers.js +++ b/src/worker-helpers.js @@ -49,11 +49,28 @@ function isDirectory(dirPath) { } export function findESLintDirectory(modulesDir, config, projectPath) { + const {localNodeModules} = config.advanced; + const {useGlobalEslint, globalNodePath} = config.global; + let eslintDir = null let locationType = null - if (config.global.useGlobalEslint) { + + if (localNodeModules && !useGlobalEslint) { + // look for local eslint in dir provided by user + locationType = 'advanced specified'; + // account for absolute path, if one was specified + eslintDir = Path.isAbsolute(cleanPath(localNodeModules)) + ? Path.join(cleanPath(localNodeModules), 'eslint') + : Path.join(projectPath || '', cleanPath(localNodeModules), 'eslint') + ; + } else if (!useGlobalEslint) { + // otherwise look in the default location + locationType = 'local project' + eslintDir = Path.join(modulesDir || '', 'eslint') + } else { + // global fallback locationType = 'global' - const configGlobal = cleanPath(config.global.globalNodePath) + const configGlobal = cleanPath(globalNodePath) const prefixPath = configGlobal || getNodePrefixPath() // NPM on Windows and Yarn on all platforms eslintDir = Path.join(prefixPath, 'node_modules', 'eslint') @@ -61,23 +78,15 @@ export function findESLintDirectory(modulesDir, config, projectPath) { // NPM on platforms other than Windows eslintDir = Path.join(prefixPath, 'lib', 'node_modules', 'eslint') } - } else if (!config.advanced.localNodeModules) { - locationType = 'local project' - eslintDir = Path.join(modulesDir || '', 'eslint') - } else if (Path.isAbsolute(cleanPath(config.advanced.localNodeModules))) { - locationType = 'advanced specified' - eslintDir = Path.join(cleanPath(config.advanced.localNodeModules), 'eslint') - } else { - locationType = 'advanced specified' - eslintDir = Path.join(projectPath || '', cleanPath(config.advanced.localNodeModules), 'eslint') } + if (isDirectory(eslintDir)) { return { path: eslintDir, type: locationType, } - } else if (config.global.useGlobalEslint) { - throw new Error('ESLint not found, please ensure the global Node path is set correctly.') + } else { + throw new Error('ESLint not found, please add a local eslint to this project or ensure the global Node path is set correctly.') } return { path: Cache.ESLINT_LOCAL_PATH, From c02424130d2b8da2262dde3b3597a18d01f3412a Mon Sep 17 00:00:00 2001 From: bcnichols3 Date: Wed, 20 Jun 2018 17:03:55 -0400 Subject: [PATCH 2/3] using more than one if to pass more tests --- src/worker-helpers.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/worker-helpers.js b/src/worker-helpers.js index 8a2ae164..81e623b7 100644 --- a/src/worker-helpers.js +++ b/src/worker-helpers.js @@ -67,7 +67,9 @@ export function findESLintDirectory(modulesDir, config, projectPath) { // otherwise look in the default location locationType = 'local project' eslintDir = Path.join(modulesDir || '', 'eslint') - } else { + } + + if (!isDirectory(eslintDir)) { // global fallback locationType = 'global' const configGlobal = cleanPath(globalNodePath) From ffb3ca69d3ac96e03ba1046823b3fd28b269b60d Mon Sep 17 00:00:00 2001 From: bcnichols3 Date: Tue, 10 Jul 2018 11:44:33 -0400 Subject: [PATCH 3/3] 1 test failing, fallback used only when option is turned on and no local linter found --- spec/linter-eslint-spec.js | 4 ++-- spec/worker-helpers-spec.js | 16 ++++++++-------- src/worker-helpers.js | 17 +++++++++-------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/spec/linter-eslint-spec.js b/spec/linter-eslint-spec.js index 9c4f5913..7fd9d7d3 100644 --- a/spec/linter-eslint-spec.js +++ b/spec/linter-eslint-spec.js @@ -530,9 +530,9 @@ describe('The eslint provider for Linter', () => { it('errors when no config file is found', async () => { const messages = await lint(editor) - const expected = 'Error while running ESLint: No ESLint configuration found..' + const expected = 'Error while running ESLint: ESLint not found, please add a local eslint to this project or ensure the global Node path is set correctly and turned on.' const description = `
No ESLint configuration found. -
Error: No ESLint configuration found. +
Error: ESLint not found, please add a local eslint to this project or ensure the global Node path is set correctly and turned on. at Config.getLocalConfigHierarchy` // The rest of the description includes paths specific to the computer running it expect(messages.length).toBe(1) diff --git a/spec/worker-helpers-spec.js b/spec/worker-helpers-spec.js index 799b5771..59c88f91 100644 --- a/spec/worker-helpers-spec.js +++ b/spec/worker-helpers-spec.js @@ -62,16 +62,16 @@ describe('Worker Helpers', () => { expect(foundEslint.type).toEqual('global') }) - it('falls back to the global eslint when no local eslint is found', () => { - const modulesDir = 'not/a/real/path' - const config = createConfig({ global: { useGlobalEslint: false, globalNodePath } }) - const foundEslint = Helpers.findESLintDirectory(modulesDir, config) - const expectedEslintPath = process.platform === 'win32' + it('falls back to the global eslint when no local eslint is found and global is turned on', () => { + const modulesDir = 'not/a/real/path' + const config = createConfig({ global: { useGlobalEslint: true, globalNodePath } }) + const foundEslint = Helpers.findESLintDirectory(modulesDir, config) + const expectedEslintPath = process.platform === 'win32' ? Path.join(globalNodePath, 'node_modules', 'eslint') : Path.join(globalNodePath, 'lib', 'node_modules', 'eslint') - expect(foundEslint.path).toEqual(expectedEslintPath) - expect(foundEslint.type).toEqual('global') - }) + expect(foundEslint.path).toEqual(expectedEslintPath) + expect(foundEslint.type).toEqual('global') + }) // it('falls back to the packaged eslint when no local or global eslint is found', () => { // const modulesDir = 'not/a/real/path' diff --git a/src/worker-helpers.js b/src/worker-helpers.js index 81e623b7..826de8e4 100644 --- a/src/worker-helpers.js +++ b/src/worker-helpers.js @@ -49,27 +49,27 @@ function isDirectory(dirPath) { } export function findESLintDirectory(modulesDir, config, projectPath) { - const {localNodeModules} = config.advanced; - const {useGlobalEslint, globalNodePath} = config.global; + const { localNodeModules } = config.advanced + const { useGlobalEslint, globalNodePath } = config.global + const { disableWhenNoEslintConfig } = config.disabling let eslintDir = null let locationType = null - if (localNodeModules && !useGlobalEslint) { + if (localNodeModules) { // look for local eslint in dir provided by user - locationType = 'advanced specified'; + locationType = 'advanced specified' // account for absolute path, if one was specified eslintDir = Path.isAbsolute(cleanPath(localNodeModules)) ? Path.join(cleanPath(localNodeModules), 'eslint') : Path.join(projectPath || '', cleanPath(localNodeModules), 'eslint') - ; } else if (!useGlobalEslint) { // otherwise look in the default location locationType = 'local project' eslintDir = Path.join(modulesDir || '', 'eslint') } - if (!isDirectory(eslintDir)) { + if (!isDirectory(eslintDir) && useGlobalEslint) { // global fallback locationType = 'global' const configGlobal = cleanPath(globalNodePath) @@ -87,8 +87,9 @@ export function findESLintDirectory(modulesDir, config, projectPath) { path: eslintDir, type: locationType, } - } else { - throw new Error('ESLint not found, please add a local eslint to this project or ensure the global Node path is set correctly.') + } + if (!disableWhenNoEslintConfig && !isDirectory(eslintDir)) { + throw new Error('ESLint not found, please add a local eslint to this project or ensure the global Node path is set correctly and turned on') } return { path: Cache.ESLINT_LOCAL_PATH,