forked from hex-ci/vscode-stylelint-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
126 lines (103 loc) · 3.48 KB
/
server.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
'use strict';
const {join, parse} = require('path');
const {createConnection, ProposedFeatures, TextDocuments} = require('vscode-languageserver');
const findPkgDir = require('find-pkg-dir');
const parseUri = require('vscode-uri').URI.parse;
const pathIsInside = require('path-is-inside');
const stylelintVSCode = require('./stylelint-vscode');
let config;
let configOverrides;
let autoFixOnSave;
let configBasedir;
let ignorePath;
let configFile;
let ignoreDisables;
let allowEmptyInput;
const connection = createConnection(ProposedFeatures.all);
const documents = new TextDocuments();
async function validate(document, isAutoFixOnSave = false) {
const options = {
fix: isAutoFixOnSave,
};
config && (options.config = config);
configBasedir && (options.configBasedir = configBasedir);
configOverrides && (options.configOverrides = configOverrides);
configFile && (options.configFile = configFile);
ignorePath && (options.ignorePath = ignorePath);
toString.call(ignoreDisables) === '[object Boolean]' && (options.ignoreDisables = ignoreDisables);
toString.call(allowEmptyInput) === '[object Boolean]' && (options.allowEmptyInput = allowEmptyInput);
const documentPath = parseUri(document.uri).fsPath;
if (documentPath) {
const workspaceFolders = await connection.workspace.getWorkspaceFolders();
if (workspaceFolders) {
for (const {uri} of workspaceFolders) {
const workspacePath = parseUri(uri).fsPath;
if (pathIsInside(documentPath, workspacePath)) {
options.ignorePath = join(workspacePath, '.stylelintignore');
break;
}
}
}
if (ignorePath) {
options.ignorePath = ignorePath
} else if (options.ignorePath === undefined) {
options.ignorePath = join(findPkgDir(documentPath) || parse(documentPath).root, '.stylelintignore');
}
}
try {
connection.sendDiagnostics({
uri: document.uri,
diagnostics: await stylelintVSCode(document, options)
});
} catch (err) {
if (err.reasons) {
for (const reason of err.reasons) {
connection.window.showErrorMessage(`stylelint: ${reason}`);
}
return;
}
// https://github.com/stylelint/stylelint/blob/10.0.1/lib/utils/configurationError.js#L10
if (err.code === 78) {
connection.window.showErrorMessage(`stylelint: ${err.message}`);
return;
}
connection.window.showErrorMessage(err.stack.replace(/\n/ug, ' '));
}
}
function validateAll() {
for (const document of documents.all()) {
validate(document);
}
}
connection.onInitialize(() => {
validateAll();
return {
capabilities: {
textDocumentSync: documents.syncKind
}
};
});
connection.onDidChangeConfiguration(({settings}) => {
config = settings.stylelint.config;
configOverrides = settings.stylelint.configOverrides;
autoFixOnSave = settings.stylelint.autoFixOnSave;
configBasedir = settings.stylelint.configBasedir;
configFile = settings.stylelint.configFile;
ignorePath = settings.stylelint.ignorePath;
ignoreDisables = settings.stylelint.ignoreDisables;
allowEmptyInput = settings.stylelint.allowEmptyInput;
validateAll();
});
connection.onDidChangeWatchedFiles(validateAll);
documents.onDidChangeContent(({document}) => validate(document));
documents.onDidClose(({document}) => connection.sendDiagnostics({
uri: document.uri,
diagnostics: []
}));
documents.onDidSave(({document}) => {
if (autoFixOnSave) {
validate(document, true);
}
});
documents.listen(connection);
connection.listen();