diff --git a/Jakefile.js b/Jakefile.js index 763a204dc9c..37227ced4b5 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -173,7 +173,8 @@ var servicesSources = [ "codeFixes/fixClassIncorrectlyImplementsInterface.ts", "codeFixes/fixClassDoesntImplementInheritedAbstractMember.ts", "codeFixes/fixClassSuperMustPrecedeThisAccess.ts", - "codeFixes/fixConstructorForDerivedNeedSuperCall.ts" + "codeFixes/fixConstructorForDerivedNeedSuperCall.ts", + "codeFixes/fixRemoveAbstractModifierInNonAbstractClass.ts" ].map(function (f) { return path.join(servicesDirectory, f); })); diff --git a/src/services/codefixes/fixRemoveAbstractModifierInNonAbstractClass.ts b/src/services/codefixes/fixRemoveAbstractModifierInNonAbstractClass.ts new file mode 100644 index 00000000000..4fdd58c31a5 --- /dev/null +++ b/src/services/codefixes/fixRemoveAbstractModifierInNonAbstractClass.ts @@ -0,0 +1,42 @@ +/* @internal */ +namespace ts.codefix { + registerCodeFix({ + errorCodes: [Diagnostics.Abstract_methods_can_only_appear_within_an_abstract_class.code], + getCodeActions: (context: CodeFixContext) => { + const sourceFile = context.sourceFile; + const start = context.span.start; + const token = getTokenAtPosition(sourceFile, start); + + Debug.assert(token.kind === SyntaxKind.AbstractKeyword); + + const propertyDeclaration = token.parent; + const classDeclaration = propertyDeclaration.parent; + const classKeywordStart = classDeclaration.getChildren()[0].getStart(); + + let codeFix: CodeAction[] = [ + { + description: `Remove abstract modifier from ${propertyDeclaration.name.getText()}.`, + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: {start: start, length: context.span.length + /*space*/ 1}, + newText: "" + }] + }] + }, + { + description: `Make class ${classDeclaration.name.getText()} abstract.`, + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: classKeywordStart, length: 0 }, + newText: "abstract " + }] + }] + } + ]; + + return codeFix; + } + }); +} diff --git a/src/services/codefixes/fixes.ts b/src/services/codefixes/fixes.ts index 7df58b03f81..982b6abc84b 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -2,4 +2,5 @@ /// /// /// -/// \ No newline at end of file +/// +/// \ No newline at end of file