Skip to content

Commit 6427711

Browse files
Better erreor messages for properties mis-handled as shorthand property declarations (microsoft#31039)
Better erreor messages for properties mis-handled as shorthand property declarations
2 parents 5508d65 + 0d4913d commit 6427711

19 files changed

+79
-45
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,7 @@ namespace ts {
22402240
const namespaceMeaning = SymbolFlags.Namespace | (isInJSFile(name) ? meaning & SymbolFlags.Value : 0);
22412241
let symbol: Symbol | undefined;
22422242
if (name.kind === SyntaxKind.Identifier) {
2243-
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : getCannotFindNameDiagnosticForName(getFirstIdentifier(name).escapedText);
2243+
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : getCannotFindNameDiagnosticForName(getFirstIdentifier(name));
22442244
const symbolFromJSPrototype = isInJSFile(name) ? resolveEntityNameFromAssignmentDeclaration(name, meaning) : undefined;
22452245
symbol = resolveName(location || name, name.escapedText, meaning, ignoreErrors || symbolFromJSPrototype ? undefined : message, name, /*isUse*/ true);
22462246
if (!symbol) {
@@ -15271,8 +15271,8 @@ namespace ts {
1527115271

1527215272
// EXPRESSION TYPE CHECKING
1527315273

15274-
function getCannotFindNameDiagnosticForName(name: __String): DiagnosticMessage {
15275-
switch (name) {
15274+
function getCannotFindNameDiagnosticForName(node: Identifier): DiagnosticMessage {
15275+
switch (node.escapedText) {
1527615276
case "document":
1527715277
case "console":
1527815278
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom;
@@ -15303,7 +15303,13 @@ namespace ts {
1530315303
case "Iterator":
1530415304
case "AsyncIterator":
1530515305
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later;
15306-
default: return Diagnostics.Cannot_find_name_0;
15306+
default:
15307+
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
15308+
return Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;
15309+
}
15310+
else {
15311+
return Diagnostics.Cannot_find_name_0;
15312+
}
1530715313
}
1530815314
}
1530915315

@@ -15315,7 +15321,7 @@ namespace ts {
1531515321
node,
1531615322
node.escapedText,
1531715323
SymbolFlags.Value | SymbolFlags.ExportValue,
15318-
getCannotFindNameDiagnosticForName(node.escapedText),
15324+
getCannotFindNameDiagnosticForName(node),
1531915325
node,
1532015326
!isWriteOnlyAccess(node),
1532115327
/*excludeGlobals*/ false,

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,5 +4950,9 @@
49504950
"Allow accessing UMD globals from modules.": {
49514951
"category": "Message",
49524952
"code": 95076
4953+
},
4954+
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer." :{
4955+
"category": "Error",
4956+
"code": 18004
49534957
}
49544958
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/bug25434.js(4,9): error TS2304: Cannot find name 'b'.
1+
tests/cases/compiler/bug25434.js(4,9): error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
22

33

44
==== tests/cases/compiler/bug25434.js (1 errors) ====
@@ -7,5 +7,5 @@ tests/cases/compiler/bug25434.js(4,9): error TS2304: Cannot find name 'b'.
77

88
Test(({ b = '5' } = {}));
99
~
10-
!!! error TS2304: Cannot find name 'b'.
10+
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
1111

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS2304: Cannot find name 'aa'.
1+
tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS18004: No value exists in scope for the shorthand property 'aa'. Either declare one or provide an initializer.
22
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ',' expected.
33

44

55
==== tests/cases/compiler/incompleteObjectLiteral1.ts (2 errors) ====
66
var tt = { aa; }
77
~~
8-
!!! error TS2304: Cannot find name 'aa'.
8+
!!! error TS18004: No value exists in scope for the shorthand property 'aa'. Either declare one or provide an initializer.
99
~
1010
!!! error TS1005: ',' expected.
1111
var x = tt;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'.
1+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts(3,5): error TS18004: No value exists in scope for the shorthand property 'undefinedVariable'. Either declare one or provide an initializer.
22

33

44
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts (1 errors) ====
55
var x = {
66
x, // OK
77
undefinedVariable // Error
88
~~~~~~~~~~~~~~~~~
9-
!!! error TS2304: Cannot find name 'undefinedVariable'.
9+
!!! error TS18004: No value exists in scope for the shorthand property 'undefinedVariable'. Either declare one or provide an initializer.
1010
}
1111

tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
77
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(9,8): error TS1005: ':' expected.
88
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(10,10): error TS1005: ':' expected.
99
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(12,1): error TS1005: ':' expected.
10-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS2304: Cannot find name 'a'.
10+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1111
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ',' expected.
12-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2304: Cannot find name 'a'.
12+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1313
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected.
1414
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,12): error TS1005: ':' expected.
15-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,5): error TS2304: Cannot find name 'a'.
15+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,5): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1616
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: ',' expected.
1717
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,9): error TS1005: ':' expected.
1818
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(20,17): error TS1005: ':' expected.
@@ -53,19 +53,19 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
5353
var x = {
5454
a.b,
5555
~
56-
!!! error TS2304: Cannot find name 'a'.
56+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
5757
~
5858
!!! error TS1005: ',' expected.
5959
a["ss"],
6060
~
61-
!!! error TS2304: Cannot find name 'a'.
61+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
6262
~
6363
!!! error TS1005: ',' expected.
6464
~
6565
!!! error TS1005: ':' expected.
6666
a[1],
6767
~
68-
!!! error TS2304: Cannot find name 'a'.
68+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
6969
~
7070
!!! error TS1005: ',' expected.
7171
~
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,11): error TS2304: Cannot find name 'a'.
1+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,11): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
22
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,12): error TS1005: ',' expected.
3-
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,14): error TS2304: Cannot find name 'b'.
3+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,14): error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
44
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,15): error TS1005: ',' expected.
5-
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,17): error TS2304: Cannot find name 'c'.
5+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,17): error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
66

77

88
==== tests/cases/compiler/objectLiteralWithSemicolons1.ts (5 errors) ====
99
var v = { a; b; c }
1010
~
11-
!!! error TS2304: Cannot find name 'a'.
11+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1212
~
1313
!!! error TS1005: ',' expected.
1414
~
15-
!!! error TS2304: Cannot find name 'b'.
15+
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
1616
~
1717
!!! error TS1005: ',' expected.
1818
~
19-
!!! error TS2304: Cannot find name 'c'.
19+
!!! error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,3): error TS2304: Cannot find name 'a'.
1+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,3): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
22
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,4): error TS1005: ',' expected.
3-
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,3): error TS2304: Cannot find name 'b'.
3+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,3): error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
44
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,4): error TS1005: ',' expected.
5-
tests/cases/compiler/objectLiteralWithSemicolons2.ts(4,3): error TS2304: Cannot find name 'c'.
5+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(4,3): error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
66

77

88
==== tests/cases/compiler/objectLiteralWithSemicolons2.ts (5 errors) ====
99
var v = {
1010
a;
1111
~
12-
!!! error TS2304: Cannot find name 'a'.
12+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1313
~
1414
!!! error TS1005: ',' expected.
1515
b;
1616
~
17-
!!! error TS2304: Cannot find name 'b'.
17+
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
1818
~
1919
!!! error TS1005: ',' expected.
2020
c
2121
~
22-
!!! error TS2304: Cannot find name 'c'.
22+
!!! error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
2323
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,3): error TS2304: Cannot find name 'a'.
1+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,3): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
22
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,4): error TS1005: ',' expected.
3-
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,3): error TS2304: Cannot find name 'b'.
3+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,3): error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
44
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,4): error TS1005: ',' expected.
5-
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,3): error TS2304: Cannot find name 'c'.
5+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,3): error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
66
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ',' expected.
77

88

99
==== tests/cases/compiler/objectLiteralWithSemicolons3.ts (6 errors) ====
1010
var v = {
1111
a;
1212
~
13-
!!! error TS2304: Cannot find name 'a'.
13+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1414
~
1515
!!! error TS1005: ',' expected.
1616
b;
1717
~
18-
!!! error TS2304: Cannot find name 'b'.
18+
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
1919
~
2020
!!! error TS1005: ',' expected.
2121
c;
2222
~
23-
!!! error TS2304: Cannot find name 'c'.
23+
!!! error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
2424
~
2525
!!! error TS1005: ',' expected.
2626
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons4.ts(2,3): error TS2304: Cannot find name 'a'.
1+
tests/cases/compiler/objectLiteralWithSemicolons4.ts(2,3): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
22
tests/cases/compiler/objectLiteralWithSemicolons4.ts(3,1): error TS1005: ',' expected.
33

44

55
==== tests/cases/compiler/objectLiteralWithSemicolons4.ts (2 errors) ====
66
var v = {
77
a
88
~
9-
!!! error TS2304: Cannot find name 'a'.
9+
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1010
;
1111
~
1212
!!! error TS1005: ',' expected.

0 commit comments

Comments
 (0)