This repository was 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
/
Copy pathmain.js
236 lines (211 loc) · 7.12 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
'use babel';
import { CompositeDisposable } from 'atom';
// Dependencies
let path;
let globule;
let spawnSync;
let consistentEnv;
let generateRange;
let helpers;
function loadDeps() {
if (!path) {
path = require('path');
}
if (!globule) {
globule = require('globule');
}
if (!consistentEnv) {
consistentEnv = require('consistent-env');
}
if (!spawnSync) {
({ spawnSync } = require('child_process'));
}
if (!generateRange) {
({ generateRange } = require('atom-linter'));
}
if (!helpers) {
helpers = require('./helpers');
}
}
const idleCallbacks = new Set();
const makeIdleCallback = (work) => {
let callbackId;
const callBack = () => {
idleCallbacks.delete(callbackId);
work();
};
callbackId = window.requestIdleCallback(callBack);
idleCallbacks.add(callbackId);
};
let prefixPath = null;
export default {
activate() {
this.subs = new CompositeDisposable();
this.subs.add(
atom.config.observe('linter-sass-lint.noConfigDisable',
(value) => { this.noConfigDisable = value; }),
atom.config.observe('linter-sass-lint.configFile',
(value) => { this.configFile = value; }),
atom.config.observe('linter-sass-lint.globalSassLint',
(value) => { this.globalSassLint = value; }),
atom.config.observe('linter-sass-lint.globalNodePath',
(value) => { this.globalPath = value; }),
atom.config.observe('linter-sass-lint.resolvePathsRelativeToConfig',
(value) => { this.resolvePathsRelativeToConfig = value; }),
);
if (!atom.inSpecMode()) {
const loadLinterSassLintDependencies = () => { loadDeps(); };
const linterSassLintInstallPeerPackages = () => {
require('atom-package-deps').install('linter-sass-lint');
};
makeIdleCallback(loadLinterSassLintDependencies);
makeIdleCallback(linterSassLintInstallPeerPackages);
}
},
deactivate() {
idleCallbacks.forEach((callbackID) => window.cancelIdleCallback(callbackID));
idleCallbacks.clear();
return this.subs.dispose();
},
// return a relative path for a file within our project
// we use this to match it to our include/exclude glob string within sass-lint's
// user specified config
getFilePath(absolutePath, configFilePath) {
if (this.resolvePathsRelativeToConfig) {
return path.relative(path.dirname(configFilePath), absolutePath);
}
return atom.project.relativizePath(absolutePath)[1];
},
// Determines whether to use the sass-lint package included with linter-sass-lint
// or the users globally installed sass-lint version
findExecutable() {
// FIXME: use resolve here
if (!this.globalSassLint) {
// eslint-disable-next-line import/no-dynamic-require
return require(path.join(__dirname, '..', 'node_modules', 'sass-lint'));
}
if ((this.globalPath === '') && (prefixPath === null)) {
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const env = { ...consistentEnv() };
try {
prefixPath = spawnSync(npmCommand, [
'get',
'prefix',
], { env }).output[1].toString().trim();
} catch (e) {
throw new Error('prefix');
}
}
if (process.platform === 'win32') {
// eslint-disable-next-line import/no-dynamic-require
return require(path.join(this.globalPath || prefixPath, 'node_modules', 'sass-lint'));
}
// eslint-disable-next-line import/no-dynamic-require
return require(path.join(this.globalPath || prefixPath, 'lib', 'node_modules', 'sass-lint'));
},
provideLinter() {
return {
name: 'sass-lint',
grammarScopes: ['source.css.scss', 'source.scss', 'source.css.sass', 'source.sass'],
scope: 'file',
lintsOnChange: true,
lint: (editor) => {
// Force the dependencies to load if they haven't already
loadDeps();
const filePath = editor.getPath();
const projectConfig = helpers.getConfig(
filePath, '.sass-lint.yml', this.noConfigDisable,
);
const globalConfig = this.configFile === '' ? null : this.configFile;
const config = projectConfig !== null ? projectConfig : globalConfig;
let linter;
try {
linter = this.findExecutable();
} catch (error) {
if (error.message === 'prefix') {
helpers.errorGettingPath();
return null;
}
helpers.warningSassLintMissing();
return null;
}
if ((config !== null) && (path.extname(config) !== '.yml')) {
helpers.warningConfigFile();
}
if ((config === null) && (this.noConfigDisable === false)) {
return [{
severity: 'info',
excerpt: 'No .sass-lint.yml config file detected or specified. '
+ 'Please check your settings',
location: {
file: filePath,
position: generateRange(editor, 0),
},
}];
}
if (config === null && this.noConfigDisable === true) {
return null;
}
let result;
try {
const compiledConfig = linter.getConfig({}, config);
const relativePath = this.getFilePath(filePath, config);
if (
globule.isMatch(compiledConfig.files.include, relativePath)
&& !globule.isMatch(compiledConfig.files.ignore, relativePath)
) {
result = linter.lintText({
text: editor.getText(),
format: helpers.getFileSyntax(filePath),
filename: filePath,
}, {}, config);
}
} catch (error) {
const match = error.message.match(/Parsing error at [^:]+: (.*) starting from line #(\d+)/);
if (match) {
const lineIdx = (Number.parseInt(match[2], 10) || 1) - 1;
return [{
severity: 'error',
excerpt: `Parsing error: ${match[1]}.`,
location: {
file: filePath,
position: generateRange(editor, lineIdx),
},
}];
}
// Leaving this here to allow people to report the errors
// eslint-disable-next-line no-console
console.log('linter-sass-lint', error);
return [{
severity: 'error',
excerpt: 'Unexpected parse error in file',
location: {
file: filePath,
position: generateRange(editor, 0),
},
}];
}
if (result) {
return result.messages.map((msg) => {
const line = msg.line ? msg.line - 1 : 0;
const col = msg.column ? msg.column - 1 : 0;
const excerpt = msg.message
? `${msg.message} (${msg.ruleId})`
: 'Unknown Error';
result = {
severity: helpers.getSeverity(msg.severity),
excerpt,
url: helpers.getRuleURI(msg.ruleId),
location: {
file: filePath,
position: generateRange(editor, line, col),
},
};
return result;
});
}
return [];
},
};
},
};