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 140
/
main.js
219 lines (187 loc) · 7.44 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
'use strict';
'use babel';
var _escapeHtml = require('escape-html');
var _escapeHtml2 = _interopRequireDefault(_escapeHtml);
var _eslintRuleDocumentation = require('eslint-rule-documentation');
var _eslintRuleDocumentation2 = _interopRequireDefault(_eslintRuleDocumentation);
var _atom = require('atom');
var _helpers = require('./helpers');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Configuration
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions
var scopes = [];
var showRule = void 0;
var ignoredRulesWhenModified = void 0;
module.exports = {
activate: function activate() {
var _this = this;
require('atom-package-deps').install();
this.subscriptions = new _atom.CompositeDisposable();
this.active = true;
this.worker = null;
this.subscriptions.add(atom.config.observe('linter-eslint.scopes', function (value) {
// Remove any old scopes
scopes.splice(0, scopes.length);
// Add the current scopes
Array.prototype.push.apply(scopes, value);
}));
var embeddedScope = 'source.js.embedded.html';
this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', function (lintHtmlFiles) {
if (lintHtmlFiles) {
scopes.push(embeddedScope);
} else if (scopes.indexOf(embeddedScope) !== -1) {
scopes.splice(scopes.indexOf(embeddedScope), 1);
}
}));
this.subscriptions.add(atom.workspace.observeTextEditors(function (editor) {
editor.onDidSave(function () {
if (scopes.indexOf(editor.getGrammar().scopeName) !== -1 && atom.config.get('linter-eslint.fixOnSave')) {
var filePath = editor.getPath();
var projectPath = atom.project.relativizePath(filePath)[0];
_this.worker.request('job', {
type: 'fix',
config: atom.config.get('linter-eslint'),
filePath: filePath,
projectPath: projectPath
}).catch(function (response) {
return atom.notifications.addWarning(response);
});
}
});
}));
this.subscriptions.add(atom.commands.add('atom-text-editor', {
'linter-eslint:fix-file': function linterEslintFixFile() {
var textEditor = atom.workspace.getActiveTextEditor();
var filePath = textEditor.getPath();
var projectPath = atom.project.relativizePath(filePath)[0];
if (!textEditor || textEditor.isModified()) {
// Abort for invalid or unsaved text editors
atom.notifications.addError('Linter-ESLint: Please save before fixing');
return;
}
_this.worker.request('job', {
type: 'fix',
config: atom.config.get('linter-eslint'),
filePath: filePath,
projectPath: projectPath
}).then(function (response) {
return atom.notifications.addSuccess(response);
}).catch(function (response) {
return atom.notifications.addWarning(response);
});
}
}));
this.subscriptions.add(atom.config.observe('linter-eslint.showRuleIdInMessage', function (value) {
showRule = value;
}));
this.subscriptions.add(atom.config.observe('linter-eslint.rulesToSilenceWhileTyping', function (ids) {
ignoredRulesWhenModified = (0, _helpers.idsToIgnoredRules)(ids);
}));
var initializeWorker = function initializeWorker() {
var _spawnWorker = (0, _helpers.spawnWorker)();
var worker = _spawnWorker.worker;
var subscription = _spawnWorker.subscription;
_this.worker = worker;
_this.subscriptions.add(subscription);
worker.onDidExit(function () {
if (_this.active) {
(0, _helpers.showError)('Worker died unexpectedly', 'Check your console for more ' + 'info. A new worker will be spawned instantly.');
setTimeout(initializeWorker, 1000);
}
});
};
initializeWorker();
},
deactivate: function deactivate() {
this.active = false;
this.subscriptions.dispose();
},
provideLinter: function provideLinter() {
var _this2 = this;
var Helpers = require('atom-linter');
return {
name: 'ESLint',
grammarScopes: scopes,
scope: 'file',
lintOnFly: true,
lint: function lint(textEditor) {
var text = textEditor.getText();
if (text.length === 0) {
return Promise.resolve([]);
}
var filePath = textEditor.getPath();
var rules = {};
if (textEditor.isModified() && Object.keys(ignoredRulesWhenModified).length > 0) {
rules = ignoredRulesWhenModified;
}
return _this2.worker.request('job', {
type: 'lint',
contents: text,
config: atom.config.get('linter-eslint'),
rules: rules,
filePath: filePath,
projectPath: atom.project.relativizePath(filePath)[0] || ''
}).then(function (response) {
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 response.map(function (_ref) {
var message = _ref.message;
var line = _ref.line;
var severity = _ref.severity;
var ruleId = _ref.ruleId;
var column = _ref.column;
var fix = _ref.fix;
var endLine = _ref.endLine;
var endColumn = _ref.endColumn;
var textBuffer = textEditor.getBuffer();
var linterFix = null;
if (fix) {
var fixRange = new _atom.Range(textBuffer.positionForCharacterIndex(fix.range[0]), textBuffer.positionForCharacterIndex(fix.range[1]));
linterFix = {
range: fixRange,
newText: fix.text
};
}
var range = void 0;
var msgLine = line - 1;
var msgCol = column ? column - 1 : column;
try {
if (endColumn && endLine) {
(0, _helpers.validatePoint)(textEditor, msgLine, msgCol);
(0, _helpers.validatePoint)(textEditor, endLine - 1, endColumn - 1);
range = [[msgLine, msgCol], [endLine - 1, endColumn - 1]];
} else {
range = Helpers.rangeFromLineNumber(textEditor, msgLine, msgCol);
}
} catch (err) {
throw new Error('Cannot mark location in editor for (' + ruleId + ') - (' + message + ')' + (' at line (' + line + ') column (' + column + ')'));
}
var ret = {
filePath: filePath,
type: severity === 1 ? 'Warning' : 'Error',
range: range
};
if (showRule) {
var elName = ruleId ? 'a' : 'span';
var href = ruleId ? ' href=' + (0, _eslintRuleDocumentation2.default)(ruleId).url : '';
ret.html = '<' + elName + href + ' class="badge badge-flexible eslint">' + ((ruleId || 'Fatal') + '</' + elName + '> ' + (0, _escapeHtml2.default)(message));
} else {
ret.text = message;
}
if (linterFix) {
ret.fix = linterFix;
}
return ret;
});
});
}
};
}
};