mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 21:36:50 -05:00
Merge pull request #27019 from iliashkolyar/codefix_add_missing_new_operator
Codefix: add quick fix for missing 'new' operator
This commit is contained in:
@@ -4748,7 +4748,6 @@
|
||||
"category": "Message",
|
||||
"code": 95062
|
||||
},
|
||||
|
||||
"Add missing enum member '{0}'": {
|
||||
"category": "Message",
|
||||
"code": 95063
|
||||
@@ -4780,5 +4779,13 @@
|
||||
"Add 'unknown' to all conversions of non-overlapping types": {
|
||||
"category": "Message",
|
||||
"code": 95070
|
||||
},
|
||||
"Add missing 'new' operator to call": {
|
||||
"category": "Message",
|
||||
"code": 95071
|
||||
},
|
||||
"Add missing 'new' operator to all calls": {
|
||||
"category": "Message",
|
||||
"code": 95072
|
||||
}
|
||||
}
|
||||
|
||||
32
src/services/codefixes/fixAddMissingNewOperator.ts
Normal file
32
src/services/codefixes/fixAddMissingNewOperator.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
const fixId = "addMissingNewOperator";
|
||||
const errorCodes = [Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new.code];
|
||||
registerCodeFix({
|
||||
errorCodes,
|
||||
getCodeActions(context) {
|
||||
const { sourceFile, span } = context;
|
||||
const changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, span));
|
||||
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_call, fixId, Diagnostics.Add_missing_new_operator_to_all_calls)];
|
||||
},
|
||||
fixIds: [fixId],
|
||||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
|
||||
addMissingNewOperator(changes, context.sourceFile, diag)),
|
||||
});
|
||||
|
||||
function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, span: TextSpan): void {
|
||||
const call = cast(findAncestorMatchingSpan(sourceFile, span), isCallExpression);
|
||||
const newExpression = createNew(call.expression, call.typeArguments, call.arguments);
|
||||
|
||||
changes.replaceNode(sourceFile, call, newExpression);
|
||||
}
|
||||
|
||||
function findAncestorMatchingSpan(sourceFile: SourceFile, span: TextSpan): Node {
|
||||
let token = getTokenAtPosition(sourceFile, span.start);
|
||||
const end = textSpanEnd(span);
|
||||
while (token.end < end) {
|
||||
token = token.parent;
|
||||
}
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,7 @@
|
||||
"codefixes/importFixes.ts",
|
||||
"codefixes/fixSpelling.ts",
|
||||
"codefixes/fixAddMissingMember.ts",
|
||||
"codefixes/fixAddMissingNewOperator.ts",
|
||||
"codefixes/fixCannotFindModule.ts",
|
||||
"codefixes/fixClassDoesntImplementInheritedAbstractMember.ts",
|
||||
"codefixes/fixClassSuperMustPrecedeThisAccess.ts",
|
||||
|
||||
Reference in New Issue
Block a user