Skip to content

Commit d38fcc4

Browse files
committed
Simplify visitObjectLiteralExpression
I ran into it and the comment at the top tripped me, then I proceeded to simplify the code.
1 parent 4ec21ed commit d38fcc4

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,62 +2566,53 @@ namespace ts {
25662566
* @param node An ObjectLiteralExpression node.
25672567
*/
25682568
function visitObjectLiteralExpression(node: ObjectLiteralExpression): Expression {
2569-
// We are here because a ComputedPropertyName was used somewhere in the expression.
25702569
const properties = node.properties;
2571-
const numProperties = properties.length;
25722570

25732571
// Find the first computed property.
25742572
// Everything until that point can be emitted as part of the initial object literal.
2575-
let numInitialProperties = numProperties;
2576-
let numInitialPropertiesWithoutYield = numProperties;
2577-
for (let i = 0; i < numProperties; i++) {
2573+
let numInitialProperties = -1;
2574+
for (let i = 0; i < properties.length; i++) {
25782575
const property = properties[i];
2579-
if ((property.transformFlags & TransformFlags.ContainsYield && hierarchyFacts & HierarchyFacts.AsyncFunctionBody)
2580-
&& i < numInitialPropertiesWithoutYield) {
2581-
numInitialPropertiesWithoutYield = i;
2582-
}
2583-
if (Debug.checkDefined(property.name).kind === SyntaxKind.ComputedPropertyName) {
2576+
if (Debug.checkDefined(property.name).kind === SyntaxKind.ComputedPropertyName
2577+
|| (property.transformFlags & TransformFlags.ContainsYield && hierarchyFacts & HierarchyFacts.AsyncFunctionBody)) {
25842578
numInitialProperties = i;
25852579
break;
25862580
}
25872581
}
25882582

2589-
if (numInitialProperties !== numProperties) {
2590-
if (numInitialPropertiesWithoutYield < numInitialProperties) {
2591-
numInitialProperties = numInitialPropertiesWithoutYield;
2592-
}
2583+
if (numInitialProperties < 0) {
2584+
return visitEachChild(node, visitor, context);
2585+
}
25932586

2594-
// For computed properties, we need to create a unique handle to the object
2595-
// literal so we can modify it without risking internal assignments tainting the object.
2596-
const temp = createTempVariable(hoistVariableDeclaration);
2587+
// For computed properties, we need to create a unique handle to the object
2588+
// literal so we can modify it without risking internal assignments tainting the object.
2589+
const temp = createTempVariable(hoistVariableDeclaration);
25972590

2598-
// Write out the first non-computed properties, then emit the rest through indexing on the temp variable.
2599-
const expressions: Expression[] = [];
2600-
const assignment = createAssignment(
2601-
temp,
2602-
setEmitFlags(
2603-
createObjectLiteral(
2604-
visitNodes(properties, visitor, isObjectLiteralElementLike, 0, numInitialProperties),
2605-
node.multiLine
2606-
),
2607-
EmitFlags.Indented
2608-
)
2609-
);
2591+
// Write out the first non-computed properties, then emit the rest through indexing on the temp variable.
2592+
const expressions: Expression[] = [];
2593+
const assignment = createAssignment(
2594+
temp,
2595+
setEmitFlags(
2596+
createObjectLiteral(
2597+
visitNodes(properties, visitor, isObjectLiteralElementLike, 0, numInitialProperties),
2598+
node.multiLine
2599+
),
2600+
EmitFlags.Indented
2601+
)
2602+
);
26102603

2611-
if (node.multiLine) {
2612-
startOnNewLine(assignment);
2613-
}
2604+
if (node.multiLine) {
2605+
startOnNewLine(assignment);
2606+
}
26142607

2615-
expressions.push(assignment);
2608+
expressions.push(assignment);
26162609

2617-
addObjectLiteralMembers(expressions, node, temp, numInitialProperties);
2610+
addObjectLiteralMembers(expressions, node, temp, numInitialProperties);
26182611

2619-
// We need to clone the temporary identifier so that we can write it on a
2620-
// new line
2621-
expressions.push(node.multiLine ? startOnNewLine(getMutableClone(temp)) : temp);
2622-
return inlineExpressions(expressions);
2623-
}
2624-
return visitEachChild(node, visitor, context);
2612+
// We need to clone the temporary identifier so that we can write it on a
2613+
// new line
2614+
expressions.push(node.multiLine ? startOnNewLine(getMutableClone(temp)) : temp);
2615+
return inlineExpressions(expressions);
26252616
}
26262617

26272618
interface ForStatementWithConvertibleInitializer extends ForStatement {

tests/baselines/reference/es5-asyncFunctionObjectLiterals.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ function objectLiteral1() {
7575
switch (_b.label) {
7676
case 0:
7777
_a = {
78-
a: y
79-
};
78+
a: y
79+
};
8080
return [4 /*yield*/, z];
8181
case 1:
8282
x = (_a.b = _b.sent(),

0 commit comments

Comments
 (0)