-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.ts
More file actions
103 lines (90 loc) · 3.47 KB
/
index.ts
File metadata and controls
103 lines (90 loc) · 3.47 KB
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
import { PluginStrictFileChecker } from './PluginStrictFileChecker';
import {
log,
PluginInfo,
setupLanguageServiceProxy,
setupStrictLanguageServiceHostProxy,
} from './utils';
import * as ts from 'typescript/lib/tsserverlibrary';
const isDiagnosticChainEquals = (
a: ts.Diagnostic['messageText'] | undefined,
b: ts.Diagnostic['messageText'] | undefined,
): boolean => {
if (a === undefined || b === undefined) {
return a === b;
} else if (typeof a === 'string' && typeof b === 'string') {
return a === b;
} else if (typeof a === 'string' || typeof b === 'string') {
return false;
} else {
const allChainsEqual: boolean =
a.next?.every((n, i) => isDiagnosticChainEquals(n, b.next?.[i])) ?? a === b;
return (
a.category === b.category &&
a.code === b.code &&
a.messageText === b.messageText &&
allChainsEqual
);
}
};
const isDiagnosticEquals = (a: ts.Diagnostic, b: ts.Diagnostic) => {
return (
a.category == b.category &&
a.code === b.code &&
a.source === b.source &&
a.length === b.length &&
a.messageText === b.messageText &&
isDiagnosticChainEquals(a.messageText, b.messageText)
);
};
const init: ts.server.PluginModuleFactory = () => {
function create(info: PluginInfo) {
const proxy = setupLanguageServiceProxy(info);
const strictLanguageServiceHost = setupStrictLanguageServiceHostProxy(info);
const strictLanguageService = ts.createLanguageService(strictLanguageServiceHost);
log(info, 'Plugin initialized');
proxy.getSemanticDiagnostics = function (filePath) {
const strictFile = new PluginStrictFileChecker(info).isFileStrict(filePath);
const convertStrictErrorsToWarnings = !!info?.config?.convertStrictErrorsToWarnings;
if (strictFile && convertStrictErrorsToWarnings) {
//To find which errors are strict errors, we check if we find the the same error when we are running nonstrict.
//If we do not, we can be sure that error only occurs when we run with strict
const strictDiagnostics = strictLanguageService.getSemanticDiagnostics(filePath);
const nonStrictDiagnostics = info.languageService.getSemanticDiagnostics(filePath);
return strictDiagnostics.map((strict) => {
const isInNonStrict = nonStrictDiagnostics.some((nonStrict) =>
isDiagnosticEquals(nonStrict, strict),
);
if (isInNonStrict || strict.category !== ts.DiagnosticCategory.Error) {
return strict;
} else {
return { ...strict, category: ts.DiagnosticCategory.Warning };
}
});
} else if (strictFile) {
return strictLanguageService.getSemanticDiagnostics(filePath);
} else {
return info.languageService.getSemanticDiagnostics(filePath);
}
};
proxy.cleanupSemanticCache = function () {
strictLanguageService.cleanupSemanticCache();
info.languageService.cleanupSemanticCache();
};
proxy.dispose = function () {
strictLanguageService.dispose();
info.languageService.dispose();
};
proxy.getQuickInfoAtPosition = function (filePath, position) {
const strictFile = new PluginStrictFileChecker(info).isFileStrict(filePath);
if (strictFile) {
return strictLanguageService.getQuickInfoAtPosition(filePath, position);
} else {
return info.languageService.getQuickInfoAtPosition(filePath, position);
}
};
return proxy;
}
return { create };
};
module.exports = init;