Skip to content

Commit 0af4b8a

Browse files
Removed tree rewriting code
1 parent a391d70 commit 0af4b8a

File tree

1 file changed

+0
-243
lines changed

1 file changed

+0
-243
lines changed

src/compiler/emitter.ts

Lines changed: 0 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,249 +2744,6 @@ module ts {
27442744
emitObjectLiteralBody(node, properties.length);
27452745
}
27462746

2747-
function createSynthesizedNode(kind: SyntaxKind): Node {
2748-
var node = createNode(kind);
2749-
node.pos = -1;
2750-
node.end = -1;
2751-
2752-
return node;
2753-
}
2754-
2755-
function emitDownlevelObjectLiteralWithComputedPropertiesThroughRewrite(node: ObjectLiteralExpression, firstComputedPropertyIndex: number): void {
2756-
var parenthesizedObjectLiteral = createDownlevelObjectLiteralWithComputedProperties(node, firstComputedPropertyIndex);
2757-
return emit(parenthesizedObjectLiteral);
2758-
}
2759-
2760-
function createDownlevelObjectLiteralWithComputedProperties(originalObjectLiteral: ObjectLiteralExpression, firstComputedPropertyIndex: number): ParenthesizedExpression {
2761-
// For computed properties, we need to create a unique handle to the object
2762-
// literal so we can modify it without risking internal assignments tainting the object.
2763-
var tempVar = createAndRecordTempVariable(originalObjectLiteral);
2764-
2765-
// Hold onto the initial non-computed properties in a new object literal,
2766-
// then create the rest through property accesses on the temp variable.
2767-
var initialObjectLiteral = <ObjectLiteralExpression>createSynthesizedNode(SyntaxKind.ObjectLiteralExpression);
2768-
initialObjectLiteral.properties = <NodeArray<ObjectLiteralElement>>originalObjectLiteral.properties.slice(0, firstComputedPropertyIndex);
2769-
initialObjectLiteral.flags |= NodeFlags.MultiLine;
2770-
2771-
// The comma expressions that will patch the object literal.
2772-
// This will end up being something like '_a = { ... }, _a.x = 10, _a.y = 20, _a'.
2773-
var propertyPatches = createBinaryExpression(tempVar, SyntaxKind.EqualsToken, initialObjectLiteral);
2774-
2775-
ts.forEach(originalObjectLiteral.properties, property => {
2776-
var patchedProperty = tryCreatePatchingPropertyAssignment(originalObjectLiteral, tempVar, property);
2777-
if (patchedProperty) {
2778-
// TODO(drosen): Preserve comments
2779-
//var leadingComments = getLeadingCommentRanges(currentSourceFile.text, property.pos);
2780-
//var trailingComments = getTrailingCommentRanges(currentSourceFile.text, property.end);
2781-
//addCommentsToSynthesizedNode(patchedProperty, leadingComments, trailingComments);
2782-
2783-
propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, patchedProperty);
2784-
}
2785-
});
2786-
2787-
// Finally, return the temp variable.
2788-
propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, tempVar);
2789-
2790-
var result = createParenthesizedExpression(propertyPatches);
2791-
2792-
// TODO(drosen): Preserve comments
2793-
// var leadingComments = getLeadingCommentRanges(currentSourceFile.text, originalObjectLiteral.pos);
2794-
// var trailingComments = getTrailingCommentRanges(currentSourceFile.text, originalObjectLiteral.end);
2795-
//addCommentsToSynthesizedNode(result, leadingComments, trailingComments);
2796-
2797-
return result;
2798-
}
2799-
2800-
function addCommentsToSynthesizedNode(node: SynthesizedNode, leadingCommentRanges: CommentRange[], trailingCommentRanges: CommentRange[]): void {
2801-
node.leadingCommentRanges = leadingCommentRanges;
2802-
node.trailingCommentRanges = trailingCommentRanges;
2803-
}
2804-
2805-
// Returns 'undefined' if a property has already been accounted for
2806-
// (e.g. a 'get' accessor which has already been emitted along with its 'set' accessor).
2807-
function tryCreatePatchingPropertyAssignment(objectLiteral: ObjectLiteralExpression, tempVar: Identifier, property: ObjectLiteralElement): Expression {
2808-
var leftHandSide = createMemberAccessForPropertyName(tempVar, property.name);
2809-
var maybeRightHandSide = tryGetRightHandSideOfPatchingPropertyAssignment(objectLiteral, property);
2810-
2811-
return maybeRightHandSide && createBinaryExpression(leftHandSide, SyntaxKind.EqualsToken, maybeRightHandSide);
2812-
}
2813-
2814-
function tryGetRightHandSideOfPatchingPropertyAssignment(objectLiteral: ObjectLiteralExpression, property: ObjectLiteralElement) {
2815-
switch (property.kind) {
2816-
case SyntaxKind.PropertyAssignment:
2817-
return (<PropertyAssignment>property).initializer;
2818-
2819-
case SyntaxKind.ShorthandPropertyAssignment:
2820-
// TODO: (andersh) Technically it isn't correct to make an identifier here since getExpressionNamePrefix returns
2821-
// a string containing a dotted name. In general I'm not a fan of mini tree rewriters as this one, elsewhere we
2822-
// manage by just emitting strings (which is a lot more performant).
2823-
//var prefix = createIdentifier(resolver.getExpressionNamePrefix((<ShorthandPropertyAssignment>property).name));
2824-
//return createPropertyAccessExpression(prefix, (<ShorthandPropertyAssignment>property).name);
2825-
return createIdentifier(resolver.getExpressionNameSubstitution((<ShorthandPropertyAssignment>property).name));
2826-
2827-
case SyntaxKind.MethodDeclaration:
2828-
return createFunctionExpression((<MethodDeclaration>property).parameters, (<MethodDeclaration>property).body);
2829-
2830-
case SyntaxKind.GetAccessor:
2831-
case SyntaxKind.SetAccessor:
2832-
var { firstAccessor, getAccessor, setAccessor } = getAllAccessorDeclarations(objectLiteral.properties, <AccessorDeclaration>property);
2833-
2834-
// Only emit the first accessor.
2835-
if (firstAccessor !== property) {
2836-
return undefined;
2837-
}
2838-
2839-
var propertyDescriptor = <ObjectLiteralExpression>createSynthesizedNode(SyntaxKind.ObjectLiteralExpression);
2840-
2841-
var descriptorProperties = <NodeArray<ObjectLiteralElement>>[];
2842-
if (getAccessor) {
2843-
var getProperty = createPropertyAssignment(createIdentifier("get"), createFunctionExpression(getAccessor.parameters, getAccessor.body));
2844-
descriptorProperties.push(getProperty);
2845-
}
2846-
if (setAccessor) {
2847-
var setProperty = createPropertyAssignment(createIdentifier("set"), createFunctionExpression(setAccessor.parameters, setAccessor.body));
2848-
descriptorProperties.push(setProperty);
2849-
}
2850-
2851-
var trueExpr = <PrimaryExpression>createSynthesizedNode(SyntaxKind.TrueKeyword);
2852-
2853-
var enumerableTrue = createPropertyAssignment(createIdentifier("enumerable"), trueExpr);
2854-
descriptorProperties.push(enumerableTrue);
2855-
2856-
var configurableTrue = createPropertyAssignment(createIdentifier("configurable"), trueExpr);
2857-
descriptorProperties.push(configurableTrue);
2858-
2859-
propertyDescriptor.properties = descriptorProperties;
2860-
2861-
var objectDotDefineProperty = createPropertyAccessExpression(createIdentifier("Object"), createIdentifier("defineProperty"));
2862-
return createCallExpression(objectDotDefineProperty, createNodeArray(propertyDescriptor));
2863-
2864-
default:
2865-
Debug.fail(`ObjectLiteralElement kind ${property.kind} not accounted for.`);
2866-
}
2867-
}
2868-
2869-
function createParenthesizedExpression(expression: Expression) {
2870-
var result = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
2871-
result.expression = expression;
2872-
2873-
return result;
2874-
}
2875-
2876-
function createNodeArray<T extends Node>(...elements: T[]): NodeArray<T> {
2877-
var result = <NodeArray<T>>elements;
2878-
result.pos = -1;
2879-
result.end = -1;
2880-
2881-
return result;
2882-
}
2883-
2884-
function createBinaryExpression(left: Expression, operator: SyntaxKind, right: Expression): BinaryExpression {
2885-
var result = <BinaryExpression>createSynthesizedNode(SyntaxKind.BinaryExpression);
2886-
result.operatorToken = createSynthesizedNode(operator);
2887-
result.left = left;
2888-
result.right = right;
2889-
2890-
return result;
2891-
}
2892-
2893-
function createMemberAccessForPropertyName(expression: LeftHandSideExpression, memberName: DeclarationName): PropertyAccessExpression | ElementAccessExpression {
2894-
if (memberName.kind === SyntaxKind.Identifier) {
2895-
return createPropertyAccessExpression(expression, <Identifier>memberName);
2896-
}
2897-
else if (memberName.kind === SyntaxKind.StringLiteral || memberName.kind === SyntaxKind.NumericLiteral) {
2898-
return createElementAccessExpression(expression, <LiteralExpression>memberName);
2899-
}
2900-
else if (memberName.kind === SyntaxKind.ComputedPropertyName) {
2901-
return createElementAccessExpression(expression, (<ComputedPropertyName>memberName).expression);
2902-
}
2903-
else {
2904-
Debug.fail(`Kind '${memberName.kind}' not accounted for.`);
2905-
}
2906-
}
2907-
2908-
function createPropertyAssignment(name: LiteralExpression | Identifier, initializer: Expression) {
2909-
var result = <PropertyAssignment>createSynthesizedNode(SyntaxKind.PropertyAssignment);
2910-
result.name = name;
2911-
result.initializer = initializer;
2912-
2913-
return result;
2914-
}
2915-
2916-
function createFunctionExpression(parameters: NodeArray<ParameterDeclaration>, body: Block): FunctionExpression {
2917-
var result = <FunctionExpression>createSynthesizedNode(SyntaxKind.FunctionExpression);
2918-
result.parameters = parameters;
2919-
result.body = body;
2920-
2921-
return result;
2922-
}
2923-
2924-
function createPropertyAccessExpression(expression: LeftHandSideExpression, name: Identifier): PropertyAccessExpression {
2925-
var result = <PropertyAccessExpression>createSynthesizedNode(SyntaxKind.PropertyAccessExpression);
2926-
result.expression = expression;
2927-
result.name = name;
2928-
2929-
return result;
2930-
}
2931-
2932-
function createElementAccessExpression(expression: LeftHandSideExpression, argumentExpression: Expression): ElementAccessExpression {
2933-
var result = <ElementAccessExpression>createSynthesizedNode(SyntaxKind.ElementAccessExpression);
2934-
result.expression = expression;
2935-
result.argumentExpression = argumentExpression;
2936-
2937-
return result;
2938-
}
2939-
2940-
function createIdentifier(name: string) {
2941-
var result = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
2942-
result.text = name;
2943-
2944-
return result;
2945-
}
2946-
2947-
function createCallExpression(invokedExpression: MemberExpression, arguments: NodeArray<Expression>) {
2948-
var result = <CallExpression>createSynthesizedNode(SyntaxKind.CallExpression);
2949-
result.expression = invokedExpression;
2950-
result.arguments = arguments;
2951-
2952-
return result;
2953-
}
2954-
2955-
function emitObjectLiteralThroughRewrite(node: ObjectLiteralExpression): void {
2956-
var properties = node.properties;
2957-
2958-
if (languageVersion < ScriptTarget.ES6) {
2959-
var numProperties = properties.length;
2960-
2961-
// Find the first computed property.
2962-
// Everything until that point can be emitted as part of the initial object literal.
2963-
var numInitialNonComputedProperties = numProperties;
2964-
for (var i = 0, n = properties.length; i < n; i++) {
2965-
if (properties[i].name.kind === SyntaxKind.ComputedPropertyName) {
2966-
numInitialNonComputedProperties = i;
2967-
break;
2968-
}
2969-
}
2970-
2971-
var hasComputedProperty = numInitialNonComputedProperties !== properties.length;
2972-
if (hasComputedProperty) {
2973-
emitDownlevelObjectLiteralWithComputedPropertiesThroughRewrite(node, numInitialNonComputedProperties);
2974-
return;
2975-
}
2976-
}
2977-
2978-
// Ordinary case: either the object has no computed properties
2979-
// or we're compiling with an ES6+ target.
2980-
write("{");
2981-
2982-
var properties = node.properties;
2983-
if (properties.length) {
2984-
emitLinePreservingList(node, properties, /*allowTrailingComma:*/ languageVersion >= ScriptTarget.ES5, /*spacesBetweenBraces:*/ true)
2985-
}
2986-
2987-
write("}");
2988-
}
2989-
29902747
function emitComputedPropertyName(node: ComputedPropertyName) {
29912748
write("[");
29922749
emit(node.expression);

0 commit comments

Comments
 (0)