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:
Daniel Rosenwasser
2020-06-30 01:56:26 -07:00
committed by GitHub
12 changed files with 129 additions and 19 deletions

View 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);
}
}

View File

@@ -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",