Skip to content

Commit 3d92117

Browse files
authored
Merge pull request #11114 from Microsoft/restructureASTToParseReactAttributesAsObjectLiteral
Restructure ast to parse react attributes as object literal
2 parents 09eb6fe + 625a253 commit 3d92117

File tree

11 files changed

+37
-26
lines changed

11 files changed

+37
-26
lines changed

src/compiler/checker.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5948,7 +5948,7 @@ namespace ts {
59485948

59495949
// Returns true if the given expression contains (at any level of nesting) a function or arrow expression
59505950
// that is subject to contextual typing.
5951-
function isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElement): boolean {
5951+
function isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike): boolean {
59525952
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
59535953
switch (node.kind) {
59545954
case SyntaxKind.FunctionExpression:
@@ -9793,7 +9793,7 @@ namespace ts {
97939793
return getContextualTypeForObjectLiteralElement(node);
97949794
}
97959795

9796-
function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElement) {
9796+
function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike) {
97979797
const objectLiteral = <ObjectLiteralExpression>element.parent;
97989798
const type = getApparentTypeOfContextualType(objectLiteral);
97999799
if (type) {
@@ -9910,7 +9910,7 @@ namespace ts {
99109910
return getContextualTypeForBinaryOperand(node);
99119911
case SyntaxKind.PropertyAssignment:
99129912
case SyntaxKind.ShorthandPropertyAssignment:
9913-
return getContextualTypeForObjectLiteralElement(<ObjectLiteralElement>parent);
9913+
return getContextualTypeForObjectLiteralElement(<ObjectLiteralElementLike>parent);
99149914
case SyntaxKind.ArrayLiteralExpression:
99159915
return getContextualTypeForElementExpression(node);
99169916
case SyntaxKind.ConditionalExpression:
@@ -13186,7 +13186,7 @@ namespace ts {
1318613186
return sourceType;
1318713187
}
1318813188

13189-
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElement, contextualMapper?: TypeMapper) {
13189+
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, contextualMapper?: TypeMapper) {
1319013190
if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) {
1319113191
const name = <PropertyName>(<PropertyAssignment>property).name;
1319213192
if (name.kind === SyntaxKind.ComputedPropertyName) {
@@ -18446,7 +18446,7 @@ namespace ts {
1844618446
// for ({ skills: { primary, secondary } } = multiRobot, i = 0; i < 1; i++) {
1844718447
if (expr.parent.kind === SyntaxKind.PropertyAssignment) {
1844818448
const typeOfParentObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>expr.parent.parent);
18449-
return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, <ObjectLiteralElement>expr.parent);
18449+
return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, <ObjectLiteralElementLike>expr.parent);
1845018450
}
1845118451
// Array literal assignment - array destructuring pattern
1845218452
Debug.assert(expr.parent.kind === SyntaxKind.ArrayLiteralExpression);

src/compiler/factory.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ namespace ts {
416416
return node;
417417
}
418418

419-
export function createObjectLiteral(properties?: ObjectLiteralElement[], location?: TextRange, multiLine?: boolean) {
419+
export function createObjectLiteral(properties?: ObjectLiteralElementLike[], location?: TextRange, multiLine?: boolean) {
420420
const node = <ObjectLiteralExpression>createNode(SyntaxKind.ObjectLiteralExpression, location);
421421
node.properties = createNodeArray(properties);
422422
if (multiLine) {
@@ -425,7 +425,7 @@ namespace ts {
425425
return node;
426426
}
427427

428-
export function updateObjectLiteral(node: ObjectLiteralExpression, properties: ObjectLiteralElement[]) {
428+
export function updateObjectLiteral(node: ObjectLiteralExpression, properties: ObjectLiteralElementLike[]) {
429429
if (node.properties !== properties) {
430430
return updateNode(createObjectLiteral(properties, node, node.multiLine), node);
431431
}
@@ -2069,7 +2069,7 @@ namespace ts {
20692069
}
20702070
}
20712071

2072-
export function createExpressionForObjectLiteralElement(node: ObjectLiteralExpression, property: ObjectLiteralElement, receiver: Expression): Expression {
2072+
export function createExpressionForObjectLiteralElementLike(node: ObjectLiteralExpression, property: ObjectLiteralElementLike, receiver: Expression): Expression {
20732073
switch (property.kind) {
20742074
case SyntaxKind.GetAccessor:
20752075
case SyntaxKind.SetAccessor:
@@ -2086,7 +2086,7 @@ namespace ts {
20862086
function createExpressionForAccessorDeclaration(properties: NodeArray<Declaration>, property: AccessorDeclaration, receiver: Expression, multiLine: boolean) {
20872087
const { firstAccessor, getAccessor, setAccessor } = getAllAccessorDeclarations(properties, property);
20882088
if (property === firstAccessor) {
2089-
const properties: ObjectLiteralElement[] = [];
2089+
const properties: ObjectLiteralElementLike[] = [];
20902090
if (getAccessor) {
20912091
const getterFunction = createFunctionExpression(
20922092
/*asteriskToken*/ undefined,

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4121,7 +4121,7 @@ namespace ts {
41214121
return undefined;
41224122
}
41234123

4124-
function parseObjectLiteralElement(): ObjectLiteralElement {
4124+
function parseObjectLiteralElement(): ObjectLiteralElementLike {
41254125
const fullStart = scanner.getStartPos();
41264126
const decorators = parseDecorators();
41274127
const modifiers = parseModifiers();

src/compiler/transformers/es6.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ namespace ts {
12811281
setNodeEmitFlags(propertyName, NodeEmitFlags.NoComments | NodeEmitFlags.NoLeadingSourceMap);
12821282
setSourceMapRange(propertyName, firstAccessor.name);
12831283

1284-
const properties: ObjectLiteralElement[] = [];
1284+
const properties: ObjectLiteralElementLike[] = [];
12851285
if (getAccessor) {
12861286
const getterFunction = transformFunctionLikeToExpression(getAccessor, /*location*/ undefined, /*name*/ undefined);
12871287
setSourceMapRange(getterFunction, getSourceMapRange(getAccessor));
@@ -1965,7 +1965,7 @@ namespace ts {
19651965
temp,
19661966
setNodeEmitFlags(
19671967
createObjectLiteral(
1968-
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties),
1968+
visitNodes(properties, visitor, isObjectLiteralElementLike, 0, numInitialProperties),
19691969
/*location*/ undefined,
19701970
node.multiLine
19711971
),
@@ -2499,7 +2499,7 @@ namespace ts {
24992499
*
25002500
* @param node A MethodDeclaration node.
25012501
*/
2502-
function visitMethodDeclaration(node: MethodDeclaration): ObjectLiteralElement {
2502+
function visitMethodDeclaration(node: MethodDeclaration): ObjectLiteralElementLike {
25032503
// We should only get here for methods on an object literal with regular identifier names.
25042504
// Methods on classes are handled in visitClassDeclaration/visitClassExpression.
25052505
// Methods with computed property names are handled in visitObjectLiteralExpression.
@@ -2518,7 +2518,7 @@ namespace ts {
25182518
*
25192519
* @param node A ShorthandPropertyAssignment node.
25202520
*/
2521-
function visitShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElement {
2521+
function visitShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElementLike {
25222522
return createPropertyAssignment(
25232523
node.name,
25242524
getSynthesizedClone(node.name),

src/compiler/transformers/generators.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ namespace ts {
10251025
const temp = declareLocal();
10261026
emitAssignment(temp,
10271027
createObjectLiteral(
1028-
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties),
1028+
visitNodes(properties, visitor, isObjectLiteralElementLike, 0, numInitialProperties),
10291029
/*location*/ undefined,
10301030
multiLine
10311031
)
@@ -1035,13 +1035,13 @@ namespace ts {
10351035
expressions.push(multiLine ? startOnNewLine(getMutableClone(temp)) : temp);
10361036
return inlineExpressions(expressions);
10371037

1038-
function reduceProperty(expressions: Expression[], property: ObjectLiteralElement) {
1038+
function reduceProperty(expressions: Expression[], property: ObjectLiteralElementLike) {
10391039
if (containsYield(property) && expressions.length > 0) {
10401040
emitStatement(createStatement(inlineExpressions(expressions)));
10411041
expressions = [];
10421042
}
10431043

1044-
const expression = createExpressionForObjectLiteralElement(node, property, temp);
1044+
const expression = createExpressionForObjectLiteralElementLike(node, property, temp);
10451045
const visited = visitNode(expression, visitor, isExpression);
10461046
if (visited) {
10471047
if (multiLine) {

src/compiler/transformers/module/module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ namespace ts {
850850
return node;
851851
}
852852

853-
function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElement {
853+
function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElementLike {
854854
const name = node.name;
855855
const exportedOrImportedName = substituteExpressionIdentifier(name);
856856
if (exportedOrImportedName !== name) {

src/compiler/transformers/module/system.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ namespace ts {
288288
}
289289
}
290290

291-
const exportedNames: ObjectLiteralElement[] = [];
291+
const exportedNames: ObjectLiteralElementLike[] = [];
292292
if (exportedLocalNames) {
293293
for (const exportedLocalName of exportedLocalNames) {
294294
// write name of exported declaration, i.e 'export var x...'
@@ -1107,7 +1107,7 @@ namespace ts {
11071107
return false;
11081108
}
11091109

1110-
function hasExportedReferenceInObjectDestructuringElement(node: ObjectLiteralElement): boolean {
1110+
function hasExportedReferenceInObjectDestructuringElement(node: ObjectLiteralElementLike): boolean {
11111111
if (isShorthandPropertyAssignment(node)) {
11121112
return isExportedBinding(node.name);
11131113
}

src/compiler/transformers/ts.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ namespace ts {
15601560

15611561
function addNewTypeMetadata(node: Declaration, decoratorExpressions: Expression[]) {
15621562
if (compilerOptions.emitDecoratorMetadata) {
1563-
let properties: ObjectLiteralElement[];
1563+
let properties: ObjectLiteralElementLike[];
15641564
if (shouldAddTypeMetadata(node)) {
15651565
(properties || (properties = [])).push(createPropertyAssignment("type", createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, [], /*type*/ undefined, /*equalsGreaterThanToken*/ undefined, serializeTypeOfNode(node))));
15661566
}
@@ -3273,7 +3273,7 @@ namespace ts {
32733273
return node;
32743274
}
32753275

3276-
function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElement {
3276+
function substituteShorthandPropertyAssignment(node: ShorthandPropertyAssignment): ObjectLiteralElementLike {
32773277
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports) {
32783278
const name = node.name;
32793279
const exportedName = trySubstituteNamespaceExportedName(name);

src/compiler/types.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ namespace ts {
648648
name?: PropertyName;
649649
}
650650

651+
export type ObjectLiteralElementLike = PropertyAssignment | ShorthandPropertyAssignment | MethodDeclaration | AccessorDeclaration;
652+
651653
// @kind(SyntaxKind.PropertyAssignment)
652654
export interface PropertyAssignment extends ObjectLiteralElement {
653655
_propertyAssignmentBrand: any;
@@ -1048,10 +1050,19 @@ namespace ts {
10481050
expression: Expression;
10491051
}
10501052

1053+
/**
1054+
* This interface is a base interface for ObjectLiteralExpression and JSXAttributes to extend from. JSXAttributes is similar to
1055+
* ObjectLiteralExpression in that it contains array of properties; however, JSXAttributes' properties can only be
1056+
* JSXAttribute or JSXSpreadAttribute. ObjectLiteralExpression, on the other hand, can only have properties of type
1057+
* ObjectLiteralElement (e.g. PropertyAssignment, ShorthandPropertyAssignment etc.)
1058+
**/
1059+
export interface ObjectLiteralExpressionBase<T extends ObjectLiteralElement> extends PrimaryExpression, Declaration {
1060+
properties: NodeArray<T>;
1061+
}
1062+
10511063
// An ObjectLiteralExpression is the declaration node for an anonymous symbol.
10521064
// @kind(SyntaxKind.ObjectLiteralExpression)
1053-
export interface ObjectLiteralExpression extends PrimaryExpression, Declaration {
1054-
properties: NodeArray<ObjectLiteralElement>;
1065+
export interface ObjectLiteralExpression extends ObjectLiteralExpressionBase<ObjectLiteralElementLike> {
10551066
/* @internal */
10561067
multiLine?: boolean;
10571068
}

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3736,7 +3736,7 @@ namespace ts {
37363736
|| kind === SyntaxKind.SemicolonClassElement;
37373737
}
37383738

3739-
export function isObjectLiteralElement(node: Node): node is ObjectLiteralElement {
3739+
export function isObjectLiteralElementLike(node: Node): node is ObjectLiteralElementLike {
37403740
const kind = node.kind;
37413741
return kind === SyntaxKind.PropertyAssignment
37423742
|| kind === SyntaxKind.ShorthandPropertyAssignment

src/compiler/visitor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ namespace ts {
772772

773773
case SyntaxKind.ObjectLiteralExpression:
774774
return updateObjectLiteral(<ObjectLiteralExpression>node,
775-
visitNodes((<ObjectLiteralExpression>node).properties, visitor, isObjectLiteralElement));
775+
visitNodes((<ObjectLiteralExpression>node).properties, visitor, isObjectLiteralElementLike));
776776

777777
case SyntaxKind.PropertyAccessExpression:
778778
return updatePropertyAccess(<PropertyAccessExpression>node,

0 commit comments

Comments
 (0)