Skip to content

Restructure ast to parse react attributes as object literal #11114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -9792,7 +9792,7 @@ namespace ts {
return getContextualTypeForObjectLiteralElement(node);
}

function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElement) {
function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike) {
const objectLiteral = <ObjectLiteralExpression>element.parent;
const type = getApparentTypeOfContextualType(objectLiteral);
if (type) {
Expand Down Expand Up @@ -9909,7 +9909,7 @@ namespace ts {
return getContextualTypeForBinaryOperand(node);
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShorthandPropertyAssignment:
return getContextualTypeForObjectLiteralElement(<ObjectLiteralElement>parent);
return getContextualTypeForObjectLiteralElement(<ObjectLiteralElementLike>parent);
case SyntaxKind.ArrayLiteralExpression:
return getContextualTypeForElementExpression(node);
case SyntaxKind.ConditionalExpression:
Expand Down Expand Up @@ -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 = <PropertyName>(<PropertyAssignment>property).name;
if (name.kind === SyntaxKind.ComputedPropertyName) {
Expand Down Expand Up @@ -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(<Expression>expr.parent.parent);
return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, <ObjectLiteralElement>expr.parent);
return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, <ObjectLiteralElementLike>expr.parent);
}
// Array literal assignment - array destructuring pattern
Debug.assert(expr.parent.kind === SyntaxKind.ArrayLiteralExpression);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <ObjectLiteralExpression>createNode(SyntaxKind.ObjectLiteralExpression, location);
node.properties = createNodeArray(properties);
if (multiLine) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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:
Expand All @@ -2086,7 +2086,7 @@ namespace ts {
function createExpressionForAccessorDeclaration(properties: NodeArray<Declaration>, 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,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4121,7 +4121,7 @@ namespace ts {
return undefined;
}

function parseObjectLiteralElement(): ObjectLiteralElement {
function parseObjectLiteralElement(): ObjectLiteralElementLike {
const fullStart = scanner.getStartPos();
const decorators = parseDecorators();
const modifiers = parseModifiers();
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/transformers/es6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
),
Expand Down Expand Up @@ -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.
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/transformers/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...'
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess JSXAttributes isn't in this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

* 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<T extends ObjectLiteralElement> extends PrimaryExpression, Declaration {
properties: NodeArray<T>;
}

// An ObjectLiteralExpression is the declaration node for an anonymous symbol.
// @kind(SyntaxKind.ObjectLiteralExpression)
export interface ObjectLiteralExpression extends PrimaryExpression, Declaration {
properties: NodeArray<ObjectLiteralElement>;
export interface ObjectLiteralExpression extends ObjectLiteralExpressionBase<ObjectLiteralElementLike> {
/* @internal */
multiLine?: boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ namespace ts {

case SyntaxKind.ObjectLiteralExpression:
return updateObjectLiteral(<ObjectLiteralExpression>node,
visitNodes((<ObjectLiteralExpression>node).properties, visitor, isObjectLiteralElement));
visitNodes((<ObjectLiteralExpression>node).properties, visitor, isObjectLiteralElementLike));

case SyntaxKind.PropertyAccessExpression:
return updatePropertyAccess(<PropertyAccessExpression>node,
Expand Down