diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 51550ef873fc5..4d8288045dada 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5947,7 +5947,7 @@ namespace 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 isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElement): boolean { + function isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike): boolean { Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node)); switch (node.kind) { case SyntaxKind.FunctionExpression: @@ -9792,7 +9792,7 @@ namespace ts { return getContextualTypeForObjectLiteralElement(node); } - function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElement) { + function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike) { const objectLiteral = element.parent; const type = getApparentTypeOfContextualType(objectLiteral); if (type) { @@ -9909,7 +9909,7 @@ namespace ts { return getContextualTypeForBinaryOperand(node); case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: - return getContextualTypeForObjectLiteralElement(parent); + return getContextualTypeForObjectLiteralElement(parent); case SyntaxKind.ArrayLiteralExpression: return getContextualTypeForElementExpression(node); case SyntaxKind.ConditionalExpression: @@ -13185,7 +13185,7 @@ namespace ts { return sourceType; } - function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElement, contextualMapper?: TypeMapper) { + function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, contextualMapper?: TypeMapper) { if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) { const name = (property).name; if (name.kind === SyntaxKind.ComputedPropertyName) { @@ -18445,7 +18445,7 @@ namespace ts { // for ({ skills: { primary, secondary } } = multiRobot, i = 0; i < 1; i++) { if (expr.parent.kind === SyntaxKind.PropertyAssignment) { const typeOfParentObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent.parent); - return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, expr.parent); + return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, expr.parent); } // Array literal assignment - array destructuring pattern Debug.assert(expr.parent.kind === SyntaxKind.ArrayLiteralExpression); diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 16eeb86f59a73..48296145dbc11 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -416,7 +416,7 @@ namespace ts { return node; } - export function createObjectLiteral(properties?: ObjectLiteralElement[], location?: TextRange, multiLine?: boolean) { + export function createObjectLiteral(properties?: ObjectLiteralElementLike[], location?: TextRange, multiLine?: boolean) { const node = createNode(SyntaxKind.ObjectLiteralExpression, location); node.properties = createNodeArray(properties); if (multiLine) { @@ -425,7 +425,7 @@ namespace ts { return node; } - export function updateObjectLiteral(node: ObjectLiteralExpression, properties: ObjectLiteralElement[]) { + export function updateObjectLiteral(node: ObjectLiteralExpression, properties: ObjectLiteralElementLike[]) { if (node.properties !== properties) { return updateNode(createObjectLiteral(properties, node, node.multiLine), node); } @@ -2069,7 +2069,7 @@ namespace ts { } } - export function createExpressionForObjectLiteralElement(node: ObjectLiteralExpression, property: ObjectLiteralElement, receiver: Expression): Expression { + export function createExpressionForObjectLiteralElementLike(node: ObjectLiteralExpression, property: ObjectLiteralElementLike, receiver: Expression): Expression { switch (property.kind) { case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: @@ -2086,7 +2086,7 @@ namespace ts { function createExpressionForAccessorDeclaration(properties: NodeArray, property: AccessorDeclaration, receiver: Expression, multiLine: boolean) { const { firstAccessor, getAccessor, setAccessor } = getAllAccessorDeclarations(properties, property); if (property === firstAccessor) { - const properties: ObjectLiteralElement[] = []; + const properties: ObjectLiteralElementLike[] = []; if (getAccessor) { const getterFunction = createFunctionExpression( /*asteriskToken*/ undefined, diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 1aec63bc588ca..9bd9311b74921 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -4121,7 +4121,7 @@ namespace ts { return undefined; } - function parseObjectLiteralElement(): ObjectLiteralElement { + function parseObjectLiteralElement(): ObjectLiteralElementLike { const fullStart = scanner.getStartPos(); const decorators = parseDecorators(); const modifiers = parseModifiers(); diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es6.ts index 51a6a088fb717..63189ba88e0f7 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es6.ts @@ -1259,7 +1259,7 @@ namespace ts { setNodeEmitFlags(propertyName, NodeEmitFlags.NoComments | NodeEmitFlags.NoLeadingSourceMap); setSourceMapRange(propertyName, firstAccessor.name); - const properties: ObjectLiteralElement[] = []; + const properties: ObjectLiteralElementLike[] = []; if (getAccessor) { const getterFunction = transformFunctionLikeToExpression(getAccessor, /*location*/ undefined, /*name*/ undefined); setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); @@ -1940,7 +1940,7 @@ namespace ts { temp, setNodeEmitFlags( createObjectLiteral( - visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties), + visitNodes(properties, visitor, isObjectLiteralElementLike, 0, numInitialProperties), /*location*/ undefined, node.multiLine ), @@ -2474,7 +2474,7 @@ namespace ts { * * @param node A MethodDeclaration node. */ - function visitMethodDeclaration(node: MethodDeclaration): ObjectLiteralElement { + function visitMethodDeclaration(node: MethodDeclaration): ObjectLiteralElementLike { // We should only get here for methods on an object literal with regular identifier names. // Methods on classes are handled in visitClassDeclaration/visitClassExpression. // Methods with computed property names are handled in visitObjectLiteralExpression. @@ -2493,7 +2493,7 @@ namespace ts { * * @param node A ShorthandPropertyAssignment node. */ - function visitShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElement { + function visitShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElementLike { return createPropertyAssignment( node.name, getSynthesizedClone(node.name), diff --git a/src/compiler/transformers/generators.ts b/src/compiler/transformers/generators.ts index d1e2b6033a684..defda08edbf29 100644 --- a/src/compiler/transformers/generators.ts +++ b/src/compiler/transformers/generators.ts @@ -1020,7 +1020,7 @@ namespace ts { const temp = declareLocal(); emitAssignment(temp, createObjectLiteral( - visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties), + visitNodes(properties, visitor, isObjectLiteralElementLike, 0, numInitialProperties), /*location*/ undefined, multiLine ) @@ -1030,13 +1030,13 @@ namespace ts { expressions.push(multiLine ? startOnNewLine(getMutableClone(temp)) : temp); return inlineExpressions(expressions); - function reduceProperty(expressions: Expression[], property: ObjectLiteralElement) { + function reduceProperty(expressions: Expression[], property: ObjectLiteralElementLike) { if (containsYield(property) && expressions.length > 0) { emitStatement(createStatement(inlineExpressions(expressions))); expressions = []; } - const expression = createExpressionForObjectLiteralElement(node, property, temp); + const expression = createExpressionForObjectLiteralElementLike(node, property, temp); const visited = visitNode(expression, visitor, isExpression); if (visited) { if (multiLine) { diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index 9abd91b47c812..53004da2c171e 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -850,7 +850,7 @@ namespace ts { return node; } - function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElement { + function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElementLike { const name = node.name; const exportedOrImportedName = substituteExpressionIdentifier(name); if (exportedOrImportedName !== name) { diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index 7b013e18e7dc0..5e3b028122775 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -288,7 +288,7 @@ namespace ts { } } - const exportedNames: ObjectLiteralElement[] = []; + const exportedNames: ObjectLiteralElementLike[] = []; if (exportedLocalNames) { for (const exportedLocalName of exportedLocalNames) { // write name of exported declaration, i.e 'export var x...' @@ -1107,7 +1107,7 @@ namespace ts { return false; } - function hasExportedReferenceInObjectDestructuringElement(node: ObjectLiteralElement): boolean { + function hasExportedReferenceInObjectDestructuringElement(node: ObjectLiteralElementLike): boolean { if (isShorthandPropertyAssignment(node)) { return isExportedBinding(node.name); } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index d41a32d92c747..1d01e51f6d8d7 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1560,7 +1560,7 @@ namespace ts { function addNewTypeMetadata(node: Declaration, decoratorExpressions: Expression[]) { if (compilerOptions.emitDecoratorMetadata) { - let properties: ObjectLiteralElement[]; + let properties: ObjectLiteralElementLike[]; if (shouldAddTypeMetadata(node)) { (properties || (properties = [])).push(createPropertyAssignment("type", createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, /*equalsGreaterThanToken*/ undefined, serializeTypeOfNode(node)))); } @@ -3273,7 +3273,7 @@ namespace ts { return node; } - function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElement { + function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElementLike { if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports) { const name = node.name; const exportedName = trySubstituteNamespaceExportedName(name); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e4bc9a29d5b03..ded4c9daef908 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -648,6 +648,8 @@ namespace ts { name?: PropertyName; } + export type ObjectLiteralElementLike = PropertyAssignment | ShorthandPropertyAssignment | MethodDeclaration | AccessorDeclaration; + // @kind(SyntaxKind.PropertyAssignment) export interface PropertyAssignment extends ObjectLiteralElement { _propertyAssignmentBrand: any; @@ -1048,10 +1050,19 @@ namespace ts { expression: Expression; } + /** + * This interface is a base interface for ObjectLiteralExpression and JSXAttributes to extend from. JSXAttributes is similar to + * ObjectLiteralExpression in that it contains array of properties; however, JSXAttributes' properties can only be + * JSXAttribute or JSXSpreadAttribute. ObjectLiteralExpression, on the other hand, can only have properties of type + * ObjectLiteralElement (e.g. PropertyAssignment, ShorthandPropertyAssignment etc.) + **/ + export interface ObjectLiteralExpressionBase extends PrimaryExpression, Declaration { + properties: NodeArray; + } + // An ObjectLiteralExpression is the declaration node for an anonymous symbol. // @kind(SyntaxKind.ObjectLiteralExpression) - export interface ObjectLiteralExpression extends PrimaryExpression, Declaration { - properties: NodeArray; + export interface ObjectLiteralExpression extends ObjectLiteralExpressionBase { /* @internal */ multiLine?: boolean; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7a22c5634d84f..3417a1830ddc4 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3736,7 +3736,7 @@ namespace ts { || kind === SyntaxKind.SemicolonClassElement; } - export function isObjectLiteralElement(node: Node): node is ObjectLiteralElement { + export function isObjectLiteralElementLike(node: Node): node is ObjectLiteralElementLike { const kind = node.kind; return kind === SyntaxKind.PropertyAssignment || kind === SyntaxKind.ShorthandPropertyAssignment diff --git a/src/compiler/visitor.ts b/src/compiler/visitor.ts index 1e0cadaaa6b99..e147eda8535a6 100644 --- a/src/compiler/visitor.ts +++ b/src/compiler/visitor.ts @@ -772,7 +772,7 @@ namespace ts { case SyntaxKind.ObjectLiteralExpression: return updateObjectLiteral(node, - visitNodes((node).properties, visitor, isObjectLiteralElement)); + visitNodes((node).properties, visitor, isObjectLiteralElementLike)); case SyntaxKind.PropertyAccessExpression: return updatePropertyAccess(node,