This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
/
linter-eslint-spec.js
364 lines (325 loc) · 12.2 KB
/
linter-eslint-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
'use babel'
import * as path from 'path'
import * as fs from 'fs'
import { tmpdir } from 'os'
import rimraf from 'rimraf'
import linter from '../lib/main'
const fixturesDir = path.join(__dirname, 'fixtures')
const goodPath = path.join(fixturesDir, 'files', 'good.js')
const badPath = path.join(fixturesDir, 'files', 'bad.js')
const emptyPath = path.join(fixturesDir, 'files', 'empty.js')
const fixPath = path.join(fixturesDir, 'files', 'fix.js')
const configPath = path.join(fixturesDir, 'configs', '.eslintrc.yml')
const importingpath = path.join(fixturesDir,
'import-resolution', 'nested', 'importing.js')
const badImportPath = path.join(fixturesDir,
'import-resolution', 'nested', 'badImport.js')
const ignoredPath = path.join(fixturesDir, 'eslintignore', 'ignored.js')
const modifiedIgnorePath = path.join(fixturesDir,
'modified-ignore-rule', 'foo.js')
const modifiedIgnoreSpacePath = path.join(fixturesDir,
'modified-ignore-rule', 'foo-space.js')
const endRangePath = path.join(fixturesDir, 'end-range', 'no-unreachable.js')
describe('The eslint provider for Linter', () => {
const { spawnWorker } = require('../lib/helpers')
const worker = spawnWorker()
const lint = linter.provideLinter.call(worker).lint
const fix = textEditor =>
worker.worker.request('job', {
type: 'fix',
config: atom.config.get('linter-eslint'),
filePath: textEditor.getPath()
})
beforeEach(() => {
atom.config.set('linter-eslint.disableFSCache', false)
atom.config.set('linter-eslint.disableEslintIgnore', true)
waitsForPromise(() =>
Promise.all([
atom.packages.activatePackage('language-javascript'),
atom.packages.activatePackage('linter-eslint'),
]).then(() =>
atom.workspace.open(goodPath)
)
)
})
describe('checks bad.js and', () => {
let editor = null
beforeEach(() => {
waitsForPromise(() =>
atom.workspace.open(badPath).then((openEditor) => {
editor = openEditor
})
)
})
it('finds at least one message', () => {
waitsForPromise(() =>
lint(editor).then(messages => expect(messages.length).toBeGreaterThan(0))
)
})
it('verifies that message', () => {
waitsForPromise(() =>
lint(editor).then((messages) => {
const expected = '<a href=http://eslint.org/docs/rules/no-undef ' +
'class="badge badge-flexible eslint">no-undef</a> ' +
''foo' is not defined.'
expect(messages[0].type).toBe('Error')
expect(messages[0].text).not.toBeDefined()
expect(messages[0].html).toBe(expected)
expect(messages[0].filePath).toBe(badPath)
expect(messages[0].range).toEqual([[0, 0], [0, 3]])
expect(Object.hasOwnProperty.call(messages[0], 'fix')).toBeFalsy()
})
)
})
})
it('finds nothing wrong with an empty file', () => {
waitsForPromise(() =>
atom.workspace.open(emptyPath).then(editor =>
lint(editor).then(messages => expect(messages.length).toBe(0))
)
)
})
it('finds nothing wrong with a valid file', () => {
waitsForPromise(() =>
atom.workspace.open(goodPath).then(editor =>
lint(editor).then(messages => expect(messages.length).toBe(0))
)
)
})
it('reports the fixes for fixable errors', () => {
waitsForPromise(() =>
atom.workspace.open(fixPath).then(editor =>
lint(editor)
).then((messages) => {
expect(messages[0].fix.range).toEqual([[0, 11], [0, 12]])
expect(messages[0].fix.newText).toBe('')
expect(messages[1].fix.range).toEqual([[2, 0], [2, 1]])
expect(messages[1].fix.newText).toBe(' ')
})
)
})
describe('when resolving import paths using eslint-plugin-import', () => {
it('correctly resolves imports from parent', () => {
waitsForPromise(() =>
atom.workspace.open(importingpath).then(editor =>
lint(editor).then(messages => expect(messages.length).toBe(0))
)
)
})
it('shows a message for an invalid import', () => {
waitsForPromise(() =>
atom.workspace.open(badImportPath).then(editor =>
lint(editor).then((messages) => {
const expected = '<a href=' +
'https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-unresolved.md ' +
'class="badge badge-flexible eslint">import/no-unresolved</a> ' +
'Unable to resolve path to module '../nonexistent'.'
expect(messages.length).toBeGreaterThan(0)
expect(messages[0].type).toBe('Error')
expect(messages[0].text).not.toBeDefined()
expect(messages[0].html).toBe(expected)
expect(messages[0].filePath).toBe(badImportPath)
expect(messages[0].range).toEqual([[0, 24], [0, 39]])
expect(Object.hasOwnProperty.call(messages[0], 'fix')).toBeFalsy()
})
)
)
})
})
describe('when a file is specified in an .eslintignore file', () => {
beforeEach(() => {
atom.config.set('linter-eslint.disableEslintIgnore', false)
})
it('will not give warnings for the file', () => {
waitsForPromise(() =>
atom.workspace.open(ignoredPath).then(editor =>
lint(editor).then(messages => expect(messages.length).toBe(0))
)
)
})
})
describe('fixes errors', () => {
let editor
let doneCheckingFixes
let tempFixtureDir
let tempFixturePath
let tempConfigPath
beforeEach(() => {
waitsForPromise(() => {
tempFixtureDir = fs.mkdtempSync(tmpdir() + path.sep)
tempFixturePath = path.join(tempFixtureDir, 'fixed.js')
tempConfigPath = path.join(tempFixtureDir, '.eslintrc.yaml')
fs.createReadStream(configPath).pipe(fs.createWriteStream(tempConfigPath))
return atom.workspace.open(fixPath).then((openEditor) => {
openEditor.saveAs(tempFixturePath)
editor = openEditor
})
})
})
afterEach(() => {
rimraf.sync(tempFixtureDir)
})
it('should fix linting errors', () => {
function firstLint(textEditor) {
return lint(textEditor)
.then((messages) => {
// The original file has two errors
expect(messages.length).toBe(2)
return textEditor
})
}
function makeFixes(textEditor) {
return fix(textEditor)
.then((messagesAfterSave) => {
// Linter reports a successful fix
expect(messagesAfterSave).toBe('Linter-ESLint: Fix Complete')
})
}
// Create a subscription to watch when the editor changes (from the fix)
editor.onDidChange(() => {
lint(editor)
.then((messagesAfterFixing) => {
// Note: this fires several times, with only the final time resulting in
// a non-null messagesAfterFixing. This is the reason for the check here
// and for the `waitsFor` which makes sure the expectation is tested.
if (messagesAfterFixing) {
// After opening the file again, there are no linting errors
expect(messagesAfterFixing.length).toBe(0)
doneCheckingFixes = true
}
})
})
waitsForPromise(() =>
firstLint(editor)
.then(makeFixes)
)
waitsFor(
() => doneCheckingFixes,
'Messages should be checked after fixing'
)
})
})
describe('Ignores specified rules when editing', () => {
const expected = '<a href=http://eslint.org/docs/rules/no-trailing-spaces ' +
'class="badge badge-flexible eslint">no-trailing-spaces</a> ' +
'Trailing spaces not allowed.'
it('does nothing on saved files', () => {
atom.config.set('linter-eslint.rulesToSilenceWhileTyping', ['no-trailing-spaces'])
waitsForPromise(() =>
atom.workspace.open(modifiedIgnoreSpacePath).then(editor =>
lint(editor).then((messages) => {
expect(messages.length).toBe(1)
expect(messages[0].type).toBe('Error')
expect(messages[0].text).not.toBeDefined()
expect(messages[0].html).toBe(expected)
expect(messages[0].filePath).toBe(modifiedIgnoreSpacePath)
expect(messages[0].range).toEqual([[0, 9], [0, 10]])
})
)
)
})
it('works when the file is modified', () => {
let done
// Set up an observer to check the editor once it is modified
waitsForPromise(() =>
atom.workspace.open(modifiedIgnorePath).then((editor) => {
editor.onDidChange(() => {
lint(editor).then((messages) => {
if (messages) {
// Verify the space is showing an error
expect(messages.length).toBe(1)
expect(messages[0].type).toBe('Error')
expect(messages[0].text).not.toBeDefined()
expect(messages[0].html).toBe(expected)
expect(messages[0].filePath).toBe(modifiedIgnorePath)
expect(messages[0].range).toEqual([[0, 9], [0, 10]])
// Enable the option under test
atom.config.set('linter-eslint.rulesToSilenceWhileTyping', ['no-trailing-spaces'])
// Check the lint results
lint(editor).then((newMessages) => {
expect(newMessages.length).toBe(0)
done = true
})
}
})
})
// Verify no error before
return lint(editor).then(messages =>
expect(messages.length).toBe(0)
)
// Insert a space into the editor
.then(() => {
editor.getBuffer().insert([0, 9], ' ')
})
})
)
waitsFor(
() => done,
'Messages should be checked after modifying the buffer'
)
})
})
describe('prints debugging information with the `debug` command', () => {
let editor
beforeEach(() => {
waitsForPromise(() =>
atom.workspace.open(goodPath).then((openEditor) => {
editor = openEditor
})
)
})
it('shows an info notification', () => {
let done
const checkNotificaton = (notification) => {
if (notification.getMessage() === 'linter-eslint debugging information') {
expect(notification.getType()).toEqual('info')
done = true
}
}
atom.notifications.onDidAddNotification(checkNotificaton)
atom.commands.dispatch(atom.views.getView(editor), 'linter-eslint:debug')
waitsFor(
() => done,
'Notification type should be checked',
2000
)
})
it('includes debugging information in the details', () => {
let done
const checkNotificaton = (notification) => {
if (notification.getMessage() === 'linter-eslint debugging information') {
const detail = notification.getDetail()
expect(detail.includes(`atom version: ${atom.getVersion()}`)).toBe(true)
expect(detail.includes('linter-eslint version:')).toBe(true)
expect(detail.includes(`platform: ${process.platform}`)).toBe(true)
expect(detail.includes('linter-eslint configuration:')).toBe(true)
expect(detail.includes('Using local project eslint')).toBe(true)
done = true
}
}
atom.notifications.onDidAddNotification(checkNotificaton)
atom.commands.dispatch(atom.views.getView(editor), 'linter-eslint:debug')
waitsFor(
() => done,
'Notification details should be checked',
2000
)
})
})
it('handles ranges in messages', () =>
waitsForPromise(() =>
atom.workspace.open(endRangePath).then(editor =>
lint(editor).then((messages) => {
const expected = '<a href=http://eslint.org/docs/rules/no-unreachable ' +
'class="badge badge-flexible eslint">no-unreachable</a> ' +
'Unreachable code.'
expect(messages[0].type).toBe('Error')
expect(messages[0].text).not.toBeDefined()
expect(messages[0].html).toBe(expected)
expect(messages[0].filePath).toBe(endRangePath)
expect(messages[0].range).toEqual([[5, 2], [6, 15]])
})
)
)
)
})