diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 1aa32c68b6a..7acb99d624d 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -397,7 +397,7 @@ module ts { } break; case SyntaxKind.Property: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: bindDeclaration(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false); break; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2b205aaca60..3aefe76746d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1697,7 +1697,7 @@ module ts { if (declaration.initializer) { var type = checkAndMarkExpression(declaration.initializer); // Widening of property assignments is handled by checkObjectLiteral, exclude them here - if (declaration.kind !== SyntaxKind.LonghandPropertyAssignment) { + if (declaration.kind !== SyntaxKind.PropertyAssignment) { var unwidenedType = type; type = getWidenedType(type); if (type !== unwidenedType) { @@ -3244,7 +3244,7 @@ module ts { // Returns true if the given expression contains (at any level of nesting) a function or arrow expression // that is subject to contextual typing. - function isContextSensitiveCore(node: Expression | MethodDeclaration | PropertyAssignment): boolean { + function isContextSensitiveCore(node: Expression | MethodDeclaration | ObjectLiteralElement): boolean { Debug.assert(node.kind !== SyntaxKind.Method || isObjectLiteralMethod(node)); switch (node.kind) { case SyntaxKind.FunctionExpression: @@ -3260,8 +3260,8 @@ module ts { case SyntaxKind.BinaryExpression: return (node).operator === SyntaxKind.BarBarToken && (isContextSensitiveCore((node).left) || isContextSensitiveCore((node).right)); - case SyntaxKind.LonghandPropertyAssignment: - return isContextSensitiveCore((node).initializer); + case SyntaxKind.PropertyAssignment: + return isContextSensitiveCore((node).initializer); case SyntaxKind.Method: return isContextSensitiveFunctionLikeDeclaration(node); } @@ -4913,14 +4913,14 @@ module ts { return node.contextualType; } - return getContextualTypeForPropertyAssignment(node); + return getContextualTypeForObjectLiteralElement(node); } - function getContextualTypeForPropertyAssignment(propertyAssignment: PropertyAssignment) { - var objectLiteral = propertyAssignment.parent; + function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElement) { + var objectLiteral = element.parent; var type = getContextualType(objectLiteral); // TODO(jfreeman): Handle this case for computed names and symbols - var name = (propertyAssignment.name).text; + var name = (element.name).text; if (type && name) { return getTypeOfPropertyOfContextualType(type, name) || isNumericName(name) && getIndexTypeOfContextualType(type, IndexKind.Number) || @@ -4974,8 +4974,8 @@ module ts { return getTypeFromTypeNode((parent).type); case SyntaxKind.BinaryExpression: return getContextualTypeForBinaryOperand(node); - case SyntaxKind.LonghandPropertyAssignment: - return getContextualTypeForPropertyAssignment(parent); + case SyntaxKind.PropertyAssignment: + return getContextualTypeForObjectLiteralElement(parent); case SyntaxKind.ArrayLiteralExpression: return getContextualTypeForElementExpression(node); case SyntaxKind.ConditionalExpression: @@ -5111,10 +5111,10 @@ module ts { if (hasProperty(members, id)) { var member = members[id]; if (member.flags & SymbolFlags.Property || isObjectLiteralMethod(member.declarations[0])) { - var memberDecl = member.declarations[0]; + var memberDecl = member.declarations[0]; var type: Type; - if (memberDecl.kind === SyntaxKind.LonghandPropertyAssignment) { - type = checkExpression((memberDecl).initializer, contextualMapper); + if (memberDecl.kind === SyntaxKind.PropertyAssignment) { + type = checkExpression((memberDecl).initializer, contextualMapper); } else if (memberDecl.kind === SyntaxKind.Method) { type = checkObjectLiteralMethod(memberDecl, contextualMapper); @@ -8633,7 +8633,7 @@ module ts { case SyntaxKind.Property: case SyntaxKind.ArrayLiteralExpression: case SyntaxKind.ObjectLiteralExpression: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.PropertyAccessExpression: case SyntaxKind.ElementAccessExpression: case SyntaxKind.CallExpression: diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 9fccef4d6bf..03951f08b4c 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2122,7 +2122,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: @@ -3549,7 +3549,7 @@ module ts { return emitArrayLiteral(node); case SyntaxKind.ObjectLiteralExpression: return emitObjectLiteral(node); - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: return emitPropertyAssignment(node); case SyntaxKind.ComputedPropertyName: return emitComputedPropertyName(node); @@ -3590,7 +3590,6 @@ module ts { case SyntaxKind.Block: case SyntaxKind.TryBlock: case SyntaxKind.FinallyBlock: - case SyntaxKind.Block: case SyntaxKind.ModuleBlock: return emitBlock(node); case SyntaxKind.VariableStatement: diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 1d7a8e2be44..bc2bb211959 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -223,7 +223,7 @@ module ts { child((node).type) || child((node).initializer); case SyntaxKind.Property: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: return children(node.modifiers) || child((node).name) || @@ -589,7 +589,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.Property: case SyntaxKind.EnumMember: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: return (parent).initializer === node; case SyntaxKind.ExpressionStatement: case SyntaxKind.IfStatement: @@ -647,7 +647,7 @@ module ts { case SyntaxKind.Method: return (node).questionToken !== undefined; case SyntaxKind.ShorthandPropertyAssignment: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.Property: return (node).questionToken !== undefined; } @@ -686,7 +686,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: @@ -3166,7 +3166,7 @@ module ts { return finishNode(node); } - function parsePropertyAssignment(): PropertyAssignment { + function parseObjectLiteralElement(): ObjectLiteralElement { var fullStart = scanner.getStartPos(); var initialToken = token; @@ -3194,12 +3194,12 @@ module ts { return finishNode(shorthandDeclaration); } else { - var longhandDeclaration = createNode(SyntaxKind.LonghandPropertyAssignment, fullStart); - longhandDeclaration.name = propertyName; - longhandDeclaration.questionToken = questionToken; + var propertyAssignment = createNode(SyntaxKind.PropertyAssignment, fullStart); + propertyAssignment.name = propertyName; + propertyAssignment.questionToken = questionToken; parseExpected(SyntaxKind.ColonToken); - longhandDeclaration.initializer = allowInAnd(parseAssignmentExpressionOrHigher); - return finishNode(longhandDeclaration); + propertyAssignment.initializer = allowInAnd(parseAssignmentExpressionOrHigher); + return finishNode(propertyAssignment); } } @@ -3210,7 +3210,7 @@ module ts { node.flags |= NodeFlags.MultiLine; } - node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parsePropertyAssignment); + node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralElement); parseExpected(SyntaxKind.CloseBraceToken); return finishNode(node); } @@ -4413,7 +4413,7 @@ module ts { case SyntaxKind.IndexSignature: return checkIndexSignature(node); case SyntaxKind.InterfaceDeclaration: return checkInterfaceDeclaration(node); case SyntaxKind.LabeledStatement: return checkLabeledStatement(node); - case SyntaxKind.LonghandPropertyAssignment: return checkLonghandPropertyAssignment(node); + case SyntaxKind.PropertyAssignment: return checkPropertyAssignment(node); case SyntaxKind.Method: return checkMethod(node); case SyntaxKind.ModuleDeclaration: return checkModuleDeclaration(node); case SyntaxKind.ObjectLiteralExpression: return checkObjectLiteralExpression(node); @@ -5039,7 +5039,7 @@ module ts { // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields var currentKind: number; - if (prop.kind === SyntaxKind.LonghandPropertyAssignment || + if (prop.kind === SyntaxKind.PropertyAssignment || prop.kind === SyntaxKind.ShorthandPropertyAssignment || prop.kind === SyntaxKind.Method) { currentKind = Property; @@ -5342,7 +5342,7 @@ module ts { } } - function checkLonghandPropertyAssignment(node: LonghandPropertyAssignment) { + function checkPropertyAssignment(node: PropertyAssignment) { return checkForInvalidQuestionMark(node, node.questionToken, Diagnostics.An_object_member_cannot_be_declared_optional); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d887082ab38..4fa7d9421c4 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -231,7 +231,7 @@ module ts { CatchClause, // Property assignments - LonghandPropertyAssignment, + PropertyAssignment, ShorthandPropertyAssignment, // Enum @@ -383,17 +383,17 @@ module ts { export type VariableOrParameterDeclaration = VariableDeclaration | ParameterDeclaration; export type VariableOrParameterOrPropertyDeclaration = VariableOrParameterDeclaration | PropertyDeclaration; - export interface PropertyAssignment extends Declaration { - _propertyAssignmentBrand: any; + export interface ObjectLiteralElement extends Declaration { + _objectLiteralBrandBrand: any; } - export interface ShorthandPropertyAssignment extends PropertyAssignment { + export interface ShorthandPropertyAssignment extends ObjectLiteralElement { name: Identifier; questionToken?: Node; } - export interface LonghandPropertyAssignment extends PropertyAssignment { - _longhandPropertyAssignmentBrand: any; + export interface PropertyAssignment extends ObjectLiteralElement { + _propertyAssignmentBrand: any; name: DeclarationName; questionToken?: Node; initializer: Expression; @@ -420,7 +420,16 @@ module ts { body?: Block; } - export interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, PropertyAssignment { + // Note that a MethodDeclaration is considered both a ClassElement and an ObjectLiteralElement. + // Both the grammars for ClassDeclaration and ObjectLiteralExpression allow for MethodDeclarations + // as child elements, and so a MethodDeclaration satisfies both interfaces. This avoids the + // alternative where we would need separate kinds/types for ClassMethodDeclaration and + // ObjectLiteralMethodDeclaration, which would look identical. + // + // Because of this, it may be necessary to determine what sort of MethodDeclaration you have + // at later stages of the compiler pipeline. In that case, you can either check the parent kind + // of the method, or use helpers like isObjectLiteralMethodDeclaration + export interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { contextualType?: Type; // Used to temporarily assign a contextual type during overload resolution body?: Block; @@ -430,7 +439,9 @@ module ts { body?: Block; } - export interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, PropertyAssignment { + // See the comment on MethodDeclaration for the intuition behind AccessorDeclaration being a + // ClassElement and an ObjectLiteralElement. + export interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { _accessorDeclarationBrand: any; body: Block; } @@ -587,7 +598,7 @@ module ts { // An ObjectLiteralExpression is the declaration node for an anonymous symbol. export interface ObjectLiteralExpression extends PrimaryExpression, Declaration { - properties: NodeArray; + properties: NodeArray; } export interface PropertyAccessExpression extends MemberExpression { diff --git a/src/services/breakpoints.ts b/src/services/breakpoints.ts index 6bfeb1dc2a6..260fa5892aa 100644 --- a/src/services/breakpoints.ts +++ b/src/services/breakpoints.ts @@ -102,10 +102,10 @@ module ts.BreakpointResolver { return spanInFunctionDeclaration(node); case SyntaxKind.Block: - return isFunctionBlock(node) - ? spanInFunctionBlock(node) - : spanInBlock(node); - + if (isFunctionBlock(node)) { + return spanInFunctionBlock(node); + } + // Fall through case SyntaxKind.TryBlock: case SyntaxKind.FinallyBlock: case SyntaxKind.ModuleBlock: @@ -240,7 +240,7 @@ module ts.BreakpointResolver { default: // If this is name of property assignment, set breakpoint in the initializer - if (node.parent.kind === SyntaxKind.LonghandPropertyAssignment && (node.parent).name === node) { + if (node.parent.kind === SyntaxKind.PropertyAssignment && (node.parent).name === node) { return spanInNode((node.parent).initializer); } @@ -483,7 +483,7 @@ module ts.BreakpointResolver { function spanInColonToken(node: Node): TextSpan { // Is this : specifying return annotation of the function declaration - if (isAnyFunction(node.parent) || node.parent.kind === SyntaxKind.LonghandPropertyAssignment) { + if (isAnyFunction(node.parent) || node.parent.kind === SyntaxKind.PropertyAssignment) { return spanInPreviousNode(node); } diff --git a/src/services/services.ts b/src/services/services.ts index 36d502bb47f..d233cf70ff9 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1973,14 +1973,14 @@ 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.LonghandPropertyAssignment || 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 { if (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) { switch (node.parent.kind) { case SyntaxKind.Property: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.GetAccessor: @@ -2647,7 +2647,7 @@ module ts { var existingMemberNames: Map = {}; forEach(existingMembers, m => { - if (m.kind !== SyntaxKind.LonghandPropertyAssignment && m.kind !== SyntaxKind.ShorthandPropertyAssignment) { + if (m.kind !== SyntaxKind.PropertyAssignment && m.kind !== SyntaxKind.ShorthandPropertyAssignment) { // Ignore omitted expressions for missing members in the object literal return; } @@ -4717,7 +4717,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: - case SyntaxKind.LonghandPropertyAssignment: + case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: