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 23
/
Copy pathmain.ts
163 lines (142 loc) · 5.52 KB
/
main.ts
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
import { CompositeDisposable, TextEditor } from 'atom';
import path from 'path';
import { promises } from 'fs';
const { stat } = promises;
import { WorkerHelper } from './workerHelper';
import { defaultConfig, ConfigSchema } from "./config"
const grammarScopes = ['source.ts', 'source.tsx'];
const editorClass = 'linter-tslint-compatible-editor';
const idleCallbacks = new Set();
const config: ConfigSchema = { ...defaultConfig } // copy of default config
// 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
function waitOnIdle() {
return new Promise((resolve) => {
const callbackID = window.requestIdleCallback(() => {
idleCallbacks.delete(callbackID);
resolve(true);
});
idleCallbacks.add(callbackID);
});
};
const subscriptions = new CompositeDisposable();
const workerHelper = new WorkerHelper();
export function activate() {
const depsCallbackID = window.requestIdleCallback(() => {
idleCallbacks.delete(depsCallbackID);
// Install package dependencies
require('atom-package-deps').install('linter-tslint');
});
idleCallbacks.add(depsCallbackID);
// Config subscriptions
subscriptions.add(
atom.config.observe('linter-tslint.rulesDirectory', async (dir) => {
if (dir && path.isAbsolute(dir)) {
const stats = await stat(dir);
if (stats && stats.isDirectory()) {
config.rulesDirectory = dir;
workerHelper.changeConfig('rulesDirectory', dir);
}
}
}),
atom.config.observe('linter-tslint.useLocalTslint', (use) => {
config.useLocalTslint = use;
workerHelper.changeConfig('useLocalTslint', use);
}),
atom.config.observe('linter-tslint.enableSemanticRules', (enableSemanticRules) => {
config.enableSemanticRules = enableSemanticRules;
workerHelper.changeConfig('enableSemanticRules', enableSemanticRules);
}),
atom.config.observe('linter-tslint.useGlobalTslint', (use) => {
config.useGlobalTslint = use;
workerHelper.changeConfig('useGlobalTslint', use);
}),
atom.config.observe('linter-tslint.globalNodePath', (globalNodePath) => {
config.globalNodePath = globalNodePath;
workerHelper.changeConfig('globalNodePath', globalNodePath);
}),
atom.config.observe('linter-tslint.ignoreTypings', (ignoreTypings) => {
config.ignoreTypings = ignoreTypings;
}),
atom.workspace.observeTextEditors((textEditor) => {
// Marks each TypeScript editor with a CSS class so that
// we can enable commands only for TypeScript editors.
const rootScopes = textEditor.getRootScopeDescriptor().getScopesArray();
if (rootScopes.some((scope) => grammarScopes.includes(scope))) {
atom.views.getView(textEditor).classList.add(editorClass);
textEditor.onDidSave(async () => {
if (atom.config.get('linter-tslint.fixOnSave')) {
if (!workerHelper.isRunning()) {
// Wait for worker to initialize
await waitOnIdle();
}
await workerHelper.requestJob('fix', textEditor);
}
});
}
}),
atom.commands.add(`atom-text-editor.${editorClass}`, {
// Command subscriptions
'linter-tslint:fix-file': async () => {
const textEditor = atom.workspace.getActiveTextEditor();
if (!textEditor || textEditor.isModified()) {
// Abort for invalid or unsaved text editors
atom.notifications.addError('Linter-TSLint: Please save before fixing');
return;
}
// The fix replaces the file content and the cursor can jump automatically
// to the beginning of the file, so save current cursor position
const cursorPosition = textEditor.getCursorBufferPosition();
try {
const results = await workerHelper.requestJob('fix', textEditor);
const notificationText = results && results.length === 0
? 'Linter-TSLint: Fix complete.'
: 'Linter-TSLint: Fix attempt complete, but linting errors remain.';
atom.notifications.addSuccess(notificationText);
} catch (err) {
atom.notifications.addWarning(err.message);
} finally {
// Restore cursor to the position before fix job
textEditor.setCursorBufferPosition(cursorPosition);
}
},
}),
);
const createWorkerCallback = window.requestIdleCallback(() => {
workerHelper.startWorker(config);
idleCallbacks.delete(createWorkerCallback);
});
idleCallbacks.add(createWorkerCallback);
}
export function deactivate() {
idleCallbacks.forEach((callbackID) => window.cancelIdleCallback(callbackID));
idleCallbacks.clear();
subscriptions.dispose();
workerHelper.terminateWorker();
}
export function provideLinter() {
return {
name: 'TSLint',
grammarScopes,
scope: 'file',
lintsOnChange: true,
lint: async (textEditor: TextEditor) => {
if (config.ignoreTypings && (textEditor.getPath() ?? "").toLowerCase().endsWith('.d.ts')) {
return [];
}
if (!workerHelper.isRunning()) {
// Wait for worker to initialize
await waitOnIdle();
}
const text = textEditor.getText();
const results = await workerHelper.requestJob('lint', textEditor);
if (textEditor.getText() !== text) {
// Text has been modified since the lint was triggered, tell linter not to update
return null;
}
return results;
},
};
}
export { config } from './config';