Skip to content

Commit

Permalink
fix(43957): insert Override keyword after static modifier (microsoft#…
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored May 5, 2021
1 parent 2ef2718 commit 6f8c3b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/services/codefixes/fixOverrideModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ namespace ts.codefix {

function doAddOverrideModifierChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const classElement = findContainerClassElement(sourceFile, pos);
const accessibilityModifier = find(classElement.modifiers || emptyArray, m => isAccessibilityModifier(m.kind));
const modifierPos = accessibilityModifier ? accessibilityModifier.end :
const modifiers = classElement.modifiers || emptyArray;
const staticModifier = find(modifiers, isStaticModifier);
const accessibilityModifier = find(modifiers, m => isAccessibilityModifier(m.kind));
const modifierPos = staticModifier ? staticModifier.end :
accessibilityModifier ? accessibilityModifier.end :
classElement.decorators ? skipTrivia(sourceFile.text, classElement.decorators.end) : classElement.getStart(sourceFile);
const options = accessibilityModifier ? { prefix: " " } : { suffix: " " };
const options = accessibilityModifier || staticModifier ? { prefix: " " } : { suffix: " " };
changeTracker.insertModifierAt(sourceFile, modifierPos, SyntaxKind.OverrideKeyword, options);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixOverrideModifier19.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true

////class Foo {
//// protected static readonly p = '';
////}
////
////export class Bar extends Foo {
//// [|protected static readonly p = ''|];
////}

verify.codeFix({
description: "Add 'override' modifier",
newRangeContent: "protected static override readonly p = ''",
index: 0
})
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixOverrideModifier20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true

////class Foo {
//// static readonly p = '';
////}
////
////export class Bar extends Foo {
//// [|static readonly p = ''|];
////}

verify.codeFix({
description: "Add 'override' modifier",
newRangeContent: "static override readonly p = ''",
index: 0
})

0 comments on commit 6f8c3b0

Please sign in to comment.