|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixId = "fixUnreachableCode"; |
| 4 | + const errorCodes = [Diagnostics.Unreachable_code_detected.code]; |
| 5 | + registerCodeFix({ |
| 6 | + errorCodes, |
| 7 | + getCodeActions(context) { |
| 8 | + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, context.span.start)); |
| 9 | + return [createCodeFixAction(fixId, changes, Diagnostics.Remove_unreachable_code, fixId, Diagnostics.Remove_all_unreachable_code)]; |
| 10 | + }, |
| 11 | + fixIds: [fixId], |
| 12 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, diag.start)), |
| 13 | + }); |
| 14 | + |
| 15 | + function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, start: number): void { |
| 16 | + const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false); |
| 17 | + const statement = findAncestor(token, isStatement); |
| 18 | + Debug.assert(statement.getStart(sourceFile) === token.getStart(sourceFile)); |
| 19 | + |
| 20 | + const container = (isBlock(statement.parent) ? statement.parent : statement).parent; |
| 21 | + switch (container.kind) { |
| 22 | + case SyntaxKind.IfStatement: |
| 23 | + if ((container as IfStatement).elseStatement) { |
| 24 | + if (isBlock(statement.parent)) { |
| 25 | + changes.deleteNodeRange(sourceFile, first(statement.parent.statements), last(statement.parent.statements)); |
| 26 | + } |
| 27 | + else { |
| 28 | + changes.replaceNode(sourceFile, statement, createBlock(emptyArray)); |
| 29 | + } |
| 30 | + break; |
| 31 | + } |
| 32 | + // falls through |
| 33 | + case SyntaxKind.WhileStatement: |
| 34 | + case SyntaxKind.ForStatement: |
| 35 | + changes.deleteNode(sourceFile, container); |
| 36 | + break; |
| 37 | + default: |
| 38 | + if (isBlock(statement.parent)) { |
| 39 | + split(sliceAfter(statement.parent.statements, statement), shouldRemove, (start, end) => changes.deleteNodeRange(sourceFile, start, end)); |
| 40 | + } |
| 41 | + else { |
| 42 | + changes.deleteNode(sourceFile, statement); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function shouldRemove(s: Statement): boolean { |
| 48 | + // Don't remove statements that can validly be used before they appear. |
| 49 | + return !isFunctionDeclaration(s) && !isPurelyTypeDeclaration(s) && |
| 50 | + // `var x;` may declare a variable used above |
| 51 | + !(isVariableStatement(s) && !(getCombinedNodeFlags(s) & (NodeFlags.Let | NodeFlags.Const)) && s.declarationList.declarations.some(d => !d.initializer)); |
| 52 | + } |
| 53 | + |
| 54 | + function isPurelyTypeDeclaration(s: Statement): boolean { |
| 55 | + switch (s.kind) { |
| 56 | + case SyntaxKind.InterfaceDeclaration: |
| 57 | + case SyntaxKind.TypeAliasDeclaration: |
| 58 | + return true; |
| 59 | + case SyntaxKind.ModuleDeclaration: |
| 60 | + return getModuleInstanceState(s as ModuleDeclaration) !== ModuleInstanceState.Instantiated; |
| 61 | + case SyntaxKind.EnumDeclaration: |
| 62 | + return hasModifier(s, ModifierFlags.Const); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + function sliceAfter<T>(arr: ReadonlyArray<T>, value: T): ReadonlyArray<T> { |
| 67 | + const index = arr.indexOf(value); |
| 68 | + Debug.assert(index !== -1); |
| 69 | + return arr.slice(index); |
| 70 | + } |
| 71 | + |
| 72 | + // Calls 'cb' with the start and end of each range where 'pred' is true. |
| 73 | + function split<T>(arr: ReadonlyArray<T>, pred: (t: T) => boolean, cb: (start: T, end: T) => void): void { |
| 74 | + let start: T | undefined; |
| 75 | + for (let i = 0; i < arr.length; i++) { |
| 76 | + const value = arr[i]; |
| 77 | + if (pred(value)) { |
| 78 | + start = start || value; |
| 79 | + } |
| 80 | + else { |
| 81 | + if (start) { |
| 82 | + cb(start, arr[i - 1]); |
| 83 | + start = undefined; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + if (start) cb(start, arr[arr.length - 1]); |
| 88 | + } |
| 89 | +} |
0 commit comments