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

Commit

Permalink
test: don't use eslint to find config
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Oct 24, 2021
1 parent 9efa019 commit 2d1fa59
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
47 changes: 37 additions & 10 deletions dist/worker-helpers.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/worker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spec/linter-eslint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ describe('The eslint provider for Linter', () => {
rimraf.sync(tempFixtureDir)
})

xit('does not report errors when no config file is found', async () => {
it('does not report errors when no config file is found', async () => {
const messages = await lint(editor)

expect(messages.length).toBe(0)
Expand Down
13 changes: 6 additions & 7 deletions spec/worker-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,22 @@ describe('Worker Helpers', () => {
})
})

describe('getConfigForFile', () => {
describe('getConfigPath', () => {
// Use the bundled ESLint for the tests
const eslint = require('eslint')
const fixtureFile = getFixturesPath(Path.join('configs', 'js', 'foo.js'))

it('uses ESLint to determine the configuration', () => {
const filePath = fixtureFile
const foundConfig = Helpers.getConfigForFile(eslint, filePath)
xit('uses ESLint to determine the configuration', () => {
const filePath = Path.dirname(fixtureFile)
const foundConfig = Helpers.getConfigPath(filePath)
expect(foundConfig.rules.semi).toEqual([2, 'never'])
})

xit('returns null when the file has no configuration', async () => {
it('returns null when the file has no configuration', async () => {
// Copy the file to a temporary folder
const filePath = await copyFileToTempDir(fixtureFile)
const tempDir = Path.dirname(filePath)

const foundConfig = Helpers.getConfigForFile(eslint, filePath)
const foundConfig = Helpers.getConfigPath(tempDir)
expect(foundConfig).toBeNull()

// Remove the temporary directory
Expand Down
39 changes: 32 additions & 7 deletions src/worker-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Cache = {
NODE_PREFIX_PATH: null,
LAST_MODULES_PATH: null
}
const rootPath = (process.platform === 'win32') ? process.cwd().split(Path.sep)[0] : '/'

/**
* Takes a path and translates `~` to the user's home directory, and replaces
Expand Down Expand Up @@ -177,15 +178,39 @@ export function log(...args) {
* @param {import("eslint")} eslint
* @param {string} filePath
*/
export function getConfigForFile(eslint, filePath) {
const cli = new eslint.CLIEngine()
try {
return cli.getConfigForFile(filePath)
} catch (e) {
// No configuration was found
return null
export function getConfigPath(fileDir) {
const configFile = findCached(fileDir, [
'.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
}
// return if we hit root folder and no eslint config found
if (Path.dirname(configFile).toUpperCase() === rootPath.toUpperCase()) {
return null
}
// 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
}
return null
}
// export function getConfigForFile(eslint, filePath) {
// const cli = new eslint.CLIEngine()
// try {
// return cli.getConfigForFile(filePath)
// } catch (e) {
// // No configuration was found
// return null
// }
// }

/**
* @param {string} fileDir
Expand Down
2 changes: 1 addition & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = async () => {
const fileDir = Path.dirname(filePath)
const eslint = Helpers.getESLintInstance(fileDir, config, projectPath)

const fileConfig = Helpers.getConfigForFile(eslint, filePath)
const fileConfig = Helpers.getConfigPath(fileDir)
if (fileConfig === null && config.disabling.disableWhenNoEslintConfig) {
emit(emitKey, { messages: [] })
return
Expand Down

0 comments on commit 2d1fa59

Please sign in to comment.