-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathgetDiagnostics.ts
324 lines (290 loc) · 11.5 KB
/
getDiagnostics.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
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
import { Warning } from 'svelte/types/compiler/interfaces';
import { Diagnostic, DiagnosticSeverity, Position, Range } from 'vscode-languageserver';
import {
Document,
isInTag,
mapObjWithRangeToOriginal,
TagInformation
} from '../../../lib/documents';
import { Logger } from '../../../logger';
import { CompilerWarningsSettings } from '../../../ls-config';
import { getLastPartOfPath, moveRangeStartToEndIfNecessary } from '../../../utils';
import { SvelteDocument, TranspileErrorSource } from '../SvelteDocument';
/**
* Returns diagnostics from the svelte compiler.
* Also tries to return errors at correct position if transpilation/preprocessing fails.
*/
export async function getDiagnostics(
document: Document,
svelteDoc: SvelteDocument,
settings: CompilerWarningsSettings
): Promise<Diagnostic[]> {
const config = await svelteDoc.config;
if (config?.loadConfigError) {
return getConfigLoadErrorDiagnostics(config.loadConfigError);
}
try {
return await tryGetDiagnostics(document, svelteDoc, settings);
} catch (error) {
return getPreprocessErrorDiagnostics(document, error);
}
}
/**
* Try to transpile and compile the svelte file and return diagnostics.
*/
async function tryGetDiagnostics(
document: Document,
svelteDoc: SvelteDocument,
settings: CompilerWarningsSettings
): Promise<Diagnostic[]> {
const transpiled = await svelteDoc.getTranspiled();
try {
const res = await svelteDoc.getCompiled();
return (((res.stats as any).warnings || res.warnings || []) as Warning[])
.filter((warning) => settings[warning.code] !== 'ignore')
.map((warning) => {
const start = warning.start || { line: 1, column: 0 };
const end = warning.end || start;
return {
range: Range.create(start.line - 1, start.column, end.line - 1, end.column),
message: warning.message,
severity:
settings[warning.code] === 'error'
? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning,
source: 'svelte',
code: warning.code
};
})
.map((diag) => mapObjWithRangeToOriginal(transpiled, diag))
.map((diag) => adjustMappings(diag, document))
.filter((diag) => isNoFalsePositive(diag, document));
} catch (err) {
return (await createParserErrorDiagnostic(err, document))
.map((diag) => mapObjWithRangeToOriginal(transpiled, diag))
.map((diag) => adjustMappings(diag, document));
}
}
/**
* Try to infer a nice diagnostic error message from the compilation error.
*/
async function createParserErrorDiagnostic(error: any, document: Document) {
const start = error.start || { line: 1, column: 0 };
const end = error.end || start;
const diagnostic: Diagnostic = {
range: Range.create(start.line - 1, start.column, end.line - 1, end.column),
message: error.message,
severity: DiagnosticSeverity.Error,
source: 'svelte',
code: error.code
};
if (diagnostic.message.includes('expected')) {
const isInStyle = isInTag(diagnostic.range.start, document.styleInfo);
const isInScript = isInTag(
diagnostic.range.start,
document.scriptInfo || document.moduleScriptInfo
);
if (
(!document.config?.preprocess || document.config.isFallbackConfig) &&
document.hasLanguageAttribute()
) {
Logger.error(
`Parsing ${document.getFilePath()} failed. No preprocess config found but lang tag exists. Skip showing error because they likely use other preprocessors.`
);
return [];
}
if (isInStyle || isInScript) {
diagnostic.message +=
'\n\nIf you expect this syntax to work, here are some suggestions: ';
if (isInScript) {
diagnostic.message +=
'\nIf you use typescript with `svelte-preprocess`, did you add `lang="ts"` to your `script` tag? ';
} else {
diagnostic.message +=
'\nIf you use less/SCSS with `svelte-preprocess`, did you add `lang="scss"`/`lang="less"` to your `style` tag? ' +
scssNodeRuntimeHint;
}
diagnostic.message +=
'\nDid you setup a `svelte.config.js`? ' +
'\nSee https://github.com/sveltejs/language-tools/tree/master/docs#using-with-preprocessors for more info.';
}
}
return [diagnostic];
}
/**
* Try to infer a nice diagnostic error message from the transpilation error.
*/
function getPreprocessErrorDiagnostics(document: Document, error: any): Diagnostic[] {
Logger.error('Preprocessing failed');
Logger.error(error);
if (document.styleInfo && error.__source === TranspileErrorSource.Style) {
return getStyleErrorDiagnostics(error, document);
}
if (
(document.scriptInfo || document.moduleScriptInfo) &&
error.__source === TranspileErrorSource.Script
) {
return getScriptErrorDiagnostics(error, document);
}
return getOtherErrorDiagnostics(error);
}
function getConfigLoadErrorDiagnostics(error: any): Diagnostic[] {
return [
{
message: 'Error in svelte.config.js\n\n' + error,
range: Range.create(Position.create(0, 0), Position.create(0, 5)),
severity: DiagnosticSeverity.Error,
source: 'svelte'
}
];
}
/**
* Try to infer a nice diagnostic error message from the transpilation error.
*/
function getStyleErrorDiagnostics(error: any, document: Document): Diagnostic[] {
// Error could be from another file that was mixed into the Svelte file as part of preprocessing.
// Some preprocessors set the file property from which we can infer that
const isErrorFromOtherFile =
typeof error?.file === 'string' &&
getLastPartOfPath(error.file) !== getLastPartOfPath(document.getFilePath() || '');
return [
{
message: getStyleErrorMessage(),
range: getStyleErrorRange(),
severity: DiagnosticSeverity.Error,
source: 'svelte(style)'
}
];
function getStyleErrorMessage() {
if (isSveltePreprocessCannotFindModulesError(error)) {
const hint = error.message.includes('node-sass') ? scssNodeRuntimeHint : '';
return getErrorMessage(error.message, 'style', hint);
}
const msg =
error.formatted /* sass error messages have this */ ||
error.message ||
'Style error. Transpilation failed.';
return isErrorFromOtherFile ? 'Error in referenced file\n\n' + msg : msg;
}
function getStyleErrorRange() {
const lineOffset = document.styleInfo?.startPos.line || 0;
const position =
!isErrorFromOtherFile &&
// Some preprocessors like sass or less return error objects with these attributes.
// Use it to display message at better position.
typeof error?.column === 'number' &&
typeof error?.line === 'number'
? Position.create(lineOffset + error.line - 1, error.column)
: document.styleInfo?.startPos || Position.create(0, 0);
return Range.create(position, position);
}
}
/**
* Try to infer a nice diagnostic error message from the transpilation error.
*/
function getScriptErrorDiagnostics(error: any, document: Document): Diagnostic[] {
return [
{
message: getScriptErrorMessage(),
range: getScriptErrorRange(),
severity: DiagnosticSeverity.Error,
source: 'svelte(script)'
}
];
function getScriptErrorMessage() {
if (isSveltePreprocessCannotFindModulesError(error)) {
return getErrorMessage(error.message, 'script');
}
return error.message || 'Script error. Transpilation failed.';
}
function getScriptErrorRange() {
const position =
document.scriptInfo?.startPos ||
document.moduleScriptInfo?.startPos ||
Position.create(0, 0);
return Range.create(position, position);
}
}
/**
* Try to infer a nice diagnostic error message from the transpilation error.
*/
function getOtherErrorDiagnostics(error: any): Diagnostic[] {
return [
{
message: getOtherErrorMessage(),
range: Range.create(Position.create(0, 0), Position.create(0, 5)),
severity: DiagnosticSeverity.Warning,
source: 'svelte'
}
];
function getOtherErrorMessage() {
if (isSveltePreprocessCannotFindModulesError(error)) {
return getErrorMessage(error.message, 'it');
}
return error.message || 'Error. Transpilation failed.';
}
}
/**
* Preprocessing could fail if packages cannot be resolved.
* A warning about a broken svelte.configs.js/preprocessor setup should be added then.
*/
function isSveltePreprocessCannotFindModulesError(error: any): error is Error {
return error instanceof Error && error.message.startsWith('Cannot find any of modules');
}
function getErrorMessage(error: any, source: string, hint = '') {
return (
error +
'\n\nThe file cannot be parsed because ' +
source +
" requires a preprocessor that doesn't seem to be setup or failed during setup. " +
'Did you setup a `svelte.config.js`? ' +
hint +
'\n\nSee https://github.com/sveltejs/language-tools/tree/master/docs#using-with-preprocessors for more info.'
);
}
function isNoFalsePositive(diag: Diagnostic, doc: Document): boolean {
if (diag.code !== 'unused-export-let') {
return true;
}
// TypeScript transpiles `export enum A` and `export namespace A` to `export var A`,
// which the compiler will warn about.
// Silence this edge case. We extract the property from the message and don't use the position
// because that position could be wrong when source mapping trips up.
const unusedExportName = diag.message.substring(
diag.message.indexOf("'") + 1,
diag.message.lastIndexOf("'")
);
const hasExportedEnumWithThatName = new RegExp(
`\\bexport\\s+?(enum|namespace)\\s+?${unusedExportName}\\b`
).test(doc.getText());
return !hasExportedEnumWithThatName;
}
/**
* Some mappings might be invalid. Try to catch these cases here.
*/
function adjustMappings(diag: Diagnostic, doc: Document): Diagnostic {
if (diag.range.start.character < 0) {
diag.range.start.character = 0;
}
if (diag.range.end.character < 0) {
diag.range.end.character = 0;
}
if (diag.range.start.line < 0) {
diag.range.start = { line: 0, character: 0 };
}
if (diag.range.end.line < 0) {
diag.range.end = { line: 0, character: 0 };
}
diag.range = moveRangeStartToEndIfNecessary(diag.range);
if (
diag.code === 'css-unused-selector' &&
doc.styleInfo &&
!isInTag(diag.range.start, doc.styleInfo)
) {
diag.range.start = (doc.styleInfo as TagInformation).startPos;
diag.range.end = diag.range.start;
}
return diag;
}
const scssNodeRuntimeHint =
'If you use SCSS, it may be necessary to add the path to your NODE runtime to the setting `svelte.language-server.runtime`, or use `sass` instead of `node-sass`. ';