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

updating global linter as fallback #1144

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"title": "Global ESLint",
"properties": {
"useGlobalEslint": {
"title": "Use global ESLint installation",
"title": "Always use global ESLint installation",
Copy link
Contributor

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" ?

"description": "Make sure you have it in your $PATH",
"type": "boolean",
"default": false,
Expand Down
4 changes: 2 additions & 2 deletions spec/linter-eslint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down
21 changes: 16 additions & 5 deletions spec/worker-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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', () => {
Expand Down
38 changes: 25 additions & 13 deletions src/worker-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,47 @@ function isDirectory(dirPath) {
}

export function findESLintDirectory(modulesDir, config, projectPath) {
const { localNodeModules } = config.advanced
Copy link
Contributor

@skylize skylize Jul 13, 2018

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDirectory is expensive. Save the result to a variable so you can use the result twice.

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,
Expand Down