Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix for potentially uncalled decorators #18969

Merged
merged 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3709,6 +3709,10 @@
"category": "Message",
"code": 90027
},
"Call decorator expression.": {
"category": "Message",
"code": 90028
},

"Convert function to an ES2015 class": {
"category": "Message",
Expand Down
20 changes: 20 additions & 0 deletions src/services/codefixes/addMissingInvocationForDecorator.ts
Original file line number Diff line number Diff line change
@@ -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()
}];
}
});
}
1 change: 1 addition & 0 deletions src/services/codefixes/fixes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="addMissingInvocationForDecorator.ts" />
/// <reference path="correctQualifiedNameToIndexedAccessType.ts" />
/// <reference path="fixClassIncorrectlyImplementsInterface.ts" />
/// <reference path="fixAddMissingMember.ts" />
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixAddForgottenDecoratorCall01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////declare function foo(): (...args: any[]) => void;
////class C {
//// [|@foo|]
//// bar() {
////
//// }
////}

verify.codeFix({
description: "Call decorator expression.",
newRangeContent: `@foo()`
});