From 150e8d30d7de44ceef8228a4256c8a31c310c867 Mon Sep 17 00:00:00 2001 From: Yui T Date: Sun, 2 Nov 2014 19:52:08 -0800 Subject: [PATCH 01/13] Store scanner position before create PropertyDeclaration node --- src/compiler/parser.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 41243efa1717f..65fada8e134a3 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2668,9 +2668,11 @@ module ts { } function parsePropertyAssignment(): PropertyDeclaration { - var node = createNode(SyntaxKind.PropertyAssignment); - node.name = parsePropertyName(); + var nodePos = scanner.getStartPos(); + var propertyName = parsePropertyName(); if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) { + var node = createNode(SyntaxKind.PropertyAssignment, nodePos); + node.name = propertyName; var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false); var body = parseBody(/* ignoreMissingOpenBrace */ false); // do not propagate property name as name for function expression @@ -2679,8 +2681,11 @@ module ts { // var y = { x() { } } // otherwise this will bring y.x into the scope of x which is incorrect node.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); + return finishNode(node); } else { + var node = createNode(SyntaxKind.PropertyAssignment, nodePos); + node.name = propertyName; parseExpected(SyntaxKind.ColonToken); node.initializer = parseAssignmentExpression(false); } From 8a779e1e85f7867b7f1bd3086f11b97cf3b0a19d Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 4 Nov 2014 14:43:43 -0800 Subject: [PATCH 02/13] Basic typechecking and emitting for short hand property assignment Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/emitter.ts --- src/compiler/binder.ts | 1 + src/compiler/checker.ts | 9 +++++++- .../diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 5 +++++ src/compiler/emitter.ts | 12 +++++++++++ src/compiler/parser.ts | 21 +++++++++++++++++-- src/compiler/types.ts | 5 +++++ .../objectLiteralShorthandProperties.ts | 19 +++++++++++++++++ .../objectLiteralShorthandProperties2.ts | 18 ++++++++++++++++ .../objectLiteralShorthandProperties3.ts | 13 ++++++++++++ .../objectLiteralShorthandProperties4.ts | 4 ++++ 11 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 tests/cases/compiler/objectLiteralShorthandProperties.ts create mode 100644 tests/cases/compiler/objectLiteralShorthandProperties2.ts create mode 100644 tests/cases/compiler/objectLiteralShorthandProperties3.ts create mode 100644 tests/cases/compiler/objectLiteralShorthandProperties4.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index fe3009da3aa89..fe00830ae518a 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -350,6 +350,7 @@ module ts { break; case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShortHandPropertyAssignment: bindDeclaration(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false); break; case SyntaxKind.EnumMember: diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2567b54de4a0f..5bd2e1f8ec84f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4968,7 +4968,14 @@ module ts { if (hasProperty(members, id)) { var member = members[id]; if (member.flags & SymbolFlags.Property) { - var type = checkExpression((member.declarations[0]).initializer, contextualMapper); + var memberDecl = member.declarations[0]; + var type: Type; + if (memberDecl.kind === SyntaxKind.PropertyAssignment) { + type = checkExpression(memberDecl.initializer, contextualMapper); + } + else { + type = checkExpression(memberDecl.name, contextualMapper); + } var prop = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.name); prop.declarations = member.declarations; prop.parent = member.parent; diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 349601a6db8e5..19d5f0c5f6bb5 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -122,6 +122,7 @@ module ts { let_declarations_can_only_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "'let' declarations can only be declared inside a block." }, Invalid_template_literal_expected: { code: 1158, category: DiagnosticCategory.Error, key: "Invalid template literal; expected '}'" }, Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1159, category: DiagnosticCategory.Error, key: "Tagged templates are only available when targeting ECMAScript 6 and higher." }, + A_object_member_cannot_be_declared_optional: { code: 1159, category: DiagnosticCategory.Error, key: "A object member cannot be declared optional." }, Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7359abefb6048..52235a0848e81 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -480,6 +480,11 @@ "code": 1159 }, + "A object member cannot be declared optional.": { + "category": "Error", + "code": 1159 + }, + "Duplicate identifier '{0}'.": { "category": "Error", "code": 2300 diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f61d4403eece6..d23380b4fb1c0 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1033,6 +1033,16 @@ module ts { emitTrailingComments(node); } + function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) { + emitLeadingComments(node); + emit(node.name); + if (compilerOptions.target !== ScriptTarget.ES6) { + write(": "); + emit(node.name); + } + emitTrailingComments(node); + } + function tryEmitConstantValue(node: PropertyAccess | IndexedAccess): boolean { var constantValue = resolver.getConstantValue(node); if (constantValue !== undefined) { @@ -2250,6 +2260,8 @@ module ts { return emitObjectLiteral(node); case SyntaxKind.PropertyAssignment: return emitPropertyAssignment(node); + case SyntaxKind.ShortHandPropertyAssignment: + return emitShortHandPropertyAssignment(node); case SyntaxKind.PropertyAccess: return emitPropertyAccess(node); case SyntaxKind.IndexedAccess: diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 65fada8e134a3..75ff2993609c7 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -198,6 +198,7 @@ module ts { child((node).initializer); case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShortHandPropertyAssignment: return child((node).name) || child((node).type) || child((node).initializer); @@ -2669,9 +2670,11 @@ module ts { function parsePropertyAssignment(): PropertyDeclaration { var nodePos = scanner.getStartPos(); + var nameToken = token; var propertyName = parsePropertyName(); + var node: PropertyDeclaration; if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) { - var node = createNode(SyntaxKind.PropertyAssignment, nodePos); + node = createNode(SyntaxKind.PropertyAssignment, nodePos); node.name = propertyName; var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false); var body = parseBody(/* ignoreMissingOpenBrace */ false); @@ -2683,8 +2686,19 @@ module ts { node.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); return finishNode(node); } + if (token === SyntaxKind.QuestionToken) { + var questionStart = scanner.getTokenPos(); + errorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional); + nextToken(); + } + + // Parse to check if it is short-hand property assignment or normal property assignment + if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) { + node = createNode(SyntaxKind.ShortHandPropertyAssignment, nodePos); + node.name = propertyName; + } else { - var node = createNode(SyntaxKind.PropertyAssignment, nodePos); + node = createNode(SyntaxKind.PropertyAssignment, nodePos); node.name = propertyName; parseExpected(SyntaxKind.ColonToken); node.initializer = parseAssignmentExpression(false); @@ -2737,6 +2751,9 @@ module ts { if (p.kind === SyntaxKind.PropertyAssignment) { currentKind = Property; } + else if (p.kind === SyntaxKind.ShortHandPropertyAssignment) { + currentKind = Property; + } else if (p.kind === SyntaxKind.GetAccessor) { currentKind = GetAccessor; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 973c06d6e2dcd..51b93821d6983 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -165,6 +165,7 @@ module ts { ArrayLiteral, ObjectLiteral, PropertyAssignment, + ShortHandPropertyAssignment, PropertyAccess, IndexedAccess, CallExpression, @@ -329,6 +330,10 @@ module ts { initializer?: Expression; } + export interface ShortHandPropertyDeclaration extends PropertyDeclaration { + name: Identifier; + } + export interface ParameterDeclaration extends VariableDeclaration { } /** diff --git a/tests/cases/compiler/objectLiteralShorthandProperties.ts b/tests/cases/compiler/objectLiteralShorthandProperties.ts new file mode 100644 index 0000000000000..95b380308e9e5 --- /dev/null +++ b/tests/cases/compiler/objectLiteralShorthandProperties.ts @@ -0,0 +1,19 @@ +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + diff --git a/tests/cases/compiler/objectLiteralShorthandProperties2.ts b/tests/cases/compiler/objectLiteralShorthandProperties2.ts new file mode 100644 index 0000000000000..38081e7d69111 --- /dev/null +++ b/tests/cases/compiler/objectLiteralShorthandProperties2.ts @@ -0,0 +1,18 @@ +// errors +var y = { + "stringLiteral", + 42, + get e, + set f, + this, + super, + var, + class, + typeof +}; + +var x = { + a.b, + a["ss"], + a[1], +}; \ No newline at end of file diff --git a/tests/cases/compiler/objectLiteralShorthandProperties3.ts b/tests/cases/compiler/objectLiteralShorthandProperties3.ts new file mode 100644 index 0000000000000..005885bb9018f --- /dev/null +++ b/tests/cases/compiler/objectLiteralShorthandProperties3.ts @@ -0,0 +1,13 @@ +// module export + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} diff --git a/tests/cases/compiler/objectLiteralShorthandProperties4.ts b/tests/cases/compiler/objectLiteralShorthandProperties4.ts new file mode 100644 index 0000000000000..1f44dc18ae86a --- /dev/null +++ b/tests/cases/compiler/objectLiteralShorthandProperties4.ts @@ -0,0 +1,4 @@ +var x = { + x, // OK + undefinedVariable // Error +} From e9122b4d85f1f7546cd2e793203fd9504fdb006c Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 10 Nov 2014 10:51:08 -0800 Subject: [PATCH 03/13] Fix get type from short-hand property assignment --- src/compiler/checker.ts | 7 +++++++ src/compiler/parser.ts | 1 + src/services/services.ts | 5 +++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5bd2e1f8ec84f..36be5f8001e05 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1665,6 +1665,13 @@ module ts { } return type; } + + // If it is a short-hand property assignment; Use the type of the identifier + if (declaration.kind === SyntaxKind.ShortHandPropertyAssignment) { + var type = checkIdentifier((declaration.name)); + return type + } + // Rest parameters default to type any[], other parameters default to type any var type = declaration.flags & NodeFlags.Rest ? createArrayType(anyType) : anyType; checkImplicitAny(type); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 75ff2993609c7..2657fc83668b5 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -574,6 +574,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShortHandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.FunctionDeclaration: diff --git a/src/services/services.ts b/src/services/services.ts index d6465084fa49b..916c468ea3dce 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1969,7 +1969,7 @@ module ts { /** Returns true if node is a name of an object literal property, e.g. "a" in x = { "a": 1 } */ function isNameOfPropertyAssignment(node: Node): boolean { return (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) && - node.parent.kind === SyntaxKind.PropertyAssignment && (node.parent).name === node; + (node.parent.kind === SyntaxKind.PropertyAssignment || node.parent.kind === SyntaxKind.ShortHandPropertyAssignment) && (node.parent).name === node; } function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: Node): boolean { @@ -2648,7 +2648,7 @@ module ts { var existingMemberNames: Map = {}; forEach(existingMembers, m => { - if (m.kind !== SyntaxKind.PropertyAssignment) { + if (m.kind !== SyntaxKind.PropertyAssignment && m.kind !== SyntaxKind.ShortHandPropertyAssignment) { // Ignore omitted expressions for missing members in the object literal return; } @@ -4569,6 +4569,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShortHandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.Constructor: From 7e39622d5d8d552bb9c541819eb60a72eab2fde4 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 10 Nov 2014 14:55:41 -0800 Subject: [PATCH 04/13] Basic implementation for finding all references --- src/compiler/checker.ts | 7 ++++++- src/compiler/types.ts | 1 + src/services/services.ts | 3 +++ .../reference/incompleteObjectLiteral1.errors.txt | 9 ++++++--- .../objectTypesWithOptionalProperties.errors.txt | 11 ++++------- tests/baselines/reference/parser512097.errors.txt | 9 ++++++--- .../parserErrorRecovery_ObjectLiteral2.errors.txt | 9 ++++++--- 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 36be5f8001e05..4ebcb925a7eda 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -110,7 +110,8 @@ module ts { getAliasedSymbol: resolveImport, isUndefinedSymbol: symbol => symbol === undefinedSymbol, isArgumentsSymbol: symbol => symbol === argumentsSymbol, - hasEarlyErrors: hasEarlyErrors + hasEarlyErrors: hasEarlyErrors, + resolveEntityNameForShortHandPropertyAssignment: resolveEntityNameForShortHandPropertyAssignment, }; var undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined"); @@ -537,6 +538,10 @@ module ts { return symbol.flags & meaning ? symbol : resolveImport(symbol); } + function resolveEntityNameForShortHandPropertyAssignment(location: Node): Symbol { + return resolveEntityName(location, location, SymbolFlags.Value); + } + function isExternalModuleNameRelative(moduleName: string): boolean { // TypeScript 1.0 spec (April 2014): 11.2.1 // An external module name is "relative" if the first term is "." or "..". diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 51b93821d6983..8e392de8ff562 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -733,6 +733,7 @@ module ts { getEnumMemberValue(node: EnumMember): number; isValidPropertyAccess(node: PropertyAccess, propertyName: string): boolean; getAliasedSymbol(symbol: Symbol): Symbol; + resolveEntityNameForShortHandPropertyAssignment(location: Node): Symbol; } export interface SymbolDisplayBuilder { diff --git a/src/services/services.ts b/src/services/services.ts index 916c468ea3dce..a85824f873b77 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4227,6 +4227,9 @@ module ts { forEach(getPropertySymbolsFromContextualType(location), contextualSymbol => { result.push.apply(result, typeInfoResolver.getRootSymbols(contextualSymbol)); }); + if (location.kind === SyntaxKind.Identifier && location.parent.kind === SyntaxKind.ShortHandPropertyAssignment) { + result.push(typeInfoResolver.resolveEntityNameForShortHandPropertyAssignment(location)); + } } // If this is a union property, add all the symbols from all its source symbols in all unioned types. diff --git a/tests/baselines/reference/incompleteObjectLiteral1.errors.txt b/tests/baselines/reference/incompleteObjectLiteral1.errors.txt index 2e363de591eda..c37f308960a67 100644 --- a/tests/baselines/reference/incompleteObjectLiteral1.errors.txt +++ b/tests/baselines/reference/incompleteObjectLiteral1.errors.txt @@ -1,11 +1,14 @@ -tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ':' expected. +tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ',' expected. tests/cases/compiler/incompleteObjectLiteral1.ts(1,16): error TS1128: Declaration or statement expected. +tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS2304: Cannot find name 'aa'. -==== tests/cases/compiler/incompleteObjectLiteral1.ts (2 errors) ==== +==== tests/cases/compiler/incompleteObjectLiteral1.ts (3 errors) ==== var tt = { aa; } ~ -!!! error TS1005: ':' expected. +!!! error TS1005: ',' expected. ~ !!! error TS1128: Declaration or statement expected. + ~~ +!!! error TS2304: Cannot find name 'aa'. var x = tt; \ No newline at end of file diff --git a/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt b/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt index 0622b7badb006..35593f4378f44 100644 --- a/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt +++ b/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt @@ -1,10 +1,9 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(12,6): error TS1112: A class member cannot be declared optional. tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(20,6): error TS1112: A class member cannot be declared optional. -tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(24,6): error TS1005: ':' expected. -tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(24,7): error TS1109: Expression expected. +tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(24,6): error TS1159: A object member cannot be declared optional. -==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (4 errors) ==== +==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (3 errors) ==== // Basic uses of optional properties var a: { @@ -33,8 +32,6 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith var b = { x?: 1 // error - ~ -!!! error TS1005: ':' expected. - ~ -!!! error TS1109: Expression expected. + +!!! error TS1159: A object member cannot be declared optional. } \ No newline at end of file diff --git a/tests/baselines/reference/parser512097.errors.txt b/tests/baselines/reference/parser512097.errors.txt index 3963d969c3464..181060db95dd3 100644 --- a/tests/baselines/reference/parser512097.errors.txt +++ b/tests/baselines/reference/parser512097.errors.txt @@ -1,13 +1,16 @@ -tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,14): error TS1005: ':' expected. +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,14): error TS1005: ',' expected. tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,16): error TS1128: Declaration or statement expected. +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,12): error TS2304: Cannot find name 'aa'. -==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts (3 errors) ==== var tt = { aa; } // After this point, no useful parsing occurs in the entire file ~ -!!! error TS1005: ':' expected. +!!! error TS1005: ',' expected. ~ !!! error TS1128: Declaration or statement expected. + ~~ +!!! error TS2304: Cannot find name 'aa'. if (true) { } \ No newline at end of file diff --git a/tests/baselines/reference/parserErrorRecovery_ObjectLiteral2.errors.txt b/tests/baselines/reference/parserErrorRecovery_ObjectLiteral2.errors.txt index 55667d42de50a..1c526f0c9ec6d 100644 --- a/tests/baselines/reference/parserErrorRecovery_ObjectLiteral2.errors.txt +++ b/tests/baselines/reference/parserErrorRecovery_ObjectLiteral2.errors.txt @@ -1,11 +1,14 @@ -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,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(1,11): error TS2304: Cannot find name 'a'. -==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts (3 errors) ==== var v = { a + ~ +!!! error TS2304: Cannot find name 'a'. return; ~~~~~~ -!!! error TS1005: ':' expected. +!!! error TS1005: ',' expected. ~ !!! error TS1005: ':' expected. \ No newline at end of file From 1bf7ecac7a80caeac280ef94215cbcc36650bad6 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 11 Nov 2014 11:01:12 -0800 Subject: [PATCH 05/13] Find all reference for short-hand property assignment --- src/services/services.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/services/services.ts b/src/services/services.ts index a85824f873b77..43b9e96b2caa3 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4061,6 +4061,14 @@ module ts { if (referenceSymbol && isRelatableToSearchSet(searchSymbols, referenceSymbol, referenceLocation)) { result.push(getReferenceEntryFromNode(referenceLocation)); } + // TODO (yuisu): Comment + else if (referenceSymbol && referenceSymbol.declarations[0].kind === SyntaxKind.ShortHandPropertyAssignment) { + var referenceSymbolDeclName = referenceSymbol.declarations[0].name; + if (searchSymbols.indexOf(typeInfoResolver.resolveEntityNameForShortHandPropertyAssignment(referenceSymbolDeclName)) >= 0 && + !(referenceSymbol).target) { + result.push(getReferenceEntryFromNode(referenceSymbolDeclName)); + } + } }); } @@ -4227,6 +4235,8 @@ module ts { forEach(getPropertySymbolsFromContextualType(location), contextualSymbol => { result.push.apply(result, typeInfoResolver.getRootSymbols(contextualSymbol)); }); + + // Add the symbol in the case of short-hand property assignment if (location.kind === SyntaxKind.Identifier && location.parent.kind === SyntaxKind.ShortHandPropertyAssignment) { result.push(typeInfoResolver.resolveEntityNameForShortHandPropertyAssignment(location)); } From 680999fe3f556c607f9d1f6dd5de8770e20a79de Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 11 Nov 2014 11:30:16 -0800 Subject: [PATCH 06/13] Move short-hand property tests to conformace folder --- .../objectLiteralShorthandProperties.ts | 19 ------------------- .../objectLiteralShorthandProperties2.ts | 18 ------------------ .../objectLiteralShorthandProperties3.ts | 13 ------------- .../objectLiteralShorthandProperties4.ts | 4 ---- 4 files changed, 54 deletions(-) delete mode 100644 tests/cases/compiler/objectLiteralShorthandProperties.ts delete mode 100644 tests/cases/compiler/objectLiteralShorthandProperties2.ts delete mode 100644 tests/cases/compiler/objectLiteralShorthandProperties3.ts delete mode 100644 tests/cases/compiler/objectLiteralShorthandProperties4.ts diff --git a/tests/cases/compiler/objectLiteralShorthandProperties.ts b/tests/cases/compiler/objectLiteralShorthandProperties.ts deleted file mode 100644 index 95b380308e9e5..0000000000000 --- a/tests/cases/compiler/objectLiteralShorthandProperties.ts +++ /dev/null @@ -1,19 +0,0 @@ -var a, b, c; - -var x1 = { - a -}; - -var x2 = { - a, -} - -var x3 = { - a: 0, - b, - c, - d() { }, - x3, - parent: x3 -}; - diff --git a/tests/cases/compiler/objectLiteralShorthandProperties2.ts b/tests/cases/compiler/objectLiteralShorthandProperties2.ts deleted file mode 100644 index 38081e7d69111..0000000000000 --- a/tests/cases/compiler/objectLiteralShorthandProperties2.ts +++ /dev/null @@ -1,18 +0,0 @@ -// errors -var y = { - "stringLiteral", - 42, - get e, - set f, - this, - super, - var, - class, - typeof -}; - -var x = { - a.b, - a["ss"], - a[1], -}; \ No newline at end of file diff --git a/tests/cases/compiler/objectLiteralShorthandProperties3.ts b/tests/cases/compiler/objectLiteralShorthandProperties3.ts deleted file mode 100644 index 005885bb9018f..0000000000000 --- a/tests/cases/compiler/objectLiteralShorthandProperties3.ts +++ /dev/null @@ -1,13 +0,0 @@ -// module export - -module m { - export var x; -} - -module m { - var z = x; - var y = { - a: x, - x - }; -} diff --git a/tests/cases/compiler/objectLiteralShorthandProperties4.ts b/tests/cases/compiler/objectLiteralShorthandProperties4.ts deleted file mode 100644 index 1f44dc18ae86a..0000000000000 --- a/tests/cases/compiler/objectLiteralShorthandProperties4.ts +++ /dev/null @@ -1,4 +0,0 @@ -var x = { - x, // OK - undefinedVariable // Error -} From bb7a0aa9d9123a10c0c181398cfdd45ff2859cf2 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 11 Nov 2014 11:31:45 -0800 Subject: [PATCH 07/13] Add conformance tests --- .../objectLiteralShorthandProperties.js | 39 ++++++++++ .../objectLiteralShorthandProperties.types | 50 ++++++++++++ ...jectLiteralShorthandProperties2.errors.txt | 72 +++++++++++++++++ .../objectLiteralShorthandProperties3.js | 30 ++++++++ .../objectLiteralShorthandProperties3.types | 31 ++++++++ ...jectLiteralShorthandProperties4.errors.txt | 11 +++ .../objectLiteralShorthandProperties4.js | 12 +++ ...ectLiteralShorthandPropertiesAssignment.js | 11 +++ ...LiteralShorthandPropertiesAssignment.types | 15 ++++ ...lShorthandPropertiesAssignment2.errors.txt | 13 ++++ ...ctLiteralShorthandPropertiesAssignment2.js | 11 +++ ...eralShorthandPropertiesFunctionArgument.js | 20 +++++ ...lShorthandPropertiesFunctionArgument.types | 33 ++++++++ ...handPropertiesFunctionArgument2.errors.txt | 14 ++++ ...ralShorthandPropertiesFunctionArgument2.js | 17 ++++ ...jectLiteralShorthandPropertiesTargetES6.js | 62 +++++++++++++++ ...tLiteralShorthandPropertiesTargetES6.types | 77 +++++++++++++++++++ .../objectLiteralShorthandProperties.ts | 20 +++++ .../objectLiteralShorthandProperties2.ts | 18 +++++ .../objectLiteralShorthandProperties3.ts | 13 ++++ .../objectLiteralShorthandProperties4.ts | 4 + ...ectLiteralShorthandPropertiesAssignment.ts | 4 + ...ctLiteralShorthandPropertiesAssignment2.ts | 4 + ...eralShorthandPropertiesFunctionArgument.ts | 10 +++ ...ralShorthandPropertiesFunctionArgument2.ts | 7 ++ ...jectLiteralShorthandPropertiesTargetES6.ts | 31 ++++++++ 26 files changed, 629 insertions(+) create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties.js create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties.types create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties3.js create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties3.types create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt create mode 100644 tests/baselines/reference/objectLiteralShorthandProperties4.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.types create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.errors.txt create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument2.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts diff --git a/tests/baselines/reference/objectLiteralShorthandProperties.js b/tests/baselines/reference/objectLiteralShorthandProperties.js new file mode 100644 index 0000000000000..31e9eabb0a953 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties.js @@ -0,0 +1,39 @@ +//// [objectLiteralShorthandProperties.ts] +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + + + +//// [objectLiteralShorthandProperties.js] +var a, b, c; +var x1 = { + a: a +}; +var x2 = { + a: a +}; +var x3 = { + a: 0, + b: b, + c: c, + d: function () { + }, + x3: x3, + parent: x3 +}; diff --git a/tests/baselines/reference/objectLiteralShorthandProperties.types b/tests/baselines/reference/objectLiteralShorthandProperties.types new file mode 100644 index 0000000000000..2f4e17be2393c --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties.types @@ -0,0 +1,50 @@ +=== tests/cases/compiler/objectLiteralShorthandProperties.ts === +var a, b, c; +>a : any +>b : any +>c : any + +var x1 = { +>x1 : { a: any; } +>{ a} : { a: any; } + + a +>a : any + +}; + +var x2 = { +>x2 : { a: any; } +>{ a,} : { a: any; } + + a, +>a : any +} + +var x3 = { +>x3 : any +>{ a: 0, b, c, d() { }, x3, parent: x3} : { a: number; b: any; c: any; d: () => void; x3: any; parent: any; } + + a: 0, +>a : number + + b, +>b : any + + c, +>c : any + + d() { }, +>d : () => void +>d() { } : () => void + + x3, +>x3 : any + + parent: x3 +>parent : any +>x3 : any + +}; + + diff --git a/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt b/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt new file mode 100644 index 0000000000000..814bbf18422fa --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt @@ -0,0 +1,72 @@ +tests/cases/compiler/objectLiteralShorthandProperties2.ts(3,20): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(4,7): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(5,10): error TS1005: '(' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(6,10): error TS1005: '(' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(7,9): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(8,10): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(9,8): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(10,10): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(12,1): error TS1005: ':' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(15,6): error TS1005: ',' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(16,6): error TS1005: ',' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(17,6): error TS1005: '=' expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(18,1): error TS1128: Declaration or statement expected. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(15,5): error TS2300: Duplicate identifier 'a'. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(15,7): error TS2304: Cannot find name 'b'. +tests/cases/compiler/objectLiteralShorthandProperties2.ts(16,5): error TS2300: Duplicate identifier 'a'. + + +==== tests/cases/compiler/objectLiteralShorthandProperties2.ts (17 errors) ==== + // errors + var y = { + "stringLiteral", + ~ +!!! error TS1005: ':' expected. + 42, + ~ +!!! error TS1005: ':' expected. + get e, + ~ +!!! error TS1005: '(' expected. + ~ +!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. + set f, + ~ +!!! error TS1005: '(' expected. + this, + ~ +!!! error TS1005: ':' expected. + super, + ~ +!!! error TS1005: ':' expected. + var, + ~ +!!! error TS1005: ':' expected. + class, + ~ +!!! error TS1005: ':' expected. + typeof + }; + ~ +!!! error TS1005: ':' expected. + + var x = { + a.b, + ~ +!!! error TS1005: ',' expected. + ~ +!!! error TS2300: Duplicate identifier 'a'. + ~ +!!! error TS2304: Cannot find name 'b'. + a["ss"], + ~ +!!! error TS1005: ',' expected. + ~ +!!! error TS2300: Duplicate identifier 'a'. + a[1], + ~ +!!! error TS1005: '=' expected. + }; + ~ +!!! error TS1128: Declaration or statement expected. \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandProperties3.js b/tests/baselines/reference/objectLiteralShorthandProperties3.js new file mode 100644 index 0000000000000..b3e567bebc802 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties3.js @@ -0,0 +1,30 @@ +//// [objectLiteralShorthandProperties3.ts] +// module export + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} + + +//// [objectLiteralShorthandProperties3.js] +// module export +var m; +(function (m) { + m.x; +})(m || (m = {})); +var m; +(function (m) { + var z = m.x; + var y = { + a: m.x, + m.x: m.x + }; +})(m || (m = {})); diff --git a/tests/baselines/reference/objectLiteralShorthandProperties3.types b/tests/baselines/reference/objectLiteralShorthandProperties3.types new file mode 100644 index 0000000000000..0ca744aaf8837 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties3.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/objectLiteralShorthandProperties3.ts === +// module export + +module m { +>m : typeof m + + export var x; +>x : any +} + +module m { +>m : typeof m + + var z = x; +>z : any +>x : any + + var y = { +>y : { a: any; x: any; } +>{ a: x, x } : { a: any; x: any; } + + a: x, +>a : any +>x : any + + x +>x : any + + }; +} + diff --git a/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt b/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt new file mode 100644 index 0000000000000..0928d8f2d94c1 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/objectLiteralShorthandProperties4.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'. + + +==== tests/cases/compiler/objectLiteralShorthandProperties4.ts (1 errors) ==== + var x = { + x, // OK + undefinedVariable // Error + ~~~~~~~~~~~~~~~~~ +!!! error TS2304: Cannot find name 'undefinedVariable'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandProperties4.js b/tests/baselines/reference/objectLiteralShorthandProperties4.js new file mode 100644 index 0000000000000..ed8ee494b17d7 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties4.js @@ -0,0 +1,12 @@ +//// [objectLiteralShorthandProperties4.ts] +var x = { + x, // OK + undefinedVariable // Error +} + + +//// [objectLiteralShorthandProperties4.js] +var x = { + x: x, + undefinedVariable: undefinedVariable // Error +}; diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js new file mode 100644 index 0000000000000..6af0a69a3044d --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js @@ -0,0 +1,11 @@ +//// [objectLiteralShorthandPropertiesAssignment.ts] +var id: number = 10000; +var name: string = "my name"; + +var person: { name: string; id: number } = { name, id }; + + +//// [objectLiteralShorthandPropertiesAssignment.js] +var id = 10000; +var name = "my name"; +var person = { name: name, id: id }; diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types new file mode 100644 index 0000000000000..f7f18c8f0c47d --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types @@ -0,0 +1,15 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts === +var id: number = 10000; +>id : number + +var name: string = "my name"; +>name : string + +var person: { name: string; id: number } = { name, id }; +>person : { name: string; id: number; } +>name : string +>id : number +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt new file mode 100644 index 0000000000000..a3ae961f6ea42 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts(4,5): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. + Property 'b' is missing in type '{ name: string; id: number; }'. + + +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts (1 errors) ==== + var id: number = 10000; + var name: string = "my name"; + + var person: { b: string; id: number } = { name, id }; // error + ~~~~~~ +!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. +!!! error TS2322: Property 'b' is missing in type '{ name: string; id: number; }'. + \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js new file mode 100644 index 0000000000000..f37bb1cd52581 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js @@ -0,0 +1,11 @@ +//// [objectLiteralShorthandPropertiesAssignment2.ts] +var id: number = 10000; +var name: string = "my name"; + +var person: { b: string; id: number } = { name, id }; // error + + +//// [objectLiteralShorthandPropertiesAssignment2.js] +var id = 10000; +var name = "my name"; +var person = { name: name, id: id }; // error diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.js b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.js new file mode 100644 index 0000000000000..f6e20b605b6d7 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.js @@ -0,0 +1,20 @@ +//// [objectLiteralShorthandPropertiesFunctionArgument.ts] +var id: number = 10000; +var name: string = "my name"; + +var person = { name, id }; + +function foo(p: { name: string; id: number }) { } +foo(person); + + +var obj = { name: name, id: id }; + +//// [objectLiteralShorthandPropertiesFunctionArgument.js] +var id = 10000; +var name = "my name"; +var person = { name: name, id: id }; +function foo(p) { +} +foo(person); +var obj = { name: name, id: id }; diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.types b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.types new file mode 100644 index 0000000000000..9b4261a74be57 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument.types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument.ts === +var id: number = 10000; +>id : number + +var name: string = "my name"; +>name : string + +var person = { name, id }; +>person : { name: string; id: number; } +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + +function foo(p: { name: string; id: number }) { } +>foo : (p: { name: string; id: number; }) => void +>p : { name: string; id: number; } +>name : string +>id : number + +foo(person); +>foo(person) : void +>foo : (p: { name: string; id: number; }) => void +>person : { name: string; id: number; } + + +var obj = { name: name, id: id }; +>obj : { name: string; id: number; } +>{ name: name, id: id } : { name: string; id: number; } +>name : string +>name : string +>id : number +>id : number + diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.errors.txt new file mode 100644 index 0000000000000..44426a8afb0f5 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.errors.txt @@ -0,0 +1,14 @@ +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument2.ts(7,5): error TS2345: Argument of type '{ name: string; id: number; }' is not assignable to parameter of type '{ a: string; id: number; }'. + + +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument2.ts (1 errors) ==== + var id: number = 10000; + var name: string = "my name"; + + var person = { name, id }; + + function foo(p: { a: string; id: number }) { } + foo(person); // error + ~~~~~~ +!!! error TS2345: Argument of type '{ name: string; id: number; }' is not assignable to parameter of type '{ a: string; id: number; }'. + \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.js b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.js new file mode 100644 index 0000000000000..2f3535ef78ad9 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesFunctionArgument2.js @@ -0,0 +1,17 @@ +//// [objectLiteralShorthandPropertiesFunctionArgument2.ts] +var id: number = 10000; +var name: string = "my name"; + +var person = { name, id }; + +function foo(p: { a: string; id: number }) { } +foo(person); // error + + +//// [objectLiteralShorthandPropertiesFunctionArgument2.js] +var id = 10000; +var name = "my name"; +var person = { name: name, id: id }; +function foo(p) { +} +foo(person); // error diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js new file mode 100644 index 0000000000000..c02911c499322 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js @@ -0,0 +1,62 @@ +//// [objectLiteralShorthandPropertiesTargetES6.ts] +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} + + +//// [objectLiteralShorthandPropertiesTargetES6.js] +var a, b, c; +var x1 = { + a +}; +var x2 = { + a, +}; +var x3 = { + a: 0, + b, + c, + d: function () { + }, + x3, + parent: x3 +}; +var m; +(function (m) { + m.x; +})(m || (m = {})); +var m; +(function (m) { + var z = m.x; + var y = { + a: m.x, + m.x + }; +})(m || (m = {})); diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types new file mode 100644 index 0000000000000..560ae4af4c02f --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types @@ -0,0 +1,77 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts === +var a, b, c; +>a : any +>b : any +>c : any + +var x1 = { +>x1 : { a: any; } +>{ a} : { a: any; } + + a +>a : any + +}; + +var x2 = { +>x2 : { a: any; } +>{ a,} : { a: any; } + + a, +>a : any +} + +var x3 = { +>x3 : any +>{ a: 0, b, c, d() { }, x3, parent: x3} : { a: number; b: any; c: any; d: () => void; x3: any; parent: any; } + + a: 0, +>a : number + + b, +>b : any + + c, +>c : any + + d() { }, +>d : () => void +>d() { } : () => void + + x3, +>x3 : any + + parent: x3 +>parent : any +>x3 : any + +}; + +module m { +>m : typeof m + + export var x; +>x : any +} + +module m { +>m : typeof m + + var z = x; +>z : any +>x : any + + var y = { +>y : { a: any; x: any; } +>{ a: x, x } : { a: any; x: any; } + + a: x, +>a : any +>x : any + + x +>x : any + + }; +} + diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties.ts new file mode 100644 index 0000000000000..ffeab28958473 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties.ts @@ -0,0 +1,20 @@ +// @target: es5 +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts new file mode 100644 index 0000000000000..38081e7d69111 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts @@ -0,0 +1,18 @@ +// errors +var y = { + "stringLiteral", + 42, + get e, + set f, + this, + super, + var, + class, + typeof +}; + +var x = { + a.b, + a["ss"], + a[1], +}; \ No newline at end of file diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts new file mode 100644 index 0000000000000..005885bb9018f --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts @@ -0,0 +1,13 @@ +// module export + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts new file mode 100644 index 0000000000000..1f44dc18ae86a --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts @@ -0,0 +1,4 @@ +var x = { + x, // OK + undefinedVariable // Error +} diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts new file mode 100644 index 0000000000000..44448a26a9b71 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts @@ -0,0 +1,4 @@ +var id: number = 10000; +var name: string = "my name"; + +var person: { name: string; id: number } = { name, id }; diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts new file mode 100644 index 0000000000000..a3f88036dd88b --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts @@ -0,0 +1,4 @@ +var id: number = 10000; +var name: string = "my name"; + +var person: { b: string; id: number } = { name, id }; // error diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument.ts new file mode 100644 index 0000000000000..c50c4e3fbdf9a --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument.ts @@ -0,0 +1,10 @@ +var id: number = 10000; +var name: string = "my name"; + +var person = { name, id }; + +function foo(p: { name: string; id: number }) { } +foo(person); + + +var obj = { name: name, id: id }; \ No newline at end of file diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument2.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument2.ts new file mode 100644 index 0000000000000..45c248a591145 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesFunctionArgument2.ts @@ -0,0 +1,7 @@ +var id: number = 10000; +var name: string = "my name"; + +var person = { name, id }; + +function foo(p: { a: string; id: number }) { } +foo(person); // error diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts new file mode 100644 index 0000000000000..21351372de2df --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts @@ -0,0 +1,31 @@ +// @target: es6 +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} From 1888f736e13dd29dd3c3a16fad823e28a0c40a89 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 13 Nov 2014 11:33:31 -0800 Subject: [PATCH 08/13] Fix emit for shotr-hand assignment for module --- src/compiler/emitter.ts | 53 +++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index d23380b4fb1c0..12e0c5cd03727 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -927,13 +927,14 @@ module ts { } } - function isNonExpressionIdentifier(node: Identifier) { + function isNotExpressionIdentifier(node: Identifier) { var parent = node.parent; switch (parent.kind) { case SyntaxKind.Parameter: case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShortHandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.FunctionDeclaration: @@ -957,17 +958,24 @@ module ts { } } - function emitIdentifier(node: Identifier) { - if (!isNonExpressionIdentifier(node)) { - var prefix = resolver.getExpressionNamePrefix(node); - if (prefix) { - write(prefix); - write("."); - } + function emitExpressionIdentifier(node: Identifier) { + var prefix = resolver.getExpressionNamePrefix(node); + if (prefix) { + write(prefix); + write("."); } write(getSourceTextOfLocalNode(node)); } + function emitIdentifier(node: Identifier) { + if (!isNotExpressionIdentifier(node)) { + emitExpressionIdentifier(node); + } + else { + write(getSourceTextOfLocalNode(node)); + } + } + function emitThis(node: Node) { if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.LexicalThis) { write("_this"); @@ -1034,13 +1042,32 @@ module ts { } function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) { - emitLeadingComments(node); - emit(node.name); - if (compilerOptions.target !== ScriptTarget.ES6) { - write(": "); + function emitAsNormalPropertyAssignment() { + emitLeadingComments(node); + // Emit identifier as an identifier emit(node.name); + write(": "); + // Even though this is stored as identified because it is in short-hand property assignment, treated it as expression + emitExpressionIdentifier(node.name); + emitTrailingComments(node); + } + + if (compilerOptions.target < ScriptTarget.ES6) { + emitAsNormalPropertyAssignment(); + } + else if (compilerOptions.target >= ScriptTarget.ES6) { + // If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment + var prefix = resolver.getExpressionNamePrefix(node.name); + if (prefix) { + emitAsNormalPropertyAssignment(); + } + // If short-hand property has no prefix, emit it as short-hand. + else { + emitLeadingComments(node); + emit(node.name); + emitTrailingComments(node); + } } - emitTrailingComments(node); } function tryEmitConstantValue(node: PropertyAccess | IndexedAccess): boolean { From a8ebdf0cbd12f34f28bd62a03cc77505ed9eac12 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 13 Nov 2014 12:02:13 -0800 Subject: [PATCH 09/13] Address code review --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 16 +++++---- .../diagnosticInformationMap.generated.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/emitter.ts | 4 +-- src/compiler/parser.ts | 18 +++++----- src/compiler/types.ts | 6 ++-- src/services/services.ts | 14 ++++---- .../objectLiteralShorthandProperties.js | 2 +- .../objectLiteralShorthandProperties.types | 2 +- ...jectLiteralShorthandProperties2.errors.txt | 36 +++++++++---------- .../objectLiteralShorthandProperties3.js | 2 +- .../objectLiteralShorthandProperties3.types | 2 +- ...jectLiteralShorthandProperties4.errors.txt | 4 +-- ...jectLiteralShorthandPropertiesTargetES6.js | 2 +- ...jectTypesWithOptionalProperties.errors.txt | 4 +-- 16 files changed, 60 insertions(+), 58 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index fe00830ae518a..6d0c4f3706e64 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -350,7 +350,7 @@ module ts { break; case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: - case SyntaxKind.ShortHandPropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: bindDeclaration(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false); break; case SyntaxKind.EnumMember: diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4ebcb925a7eda..0bf17edbf19ee 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -94,6 +94,7 @@ module ts { getReturnTypeOfSignature: getReturnTypeOfSignature, getSymbolsInScope: getSymbolsInScope, getSymbolInfo: getSymbolInfo, + getValueSymbolInfo: getValueSymbolInfo, getTypeOfNode: getTypeOfNode, typeToString: typeToString, getSymbolDisplayBuilder: getSymbolDisplayBuilder, @@ -111,7 +112,6 @@ module ts { isUndefinedSymbol: symbol => symbol === undefinedSymbol, isArgumentsSymbol: symbol => symbol === argumentsSymbol, hasEarlyErrors: hasEarlyErrors, - resolveEntityNameForShortHandPropertyAssignment: resolveEntityNameForShortHandPropertyAssignment, }; var undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined"); @@ -538,10 +538,6 @@ module ts { return symbol.flags & meaning ? symbol : resolveImport(symbol); } - function resolveEntityNameForShortHandPropertyAssignment(location: Node): Symbol { - return resolveEntityName(location, location, SymbolFlags.Value); - } - function isExternalModuleNameRelative(moduleName: string): boolean { // TypeScript 1.0 spec (April 2014): 11.2.1 // An external module name is "relative" if the first term is "." or "..". @@ -1672,8 +1668,8 @@ module ts { } // If it is a short-hand property assignment; Use the type of the identifier - if (declaration.kind === SyntaxKind.ShortHandPropertyAssignment) { - var type = checkIdentifier((declaration.name)); + if (declaration.kind === SyntaxKind.ShorthandPropertyAssignment) { + var type = checkIdentifier(declaration.name); return type } @@ -4986,6 +4982,7 @@ module ts { type = checkExpression(memberDecl.initializer, contextualMapper); } else { + Debug.assert(memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment); type = checkExpression(memberDecl.name, contextualMapper); } var prop = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.name); @@ -8647,6 +8644,11 @@ module ts { return undefined; } + function getValueSymbolInfo(node: Node): Symbol { + // Get symbol information as value + return resolveEntityName(node, node, SymbolFlags.Value); + } + function getTypeOfNode(node: Node): Type { if (isInsideWithStatementBody(node)) { // We cannot answer semantic questions within a with block, do not proceed any further diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 19d5f0c5f6bb5..e9d5b5571e172 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -122,7 +122,7 @@ module ts { let_declarations_can_only_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "'let' declarations can only be declared inside a block." }, Invalid_template_literal_expected: { code: 1158, category: DiagnosticCategory.Error, key: "Invalid template literal; expected '}'" }, Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1159, category: DiagnosticCategory.Error, key: "Tagged templates are only available when targeting ECMAScript 6 and higher." }, - A_object_member_cannot_be_declared_optional: { code: 1159, category: DiagnosticCategory.Error, key: "A object member cannot be declared optional." }, + A_object_member_cannot_be_declared_optional: { code: 1160, category: DiagnosticCategory.Error, key: "A object member cannot be declared optional." }, Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 52235a0848e81..9a13299e4b5fa 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -482,7 +482,7 @@ "A object member cannot be declared optional.": { "category": "Error", - "code": 1159 + "code": 1160 }, "Duplicate identifier '{0}'.": { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 12e0c5cd03727..5781875ac9b65 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -934,7 +934,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: - case SyntaxKind.ShortHandPropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.FunctionDeclaration: @@ -2287,7 +2287,7 @@ module ts { return emitObjectLiteral(node); case SyntaxKind.PropertyAssignment: return emitPropertyAssignment(node); - case SyntaxKind.ShortHandPropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: return emitShortHandPropertyAssignment(node); case SyntaxKind.PropertyAccess: return emitPropertyAccess(node); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 2657fc83668b5..2fe3b8aa67001 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -198,7 +198,7 @@ module ts { child((node).initializer); case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: - case SyntaxKind.ShortHandPropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: return child((node).name) || child((node).type) || child((node).initializer); @@ -574,7 +574,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: - case SyntaxKind.ShortHandPropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.FunctionDeclaration: @@ -2669,11 +2669,11 @@ module ts { return finishNode(node); } - function parsePropertyAssignment(): PropertyDeclaration { + function parsePropertyAssignment(): Declaration { var nodePos = scanner.getStartPos(); var nameToken = token; var propertyName = parsePropertyName(); - var node: PropertyDeclaration; + var node: Declaration; if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) { node = createNode(SyntaxKind.PropertyAssignment, nodePos); node.name = propertyName; @@ -2684,25 +2684,25 @@ module ts { // var x = 1; // var y = { x() { } } // otherwise this will bring y.x into the scope of x which is incorrect - node.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); + (node).initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); return finishNode(node); } if (token === SyntaxKind.QuestionToken) { var questionStart = scanner.getTokenPos(); - errorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional); + grammarErrorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional); nextToken(); } // Parse to check if it is short-hand property assignment or normal property assignment if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) { - node = createNode(SyntaxKind.ShortHandPropertyAssignment, nodePos); + node = createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos); node.name = propertyName; } else { node = createNode(SyntaxKind.PropertyAssignment, nodePos); node.name = propertyName; parseExpected(SyntaxKind.ColonToken); - node.initializer = parseAssignmentExpression(false); + (node).initializer = parseAssignmentExpression(false); } return finishNode(node); } @@ -2752,7 +2752,7 @@ module ts { if (p.kind === SyntaxKind.PropertyAssignment) { currentKind = Property; } - else if (p.kind === SyntaxKind.ShortHandPropertyAssignment) { + else if (p.kind === SyntaxKind.ShorthandPropertyAssignment) { currentKind = Property; } else if (p.kind === SyntaxKind.GetAccessor) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 8e392de8ff562..f2690a824622a 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -165,7 +165,7 @@ module ts { ArrayLiteral, ObjectLiteral, PropertyAssignment, - ShortHandPropertyAssignment, + ShorthandPropertyAssignment, PropertyAccess, IndexedAccess, CallExpression, @@ -330,7 +330,7 @@ module ts { initializer?: Expression; } - export interface ShortHandPropertyDeclaration extends PropertyDeclaration { + export interface ShortHandPropertyDeclaration extends Declaration { name: Identifier; } @@ -715,6 +715,7 @@ module ts { getReturnTypeOfSignature(signature: Signature): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; getSymbolInfo(node: Node): Symbol; + getValueSymbolInfo(node: Node): Symbol; getTypeOfNode(node: Node): Type; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; @@ -733,7 +734,6 @@ module ts { getEnumMemberValue(node: EnumMember): number; isValidPropertyAccess(node: PropertyAccess, propertyName: string): boolean; getAliasedSymbol(symbol: Symbol): Symbol; - resolveEntityNameForShortHandPropertyAssignment(location: Node): Symbol; } export interface SymbolDisplayBuilder { diff --git a/src/services/services.ts b/src/services/services.ts index 43b9e96b2caa3..822795a1c1587 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1969,7 +1969,7 @@ module ts { /** Returns true if node is a name of an object literal property, e.g. "a" in x = { "a": 1 } */ function isNameOfPropertyAssignment(node: Node): boolean { return (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) && - (node.parent.kind === SyntaxKind.PropertyAssignment || node.parent.kind === SyntaxKind.ShortHandPropertyAssignment) && (node.parent).name === node; + (node.parent.kind === SyntaxKind.PropertyAssignment || node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) && (node.parent).name === node; } function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: Node): boolean { @@ -2648,7 +2648,7 @@ module ts { var existingMemberNames: Map = {}; forEach(existingMembers, m => { - if (m.kind !== SyntaxKind.PropertyAssignment && m.kind !== SyntaxKind.ShortHandPropertyAssignment) { + if (m.kind !== SyntaxKind.PropertyAssignment && m.kind !== SyntaxKind.ShorthandPropertyAssignment) { // Ignore omitted expressions for missing members in the object literal return; } @@ -4062,9 +4062,9 @@ module ts { result.push(getReferenceEntryFromNode(referenceLocation)); } // TODO (yuisu): Comment - else if (referenceSymbol && referenceSymbol.declarations[0].kind === SyntaxKind.ShortHandPropertyAssignment) { + else if (referenceSymbol && referenceSymbol.declarations[0].kind === SyntaxKind.ShorthandPropertyAssignment) { var referenceSymbolDeclName = referenceSymbol.declarations[0].name; - if (searchSymbols.indexOf(typeInfoResolver.resolveEntityNameForShortHandPropertyAssignment(referenceSymbolDeclName)) >= 0 && + if (searchSymbols.indexOf(typeInfoResolver.getValueSymbolInfo(referenceSymbolDeclName)) >= 0 && !(referenceSymbol).target) { result.push(getReferenceEntryFromNode(referenceSymbolDeclName)); } @@ -4237,8 +4237,8 @@ module ts { }); // Add the symbol in the case of short-hand property assignment - if (location.kind === SyntaxKind.Identifier && location.parent.kind === SyntaxKind.ShortHandPropertyAssignment) { - result.push(typeInfoResolver.resolveEntityNameForShortHandPropertyAssignment(location)); + if (location.kind === SyntaxKind.Identifier && location.parent.kind === SyntaxKind.ShorthandPropertyAssignment) { + result.push(typeInfoResolver.getValueSymbolInfo(location)); } } @@ -4582,7 +4582,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: - case SyntaxKind.ShortHandPropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.Constructor: diff --git a/tests/baselines/reference/objectLiteralShorthandProperties.js b/tests/baselines/reference/objectLiteralShorthandProperties.js index 31e9eabb0a953..776b2bbe45408 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties.js +++ b/tests/baselines/reference/objectLiteralShorthandProperties.js @@ -26,7 +26,7 @@ var x1 = { a: a }; var x2 = { - a: a + a: a, }; var x3 = { a: 0, diff --git a/tests/baselines/reference/objectLiteralShorthandProperties.types b/tests/baselines/reference/objectLiteralShorthandProperties.types index 2f4e17be2393c..f28f25e815c48 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties.types +++ b/tests/baselines/reference/objectLiteralShorthandProperties.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/objectLiteralShorthandProperties.ts === +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties.ts === var a, b, c; >a : any >b : any diff --git a/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt b/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt index 814bbf18422fa..0e6b9eba6162b 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt +++ b/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt @@ -1,23 +1,23 @@ -tests/cases/compiler/objectLiteralShorthandProperties2.ts(3,20): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(4,7): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(5,10): error TS1005: '(' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(6,10): error TS1005: '(' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(7,9): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(8,10): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(9,8): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(10,10): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(12,1): error TS1005: ':' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(15,6): error TS1005: ',' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(16,6): error TS1005: ',' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(17,6): error TS1005: '=' expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(18,1): error TS1128: Declaration or statement expected. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(15,5): error TS2300: Duplicate identifier 'a'. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(15,7): error TS2304: Cannot find name 'b'. -tests/cases/compiler/objectLiteralShorthandProperties2.ts(16,5): error TS2300: Duplicate identifier 'a'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(3,20): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(4,7): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(5,10): error TS1005: '(' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(6,10): error TS1005: '(' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(7,9): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(8,10): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(9,8): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(10,10): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(12,1): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(15,6): error TS1005: ',' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(16,6): error TS1005: ',' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(17,6): error TS1005: '=' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(18,1): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(15,5): error TS2300: Duplicate identifier 'a'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(15,7): error TS2304: Cannot find name 'b'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(16,5): error TS2300: Duplicate identifier 'a'. -==== tests/cases/compiler/objectLiteralShorthandProperties2.ts (17 errors) ==== +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts (17 errors) ==== // errors var y = { "stringLiteral", diff --git a/tests/baselines/reference/objectLiteralShorthandProperties3.js b/tests/baselines/reference/objectLiteralShorthandProperties3.js index b3e567bebc802..0d436efa787b2 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties3.js +++ b/tests/baselines/reference/objectLiteralShorthandProperties3.js @@ -25,6 +25,6 @@ var m; var z = m.x; var y = { a: m.x, - m.x: m.x + x: m.x }; })(m || (m = {})); diff --git a/tests/baselines/reference/objectLiteralShorthandProperties3.types b/tests/baselines/reference/objectLiteralShorthandProperties3.types index 0ca744aaf8837..5d58532b3d1fc 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties3.types +++ b/tests/baselines/reference/objectLiteralShorthandProperties3.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/objectLiteralShorthandProperties3.ts === +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts === // module export module m { diff --git a/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt b/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt index 0928d8f2d94c1..678e18143e94e 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt +++ b/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt @@ -1,7 +1,7 @@ -tests/cases/compiler/objectLiteralShorthandProperties4.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'. -==== tests/cases/compiler/objectLiteralShorthandProperties4.ts (1 errors) ==== +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts (1 errors) ==== var x = { x, // OK undefinedVariable // Error diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js index c02911c499322..a35fc69770685 100644 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js @@ -57,6 +57,6 @@ var m; var z = m.x; var y = { a: m.x, - m.x + x: m.x }; })(m || (m = {})); diff --git a/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt b/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt index 35593f4378f44..e73e8adc9303a 100644 --- a/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt +++ b/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(12,6): error TS1112: A class member cannot be declared optional. tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(20,6): error TS1112: A class member cannot be declared optional. -tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(24,6): error TS1159: A object member cannot be declared optional. +tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(24,6): error TS1160: A object member cannot be declared optional. ==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (3 errors) ==== @@ -33,5 +33,5 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith var b = { x?: 1 // error -!!! error TS1159: A object member cannot be declared optional. +!!! error TS1160: A object member cannot be declared optional. } \ No newline at end of file From 8bd7aae869d222470e724f86cce690a185711273 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 13 Nov 2014 17:39:10 -0800 Subject: [PATCH 10/13] Add comment and clean up implementation on findAllReferences --- src/compiler/checker.ts | 19 ++++++++++++------- src/compiler/types.ts | 2 +- src/services/services.ts | 40 ++++++++++++++++++++++++++++------------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0bf17edbf19ee..6ca1df3dc85e9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -94,7 +94,7 @@ module ts { getReturnTypeOfSignature: getReturnTypeOfSignature, getSymbolsInScope: getSymbolsInScope, getSymbolInfo: getSymbolInfo, - getValueSymbolInfo: getValueSymbolInfo, + getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, getTypeOfNode: getTypeOfNode, typeToString: typeToString, getSymbolDisplayBuilder: getSymbolDisplayBuilder, @@ -2407,7 +2407,7 @@ module ts { } // Return the symbol for the property with the given name in the given type. Creates synthetic union properties when - // necessary, maps primtive types and type parameters are to their apparent types, and augments with properties from + // necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from // Object and Function as appropriate. function getPropertyOfType(type: Type, name: string): Symbol { if (type.flags & TypeFlags.Union) { @@ -2442,7 +2442,7 @@ module ts { } // Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and - // maps primtive types and type parameters are to their apparent types. + // maps primitive types and type parameters are to their apparent types. function getSignaturesOfType(type: Type, kind: SignatureKind): Signature[] { return getSignaturesOfObjectOrUnionType(getApparentType(type), kind); } @@ -2455,7 +2455,7 @@ module ts { } // Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and - // maps primtive types and type parameters are to their apparent types. + // maps primitive types and type parameters are to their apparent types. function getIndexTypeOfType(type: Type, kind: IndexKind): Type { return getIndexTypeOfObjectOrUnionType(getApparentType(type), kind); } @@ -8644,9 +8644,14 @@ module ts { return undefined; } - function getValueSymbolInfo(node: Node): Symbol { - // Get symbol information as value - return resolveEntityName(node, node, SymbolFlags.Value); + function getShorthandAssignmentValueSymbol(location: Node): Symbol { + // The function returns a value symbol of an identifier in the short-hand property assignment. + // This is necessary as an identifier in short-hand property assignment can contains two meaning: + // property name and property value. + if (location && location.kind === SyntaxKind.ShorthandPropertyAssignment) { + return resolveEntityName(location, (location).name, SymbolFlags.Value); + } + return undefined; } function getTypeOfNode(node: Node): Type { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f2690a824622a..a4d3fc36689bd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -715,7 +715,7 @@ module ts { getReturnTypeOfSignature(signature: Signature): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; getSymbolInfo(node: Node): Symbol; - getValueSymbolInfo(node: Node): Symbol; + getShorthandAssignmentValueSymbol(location: Node): Symbol; getTypeOfNode(node: Node): Type; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; diff --git a/src/services/services.ts b/src/services/services.ts index 822795a1c1587..21cc23bcee53b 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4058,15 +4058,20 @@ module ts { } var referenceSymbol = typeInfoResolver.getSymbolInfo(referenceLocation); - if (referenceSymbol && isRelatableToSearchSet(searchSymbols, referenceSymbol, referenceLocation)) { - result.push(getReferenceEntryFromNode(referenceLocation)); - } - // TODO (yuisu): Comment - else if (referenceSymbol && referenceSymbol.declarations[0].kind === SyntaxKind.ShorthandPropertyAssignment) { - var referenceSymbolDeclName = referenceSymbol.declarations[0].name; - if (searchSymbols.indexOf(typeInfoResolver.getValueSymbolInfo(referenceSymbolDeclName)) >= 0 && - !(referenceSymbol).target) { - result.push(getReferenceEntryFromNode(referenceSymbolDeclName)); + if (referenceSymbol) { + var referenceSymbolDeclaration = referenceSymbol.valueDeclaration; + var shorthandValueSymbol = typeInfoResolver.getShorthandAssignmentValueSymbol(referenceSymbolDeclaration); + if (isRelatableToSearchSet(searchSymbols, referenceSymbol, referenceLocation)) { + result.push(getReferenceEntryFromNode(referenceLocation)); + } + /* Because in short-hand property assignment, an identifier which stored as name of the short-hand property assignment + * has two meaning : property name and property value. Therefore when we do findAllReference at the position where + * an identifier is declared, the language service should return the position of the variable declaration as well as + * the position in short-hand property assignment excluding property accessing. However, if we do findAllReference at the + * position of property accessing, the referenceEntry of such position will be handled in the first case. + */ + else if (!(referenceSymbol.flags & SymbolFlags.Transient) && searchSymbols.indexOf(shorthandValueSymbol) >= 0) { + result.push(getReferenceEntryFromNode(referenceSymbolDeclaration.name)); } } }); @@ -4236,9 +4241,20 @@ module ts { result.push.apply(result, typeInfoResolver.getRootSymbols(contextualSymbol)); }); - // Add the symbol in the case of short-hand property assignment - if (location.kind === SyntaxKind.Identifier && location.parent.kind === SyntaxKind.ShorthandPropertyAssignment) { - result.push(typeInfoResolver.getValueSymbolInfo(location)); + /* Because in short-hand property assignment, location has two meaning : property name and as value of the property + * When we do findAllReference at the position of the short-hand property assignment, we would want to have references to position of + * property name and variable declaration of the identifier. + * Like in below example, when querying for all references for an identifier 'name', of the property assignment, the language service + * should show both 'name' in 'obj' and 'name' in variable declaration + * var name = "Foo"; + * var obj = { name }; + * In order to do that, we will populate the search set with the value symbol of the identifier as a value of the property assignment + * so that when matching with potential reference symbol, both symbols from property declaration and variable declaration + * will be included correctly. + */ + var shorthandValueSymbol = typeInfoResolver.getShorthandAssignmentValueSymbol(location.parent); + if (shorthandValueSymbol) { + result.push(shorthandValueSymbol); } } From 03e0722927b1f7597080a8fade2e1a29d5220a2c Mon Sep 17 00:00:00 2001 From: Yui T Date: Fri, 14 Nov 2014 15:44:06 -0800 Subject: [PATCH 11/13] Add test cases and rename test files --- .../objectLiteralShorthandProperties4.js | 12 ---- ...ectLiteralShorthandPropertiesAssignment.js | 25 +++++++ ...LiteralShorthandPropertiesAssignment.types | 53 +++++++++++++++ ...lShorthandPropertiesAssignment2.errors.txt | 13 ---- ...ctLiteralShorthandPropertiesAssignment2.js | 11 --- ...LiteralShorthandPropertiesAssignmentES6.js | 36 ++++++++++ ...eralShorthandPropertiesAssignmentES6.types | 68 +++++++++++++++++++ ...rthandPropertiesAssignmentError.errors.txt | 44 ++++++++++++ ...nmentErrorFromMissingIdentifier.errors.txt | 48 +++++++++++++ .../objectLiteralShorthandPropertiesES6.js | 39 +++++++++++ ...objectLiteralShorthandPropertiesES6.types} | 29 +------- ...rrorFromNoneExistingIdentifier.errors.txt} | 4 +- ...opertiesErrorFromNoneExistingIdentifier.js | 12 ++++ ...iesErrorFromNotUsingIdentifier.errors.txt} | 36 +++++----- ...jectLiteralShorthandPropertiesTargetES6.js | 62 ----------------- ...ctLiteralShorthandPropertiesWithModule.js} | 4 +- ...iteralShorthandPropertiesWithModule.types} | 2 +- ...LiteralShorthandPropertiesWithModuleES6.js | 28 ++++++++ ...eralShorthandPropertiesWithModuleES6.types | 30 ++++++++ ...ectLiteralShorthandPropertiesAssignment.ts | 9 +++ ...ctLiteralShorthandPropertiesAssignment2.ts | 4 -- ...LiteralShorthandPropertiesAssignmentES6.ts | 14 ++++ ...teralShorthandPropertiesAssignmentError.ts | 9 +++ ...iesAssignmentErrorFromMissingIdentifier.ts | 8 +++ .../objectLiteralShorthandPropertiesES6.ts | 20 ++++++ ...pertiesErrorFromNoneExistingIdentifier.ts} | 0 ...dPropertiesErrorFromNotUsingIdentifier.ts} | 0 ...jectLiteralShorthandPropertiesTargetES6.ts | 31 --------- ...ectLiteralShorthandPropertiesWithModule.ts | 14 ++++ ...iteralShorthandPropertiesWithModuleES6.ts} | 2 +- ...tionEntryForShorthandPropertyAssignment.ts | 6 ++ ...etionListForShorthandPropertyAssignment.ts | 7 ++ ...tionListForShorthandPropertyAssignment2.ts | 7 ++ ...dAllRefsWithShorthandPropertyAssignment.ts | 19 ++++++ ...AllRefsWithShorthandPropertyAssignment2.ts | 23 +++++++ 35 files changed, 544 insertions(+), 185 deletions(-) delete mode 100644 tests/baselines/reference/objectLiteralShorthandProperties4.js delete mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt delete mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.types create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentError.errors.txt create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.errors.txt create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesES6.js rename tests/baselines/reference/{objectLiteralShorthandPropertiesTargetES6.types => objectLiteralShorthandPropertiesES6.types} (59%) rename tests/baselines/reference/{objectLiteralShorthandProperties4.errors.txt => objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt} (58%) create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js rename tests/baselines/reference/{objectLiteralShorthandProperties2.errors.txt => objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt} (55%) delete mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js rename tests/baselines/reference/{objectLiteralShorthandProperties3.js => objectLiteralShorthandPropertiesWithModule.js} (71%) rename tests/baselines/reference/{objectLiteralShorthandProperties3.types => objectLiteralShorthandPropertiesWithModule.types} (82%) create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.js create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.types delete mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentES6.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesES6.ts rename tests/cases/conformance/es6/shorthandPropertyAssignment/{objectLiteralShorthandProperties4.ts => objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts} (100%) rename tests/cases/conformance/es6/shorthandPropertyAssignment/{objectLiteralShorthandProperties2.ts => objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts} (100%) delete mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts rename tests/cases/conformance/es6/shorthandPropertyAssignment/{objectLiteralShorthandProperties3.ts => objectLiteralShorthandPropertiesWithModuleES6.ts} (77%) create mode 100644 tests/cases/fourslash/completionEntryForShorthandPropertyAssignment.ts create mode 100644 tests/cases/fourslash/completionListForShorthandPropertyAssignment.ts create mode 100644 tests/cases/fourslash/completionListForShorthandPropertyAssignment2.ts create mode 100644 tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment.ts create mode 100644 tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment2.ts diff --git a/tests/baselines/reference/objectLiteralShorthandProperties4.js b/tests/baselines/reference/objectLiteralShorthandProperties4.js deleted file mode 100644 index ed8ee494b17d7..0000000000000 --- a/tests/baselines/reference/objectLiteralShorthandProperties4.js +++ /dev/null @@ -1,12 +0,0 @@ -//// [objectLiteralShorthandProperties4.ts] -var x = { - x, // OK - undefinedVariable // Error -} - - -//// [objectLiteralShorthandProperties4.js] -var x = { - x: x, - undefinedVariable: undefinedVariable // Error -}; diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js index 6af0a69a3044d..2ef29a3708402 100644 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js @@ -3,9 +3,34 @@ var id: number = 10000; var name: string = "my name"; var person: { name: string; id: number } = { name, id }; +function foo( obj:{ name: string }): void { }; +function bar(name: string, id: number) { return { name, id }; } +function bar1(name: string, id: number) { return { name }; } +function baz(name: string, id: number): { name: string; id: number } { return { name, id }; } + +foo(person); +var person1 = bar("Hello", 5); +var person2: { name: string } = bar("Hello", 5); +var person3: { name: string; id:number } = bar("Hello", 5); //// [objectLiteralShorthandPropertiesAssignment.js] var id = 10000; var name = "my name"; var person = { name: name, id: id }; +function foo(obj) { +} +; +function bar(name, id) { + return { name: name, id: id }; +} +function bar1(name, id) { + return { name: name }; +} +function baz(name, id) { + return { name: name, id: id }; +} +foo(person); +var person1 = bar("Hello", 5); +var person2 = bar("Hello", 5); +var person3 = bar("Hello", 5); diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types index f7f18c8f0c47d..91d9e528d6355 100644 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types @@ -13,3 +13,56 @@ var person: { name: string; id: number } = { name, id }; >name : string >id : number +function foo( obj:{ name: string }): void { }; +>foo : (obj: { name: string; }) => void +>obj : { name: string; } +>name : string + +function bar(name: string, id: number) { return { name, id }; } +>bar : (name: string, id: number) => { name: string; id: number; } +>name : string +>id : number +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + +function bar1(name: string, id: number) { return { name }; } +>bar1 : (name: string, id: number) => { name: string; } +>name : string +>id : number +>{ name } : { name: string; } +>name : string + +function baz(name: string, id: number): { name: string; id: number } { return { name, id }; } +>baz : (name: string, id: number) => { name: string; id: number; } +>name : string +>id : number +>name : string +>id : number +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + +foo(person); +>foo(person) : void +>foo : (obj: { name: string; }) => void +>person : { name: string; id: number; } + +var person1 = bar("Hello", 5); +>person1 : { name: string; id: number; } +>bar("Hello", 5) : { name: string; id: number; } +>bar : (name: string, id: number) => { name: string; id: number; } + +var person2: { name: string } = bar("Hello", 5); +>person2 : { name: string; } +>name : string +>bar("Hello", 5) : { name: string; id: number; } +>bar : (name: string, id: number) => { name: string; id: number; } + +var person3: { name: string; id:number } = bar("Hello", 5); +>person3 : { name: string; id: number; } +>name : string +>id : number +>bar("Hello", 5) : { name: string; id: number; } +>bar : (name: string, id: number) => { name: string; id: number; } + diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt deleted file mode 100644 index a3ae961f6ea42..0000000000000 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.errors.txt +++ /dev/null @@ -1,13 +0,0 @@ -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts(4,5): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. - Property 'b' is missing in type '{ name: string; id: number; }'. - - -==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts (1 errors) ==== - var id: number = 10000; - var name: string = "my name"; - - var person: { b: string; id: number } = { name, id }; // error - ~~~~~~ -!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. -!!! error TS2322: Property 'b' is missing in type '{ name: string; id: number; }'. - \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js deleted file mode 100644 index f37bb1cd52581..0000000000000 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment2.js +++ /dev/null @@ -1,11 +0,0 @@ -//// [objectLiteralShorthandPropertiesAssignment2.ts] -var id: number = 10000; -var name: string = "my name"; - -var person: { b: string; id: number } = { name, id }; // error - - -//// [objectLiteralShorthandPropertiesAssignment2.js] -var id = 10000; -var name = "my name"; -var person = { name: name, id: id }; // error diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.js b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.js new file mode 100644 index 0000000000000..b2f9004b076ce --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.js @@ -0,0 +1,36 @@ +//// [objectLiteralShorthandPropertiesAssignmentES6.ts] +var id: number = 10000; +var name: string = "my name"; + +var person: { name: string; id: number } = { name, id }; +function foo(obj: { name: string }): void { }; +function bar(name: string, id: number) { return { name, id }; } +function bar1(name: string, id: number) { return { name }; } +function baz(name: string, id: number): { name: string; id: number } { return { name, id }; } + +foo(person); +var person1 = bar("Hello", 5); +var person2: { name: string } = bar("Hello", 5); +var person3: { name: string; id: number } = bar("Hello", 5); + + +//// [objectLiteralShorthandPropertiesAssignmentES6.js] +var id = 10000; +var name = "my name"; +var person = { name, id }; +function foo(obj) { +} +; +function bar(name, id) { + return { name, id }; +} +function bar1(name, id) { + return { name }; +} +function baz(name, id) { + return { name, id }; +} +foo(person); +var person1 = bar("Hello", 5); +var person2 = bar("Hello", 5); +var person3 = bar("Hello", 5); diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.types b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.types new file mode 100644 index 0000000000000..38791fc1beb94 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentES6.types @@ -0,0 +1,68 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentES6.ts === +var id: number = 10000; +>id : number + +var name: string = "my name"; +>name : string + +var person: { name: string; id: number } = { name, id }; +>person : { name: string; id: number; } +>name : string +>id : number +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + +function foo(obj: { name: string }): void { }; +>foo : (obj: { name: string; }) => void +>obj : { name: string; } +>name : string + +function bar(name: string, id: number) { return { name, id }; } +>bar : (name: string, id: number) => { name: string; id: number; } +>name : string +>id : number +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + +function bar1(name: string, id: number) { return { name }; } +>bar1 : (name: string, id: number) => { name: string; } +>name : string +>id : number +>{ name } : { name: string; } +>name : string + +function baz(name: string, id: number): { name: string; id: number } { return { name, id }; } +>baz : (name: string, id: number) => { name: string; id: number; } +>name : string +>id : number +>name : string +>id : number +>{ name, id } : { name: string; id: number; } +>name : string +>id : number + +foo(person); +>foo(person) : void +>foo : (obj: { name: string; }) => void +>person : { name: string; id: number; } + +var person1 = bar("Hello", 5); +>person1 : { name: string; id: number; } +>bar("Hello", 5) : { name: string; id: number; } +>bar : (name: string, id: number) => { name: string; id: number; } + +var person2: { name: string } = bar("Hello", 5); +>person2 : { name: string; } +>name : string +>bar("Hello", 5) : { name: string; id: number; } +>bar : (name: string, id: number) => { name: string; id: number; } + +var person3: { name: string; id: number } = bar("Hello", 5); +>person3 : { name: string; id: number; } +>name : string +>id : number +>bar("Hello", 5) : { name: string; id: number; } +>bar : (name: string, id: number) => { name: string; id: number; } + diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentError.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentError.errors.txt new file mode 100644 index 0000000000000..2db47d0f7ae57 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentError.errors.txt @@ -0,0 +1,44 @@ +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(5,16): error TS1131: Property or signature expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(5,25): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(6,53): error TS1005: ';' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(4,5): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. + Property 'b' is missing in type '{ name: string; id: number; }'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(5,22): error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'number', but here has type 'any'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(6,79): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ id: string; name: number; }'. + Types of property 'id' are incompatible. + Type 'number' is not assignable to type 'string'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(8,5): error TS2345: Argument of type '{ name: string; id: number; }' is not assignable to parameter of type '{ name: string; id: boolean; }'. + Types of property 'id' are incompatible. + Type 'number' is not assignable to type 'boolean'. + + +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts (7 errors) ==== + var id: number = 10000; + var name: string = "my name"; + + var person: { b: string; id: number } = { name, id }; // error + ~~~~~~ +!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. +!!! error TS2322: Property 'b' is missing in type '{ name: string; id: number; }'. + var person1: { name, id }; // error: can't use short-hand property assignment in type position + ~~~~ +!!! error TS1131: Property or signature expected. + ~ +!!! error TS1128: Declaration or statement expected. + ~~ +!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'number', but here has type 'any'. + function foo(name: string, id: number): { id: string, name: number } { return { name, id }; } // error + ~ +!!! error TS1005: ';' expected. + ~~~~~~~~~~~~ +!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ id: string; name: number; }'. +!!! error TS2322: Types of property 'id' are incompatible. +!!! error TS2322: Type 'number' is not assignable to type 'string'. + function bar(obj: { name: string; id: boolean }) { } + bar({ name, id }); // error + ~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ name: string; id: number; }' is not assignable to parameter of type '{ name: string; id: boolean; }'. +!!! error TS2345: Types of property 'id' are incompatible. +!!! error TS2345: Type 'number' is not assignable to type 'boolean'. + + \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.errors.txt new file mode 100644 index 0000000000000..708bbb0935f85 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.errors.txt @@ -0,0 +1,48 @@ +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(5,55): error TS1005: ';' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(6,55): error TS1005: ';' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(7,16): error TS1131: Property or signature expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(7,25): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(8,28): error TS1005: ';' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(4,5): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. + Property 'b' is missing in type '{ name: string; id: number; }'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(5,79): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ name: number; id: string; }'. + Types of property 'name' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(7,22): error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'number', but here has type 'any'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(8,5): error TS2322: Type '{ name: number; id: string; }' is not assignable to type '{ name: string; id: number; }'. + Types of property 'name' are incompatible. + Type 'number' is not assignable to type 'string'. + + +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts (9 errors) ==== + var id: number = 10000; + var name: string = "my name"; + + var person: { b: string; id: number } = { name, id }; // error + ~~~~~~ +!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'. +!!! error TS2322: Property 'b' is missing in type '{ name: string; id: number; }'. + function bar(name: string, id: number): { name: number, id: string } { return { name, id }; } // error + ~ +!!! error TS1005: ';' expected. + ~~~~~~~~~~~~ +!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ name: number; id: string; }'. +!!! error TS2322: Types of property 'name' are incompatible. +!!! error TS2322: Type 'string' is not assignable to type 'number'. + function foo(name: string, id: number): { name: string, id: number } { return { name, id }; } // error + ~ +!!! error TS1005: ';' expected. + var person1: { name, id }; // error : Can't use shorthand in the type position + ~~~~ +!!! error TS1131: Property or signature expected. + ~ +!!! error TS1128: Declaration or statement expected. + ~~ +!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'number', but here has type 'any'. + var person2: { name: string, id: number } = bar("hello", 5); + ~ +!!! error TS1005: ';' expected. + ~~~~~~~ +!!! error TS2322: Type '{ name: number; id: string; }' is not assignable to type '{ name: string; id: number; }'. +!!! error TS2322: Types of property 'name' are incompatible. +!!! error TS2322: Type 'number' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesES6.js b/tests/baselines/reference/objectLiteralShorthandPropertiesES6.js new file mode 100644 index 0000000000000..b398a23135161 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesES6.js @@ -0,0 +1,39 @@ +//// [objectLiteralShorthandPropertiesES6.ts] +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + + + +//// [objectLiteralShorthandPropertiesES6.js] +var a, b, c; +var x1 = { + a +}; +var x2 = { + a, +}; +var x3 = { + a: 0, + b, + c, + d: function () { + }, + x3, + parent: x3 +}; diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types b/tests/baselines/reference/objectLiteralShorthandPropertiesES6.types similarity index 59% rename from tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types rename to tests/baselines/reference/objectLiteralShorthandPropertiesES6.types index 560ae4af4c02f..0383a3f5d2efc 100644 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.types +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesES6.types @@ -1,4 +1,4 @@ -=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts === +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesES6.ts === var a, b, c; >a : any >b : any @@ -47,31 +47,4 @@ var x3 = { }; -module m { ->m : typeof m - - export var x; ->x : any -} - -module m { ->m : typeof m - - var z = x; ->z : any ->x : any - - var y = { ->y : { a: any; x: any; } ->{ a: x, x } : { a: any; x: any; } - - a: x, ->a : any ->x : any - - x ->x : any - - }; -} diff --git a/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt similarity index 58% rename from tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt rename to tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt index 678e18143e94e..fcd04b27754ec 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties4.errors.txt +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'. -==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts (1 errors) ==== +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts (1 errors) ==== var x = { x, // OK undefinedVariable // Error diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js new file mode 100644 index 0000000000000..6050e5b756f08 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js @@ -0,0 +1,12 @@ +//// [objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts] +var x = { + x, // OK + undefinedVariable // Error +} + + +//// [objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js] +var x = { + x: x, + undefinedVariable: undefinedVariable // Error +}; diff --git a/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt similarity index 55% rename from tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt rename to tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt index 0e6b9eba6162b..0a317d70f1078 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties2.errors.txt +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt @@ -1,23 +1,23 @@ -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(3,20): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(4,7): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(5,10): error TS1005: '(' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(6,10): error TS1005: '(' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(7,9): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(8,10): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(9,8): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(10,10): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(12,1): error TS1005: ':' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(15,6): error TS1005: ',' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(16,6): error TS1005: ',' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(17,6): error TS1005: '=' expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(18,1): error TS1128: Declaration or statement expected. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(15,5): error TS2300: Duplicate identifier 'a'. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(15,7): error TS2304: Cannot find name 'b'. -tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts(16,5): error TS2300: Duplicate identifier 'a'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(3,20): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(4,7): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(5,10): error TS1005: '(' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(6,10): error TS1005: '(' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(7,9): error TS1005: ':' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(8,10): error TS1005: ':' expected. +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,6): error TS1005: ',' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: '=' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(18,1): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS2300: Duplicate identifier 'a'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,7): error TS2304: Cannot find name 'b'. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2300: Duplicate identifier 'a'. -==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts (17 errors) ==== +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (17 errors) ==== // errors var y = { "stringLiteral", diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js b/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js deleted file mode 100644 index a35fc69770685..0000000000000 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesTargetES6.js +++ /dev/null @@ -1,62 +0,0 @@ -//// [objectLiteralShorthandPropertiesTargetES6.ts] -var a, b, c; - -var x1 = { - a -}; - -var x2 = { - a, -} - -var x3 = { - a: 0, - b, - c, - d() { }, - x3, - parent: x3 -}; - -module m { - export var x; -} - -module m { - var z = x; - var y = { - a: x, - x - }; -} - - -//// [objectLiteralShorthandPropertiesTargetES6.js] -var a, b, c; -var x1 = { - a -}; -var x2 = { - a, -}; -var x3 = { - a: 0, - b, - c, - d: function () { - }, - x3, - parent: x3 -}; -var m; -(function (m) { - m.x; -})(m || (m = {})); -var m; -(function (m) { - var z = m.x; - var y = { - a: m.x, - x: m.x - }; -})(m || (m = {})); diff --git a/tests/baselines/reference/objectLiteralShorthandProperties3.js b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.js similarity index 71% rename from tests/baselines/reference/objectLiteralShorthandProperties3.js rename to tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.js index 0d436efa787b2..e2eb1fc128c20 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties3.js +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.js @@ -1,4 +1,4 @@ -//// [objectLiteralShorthandProperties3.ts] +//// [objectLiteralShorthandPropertiesWithModule.ts] // module export module m { @@ -14,7 +14,7 @@ module m { } -//// [objectLiteralShorthandProperties3.js] +//// [objectLiteralShorthandPropertiesWithModule.js] // module export var m; (function (m) { diff --git a/tests/baselines/reference/objectLiteralShorthandProperties3.types b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.types similarity index 82% rename from tests/baselines/reference/objectLiteralShorthandProperties3.types rename to tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.types index 5d58532b3d1fc..77e139a3426f3 100644 --- a/tests/baselines/reference/objectLiteralShorthandProperties3.types +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.types @@ -1,4 +1,4 @@ -=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts === +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts === // module export module m { diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.js b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.js new file mode 100644 index 0000000000000..edb4e6c295328 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.js @@ -0,0 +1,28 @@ +//// [objectLiteralShorthandPropertiesWithModuleES6.ts] + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} + + +//// [objectLiteralShorthandPropertiesWithModuleES6.js] +var m; +(function (m) { + m.x; +})(m || (m = {})); +var m; +(function (m) { + var z = m.x; + var y = { + a: m.x, + x: m.x + }; +})(m || (m = {})); diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.types b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.types new file mode 100644 index 0000000000000..1030010d6c640 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModuleES6.types @@ -0,0 +1,30 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModuleES6.ts === + +module m { +>m : typeof m + + export var x; +>x : any +} + +module m { +>m : typeof m + + var z = x; +>z : any +>x : any + + var y = { +>y : { a: any; x: any; } +>{ a: x, x } : { a: any; x: any; } + + a: x, +>a : any +>x : any + + x +>x : any + + }; +} + diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts index 44448a26a9b71..38782100166bf 100644 --- a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts @@ -2,3 +2,12 @@ var name: string = "my name"; var person: { name: string; id: number } = { name, id }; +function foo( obj:{ name: string }): void { }; +function bar(name: string, id: number) { return { name, id }; } +function bar1(name: string, id: number) { return { name }; } +function baz(name: string, id: number): { name: string; id: number } { return { name, id }; } + +foo(person); +var person1 = bar("Hello", 5); +var person2: { name: string } = bar("Hello", 5); +var person3: { name: string; id:number } = bar("Hello", 5); diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts deleted file mode 100644 index a3f88036dd88b..0000000000000 --- a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment2.ts +++ /dev/null @@ -1,4 +0,0 @@ -var id: number = 10000; -var name: string = "my name"; - -var person: { b: string; id: number } = { name, id }; // error diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentES6.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentES6.ts new file mode 100644 index 0000000000000..19fd21b47b573 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentES6.ts @@ -0,0 +1,14 @@ +// @target: es6 +var id: number = 10000; +var name: string = "my name"; + +var person: { name: string; id: number } = { name, id }; +function foo(obj: { name: string }): void { }; +function bar(name: string, id: number) { return { name, id }; } +function bar1(name: string, id: number) { return { name }; } +function baz(name: string, id: number): { name: string; id: number } { return { name, id }; } + +foo(person); +var person1 = bar("Hello", 5); +var person2: { name: string } = bar("Hello", 5); +var person3: { name: string; id: number } = bar("Hello", 5); diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts new file mode 100644 index 0000000000000..b745b93b3ca15 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts @@ -0,0 +1,9 @@ +var id: number = 10000; +var name: string = "my name"; + +var person: { b: string; id: number } = { name, id }; // error +var person1: { name, id }; // error: can't use short-hand property assignment in type position +function foo(name: string, id: number): { id: string, name: number } { return { name, id }; } // error +function bar(obj: { name: string; id: boolean }) { } +bar({ name, id }); // error + diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts new file mode 100644 index 0000000000000..6b0943a8d77be --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts @@ -0,0 +1,8 @@ +var id: number = 10000; +var name: string = "my name"; + +var person: { b: string; id: number } = { name, id }; // error +function bar(name: string, id: number): { name: number, id: string } { return { name, id }; } // error +function foo(name: string, id: number): { name: string, id: number } { return { name, id }; } // error +var person1: { name, id }; // error : Can't use shorthand in the type position +var person2: { name: string, id: number } = bar("hello", 5); \ No newline at end of file diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesES6.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesES6.ts new file mode 100644 index 0000000000000..bf4c2583c078d --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesES6.ts @@ -0,0 +1,20 @@ +// @target: es6 +var a, b, c; + +var x1 = { + a +}; + +var x2 = { + a, +} + +var x3 = { + a: 0, + b, + c, + d() { }, + x3, + parent: x3 +}; + diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts similarity index 100% rename from tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties4.ts rename to tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts similarity index 100% rename from tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties2.ts rename to tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts deleted file mode 100644 index 21351372de2df..0000000000000 --- a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesTargetES6.ts +++ /dev/null @@ -1,31 +0,0 @@ -// @target: es6 -var a, b, c; - -var x1 = { - a -}; - -var x2 = { - a, -} - -var x3 = { - a: 0, - b, - c, - d() { }, - x3, - parent: x3 -}; - -module m { - export var x; -} - -module m { - var z = x; - var y = { - a: x, - x - }; -} diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts new file mode 100644 index 0000000000000..3f609089e2c5f --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts @@ -0,0 +1,14 @@ +// module export +var x = "Foo"; +module m { + export var x; +} + +module m { + var z = 10000; + export var y = { + x + }; +} + +m.y.x; diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModuleES6.ts similarity index 77% rename from tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts rename to tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModuleES6.ts index 005885bb9018f..f8937625afefa 100644 --- a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandProperties3.ts +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModuleES6.ts @@ -1,4 +1,4 @@ -// module export +// @target: es6 module m { export var x; diff --git a/tests/cases/fourslash/completionEntryForShorthandPropertyAssignment.ts b/tests/cases/fourslash/completionEntryForShorthandPropertyAssignment.ts new file mode 100644 index 0000000000000..dae5762d92cf3 --- /dev/null +++ b/tests/cases/fourslash/completionEntryForShorthandPropertyAssignment.ts @@ -0,0 +1,6 @@ +/// + +//// var person: {name:string; id:number} = {n/**/ + +goTo.marker(); +verify.completionListContains("name", /*text*/ undefined, /*documentation*/ undefined, "property"); \ No newline at end of file diff --git a/tests/cases/fourslash/completionListForShorthandPropertyAssignment.ts b/tests/cases/fourslash/completionListForShorthandPropertyAssignment.ts new file mode 100644 index 0000000000000..d8262d710474c --- /dev/null +++ b/tests/cases/fourslash/completionListForShorthandPropertyAssignment.ts @@ -0,0 +1,7 @@ +/// + +//// var person: {name:string; id: number} = { n/**/ + +goTo.marker(); +verify.memberListContains('name'); +verify.memberListContains('id'); \ No newline at end of file diff --git a/tests/cases/fourslash/completionListForShorthandPropertyAssignment2.ts b/tests/cases/fourslash/completionListForShorthandPropertyAssignment2.ts new file mode 100644 index 0000000000000..d8262d710474c --- /dev/null +++ b/tests/cases/fourslash/completionListForShorthandPropertyAssignment2.ts @@ -0,0 +1,7 @@ +/// + +//// var person: {name:string; id: number} = { n/**/ + +goTo.marker(); +verify.memberListContains('name'); +verify.memberListContains('id'); \ No newline at end of file diff --git a/tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment.ts b/tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment.ts new file mode 100644 index 0000000000000..7a1f7a0225fa7 --- /dev/null +++ b/tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment.ts @@ -0,0 +1,19 @@ +/// + +//// var /*1*/name = "Foo"; +//// +//// var obj = { /*2*/name }; +//// var obj1 = { /*3*/name:name }; +//// obj./*4*/name; + +goTo.marker('1'); +verify.referencesCountIs(3); + +goTo.marker('2'); +verify.referencesCountIs(4); + +goTo.marker('3'); +verify.referencesCountIs(1); + +goTo.marker('4'); +verify.referencesCountIs(2); diff --git a/tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment2.ts b/tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment2.ts new file mode 100644 index 0000000000000..e9bb7bf02a86d --- /dev/null +++ b/tests/cases/fourslash/findAllRefsWithShorthandPropertyAssignment2.ts @@ -0,0 +1,23 @@ +/// + +//// var /*1*/dx = "Foo"; +//// +//// module M { export var /*2*/dx; } +//// module M { +//// var z = 100; +//// export var y = { /*3*/dx, z }; +//// } +//// M.y./*4*/dx; + +goTo.marker('1'); +debugger; +verify.referencesCountIs(1); + +goTo.marker('2'); +verify.referencesCountIs(2); + +goTo.marker('3'); +verify.referencesCountIs(3); + +goTo.marker('4'); +verify.referencesCountIs(2); From 880e958d118deeea13d23a7e7a726b7c8d54ac59 Mon Sep 17 00:00:00 2001 From: Yui T Date: Fri, 14 Nov 2014 16:30:03 -0800 Subject: [PATCH 12/13] Fix comment --- src/compiler/emitter.ts | 3 ++- src/compiler/parser.ts | 1 + ...tiesErrorFromNotUsingIdentifier.errors.txt | 9 +++++-- ...rthandPropertiesErrorWithModule.errors.txt | 24 +++++++++++++++++++ ...ndPropertiesErrorFromNotUsingIdentifier.ts | 4 +++- ...teralShorthandPropertiesErrorWithModule.ts | 14 +++++++++++ ...ectLiteralShorthandPropertiesWithModule.ts | 9 ++++--- 7 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.errors.txt create mode 100644 tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 5781875ac9b65..b577f66b97844 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1047,7 +1047,8 @@ module ts { // Emit identifier as an identifier emit(node.name); write(": "); - // Even though this is stored as identified because it is in short-hand property assignment, treated it as expression + // Even though this is stored as identified because it is in short-hand property assignment, + // treated it as expression emitExpressionIdentifier(node.name); emitTrailingComments(node); } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 2fe3b8aa67001..43e6aee153c13 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2687,6 +2687,7 @@ module ts { (node).initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); return finishNode(node); } + // Disallow optional property assignment if (token === SyntaxKind.QuestionToken) { var questionStart = scanner.getTokenPos(); grammarErrorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional); diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt index 0a317d70f1078..3c5f4e97d8046 100644 --- a/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt @@ -11,13 +11,14 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected. tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: '=' expected. tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(18,1): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(20,17): error TS1005: ':' expected. tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement. tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS2300: Duplicate identifier 'a'. tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,7): error TS2304: Cannot find name 'b'. tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2300: Duplicate identifier 'a'. -==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (17 errors) ==== +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (18 errors) ==== // errors var y = { "stringLiteral", @@ -69,4 +70,8 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr !!! error TS1005: '=' expected. }; ~ -!!! error TS1128: Declaration or statement expected. \ No newline at end of file +!!! error TS1128: Declaration or statement expected. + + var v = { class }; // error + ~ +!!! error TS1005: ':' expected. \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.errors.txt new file mode 100644 index 0000000000000..f5fd8ed53f25c --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(10,10): error TS1005: ',' expected. +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(14,3): error TS2339: Property 'y' does not exist on type 'typeof m'. + + +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts (2 errors) ==== + // module export + var x = "Foo"; + module m { + export var x; + } + + module n { + var z = 10000; + export var y = { + m.x // error + ~ +!!! error TS1005: ',' expected. + }; + } + + m.y.x; + ~ +!!! error TS2339: Property 'y' does not exist on type 'typeof m'. + \ No newline at end of file diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts index 38081e7d69111..527154c66bd73 100644 --- a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts @@ -15,4 +15,6 @@ var x = { a.b, a["ss"], a[1], -}; \ No newline at end of file +}; + +var v = { class }; // error \ No newline at end of file diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts new file mode 100644 index 0000000000000..7da6e7b0c709f --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts @@ -0,0 +1,14 @@ +// module export +var x = "Foo"; +module m { + export var x; +} + +module n { + var z = 10000; + export var y = { + m.x // error + }; +} + +m.y.x; diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts index 3f609089e2c5f..005885bb9018f 100644 --- a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts @@ -1,14 +1,13 @@ // module export -var x = "Foo"; + module m { export var x; } module m { - var z = 10000; - export var y = { + var z = x; + var y = { + a: x, x }; } - -m.y.x; From b3078f265ee65c0538d80e11173a327625817523 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 17 Nov 2014 10:56:59 -0800 Subject: [PATCH 13/13] Add a test for quick-info --- .../quickInfoForShorthandProperty.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/cases/fourslash/quickInfoForShorthandProperty.ts diff --git a/tests/cases/fourslash/quickInfoForShorthandProperty.ts b/tests/cases/fourslash/quickInfoForShorthandProperty.ts new file mode 100644 index 0000000000000..21ba9db683de2 --- /dev/null +++ b/tests/cases/fourslash/quickInfoForShorthandProperty.ts @@ -0,0 +1,21 @@ +/// + +//// var name1 = undefined, id1 = undefined; +//// var /*obj1*/obj1 = {/*name1*/name1, /*id1*/id1}; +//// var name2 = "Hello"; +//// var id2 = 10000; +//// var /*obj2*/obj2 = {/*name2*/name2, /*id2*/id2}; + +goTo.marker("obj1"); +verify.quickInfoIs("(var) obj1: {\n name1: any;\n id1: any;\n}"); +goTo.marker("name1"); +verify.quickInfoIs("(property) name1: any"); +goTo.marker("id1"); +verify.quickInfoIs("(property) id1: any"); + +goTo.marker("obj2"); +verify.quickInfoIs("(var) obj2: {\n name2: string;\n id2: number;\n}"); +goTo.marker("name2"); +verify.quickInfoIs("(property) name2: string"); +goTo.marker("id2"); +verify.quickInfoIs("(property) id2: number");