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 5
/
linter-reek.js
150 lines (133 loc) · 4.18 KB
/
linter-reek.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
'use babel';
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions
import { CompositeDisposable } from 'atom';
import * as RuleHelpers from './rule-helpers';
let helpers;
let path;
const loadDeps = () => {
if (!helpers) {
helpers = require('atom-linter');
}
if (!path) {
path = require('path');
}
};
const parseReekSyntaxError = (error, filePath, editor) => {
const exceptionMessage = /Exception message:\s*(.*)/g.exec(error);
return [{
severity: 'error',
excerpt: exceptionMessage ? `linter-reek: ${exceptionMessage[1]}` : 'linter-reek: unexpected error',
location: {
file: filePath,
// first line of the file
position: helpers.generateRange(editor, 0),
},
}];
};
const reekSmellToLinter = (smell, file, editor) => smell.lines.map((line) => ({
url: smell.documentation_link,
description: () => RuleHelpers.getRuleMarkDown(smell.smell_type, smell.documentation_link),
excerpt: `${smell.smell_type}: ${smell.context} ${smell.message}`,
severity: 'warning',
location: {
file,
position: helpers.generateRange(editor, line - 1),
},
}));
const parseJSON = (stdout) => {
let parsed;
try {
parsed = JSON.parse(stdout);
} catch (error) {
atom.notifications.addError('linter-reek: Unexpected error', { description: error.message });
}
return parsed;
};
export default {
activate() {
this.idleCallbacks = new Set();
let depsCallbackID;
const installLinterReekDeps = () => {
this.idleCallbacks.delete(depsCallbackID);
if (!atom.inSpecMode()) {
require('atom-package-deps').install('linter-reek');
}
loadDeps();
if (atom.inDevMode()) {
// eslint-disable-next-line no-console
console.log('linter-reek: All dependencies installed.');
}
};
depsCallbackID = window.requestIdleCallback(installLinterReekDeps);
this.idleCallbacks.add(depsCallbackID);
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.config.observe(
'linter-reek.executablePath',
(value) => { this.executablePath = value; },
));
if (atom.inDevMode()) {
/* eslint-disable no-console */
console.log('linter-reek: Reek linter is now activated.');
console.log(`linter-reek: Command path: ${this.executablePath}`);
/* eslint-enable no-console */
}
},
deactivate() {
this.idleCallbacks.forEach((callbackID) => window.cancelIdleCallback(callbackID));
this.idleCallbacks.clear();
this.subscriptions.dispose();
},
provideLinter() {
return {
name: 'reek',
grammarScopes: [
'source.ruby',
'source.ruby.rails',
'source.ruby.rspec',
],
scope: 'file',
lintsOnChange: false,
lint: async (editor) => {
const filePath = editor.getPath();
if (!filePath) {
// Somehow a TextEditor without a path was passed in
return null;
}
const fileText = editor.getText();
loadDeps();
const args = [];
args.push('--format', 'json');
args.push(filePath);
const execOpts = {
cwd: path.dirname(filePath),
ignoreExitCode: true,
};
let output;
try {
output = await helpers.exec(this.executablePath, args, execOpts);
} catch (e) {
if (e.message !== 'Process execution timed out') {
if (/Error: !!!/g.exec(e) === null) {
throw e;
} else {
return parseReekSyntaxError(e, filePath, editor);
}
} else {
atom.notifications.addInfo('linter-reek: reek timed out', {
description: 'A timeout occured while executing reek, it could be due to lower resources '
+ 'or a temporary overload.',
});
return null;
}
}
if (editor.getText() !== fileText) {
// Editor contents have changed, tell Linter not to update
return null;
}
return (parseJSON(output) || []).map(
(offense) => reekSmellToLinter(offense, filePath, editor),
).reduce((offenses, offense) => offenses.concat(offense), []);
},
};
},
};