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 9
/
init.js
383 lines (348 loc) · 11.4 KB
/
init.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'use babel';
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions
import { CompositeDisposable, Range } from 'atom';
import { find, generateRange, exec } from 'atom-linter';
import { dirname, join, relative, sep, isAbsolute } from 'path';
import { existsSync, readFileSync, readdirSync } from 'fs';
const tmp = require('tmp');
// Internal values
const elixirProjectPathCache = new Map();
let elixircPath;
let mixPath;
let forceElixirc;
let mixEnv;
function regexp(string, flags) {
return new RegExp(
string
.replace(/\\ /gm, 'randomstring123')
.replace(/\s/gm, '')
.replace(/randomstring123/gm, '\\ '),
flags,
);
}
// Find elixir project in the file path by locating mix.exs, otherwise
// fallback to project path or file path
const findElixirProjectPath = async (editorPath) => {
const editorDir = dirname(editorPath);
const mixexsPath = find(editorDir, 'mix.exs');
if (mixexsPath !== null) {
const pathArray = mixexsPath.split(sep);
if (pathArray.length > 3 && pathArray[pathArray.length - 3] === 'apps') {
// Treat this as an umbrella app. This may be wrong -
// If you happen to keep your code in a directory called 'apps'
pathArray.splice((pathArray.length - 3), 3);
const umbrellaProjectPath = pathArray.join(sep);
// Safety check by looking for a `mix.exs` file in the same directory as
// 'apps'. If it exists, then it's likely an umbrella project
if (existsSync(join(umbrellaProjectPath, 'mix.exs'))) {
return umbrellaProjectPath;
}
}
return dirname(mixexsPath);
}
const projPath = atom.project.relativizePath(editorPath)[0];
if (projPath !== null) {
return projPath;
}
return editorDir;
};
// Memoize the project path per file (traversing is quite expensive)
const elixirProjectPath = async (filePath) => {
if (elixirProjectPathCache.has(filePath)) {
return elixirProjectPathCache.get(filePath);
}
const projectPath = await findElixirProjectPath(filePath);
elixirProjectPathCache.set(filePath, projectPath);
return projectPath;
};
const isMixProject = async (filePath) => {
const project = await elixirProjectPath(filePath);
return existsSync(join(project, 'mix.exs'));
};
const isUmbrellaProject = async (filePath) => {
const project = await elixirProjectPath(filePath);
return existsSync(join(project, 'apps'));
};
const isTestFile = async (filePath) => {
const umbrellaProject = await isUmbrellaProject(filePath);
const project = await elixirProjectPath(filePath);
const relativePath = relative(project, filePath);
if (umbrellaProject) {
// Is the structure "apps/app_name/test/..."
return relativePath.split(sep)[2] === 'test';
}
// Is the structure "test/..."
return relativePath.split(sep)[0] === 'test';
};
const isForcedElixirc = () => forceElixirc;
const isExsFile = filePath => filePath.endsWith('.exs');
const isPhoenixProject = async (filePath) => {
const project = await elixirProjectPath(filePath);
const mixLockPath = join(project, 'mix.lock');
try {
const mixLockContent = readFileSync(mixLockPath, 'utf-8');
return mixLockContent.indexOf('"phoenix"') > 0;
} catch (error) {
return false;
}
};
const findTextEditor = (filePath) => {
const allEditors = atom.workspace.getTextEditors();
const matchingEditor = allEditors.find(textEditor => textEditor.getPath() === filePath);
if (matchingEditor !== undefined) {
return matchingEditor;
}
return false;
};
const ensureAbsolutePath = (projectPath, filePath) => {
if (isAbsolute(filePath)) {
return filePath;
}
return join(projectPath, filePath);
};
const parseError = async (toParse, sourceFilePath) => {
const messages = [];
const re = regexp(
`
\\*\\*[\\ ]+
\\((\\w+)\\) ${''/* 1 - (TypeOfError) */}
[\\ ](?: ${''/* Two message formats.... mode one */}
([\\w\\ ]+) ${''/* 2 - Message */}
[\\r\\n]{1,2}.+[\\r\\n]{1,2} ${''/* Internal elixir code */}
[\\ ]+(.+) ${''/* 3 - File */}
:(\\d+): ${''/* 4 - Line */}
| ${''/* Or... mode two */}
(.+) ${''/* 5 - File */}
:(\\d+): ${''/* 6 - Line */}
[\\ ](.+) ${''/* 7 - Message */}
)
`,
'gm',
);
const projectPath = await elixirProjectPath(sourceFilePath);
let reResult = re.exec(toParse);
while (reResult !== null) {
let excerpt;
let filePath;
let range;
if (reResult[2] !== undefined) {
excerpt = `(${reResult[1]}) ${reResult[2]}`;
filePath = ensureAbsolutePath(projectPath, reResult[3]);
const fileEditor = findTextEditor(filePath);
if (fileEditor) {
// If there is an open TextEditor instance for the file from the Error,
// we can get a better range using generateRange, otherwise
// generate a 1 character range that can be updated to a proper range
// if/when the file is opened.
range = generateRange(fileEditor, reResult[4] - 1);
} else {
range = new Range([reResult[4] - 1, 0], [reResult[4] - 1, 1]);
}
} else {
excerpt = `(${reResult[1]}) ${reResult[7]}`;
filePath = ensureAbsolutePath(projectPath, reResult[5]);
const fileEditor = findTextEditor(filePath);
if (fileEditor) {
range = generateRange(fileEditor, reResult[6] - 1);
} else {
range = new Range([reResult[6] - 1, 0], [reResult[6] - 1, 1]);
}
}
messages.push({
severity: 'error',
excerpt,
location: { file: filePath, position: range },
});
reResult = re.exec(toParse);
}
return messages;
};
// Only Elixir 1.3+
const parseWarning = async (toParse, sourceFilePath) => {
const messages = [];
const re = regexp(
`
warning:\\ (.*)\\n ${''/* warning */}
\\ \\ (.*):([0-9]+) ${''/* file and file number */}
`,
'g',
);
const projectPath = await elixirProjectPath(sourceFilePath);
let reResult = re.exec(toParse);
while (reResult != null) {
const filePath = ensureAbsolutePath(projectPath, reResult[2]);
try {
let range;
const fileEditor = findTextEditor(filePath);
if (fileEditor) {
range = generateRange(fileEditor, reResult[3] - 1);
} else {
range = new Range([reResult[3] - 1, 0], [reResult[3] - 1, 1]);
}
messages.push({
severity: 'warning',
excerpt: reResult[1],
location: { file: filePath, position: range },
});
} catch (Error) {
// eslint-disable-next-line no-console
console.error('linter-elixirc:', Error);
}
reResult = re.exec(toParse);
}
return messages;
};
// Parses warning for elixir 1.2 and below
const parseLegacyWarning = async (toParse, sourceFilePath) => {
const messages = [];
const re = regexp(
`
([^:\\n]*) ${''/* 1 - File name */}
:(\\d+) ${''/* 2 - Line */}
:\\ warning
:\\ (.*) ${''/* 3 - Message */}
`,
'g',
);
const projectPath = await elixirProjectPath(sourceFilePath);
let reResult = re.exec(toParse);
while (reResult !== null) {
const filePath = ensureAbsolutePath(projectPath, reResult[1]);
try {
let range;
const fileEditor = findTextEditor(filePath);
if (fileEditor) {
range = generateRange(fileEditor, reResult[3] - 1);
} else {
range = new Range([reResult[3] - 1, 0], [reResult[3] - 1, 1]);
}
messages.push({
severity: 'warning',
excerpt: reResult[3],
location: { file: filePath, position: range },
});
} catch (Error) {
// eslint-disable-next-line no-console
console.error('linter-elixirc:', Error);
}
reResult = re.exec(toParse);
}
return messages;
};
const handleResult = async (compileResult, filePath) => {
const resultString = `${compileResult.stdout}\n${compileResult.stderr}`;
try {
const errorStack = await parseError(resultString, filePath);
const warningStack = await parseWarning(resultString, filePath);
const legacyWarningStack = await parseLegacyWarning(resultString, filePath);
const results = errorStack.concat(warningStack).concat(legacyWarningStack)
.filter(error => error !== null)
.map(error => error);
return results;
} catch (Error) {
// eslint-disable-next-line no-console
console.error('linter-elixirc:', Error);
return []; // Error is in a different file, just suppress
}
};
const getOpts = async filePath => ({
cwd: await elixirProjectPath(filePath),
throwOnStderr: false,
stream: 'both',
allowEmptyStderr: true,
env: { MIX_ENV: mixEnv },
});
const getDepsPa = async (filePath) => {
const env = (await isTestFile(filePath)) ? 'test' : 'dev';
const buildDir = join('_build', env, 'lib');
const projectPath = await elixirProjectPath(filePath);
try {
return readdirSync(join(projectPath, buildDir))
.map(item => join(projectPath, buildDir, item, 'ebin'));
} catch (e) {
return [];
}
};
const lintElixirc = async (textEditor) => {
const filePath = textEditor.getPath();
const tempDir = tmp.dirSync({ unsafeCleanup: true });
const elixircArgs = [
'--ignore-module-conflict',
'--app',
'mix',
'--app',
'ex_unit',
'-o',
tempDir.name,
];
const paDeps = await getDepsPa(filePath);
paDeps.forEach((item) => {
elixircArgs.push('-pa', item);
});
elixircArgs.push(filePath);
const fileText = textEditor.getText();
const execOpts = await getOpts(filePath);
const result = await exec(elixircPath, elixircArgs, execOpts);
// Cleanup the temp dir
tempDir.removeCallback();
if (textEditor.getText() !== fileText) {
// File contents have changed since the run was triggered, don't update messages
return null;
}
return handleResult(result, filePath);
};
const lintMix = async (textEditor) => {
const filePath = textEditor.getPath();
const fileText = textEditor.getText();
const execOpts = await getOpts(filePath);
const result = await exec(mixPath, ['compile'], execOpts);
if (textEditor.getText() !== fileText) {
// File contents have changed since the run was triggered, don't update messages
return null;
}
return handleResult(result, filePath);
};
export default {
activate() {
require('atom-package-deps').install('linter-elixirc');
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(
atom.config.observe('linter-elixirc.elixircPath', (value) => {
elixircPath = value;
}),
atom.config.observe('linter-elixirc.mixPath', (value) => {
mixPath = value;
}),
atom.config.observe('linter-elixirc.forceElixirc', (value) => {
forceElixirc = value;
}),
atom.config.observe('linter-elixirc.mixEnv', (value) => {
mixEnv = value;
}),
);
},
deactivate() {
this.subscriptions.dispose();
},
provideLinter() {
return {
grammarScopes: ['source.elixir'],
scope: 'project',
lintsOnChange: false,
name: 'Elixir',
async lint(textEditor) {
const filePath = textEditor.getPath();
if (
isForcedElixirc() ||
!(await isMixProject(filePath)) ||
isExsFile(filePath) ||
(await isPhoenixProject(filePath)) ||
(await isUmbrellaProject(filePath))
) {
return lintElixirc(textEditor);
}
return lintMix(textEditor);
},
};
},
};