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

Commit 6a5e24a

Browse files
author
Ian VanSchooten
committed
Provide correct filename to eslint
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).
1 parent 67055f9 commit 6a5e24a

File tree

9 files changed

+58
-4
lines changed

9 files changed

+58
-4
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
spec/files/bad.js
2+
spec/eslintignore/ignored.js

lib/worker.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ Communication.on('JOB', function(job) {
6666
} else process.env.NODE_PATH = ''
6767
require('module').Module._initPaths()
6868

69-
process.chdir(params.fileDir)
7069

7170
if (params.global) {
7271
if (params.nodePath === '' && prefixPath === null) {
@@ -102,7 +101,14 @@ Communication.on('JOB', function(job) {
102101
}
103102

104103
job.Response = new Promise(function(resolve) {
105-
const filePath = (eslintignoreDir) ? Path.relative(eslintignoreDir, params.filePath) : params.filePath
104+
let filePath
105+
if (eslintignoreDir) {
106+
filePath = Path.relative(eslintignoreDir, params.filePath)
107+
process.chdir(eslintignoreDir)
108+
} else {
109+
filePath = Path.basename(params.filePath)
110+
process.chdir(params.fileDir)
111+
}
106112
const argv = [
107113
process.execPath,
108114
eslintPath,

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
},
2020
"devDependencies": {
2121
"babel-eslint": "^4.1.5",
22-
"eslint-plugin-react": "^3.10.0",
22+
"eslint-config-airbnb": "latest",
2323
"eslint-config-steelbrain": "latest",
24-
"eslint-config-airbnb": "latest"
24+
"eslint-plugin-import": "^0.11.0",
25+
"eslint-plugin-react": "^3.10.0"
2526
},
2627
"package-deps": [
2728
"linter"

spec/eslintignore/.eslintrc.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
3+
root: true
4+
5+
rules:
6+
semi:
7+
- 2
8+
- never

spec/eslintignore/ignored.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = 2;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
3+
root: true
4+
5+
parser: babel-eslint
6+
7+
plugins:
8+
- import
9+
10+
rules:
11+
import/no-unresolved: 2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const meaning = 42
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { meaning } from '../exporting'

spec/linter-eslint-spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,28 @@ describe('The eslint provider for Linter', () => {
6565
});
6666
})
6767
});
68+
69+
describe('when resolving import paths using eslint-plugin-import', () => {
70+
it('correctly resolves imports from parent', () => {
71+
waitsForPromise(() => {
72+
return atom.workspace.open(`${__dirname}/import-resolution/nested/importing.js`).then(editor => {
73+
return lint(editor).then(messages => {
74+
expect(messages.length).toEqual(0);
75+
})
76+
})
77+
})
78+
})
79+
})
80+
81+
describe('when a file is specified in an .eslintignore file', () => {
82+
it('will not give warnings for the file', () => {
83+
waitsForPromise(() => {
84+
return atom.workspace.open(`${__dirname}/eslintignore/ignored.js`).then(editor => {
85+
return lint(editor).then(messages => {
86+
expect(messages.length).toEqual(0);
87+
})
88+
})
89+
})
90+
})
91+
})
6892
});

0 commit comments

Comments
 (0)