-
Notifications
You must be signed in to change notification settings - Fork 141
updating global linter as fallback #1144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not bloat the specs with the entire formatted error message. Just match on a small distinctive phrase from the message. (Yeah this problem was already there, but your longer message exaggerates it.) |
||
const description = `<div style="white-space: pre-wrap">No ESLint configuration found. | ||
<hr />Error: No ESLint configuration found. | ||
<hr />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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
it('falls back to the global eslint when no local eslint is found and global is turned on', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check for other "found eslint" type specs like this one, that might not make sense anymore. |
||
const modulesDir = 'not/a/real/path' | ||
const config = createConfig({ global: { useGlobalEslint: false } }) | ||
const config = createConfig({ global: { useGlobalEslint: true, globalNodePath } }) | ||
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') | ||
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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is there a commented out test? |
||
// 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', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,35 +49,47 @@ function isDirectory(dirPath) { | |
} | ||
|
||
export function findESLintDirectory(modulesDir, config, projectPath) { | ||
const { localNodeModules } = config.advanced | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Destructuring improves readability of the rest of the code. |
||
const { useGlobalEslint, globalNodePath } = config.global | ||
const { disableWhenNoEslintConfig } = config.disabling | ||
|
||
let eslintDir = null | ||
let locationType = null | ||
if (config.global.useGlobalEslint) { | ||
|
||
if (localNodeModules) { | ||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we were going to look in local first? This prevents looking in local at all (unless advanced specified) if global is enabled. 😕 |
||
// otherwise look in the default location | ||
locationType = 'local project' | ||
eslintDir = Path.join(modulesDir || '', 'eslint') | ||
} | ||
|
||
if (!isDirectory(eslintDir) && useGlobalEslint) { | ||
// 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') | ||
if (!isDirectory(eslintDir)) { | ||
// 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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.') | ||
} | ||
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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "Enable fallback to global ESLint installation" ?