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

Commit

Permalink
Provide correct filename to eslint
Browse files Browse the repository at this point in the history
By default, process.cwd is the directory containing the file being linted, so
normally it is only necessary to provide the name of the file itself, with no path.

But, if a .gitignore file is present, eslint will not properly ignore a file if
cwd is not set to the project root when executing with text and providing
a filename, as we are doing. So, if we find an .eslintignore, we can simply
change the cwd to the root directory (same directory as the .eslintignore).
  • Loading branch information
Ian VanSchooten committed Nov 28, 2015
1 parent 67055f9 commit 6a5e24a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
spec/files/bad.js
spec/eslintignore/ignored.js
10 changes: 8 additions & 2 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Communication.on('JOB', function(job) {
} else process.env.NODE_PATH = ''
require('module').Module._initPaths()

process.chdir(params.fileDir)

if (params.global) {
if (params.nodePath === '' && prefixPath === null) {
Expand Down Expand Up @@ -102,7 +101,14 @@ Communication.on('JOB', function(job) {
}

job.Response = new Promise(function(resolve) {
const filePath = (eslintignoreDir) ? Path.relative(eslintignoreDir, params.filePath) : params.filePath
let filePath
if (eslintignoreDir) {
filePath = Path.relative(eslintignoreDir, params.filePath)
process.chdir(eslintignoreDir)
} else {
filePath = Path.basename(params.filePath)
process.chdir(params.fileDir)
}
const argv = [
process.execPath,
eslintPath,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
},
"devDependencies": {
"babel-eslint": "^4.1.5",
"eslint-plugin-react": "^3.10.0",
"eslint-config-airbnb": "latest",
"eslint-config-steelbrain": "latest",
"eslint-config-airbnb": "latest"
"eslint-plugin-import": "^0.11.0",
"eslint-plugin-react": "^3.10.0"
},
"package-deps": [
"linter"
Expand Down
8 changes: 8 additions & 0 deletions spec/eslintignore/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

root: true

rules:
semi:
- 2
- never
1 change: 1 addition & 0 deletions spec/eslintignore/ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = 2;
11 changes: 11 additions & 0 deletions spec/import-resolution/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---

root: true

parser: babel-eslint

plugins:
- import

rules:
import/no-unresolved: 2
1 change: 1 addition & 0 deletions spec/import-resolution/exporting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const meaning = 42
1 change: 1 addition & 0 deletions spec/import-resolution/nested/importing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { meaning } from '../exporting'
24 changes: 24 additions & 0 deletions spec/linter-eslint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ describe('The eslint provider for Linter', () => {
});
})
});

describe('when resolving import paths using eslint-plugin-import', () => {
it('correctly resolves imports from parent', () => {
waitsForPromise(() => {
return atom.workspace.open(`${__dirname}/import-resolution/nested/importing.js`).then(editor => {
return lint(editor).then(messages => {
expect(messages.length).toEqual(0);
})
})
})
})
})

describe('when a file is specified in an .eslintignore file', () => {
it('will not give warnings for the file', () => {
waitsForPromise(() => {
return atom.workspace.open(`${__dirname}/eslintignore/ignored.js`).then(editor => {
return lint(editor).then(messages => {
expect(messages.length).toEqual(0);
})
})
})
})
})
});

0 comments on commit 6a5e24a

Please sign in to comment.