From e5eccf0a22104f2242cebbf636e6ad2336d328f1 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 30 Sep 2017 00:58:09 -0700 Subject: [PATCH 1/2] Added test. --- .../codeFixAddForgottenDecoratorCall01.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts diff --git a/tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts b/tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts new file mode 100644 index 0000000000000..17ae7f626f069 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts @@ -0,0 +1,14 @@ +/// + +////declare function foo(): (...args: any[]) => void; +////class C { +//// [|@foo|] +//// bar() { +//// +//// } +////} + +verify.codeFix({ + description: "Call decorator expression.", + newRangeContent: `@foo()` +}); From ea2021dd3e49a016dac58de326601e24c02f8a09 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 30 Sep 2017 00:58:46 -0700 Subject: [PATCH 2/2] Create fix for uninvoked decorators. --- src/compiler/diagnosticMessages.json | 4 ++++ .../addMissingInvocationForDecorator.ts | 20 +++++++++++++++++++ src/services/codefixes/fixes.ts | 1 + 3 files changed, 25 insertions(+) create mode 100644 src/services/codefixes/addMissingInvocationForDecorator.ts diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 17c2366082035..f3d6d4fcc4706 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3709,6 +3709,10 @@ "category": "Message", "code": 90027 }, + "Call decorator expression.": { + "category": "Message", + "code": 90028 + }, "Convert function to an ES2015 class": { "category": "Message", diff --git a/src/services/codefixes/addMissingInvocationForDecorator.ts b/src/services/codefixes/addMissingInvocationForDecorator.ts new file mode 100644 index 0000000000000..7f17aab6db2a8 --- /dev/null +++ b/src/services/codefixes/addMissingInvocationForDecorator.ts @@ -0,0 +1,20 @@ +/* @internal */ +namespace ts.codefix { + registerCodeFix({ + errorCodes: [Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0.code], + getCodeActions: (context: CodeFixContext) => { + const sourceFile = context.sourceFile; + const token = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false); + const decorator = getAncestor(token, SyntaxKind.Decorator) as Decorator; + Debug.assert(!!decorator, "Expected position to be owned by a decorator."); + const replacement = createCall(decorator.expression, /*typeArguments*/ undefined, /*argumentsArray*/ undefined); + const changeTracker = textChanges.ChangeTracker.fromContext(context); + changeTracker.replaceNode(sourceFile, decorator.expression, replacement); + + return [{ + description: getLocaleSpecificMessage(Diagnostics.Call_decorator_expression), + changes: changeTracker.getChanges() + }]; + } + }); +} diff --git a/src/services/codefixes/fixes.ts b/src/services/codefixes/fixes.ts index 9bc80cad69117..b024dfae7cd82 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -1,3 +1,4 @@ +/// /// /// ///