Skip to content

Commit 28658de

Browse files
Merge pull request #18969 from Microsoft/uncalledDecoratorQuickFix
Quick fix for potentially uncalled decorators
2 parents 9244604 + ea2021d commit 28658de

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3709,6 +3709,10 @@
37093709
"category": "Message",
37103710
"code": 90027
37113711
},
3712+
"Call decorator expression.": {
3713+
"category": "Message",
3714+
"code": 90028
3715+
},
37123716

37133717
"Convert function to an ES2015 class": {
37143718
"category": "Message",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
registerCodeFix({
4+
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],
5+
getCodeActions: (context: CodeFixContext) => {
6+
const sourceFile = context.sourceFile;
7+
const token = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
8+
const decorator = getAncestor(token, SyntaxKind.Decorator) as Decorator;
9+
Debug.assert(!!decorator, "Expected position to be owned by a decorator.");
10+
const replacement = createCall(decorator.expression, /*typeArguments*/ undefined, /*argumentsArray*/ undefined);
11+
const changeTracker = textChanges.ChangeTracker.fromContext(context);
12+
changeTracker.replaceNode(sourceFile, decorator.expression, replacement);
13+
14+
return [{
15+
description: getLocaleSpecificMessage(Diagnostics.Call_decorator_expression),
16+
changes: changeTracker.getChanges()
17+
}];
18+
}
19+
});
20+
}

src/services/codefixes/fixes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference path="addMissingInvocationForDecorator.ts" />
12
/// <reference path="correctQualifiedNameToIndexedAccessType.ts" />
23
/// <reference path="fixClassIncorrectlyImplementsInterface.ts" />
34
/// <reference path="fixAddMissingMember.ts" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////declare function foo(): (...args: any[]) => void;
4+
////class C {
5+
//// [|@foo|]
6+
//// bar() {
7+
////
8+
//// }
9+
////}
10+
11+
verify.codeFix({
12+
description: "Call decorator expression.",
13+
newRangeContent: `@foo()`
14+
});

0 commit comments

Comments
 (0)