From a8badd921e9ee036f2d03eb10667a2355bd456c1 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 2 Dec 2014 22:01:52 -0800 Subject: [PATCH] Update the name of 'PropertyAssignment' to 'LonghandPropertyAssignment'. 'PropertyAssignment' is now the name of the super interface brand we use for all thigns that can be members of an object literal. --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 12 ++++---- src/compiler/emitter.ts | 12 ++++---- src/compiler/parser.ts | 58 ++++++++++++++++++------------------- src/compiler/types.ts | 24 +++++++++++---- src/services/breakpoints.ts | 4 +-- src/services/services.ts | 8 ++--- 7 files changed, 67 insertions(+), 53 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index a45561e7ed6..5051e771a29 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -397,7 +397,7 @@ module ts { } break; case SyntaxKind.Property: - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: 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 0e428b4a4e0..cdb0babd587 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.PropertyAssignment) { + if (declaration.kind !== SyntaxKind.LonghandPropertyAssignment) { var unwidenedType = type; type = getWidenedType(type); if (type !== unwidenedType) { @@ -3247,7 +3247,7 @@ module ts { return !(node).typeParameters && !forEach((node).parameters, p => p.type); case SyntaxKind.ObjectLiteralExpression: return forEach((node).properties, p => - p.kind === SyntaxKind.PropertyAssignment && isContextSensitiveExpression((p).initializer)); + p.kind === SyntaxKind.LonghandPropertyAssignment && isContextSensitiveExpression((p).initializer)); case SyntaxKind.ArrayLiteralExpression: return forEach((node).elements, e => isContextSensitiveExpression(e)); case SyntaxKind.ConditionalExpression: @@ -4949,7 +4949,7 @@ module ts { return getTypeFromTypeNode((parent).type); case SyntaxKind.BinaryExpression: return getContextualTypeForBinaryOperand(node); - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: return getContextualTypeForPropertyExpression(node); case SyntaxKind.ArrayLiteralExpression: return getContextualTypeForElementExpression(node); @@ -5085,7 +5085,7 @@ module ts { if (member.flags & SymbolFlags.Property) { var memberDecl = member.declarations[0]; var type: Type; - if (memberDecl.kind === SyntaxKind.PropertyAssignment) { + if (memberDecl.kind === SyntaxKind.LonghandPropertyAssignment) { type = checkExpression(memberDecl.initializer, contextualMapper); } else { @@ -8573,7 +8573,7 @@ module ts { case SyntaxKind.Property: case SyntaxKind.ArrayLiteralExpression: case SyntaxKind.ObjectLiteralExpression: - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.PropertyAccessExpression: case SyntaxKind.ElementAccessExpression: case SyntaxKind.CallExpression: @@ -9022,7 +9022,7 @@ module ts { // 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 resolveEntityName(location, (location).name, SymbolFlags.Value); } return undefined; } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 7193523bf96..38cd18aea07 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.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: @@ -2236,7 +2236,7 @@ module ts { emitTrailingComments(node); } - function emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) { + function emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyAssignment) { emitLeadingComments(node); // Emit identifier as an identifier emit(node.name); @@ -2247,7 +2247,7 @@ module ts { emitTrailingComments(node); } - function emitShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) { + function emitShorthandPropertyAssignment(node: ShorthandPropertyAssignment) { // If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment. For example: // module m { // export var y; @@ -3525,7 +3525,7 @@ module ts { return emitArrayLiteral(node); case SyntaxKind.ObjectLiteralExpression: return emitObjectLiteral(node); - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: return emitPropertyAssignment(node); case SyntaxKind.ComputedPropertyName: return emitComputedPropertyName(node); @@ -3628,7 +3628,7 @@ module ts { // Emit node down-level switch (node.kind) { case SyntaxKind.ShorthandPropertyAssignment: - return emitDownlevelShorthandPropertyAssignment(node); + return emitDownlevelShorthandPropertyAssignment(node); } } else { @@ -3636,7 +3636,7 @@ module ts { Debug.assert(compilerOptions.target >= ScriptTarget.ES6, "Invalid ScriptTarget. We should emit as ES6 or above"); switch (node.kind) { case SyntaxKind.ShorthandPropertyAssignment: - return emitShorthandPropertyAssignment(node); + return emitShorthandPropertyAssignment(node); } } } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b36ded65dc4..6f578433beb 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.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: return children(node.modifiers) || child((node).name) || @@ -583,7 +583,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.Property: case SyntaxKind.EnumMember: - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: return (parent).initializer === node; case SyntaxKind.ExpressionStatement: case SyntaxKind.IfStatement: @@ -641,7 +641,7 @@ module ts { case SyntaxKind.Method: return (node).questionToken !== undefined; case SyntaxKind.ShorthandPropertyAssignment: - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.Property: return (node).questionToken !== undefined; } @@ -680,7 +680,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: @@ -3166,16 +3166,16 @@ module ts { return finishNode(node); } - function parsePropertyAssignment(): Declaration { + function parseShortOrLonghandPropertyAssignment(): PropertyAssignment { var nodePos = scanner.getStartPos(); + var asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken); var tokenIsIdentifier = isIdentifier(); var nameToken = token; var propertyName = parsePropertyName(); - var node: Declaration; if (asteriskToken || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) { - node = createNode(SyntaxKind.PropertyAssignment, nodePos); - node.name = propertyName; + var longhandDeclaration = createNode(SyntaxKind.LonghandPropertyAssignment, nodePos); + longhandDeclaration.name = propertyName; var sig = parseSignature(/*yieldAndGeneratorParameterContext:*/ !!asteriskToken); var body = parseFunctionBlock(!!asteriskToken, /* ignoreMissingOpenBrace */ false); @@ -3184,8 +3184,8 @@ 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, asteriskToken, undefined, sig, body); - return finishNode(node); + longhandDeclaration.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, longhandDeclaration.pos, asteriskToken, undefined, sig, body); + return finishNode(longhandDeclaration); } // Disallowing of optional property assignments happens in the grammar checker. @@ -3193,29 +3193,29 @@ module ts { // Parse to check if it is short-hand property assignment or normal property assignment if ((token === SyntaxKind.CommaToken || token === SyntaxKind.CloseBraceToken) && tokenIsIdentifier) { - var shorthandDeclaration = createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos); + var shorthandDeclaration = createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos); shorthandDeclaration.name = propertyName; shorthandDeclaration.questionToken = questionToken; return finishNode(shorthandDeclaration); } else { - var propertyDeclaration = createNode(SyntaxKind.PropertyAssignment, nodePos); - propertyDeclaration.name = propertyName; - propertyDeclaration.questionToken = questionToken; + var longhandDeclaration = createNode(SyntaxKind.LonghandPropertyAssignment, nodePos); + longhandDeclaration.name = propertyName; + longhandDeclaration.questionToken = questionToken; parseExpected(SyntaxKind.ColonToken); - propertyDeclaration.initializer = allowInAnd(parseAssignmentExpressionOrHigher); - return finishNode(propertyDeclaration); + longhandDeclaration.initializer = allowInAnd(parseAssignmentExpressionOrHigher); + return finishNode(longhandDeclaration); } } - function parseObjectLiteralMember(): Declaration { + function parsePropertyAssignment(): PropertyAssignment { var initialPos = getNodePos(); var initialToken = token; if (parseContextualModifier(SyntaxKind.GetKeyword) || parseContextualModifier(SyntaxKind.SetKeyword)) { var kind = initialToken === SyntaxKind.GetKeyword ? SyntaxKind.GetAccessor : SyntaxKind.SetAccessor; - return parseMemberAccessorDeclaration(kind, initialPos, /*modifiers*/ undefined); + return parseAccessorDeclaration(kind, initialPos, /*modifiers*/ undefined); } - return parsePropertyAssignment(); + return parseShortOrLonghandPropertyAssignment(); } function parseObjectLiteralExpression(): ObjectLiteralExpression { @@ -3225,7 +3225,7 @@ module ts { node.flags |= NodeFlags.MultiLine; } - node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralMember); + node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parsePropertyAssignment); parseExpected(SyntaxKind.CloseBraceToken); return finishNode(node); } @@ -3772,8 +3772,8 @@ module ts { return parseInitializer(/*inParameter*/ false); } - function parseMemberAccessorDeclaration(kind: SyntaxKind, fullStart: number, modifiers: ModifiersArray): MethodDeclaration { - var node = createNode(kind, fullStart); + function parseAccessorDeclaration(kind: SyntaxKind, fullStart: number, modifiers: ModifiersArray): AccessorDeclaration { + var node = createNode(kind, fullStart); setModifiers(node, modifiers); node.name = parsePropertyName(); fillSignature(SyntaxKind.ColonToken, /*yieldAndGeneratorParameterContext:*/ false, node); @@ -3864,10 +3864,10 @@ module ts { var fullStart = getNodePos(); var modifiers = parseModifiers(); if (parseContextualModifier(SyntaxKind.GetKeyword)) { - return parseMemberAccessorDeclaration(SyntaxKind.GetAccessor, fullStart, modifiers); + return parseAccessorDeclaration(SyntaxKind.GetAccessor, fullStart, modifiers); } if (parseContextualModifier(SyntaxKind.SetKeyword)) { - return parseMemberAccessorDeclaration(SyntaxKind.SetAccessor, fullStart, modifiers); + return parseAccessorDeclaration(SyntaxKind.SetAccessor, fullStart, modifiers); } if (token === SyntaxKind.ConstructorKeyword) { return parseConstructorDeclaration(fullStart, modifiers); @@ -4435,6 +4435,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.Method: return checkMethod(node); case SyntaxKind.ModuleDeclaration: return checkModuleDeclaration(node); case SyntaxKind.ObjectLiteralExpression: return checkObjectLiteralExpression(node); @@ -4443,11 +4444,10 @@ module ts { case SyntaxKind.PostfixUnaryExpression: return checkPostfixUnaryExpression(node); case SyntaxKind.PrefixUnaryExpression: return checkPrefixUnaryExpression(node); case SyntaxKind.Property: return checkProperty(node); - case SyntaxKind.PropertyAssignment: return checkPropertyAssignment(node); case SyntaxKind.ReturnStatement: return checkReturnStatement(node); case SyntaxKind.SetAccessor: return checkSetAccessor(node); case SyntaxKind.SourceFile: return checkSourceFile(node); - case SyntaxKind.ShorthandPropertyAssignment: return checkShorthandPropertyAssignment(node); + case SyntaxKind.ShorthandPropertyAssignment: return checkShorthandPropertyAssignment(node); case SyntaxKind.SwitchStatement: return checkSwitchStatement(node); case SyntaxKind.TaggedTemplateExpression: return checkTaggedTemplateExpression(node); case SyntaxKind.ThrowStatement: return checkThrowStatement(node); @@ -5060,7 +5060,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.PropertyAssignment) { + if (prop.kind === SyntaxKind.LonghandPropertyAssignment) { currentKind = Property; } else if (prop.kind === SyntaxKind.ShorthandPropertyAssignment) { @@ -5364,7 +5364,7 @@ module ts { } } - function checkPropertyAssignment(node: PropertyDeclaration) { + function checkLonghandPropertyAssignment(node: LonghandPropertyAssignment) { return checkForInvalidQuestionMark(node, node.questionToken, Diagnostics.An_object_member_cannot_be_declared_optional); } @@ -5464,7 +5464,7 @@ module ts { return grammarErrorOnFirstToken(node, Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); } - function checkShorthandPropertyAssignment(node: ShorthandPropertyDeclaration): boolean { + function checkShorthandPropertyAssignment(node: ShorthandPropertyAssignment): boolean { 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 edf052a8876..aa8a98db902 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -232,8 +232,9 @@ module ts { CatchClause, // Property assignments - PropertyAssignment, + LonghandPropertyAssignment, ShorthandPropertyAssignment, + // Enum EnumMember, // Top-level nodes @@ -378,6 +379,7 @@ module ts { } export interface PropertyDeclaration extends Declaration, ClassElement { + _propertyDeclarationBrand: any; questionToken?: Node; type?: TypeNode; initializer?: Expression; @@ -386,11 +388,22 @@ module ts { export type VariableOrParameterDeclaration = VariableDeclaration | ParameterDeclaration; export type VariableOrParameterOrPropertyDeclaration = VariableOrParameterDeclaration | PropertyDeclaration; - export interface ShorthandPropertyDeclaration extends Declaration { + export interface PropertyAssignment extends Declaration { + _propertyAssignmentBrand: any; + } + + export interface ShorthandPropertyAssignment extends PropertyAssignment { name: Identifier; questionToken?: Node; } + export interface LonghandPropertyAssignment extends PropertyAssignment { + _longhandPropertyAssignmentBrand: any; + name: DeclarationName; + questionToken?: Node; + initializer: Expression; + } + /** * Several node kinds share function-like features such as a signature, * a name, and a body. These nodes should extend FunctionLikeDeclaration. @@ -420,8 +433,9 @@ module ts { body?: Block; } - export interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement { - body?: Block; + export interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, PropertyAssignment { + _accessorDeclarationBrand: any; + body: Block; } export interface IndexSignatureDeclaration extends SignatureDeclaration, ClassElement { @@ -576,7 +590,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 a67a89dd82f..d70884e5d87 100644 --- a/src/services/breakpoints.ts +++ b/src/services/breakpoints.ts @@ -239,7 +239,7 @@ module ts.BreakpointResolver { default: // If this is name of property assignment, set breakpoint in the initializer - if (node.parent.kind === SyntaxKind.PropertyAssignment && (node.parent).name === node) { + if (node.parent.kind === SyntaxKind.LonghandPropertyAssignment && (node.parent).name === node) { return spanInNode((node.parent).initializer); } @@ -477,7 +477,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.PropertyAssignment) { + if (isAnyFunction(node.parent) || node.parent.kind === SyntaxKind.LonghandPropertyAssignment) { return spanInPreviousNode(node); } diff --git a/src/services/services.ts b/src/services/services.ts index 44c587974df..a04c85071e2 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1968,14 +1968,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.PropertyAssignment || node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) && (node.parent).name === node; + (node.parent.kind === SyntaxKind.LonghandPropertyAssignment || 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.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: case SyntaxKind.GetAccessor: @@ -2642,7 +2642,7 @@ module ts { var existingMemberNames: Map = {}; forEach(existingMembers, m => { - if (m.kind !== SyntaxKind.PropertyAssignment && m.kind !== SyntaxKind.ShorthandPropertyAssignment) { + if (m.kind !== SyntaxKind.LonghandPropertyAssignment && m.kind !== SyntaxKind.ShorthandPropertyAssignment) { // Ignore omitted expressions for missing members in the object literal return; } @@ -4703,7 +4703,7 @@ module ts { case SyntaxKind.Parameter: case SyntaxKind.VariableDeclaration: case SyntaxKind.Property: - case SyntaxKind.PropertyAssignment: + case SyntaxKind.LonghandPropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.EnumMember: case SyntaxKind.Method: