Skip to content

Commit 6c0ee23

Browse files
committed
feat(25259): add better error to report for equals instead of colon in object literals
1 parent 3e0d58f commit 6c0ee23

10 files changed

+119
-6
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37526,7 +37526,7 @@ namespace ts {
3752637526
if (prop.kind === SyntaxKind.ShorthandPropertyAssignment && !inDestructuring && prop.objectAssignmentInitializer) {
3752737527
// having objectAssignmentInitializer is only valid in ObjectAssignmentPattern
3752837528
// outside of destructuring it is a syntax error
37529-
return grammarErrorOnNode(prop.equalsToken!, Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment);
37529+
return grammarErrorOnNode(prop.equalsToken!, Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment_Did_you_mean_to_use_a_Colon);
3753037530
}
3753137531

3753237532
if (name.kind === SyntaxKind.PrivateIdentifier) {

src/compiler/diagnosticMessages.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@
875875
"category": "Error",
876876
"code": 1308
877877
},
878-
"'=' can only be used in an object literal property inside a destructuring assignment.": {
878+
"'=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?": {
879879
"category": "Error",
880880
"code": 1312
881881
},
@@ -5705,6 +5705,10 @@
57055705
"category": "Message",
57065706
"code": 95118
57075707
},
5708+
"Change all '{0}' to '{1}'": {
5709+
"category": "Message",
5710+
"code": 95119
5711+
},
57085712

57095713
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
57105714
"category": "Error",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixId = "fixPropertyAssignment";
4+
const errorCodes = [
5+
Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment_Did_you_mean_to_use_a_Colon.code
6+
];
7+
8+
registerCodeFix({
9+
errorCodes,
10+
fixIds: [fixId],
11+
getCodeActions(context) {
12+
const { sourceFile, span } = context;
13+
const property = getProperty(sourceFile, span.start);
14+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
15+
return [createCodeFixAction(fixId, changes, [Diagnostics.Change_0_to_1, "=", ":"], fixId, [Diagnostics.Change_all_0_to_1, "=", ":"])];
16+
},
17+
getAllCodeActions: context =>
18+
codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getProperty(diag.file, diag.start)))
19+
});
20+
21+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: ShorthandPropertyAssignment): void {
22+
changes.replaceNode(sourceFile, node, createPropertyAssignment(node.name, node.objectAssignmentInitializer as Expression));
23+
}
24+
25+
function getProperty(sourceFile: SourceFile, pos: number): ShorthandPropertyAssignment {
26+
return cast(getTokenAtPosition(sourceFile, pos).parent, isShorthandPropertyAssignment);
27+
}
28+
}

src/services/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"codefixes/fixEnableExperimentalDecorators.ts",
7777
"codefixes/fixEnableJsxFlag.ts",
7878
"codefixes/fixModuleAndTargetOptions.ts",
79+
"codefixes/fixPropertyAssignment.ts",
7980
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
8081
"codefixes/fixForgottenThisPropertyAccess.ts",
8182
"codefixes/fixInvalidJsxCharacters.ts",

tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(85,19): erro
1414
Type 'number' is not assignable to type 'string'.
1515
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(85,26): error TS2322: Type 'number' is not assignable to type 'string'.
1616
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,12): error TS18004: No value exists in scope for the shorthand property 's'. Either declare one or provide an initializer.
17-
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
17+
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?
1818

1919

2020
==== tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts (15 errors) ====
@@ -162,7 +162,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,14): err
162162
~
163163
!!! error TS18004: No value exists in scope for the shorthand property 's'. Either declare one or provide an initializer.
164164
~
165-
!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
165+
!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?
166166
});
167167

168168
function foo({a = 4, b = { x: 5 }}) {

tests/baselines/reference/shorthandPropertyAssignmentsInDestructuring_ES6.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(85,19):
1414
Type 'number' is not assignable to type 'string'.
1515
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(85,26): error TS2322: Type 'number' is not assignable to type 'string'.
1616
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,12): error TS18004: No value exists in scope for the shorthand property 's'. Either declare one or provide an initializer.
17-
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
17+
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?
1818

1919

2020
==== tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts (15 errors) ====
@@ -162,7 +162,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,14):
162162
~
163163
!!! error TS18004: No value exists in scope for the shorthand property 's'. Either declare one or provide an initializer.
164164
~
165-
!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
165+
!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?
166166
});
167167

168168
function foo({a = 4, b = { x: 5 }}) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////const a = {
4+
//// x/**/= 1
5+
////}
6+
7+
verify.codeFix({
8+
description: [ts.Diagnostics.Change_0_to_1.message, "=", ":"],
9+
index: 0,
10+
newFileContent:
11+
`const a = {
12+
x: 1
13+
}`,
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////const a = {
4+
//// x /**/= 1
5+
////}
6+
7+
verify.codeFix({
8+
description: [ts.Diagnostics.Change_0_to_1.message, "=", ":"],
9+
index: 0,
10+
newFileContent:
11+
`const a = {
12+
x: 1
13+
}`,
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////const a = {
4+
//// x: 1,
5+
//// y /**/= 1,
6+
//// z: 1
7+
////}
8+
9+
verify.codeFix({
10+
description: [ts.Diagnostics.Change_0_to_1.message, "=", ":"],
11+
index: 0,
12+
newFileContent:
13+
`const a = {
14+
x: 1,
15+
y: 1,
16+
z: 1
17+
}`,
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////const a = {
4+
//// x: 1,
5+
//// y = 1,
6+
//// z: 1
7+
////}
8+
////const b = {
9+
//// x = 1,
10+
//// y: 1
11+
////}
12+
////const c = {
13+
//// x: 1,
14+
//// y = 1
15+
////}
16+
17+
verify.codeFixAll({
18+
fixAllDescription: "Change all '=' to ':'",
19+
fixId: "fixPropertyAssignment",
20+
newFileContent:
21+
`const a = {
22+
x: 1,
23+
y: 1,
24+
z: 1
25+
}
26+
const b = {
27+
x: 1,
28+
y: 1
29+
}
30+
const c = {
31+
x: 1,
32+
y: 1
33+
}`
34+
});

0 commit comments

Comments
 (0)