forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixUnreachableCode.ts
More file actions
89 lines (84 loc) · 3.3 KB
/
fixUnreachableCode.ts
File metadata and controls
89 lines (84 loc) · 3.3 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
import {
Debug,
Diagnostics,
emptyArray,
factory,
findAncestor,
first,
getTokenAtPosition,
IfStatement,
isBlock,
isStatement,
sliceAfter,
SourceFile,
SyntaxKind,
textChanges,
} from "../_namespaces/ts";
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix";
const fixId = "fixUnreachableCode";
const errorCodes = [Diagnostics.Unreachable_code_detected.code];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const syntacticDiagnostics = context.program.getSyntacticDiagnostics(context.sourceFile, context.cancellationToken);
if (syntacticDiagnostics.length) return;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, context.span.start, context.span.length, context.errorCode));
return [createCodeFixAction(fixId, changes, Diagnostics.Remove_unreachable_code, fixId, Diagnostics.Remove_all_unreachable_code)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, diag.start, diag.length, diag.code)),
});
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number, length: number, errorCode: number): void {
const token = getTokenAtPosition(sourceFile, start);
const statement = findAncestor(token, isStatement)!;
if (statement.getStart(sourceFile) !== token.getStart(sourceFile)) {
const logData = JSON.stringify({
statementKind: Debug.formatSyntaxKind(statement.kind),
tokenKind: Debug.formatSyntaxKind(token.kind),
errorCode,
start,
length
});
Debug.fail("Token and statement should start at the same point. " + logData);
}
const container = (isBlock(statement.parent) ? statement.parent : statement).parent;
if (!isBlock(statement.parent) || statement === first(statement.parent.statements)) {
switch (container.kind) {
case SyntaxKind.IfStatement:
if ((container as IfStatement).elseStatement) {
if (isBlock(statement.parent)) {
break;
}
else {
changes.replaceNode(sourceFile, statement, factory.createBlock(emptyArray));
}
return;
}
// falls through
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
changes.delete(sourceFile, container);
return;
}
}
if (isBlock(statement.parent)) {
const end = start + length;
const lastStatement = Debug.checkDefined(lastWhere(sliceAfter(statement.parent.statements, statement), s => s.pos < end), "Some statement should be last");
changes.deleteNodeRange(sourceFile, statement, lastStatement);
}
else {
changes.delete(sourceFile, statement);
}
}
function lastWhere<T>(a: readonly T[], pred: (value: T) => boolean): T | undefined {
let last: T | undefined;
for (const value of a) {
if (!pred(value)) break;
last = value;
}
return last;
}