Better erreor messages for properties mis-handled as shorthand property declarations (#31039)

Better erreor messages for properties mis-handled as shorthand property declarations
This commit is contained in:
Daniel Rosenwasser
2019-04-22 16:14:21 -04:00
committed by GitHub
19 changed files with 79 additions and 45 deletions

View File

@@ -2240,7 +2240,7 @@ namespace ts {
const namespaceMeaning = SymbolFlags.Namespace | (isInJSFile(name) ? meaning & SymbolFlags.Value : 0);
let symbol: Symbol | undefined;
if (name.kind === SyntaxKind.Identifier) {
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : getCannotFindNameDiagnosticForName(getFirstIdentifier(name).escapedText);
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : getCannotFindNameDiagnosticForName(getFirstIdentifier(name));
const symbolFromJSPrototype = isInJSFile(name) ? resolveEntityNameFromAssignmentDeclaration(name, meaning) : undefined;
symbol = resolveName(location || name, name.escapedText, meaning, ignoreErrors || symbolFromJSPrototype ? undefined : message, name, /*isUse*/ true);
if (!symbol) {
@@ -15271,8 +15271,8 @@ namespace ts {
// EXPRESSION TYPE CHECKING
function getCannotFindNameDiagnosticForName(name: __String): DiagnosticMessage {
switch (name) {
function getCannotFindNameDiagnosticForName(node: Identifier): DiagnosticMessage {
switch (node.escapedText) {
case "document":
case "console":
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 {
case "Iterator":
case "AsyncIterator":
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later;
default: return Diagnostics.Cannot_find_name_0;
default:
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
return Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;
}
else {
return Diagnostics.Cannot_find_name_0;
}
}
}
@@ -15315,7 +15321,7 @@ namespace ts {
node,
node.escapedText,
SymbolFlags.Value | SymbolFlags.ExportValue,
getCannotFindNameDiagnosticForName(node.escapedText),
getCannotFindNameDiagnosticForName(node),
node,
!isWriteOnlyAccess(node),
/*excludeGlobals*/ false,

View File

@@ -4950,5 +4950,9 @@
"Allow accessing UMD globals from modules.": {
"category": "Message",
"code": 95076
},
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer." :{
"category": "Error",
"code": 18004
}
}

View File

@@ -1,4 +1,4 @@
tests/cases/compiler/bug25434.js(4,9): error TS2304: Cannot find name 'b'.
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.
==== tests/cases/compiler/bug25434.js (1 errors) ====
@@ -7,5 +7,5 @@ tests/cases/compiler/bug25434.js(4,9): error TS2304: Cannot find name 'b'.
Test(({ b = '5' } = {}));
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.

View File

@@ -1,11 +1,11 @@
tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS2304: Cannot find name 'aa'.
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.
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ',' expected.
==== tests/cases/compiler/incompleteObjectLiteral1.ts (2 errors) ====
var tt = { aa; }
~~
!!! error TS2304: Cannot find name 'aa'.
!!! error TS18004: No value exists in scope for the shorthand property 'aa'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
var x = tt;

View File

@@ -1,4 +1,4 @@
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'.
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.
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts (1 errors) ====
@@ -6,6 +6,6 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
x, // OK
undefinedVariable // Error
~~~~~~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'undefinedVariable'.
!!! error TS18004: No value exists in scope for the shorthand property 'undefinedVariable'. Either declare one or provide an initializer.
}

View File

@@ -7,12 +7,12 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(9,8): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(10,10): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(12,1): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS2304: Cannot find name 'a'.
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.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2304: Cannot find name 'a'.
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.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,12): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,5): error TS2304: Cannot find name 'a'.
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.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,9): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(20,17): error TS1005: ':' expected.
@@ -53,19 +53,19 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
var x = {
a.b,
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
a["ss"],
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
~
!!! error TS1005: ':' expected.
a[1],
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
~

View File

@@ -1,19 +1,19 @@
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,11): error TS2304: Cannot find name 'a'.
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.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,12): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,14): error TS2304: Cannot find name 'b'.
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.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,15): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,17): error TS2304: Cannot find name 'c'.
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.
==== tests/cases/compiler/objectLiteralWithSemicolons1.ts (5 errors) ====
var v = { a; b; c }
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
~
!!! error TS2304: Cannot find name 'c'.
!!! error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.

View File

@@ -1,23 +1,23 @@
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,3): error TS2304: Cannot find name 'a'.
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.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,3): error TS2304: Cannot find name 'b'.
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.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(4,3): error TS2304: Cannot find name 'c'.
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.
==== tests/cases/compiler/objectLiteralWithSemicolons2.ts (5 errors) ====
var v = {
a;
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
b;
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
c
~
!!! error TS2304: Cannot find name 'c'.
!!! error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
}

View File

@@ -1,8 +1,8 @@
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,3): error TS2304: Cannot find name 'a'.
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.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,3): error TS2304: Cannot find name 'b'.
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.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,3): error TS2304: Cannot find name 'c'.
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.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ',' expected.
@@ -10,17 +10,17 @@ tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ',' exp
var v = {
a;
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
b;
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
c;
~
!!! error TS2304: Cannot find name 'c'.
!!! error TS18004: No value exists in scope for the shorthand property 'c'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.
}

View File

@@ -1,4 +1,4 @@
tests/cases/compiler/objectLiteralWithSemicolons4.ts(2,3): error TS2304: Cannot find name 'a'.
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.
tests/cases/compiler/objectLiteralWithSemicolons4.ts(3,1): error TS1005: ',' expected.
@@ -6,7 +6,7 @@ tests/cases/compiler/objectLiteralWithSemicolons4.ts(3,1): error TS1005: ',' exp
var v = {
a
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
;
~
!!! error TS1005: ',' expected.

View File

@@ -1,11 +1,11 @@
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,12): error TS2304: Cannot find name 'aa'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,12): error TS18004: No value exists in scope for the shorthand property 'aa'. Either declare one or provide an initializer.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,14): error TS1005: ',' expected.
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts (2 errors) ====
var tt = { aa; }
~~
!!! error TS2304: Cannot find name 'aa'.
!!! error TS18004: No value exists in scope for the shorthand property 'aa'. Either declare one or provide an initializer.
~
!!! error TS1005: ',' expected.

View File

@@ -1,4 +1,4 @@
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(1,11): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(1,11): error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(2,1): error TS1005: ',' expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(2,7): error TS1005: ':' expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(2,8): error TS1005: '}' expected.
@@ -7,7 +7,7 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts (4 errors) ====
var v = { a
~
!!! error TS2304: Cannot find name 'a'.
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
return;
~~~~~~
!!! error TS1005: ',' expected.

View File

@@ -13,7 +13,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(85,19): erro
Types of property 'x' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(85,26): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,12): error TS2304: Cannot find name 's'.
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.
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
@@ -160,7 +160,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring.ts(111,14): err
(function() {
let a = { s = 5 };
~
!!! error TS2304: Cannot find name 's'.
!!! error TS18004: No value exists in scope for the shorthand property 's'. Either declare one or provide an initializer.
~
!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
});

View File

@@ -13,7 +13,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(85,19):
Types of property 'x' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(85,26): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,12): error TS2304: Cannot find name 's'.
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.
tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,14): error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
@@ -160,7 +160,7 @@ tests/cases/compiler/shorthandPropertyAssignmentsInDestructuring_ES6.ts(111,14):
(function() {
let a = { s = 5 };
~
!!! error TS2304: Cannot find name 's'.
!!! error TS18004: No value exists in scope for the shorthand property 's'. Either declare one or provide an initializer.
~
!!! error TS1312: '=' can only be used in an object literal property inside a destructuring assignment.
});

View File

@@ -0,0 +1,7 @@
tests/cases/compiler/shorthandPropertyUndefined.ts(1,11): error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.
==== tests/cases/compiler/shorthandPropertyUndefined.ts (1 errors) ====
var a = { b };
~
!!! error TS18004: No value exists in scope for the shorthand property 'b'. Either declare one or provide an initializer.

View File

@@ -0,0 +1,5 @@
//// [shorthandPropertyUndefined.ts]
var a = { b };
//// [shorthandPropertyUndefined.js]
var a = { b: b };

View File

@@ -0,0 +1,5 @@
=== tests/cases/compiler/shorthandPropertyUndefined.ts ===
var a = { b };
>a : Symbol(a, Decl(shorthandPropertyUndefined.ts, 0, 3))
>b : Symbol(b, Decl(shorthandPropertyUndefined.ts, 0, 9))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/shorthandPropertyUndefined.ts ===
var a = { b };
>a : { b: any; }
>{ b } : { b: any; }
>b : any

View File

@@ -0,0 +1 @@
var a = { b };