mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Merge pull request #38754 from a-tarasyuk/feat/25259
feat(25259): Better error report for equals instead of colon in object literals
This commit is contained in:
28
src/services/codefixes/fixPropertyAssignment.ts
Normal file
28
src/services/codefixes/fixPropertyAssignment.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
const fixId = "fixPropertyAssignment";
|
||||
const errorCodes = [
|
||||
Diagnostics.Did_you_mean_to_use_a_Colon_When_following_property_names_in_an_object_literal_implies_a_destructuring_assignment.code
|
||||
];
|
||||
|
||||
registerCodeFix({
|
||||
errorCodes,
|
||||
fixIds: [fixId],
|
||||
getCodeActions(context) {
|
||||
const { sourceFile, span } = context;
|
||||
const property = getProperty(sourceFile, span.start);
|
||||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
|
||||
return [createCodeFixAction(fixId, changes, [Diagnostics.Change_0_to_1, "=", ":"], fixId, [Diagnostics.Switch_each_misused_0_to_1, "=", ":"])];
|
||||
},
|
||||
getAllCodeActions: context =>
|
||||
codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getProperty(diag.file, diag.start)))
|
||||
});
|
||||
|
||||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: ShorthandPropertyAssignment): void {
|
||||
changes.replaceNode(sourceFile, node, factory.createPropertyAssignment(node.name, node.objectAssignmentInitializer as Expression));
|
||||
}
|
||||
|
||||
function getProperty(sourceFile: SourceFile, pos: number): ShorthandPropertyAssignment {
|
||||
return cast(getTokenAtPosition(sourceFile, pos).parent, isShorthandPropertyAssignment);
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,7 @@
|
||||
"codefixes/fixEnableExperimentalDecorators.ts",
|
||||
"codefixes/fixEnableJsxFlag.ts",
|
||||
"codefixes/fixModuleAndTargetOptions.ts",
|
||||
"codefixes/fixPropertyAssignment.ts",
|
||||
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
|
||||
"codefixes/fixForgottenThisPropertyAccess.ts",
|
||||
"codefixes/fixInvalidJsxCharacters.ts",
|
||||
|
||||
Reference in New Issue
Block a user