This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
/
main.js
266 lines (232 loc) · 7.65 KB
/
main.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
'use babel'
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions
import { CompositeDisposable, Task } from 'atom'
// Dependencies
// NOTE: We are not directly requiring these in order to reduce the time it
// takes to require this file as that causes delays in Atom loading this package
let path
let helpers
let workerHelpers
let isConfigAtHomeRoot
// Configuration
const scopes = []
let showRule
let lintHtmlFiles
let ignoredRulesWhenModified
let ignoredRulesWhenFixing
let disableWhenNoEslintConfig
// Internal variables
const idleCallbacks = new Set()
// Internal functions
const idsToIgnoredRules = ruleIds =>
ruleIds.reduce((ids, id) => {
ids[id] = 0 // 0 is the severity to turn off a rule
return ids
}, {})
// Worker still hasn't initialized, since the queued idle callbacks are
// done in order, waiting on a newly queued idle callback will ensure that
// the worker has been initialized
const waitOnIdle = async () =>
new Promise((resolve) => {
const callbackID = window.requestIdleCallback(() => {
idleCallbacks.delete(callbackID)
resolve()
})
idleCallbacks.add(callbackID)
})
module.exports = {
activate() {
let callbackID
const installLinterEslintDeps = () => {
idleCallbacks.delete(callbackID)
if (!atom.inSpecMode()) {
require('atom-package-deps').install('linter-eslint')
}
}
callbackID = window.requestIdleCallback(installLinterEslintDeps)
idleCallbacks.add(callbackID)
this.subscriptions = new CompositeDisposable()
this.worker = null
const embeddedScope = 'source.js.embedded.html'
this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles',
(value) => {
lintHtmlFiles = value
if (lintHtmlFiles) {
scopes.push(embeddedScope)
} else if (scopes.indexOf(embeddedScope) !== -1) {
scopes.splice(scopes.indexOf(embeddedScope), 1)
}
})
)
this.subscriptions.add(
atom.config.observe('linter-eslint.scopes', (value) => {
// Remove any old scopes
scopes.splice(0, scopes.length)
// Add the current scopes
Array.prototype.push.apply(scopes, value)
// Ensure HTML linting still works if the setting is updated
if (lintHtmlFiles && !scopes.includes(embeddedScope)) {
scopes.push(embeddedScope)
}
})
)
this.subscriptions.add(atom.workspace.observeTextEditors((editor) => {
editor.onDidSave(async () => {
const validScope = editor.getCursors().some(cursor =>
cursor.getScopeDescriptor().getScopesArray().some(scope =>
scopes.includes(scope)))
if (validScope && atom.config.get('linter-eslint.fixOnSave')) {
await this.fixJob(true)
}
})
}))
this.subscriptions.add(atom.commands.add('atom-text-editor', {
'linter-eslint:debug': async () => {
if (!helpers) {
helpers = require('./helpers')
}
if (!this.worker) {
await waitOnIdle()
}
const debugString = await helpers.generateDebugString(this.worker)
const notificationOptions = { detail: debugString, dismissable: true }
atom.notifications.addInfo('linter-eslint debugging information', notificationOptions)
}
}))
this.subscriptions.add(atom.commands.add('atom-text-editor', {
'linter-eslint:fix-file': async () => {
await this.fixJob()
}
}))
this.subscriptions.add(atom.config.observe('linter-eslint.showRuleIdInMessage',
(value) => {
showRule = value
})
)
this.subscriptions.add(atom.config.observe('linter-eslint.disableWhenNoEslintConfig',
(value) => {
disableWhenNoEslintConfig = value
})
)
this.subscriptions.add(atom.config.observe('linter-eslint.rulesToSilenceWhileTyping', (ids) => {
ignoredRulesWhenModified = idsToIgnoredRules(ids)
}))
this.subscriptions.add(atom.config.observe('linter-eslint.rulesToDisableWhileFixing', (ids) => {
ignoredRulesWhenFixing = idsToIgnoredRules(ids)
}))
const initializeESLintWorker = () => {
this.worker = new Task(require.resolve('./worker.js'))
}
// Initialize the worker during an idle time
window.requestIdleCallback(initializeESLintWorker)
},
deactivate() {
if (this.worker !== null) {
this.worker.terminate()
this.worker = null
}
idleCallbacks.forEach(callbackID => window.cancelIdleCallback(callbackID))
idleCallbacks.clear()
this.subscriptions.dispose()
},
provideLinter() {
return {
name: 'ESLint',
grammarScopes: scopes,
scope: 'file',
lintsOnChange: true,
lint: async (textEditor) => {
const text = textEditor.getText()
if (text.length === 0) {
return []
}
const filePath = textEditor.getPath()
let rules = {}
if (textEditor.isModified() && Object.keys(ignoredRulesWhenModified).length > 0) {
rules = ignoredRulesWhenModified
}
if (!helpers) {
helpers = require('./helpers')
}
if (!this.worker) {
await waitOnIdle()
}
const response = await helpers.sendJob(this.worker, {
type: 'lint',
contents: text,
config: atom.config.get('linter-eslint'),
rules,
filePath,
projectPath: atom.project.relativizePath(filePath)[0] || ''
})
if (textEditor.getText() !== text) {
/*
The editor text has been modified since the lint was triggered,
as we can't be sure that the results will map properly back to
the new contents, simply return `null` to tell the
`provideLinter` consumer not to update the saved results.
*/
return null
}
return helpers.processESLintMessages(response, textEditor, showRule, this.worker)
}
}
},
async fixJob(isSave = false) {
const textEditor = atom.workspace.getActiveTextEditor()
if (!textEditor || textEditor.isModified()) {
// Abort for invalid or unsaved text editors
const message = 'Linter-ESLint: Please save before fixing'
atom.notifications.addError(message)
}
if (!path) {
path = require('path')
}
if (!isConfigAtHomeRoot) {
isConfigAtHomeRoot = require('./is-config-at-home-root')
}
if (!workerHelpers) {
workerHelpers = require('./worker-helpers')
}
const filePath = textEditor.getPath()
const fileDir = path.dirname(filePath)
const projectPath = atom.project.relativizePath(filePath)[0]
// Get the text from the editor, so we can use executeOnText
const text = textEditor.getText()
// Do not try to make fixes on an empty file
if (text.length === 0) {
return
}
// Do not try to fix if linting should be disabled
const configPath = workerHelpers.getConfigPath(fileDir)
const noProjectConfig = (configPath === null || isConfigAtHomeRoot(configPath))
if (noProjectConfig && disableWhenNoEslintConfig) {
return
}
let rules = {}
if (Object.keys(ignoredRulesWhenFixing).length > 0) {
rules = ignoredRulesWhenFixing
}
if (!helpers) {
helpers = require('./helpers')
}
if (!this.worker) {
await waitOnIdle()
}
try {
const response = await helpers.sendJob(this.worker, {
type: 'fix',
config: atom.config.get('linter-eslint'),
contents: text,
rules,
filePath,
projectPath
})
if (!isSave) {
atom.notifications.addSuccess(response)
}
} catch (err) {
atom.notifications.addWarning(err.message)
}
},
}