diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 18fd8c711b852..3622f4cda2fa9 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -380,6 +380,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 0e42f2eb4eea4..7d35478f9336d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -93,6 +93,7 @@ module ts { getReturnTypeOfSignature: getReturnTypeOfSignature, getSymbolsInScope: getSymbolsInScope, getSymbolInfo: getSymbolInfo, + getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, getTypeOfNode: getTypeOfNode, typeToString: typeToString, getSymbolDisplayBuilder: getSymbolDisplayBuilder, @@ -109,6 +110,7 @@ module ts { getAliasedSymbol: resolveImport, isUndefinedSymbol: symbol => symbol === undefinedSymbol, isArgumentsSymbol: symbol => symbol === argumentsSymbol, + hasEarlyErrors: hasEarlyErrors, isEmitBlocked: isEmitBlocked }; @@ -1665,6 +1667,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); @@ -2399,7 +2408,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) { @@ -2434,7 +2443,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); } @@ -2447,7 +2456,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); } @@ -5000,7 +5009,15 @@ 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 { + Debug.assert(memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment); + type = checkExpression(memberDecl.name, contextualMapper); + } var prop = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.name); prop.declarations = member.declarations; prop.parent = member.parent; @@ -8816,6 +8833,16 @@ module ts { return undefined; } + 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 { 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 bae908b27742e..acb7aaaed26a4 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: 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 93638b6dadeba..9145b345b2d9a 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": 1160 + }, + "Duplicate identifier '{0}'.": { "category": "Error", "code": 2300 diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 00d958604c7e8..fe80e48dc6417 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"); @@ -1033,6 +1041,36 @@ module ts { emitTrailingComments(node); } + function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) { + 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); + } + } + } + function tryEmitConstantValue(node: PropertyAccess | IndexedAccess): boolean { var constantValue = resolver.getConstantValue(node); if (constantValue !== undefined) { @@ -2250,6 +2288,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 9cb61feeea676..924f6e83930b5 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -202,6 +202,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); @@ -580,6 +581,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.FunctionDeclaration: @@ -2729,10 +2731,14 @@ module ts { return finishNode(node); } - function parsePropertyAssignment(): PropertyDeclaration { - var node = createNode(SyntaxKind.PropertyAssignment); - node.name = parsePropertyName(); + function parsePropertyAssignment(): Declaration { + var nodePos = scanner.getStartPos(); + var nameToken = token; + var propertyName = parsePropertyName(); + var node: Declaration; if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) { + 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 @@ -2740,11 +2746,26 @@ 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); + } + // 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); + 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 { + node = createNode(SyntaxKind.PropertyAssignment, nodePos); + node.name = propertyName; parseExpected(SyntaxKind.ColonToken); - node.initializer = parseAssignmentExpression(false); + (node).initializer = parseAssignmentExpression(false); } return finishNode(node); } @@ -2794,6 +2815,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 810b5a759e464..19e0b79f42376 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -167,6 +167,7 @@ module ts { ArrayLiteral, ObjectLiteral, PropertyAssignment, + ShorthandPropertyAssignment, PropertyAccess, IndexedAccess, CallExpression, @@ -335,6 +336,10 @@ module ts { initializer?: Expression; } + export interface ShortHandPropertyDeclaration extends Declaration { + name: Identifier; + } + export interface ParameterDeclaration extends VariableDeclaration { } /** @@ -719,6 +724,7 @@ module ts { getReturnTypeOfSignature(signature: Signature): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; getSymbolInfo(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 1eed221d403dd..334bcc9da7e6a 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2051,7 +2051,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 { @@ -2745,7 +2745,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; } @@ -4165,8 +4165,21 @@ module ts { } var referenceSymbol = typeInfoResolver.getSymbolInfo(referenceLocation); - if (referenceSymbol && isRelatableToSearchSet(searchSymbols, referenceSymbol, referenceLocation)) { - result.push(getReferenceEntryFromNode(referenceLocation)); + 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)); + } } }); } @@ -4334,6 +4347,22 @@ module ts { forEach(getPropertySymbolsFromContextualType(location), contextualSymbol => { result.push.apply(result, typeInfoResolver.getRootSymbols(contextualSymbol)); }); + + /* 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); + } } // If this is a union property, add all the symbols from all its source symbols in all unioned types. @@ -4674,6 +4703,7 @@ module ts { case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: case SyntaxKind.PropertyAssignment: + case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.Constructor: 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/objectLiteralShorthandProperties.js b/tests/baselines/reference/objectLiteralShorthandProperties.js new file mode 100644 index 0000000000000..776b2bbe45408 --- /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..f28f25e815c48 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandProperties.types @@ -0,0 +1,50 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/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/objectLiteralShorthandPropertiesAssignment.js b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js new file mode 100644 index 0000000000000..2ef29a3708402 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.js @@ -0,0 +1,36 @@ +//// [objectLiteralShorthandPropertiesAssignment.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); + + +//// [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 new file mode 100644 index 0000000000000..91d9e528d6355 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesAssignment.types @@ -0,0 +1,68 @@ +=== 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 + +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/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/objectLiteralShorthandPropertiesES6.types b/tests/baselines/reference/objectLiteralShorthandPropertiesES6.types new file mode 100644 index 0000000000000..0383a3f5d2efc --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesES6.types @@ -0,0 +1,50 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesES6.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/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt new file mode 100644 index 0000000000000..fcd04b27754ec --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts(3,5): error TS2304: Cannot find name 'undefinedVariable'. + + +==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.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/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/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt new file mode 100644 index 0000000000000..3c5f4e97d8046 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt @@ -0,0 +1,77 @@ +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(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 (18 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. + + 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/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/objectLiteralShorthandPropertiesWithModule.js b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.js new file mode 100644 index 0000000000000..e2eb1fc128c20 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.js @@ -0,0 +1,30 @@ +//// [objectLiteralShorthandPropertiesWithModule.ts] +// module export + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + x + }; +} + + +//// [objectLiteralShorthandPropertiesWithModule.js] +// module export +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/objectLiteralShorthandPropertiesWithModule.types b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.types new file mode 100644 index 0000000000000..77e139a3426f3 --- /dev/null +++ b/tests/baselines/reference/objectLiteralShorthandPropertiesWithModule.types @@ -0,0 +1,31 @@ +=== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.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/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/baselines/reference/objectTypesWithOptionalProperties.errors.txt b/tests/baselines/reference/objectTypesWithOptionalProperties.errors.txt index 0622b7badb006..e73e8adc9303a 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 TS1160: 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 TS1160: 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 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/objectLiteralShorthandPropertiesAssignment.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts new file mode 100644 index 0000000000000..38782100166bf --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignment.ts @@ -0,0 +1,13 @@ +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/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/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts new file mode 100644 index 0000000000000..1f44dc18ae86a --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.ts @@ -0,0 +1,4 @@ +var x = { + x, // OK + undefinedVariable // Error +} diff --git a/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts new file mode 100644 index 0000000000000..527154c66bd73 --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts @@ -0,0 +1,20 @@ +// errors +var y = { + "stringLiteral", + 42, + get e, + set f, + this, + super, + var, + class, + typeof +}; + +var x = { + a.b, + a["ss"], + a[1], +}; + +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/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/objectLiteralShorthandPropertiesWithModule.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.ts new file mode 100644 index 0000000000000..005885bb9018f --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModule.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/objectLiteralShorthandPropertiesWithModuleES6.ts b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModuleES6.ts new file mode 100644 index 0000000000000..f8937625afefa --- /dev/null +++ b/tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesWithModuleES6.ts @@ -0,0 +1,13 @@ +// @target: es6 + +module m { + export var x; +} + +module m { + var z = x; + var y = { + a: x, + 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); 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");