-
Notifications
You must be signed in to change notification settings - Fork 3
/
supportedCodeFixes.ts
52 lines (47 loc) · 2.17 KB
/
supportedCodeFixes.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
import * as ts from 'typescript'
import { flat } from 'typescript-ast-util';
let supportedCodeFixes: TsDiagnostic[];
export function getAllSupportedCodeFixeDefinitions() {
if (!supportedCodeFixes) {
const supported = getAllSupportedCodeFixCodes();
const tsDiagnostics = getTsDiagnostics();
supportedCodeFixes = tsDiagnostics.filter(d => supported.includes(d.code));
}
return supportedCodeFixes;
}
let allSupportedCodeFixCodes: number[]
function getAllSupportedCodeFixCodes() {
if (!allSupportedCodeFixCodes) {
allSupportedCodeFixCodes = ts.getSupportedCodeFixes().map(s => parseInt(s, 10))
}
return allSupportedCodeFixCodes
}
interface TsDiagnostic {
code: number;
category: number;
key: string;
message: string;
reportsUnneccesary: any;
}
function getTsDiagnostics(): TsDiagnostic[] {
return Object.values((ts as any).Diagnostics);
}
export function getAllSupportedCodeFixes(languageService: ts.LanguageService, fileName: string, positionOrRange: number | ts.TextRange, formatOptions: ts.FormatCodeSettings = {}, preferences: ts.UserPreferences = {}): ReadonlyArray<ts.CodeFixAction> {
const range: ts.TextRange = typeof (positionOrRange) === 'number' ? { pos: positionOrRange, end: positionOrRange } : positionOrRange
const codeFixes = languageService.getCodeFixesAtPosition(fileName, range.pos, range.end, getAllSupportedCodeFixCodes(), formatOptions, preferences);
return codeFixes
}
export interface CodeFixActionExtended extends ts.CodeFixAction {
diagnostic: TsDiagnostic
}
export function getAllSupportedCodeFixesTryEach(languageService: ts.LanguageService, fileName: string, positionOrRange: number | ts.TextRange, formatOptions: ts.FormatCodeSettings = {}, preferences: ts.UserPreferences = {}): CodeFixActionExtended[] {
const range: ts.TextRange = typeof (positionOrRange) === 'number' ? { pos: positionOrRange, end: positionOrRange } : positionOrRange
const all= getAllSupportedCodeFixeDefinitions().map(c=>{
try {
return languageService.getCodeFixesAtPosition(fileName, range.pos, range.end, [c.code], formatOptions, preferences).map(tsc=>({...tsc, diagnostic: c}));
} catch (error) {
return []
}
})
return flat(all as any)
}