@@ -2555,6 +2555,195 @@ module ts {
25552555 }
25562556 }
25572557
2558+ function emitObjectLiteralBody ( node : ObjectLiteralExpression , numElements : number ) : void {
2559+ if ( numElements === 0 ) {
2560+ write ( "{}" ) ;
2561+ return ;
2562+ }
2563+
2564+ write ( "{" ) ;
2565+
2566+ if ( numElements > 0 ) {
2567+ var properties = node . properties ;
2568+
2569+
2570+ // If we are not doing a downlevel transformation for object literals,
2571+ // then try to preserve the original shape of the object literal.
2572+ // Otherwise just try to preserve the formatting.
2573+ if ( numElements === properties . length ) {
2574+ emitLinePreservingList ( node , properties , /* allowTrailingComma */ languageVersion >= ScriptTarget . ES5 , /* spacesBetweenBraces */ true ) ;
2575+ }
2576+ else {
2577+ var multiLine = ( node . flags & NodeFlags . MultiLine ) !== 0 ;
2578+ if ( ! multiLine ) {
2579+ write ( " " ) ;
2580+ }
2581+ else {
2582+ increaseIndent ( ) ;
2583+ }
2584+
2585+ emitList ( properties , 0 , numElements , /*multiLine*/ multiLine , /*trailingComma*/ false ) ;
2586+
2587+ if ( ! multiLine ) {
2588+ write ( " " ) ;
2589+ }
2590+ else {
2591+ decreaseIndent ( ) ;
2592+ }
2593+ }
2594+ }
2595+
2596+ write ( "}" ) ;
2597+ }
2598+
2599+ function emitDownlevelObjectLiteralWithComputedProperties ( node : ObjectLiteralExpression , firstComputedPropertyIndex : number ) {
2600+ var multiLine = ( node . flags & NodeFlags . MultiLine ) !== 0 ;
2601+ var properties = node . properties ;
2602+
2603+ write ( "(" ) ;
2604+
2605+ if ( multiLine ) {
2606+ increaseIndent ( ) ;
2607+ }
2608+
2609+ // For computed properties, we need to create a unique handle to the object
2610+ // literal so we can modify it without risking internal assignments tainting the object.
2611+ var tempVar = createAndRecordTempVariable ( node ) ;
2612+
2613+ // Write out the first non-computed properties
2614+ // (or all properties if none of them are computed),
2615+ // then emit the rest through indexing on the temp variable.
2616+ emit ( tempVar )
2617+ write ( " = " ) ;
2618+ emitObjectLiteralBody ( node , firstComputedPropertyIndex ) ;
2619+
2620+
2621+ for ( var i = firstComputedPropertyIndex , n = properties . length ; i < n ; i ++ ) {
2622+ writeComma ( ) ;
2623+
2624+ var property = properties [ i ] ;
2625+
2626+ emitStart ( property )
2627+ if ( property . kind === SyntaxKind . GetAccessor || property . kind === SyntaxKind . SetAccessor ) {
2628+ // TODO (drosen): Reconcile with 'emitMemberFunctions'.
2629+ var accessors = getAllAccessorDeclarations ( node . properties , < AccessorDeclaration > property ) ;
2630+ if ( property !== accessors . firstAccessor ) {
2631+ continue ;
2632+ }
2633+ write ( "Object.defineProperty(" ) ;
2634+ emit ( tempVar ) ;
2635+ write ( ", " ) ;
2636+ emitStart ( node . name ) ;
2637+ emitExpressionForPropertyName ( property . name ) ;
2638+ emitEnd ( property . name ) ;
2639+ write ( ", {" ) ;
2640+ increaseIndent ( ) ;
2641+ if ( accessors . getAccessor ) {
2642+ writeLine ( )
2643+ emitLeadingComments ( accessors . getAccessor ) ;
2644+ write ( "get: " ) ;
2645+ emitStart ( accessors . getAccessor ) ;
2646+ write ( "function " ) ;
2647+ emitSignatureAndBody ( accessors . getAccessor ) ;
2648+ emitEnd ( accessors . getAccessor ) ;
2649+ emitTrailingComments ( accessors . getAccessor ) ;
2650+ write ( "," ) ;
2651+ }
2652+ if ( accessors . setAccessor ) {
2653+ writeLine ( ) ;
2654+ emitLeadingComments ( accessors . setAccessor ) ;
2655+ write ( "set: " ) ;
2656+ emitStart ( accessors . setAccessor ) ;
2657+ write ( "function " ) ;
2658+ emitSignatureAndBody ( accessors . setAccessor ) ;
2659+ emitEnd ( accessors . setAccessor ) ;
2660+ emitTrailingComments ( accessors . setAccessor ) ;
2661+ write ( "," ) ;
2662+ }
2663+ writeLine ( ) ;
2664+ write ( "enumerable: true," ) ;
2665+ writeLine ( ) ;
2666+ write ( "configurable: true" ) ;
2667+ decreaseIndent ( ) ;
2668+ writeLine ( ) ;
2669+ write ( "})" ) ;
2670+ emitEnd ( property ) ;
2671+ }
2672+ else {
2673+ emitLeadingComments ( property ) ;
2674+ emitStart ( property . name ) ;
2675+ emit ( tempVar ) ;
2676+ emitMemberAccessForPropertyName ( property . name ) ;
2677+ emitEnd ( property . name ) ;
2678+
2679+ write ( " = " ) ;
2680+
2681+ if ( property . kind === SyntaxKind . PropertyAssignment ) {
2682+ emit ( ( < PropertyAssignment > property ) . initializer ) ;
2683+ }
2684+ else if ( property . kind === SyntaxKind . ShorthandPropertyAssignment ) {
2685+ emitExpressionIdentifier ( ( < ShorthandPropertyAssignment > property ) . name ) ;
2686+ }
2687+ else if ( property . kind === SyntaxKind . MethodDeclaration ) {
2688+ emitFunctionDeclaration ( < MethodDeclaration > property ) ;
2689+ }
2690+ else {
2691+ Debug . fail ( "ObjectLiteralElement type not accounted for: " + property . kind ) ;
2692+ }
2693+ }
2694+
2695+ emitEnd ( property ) ;
2696+ }
2697+
2698+ writeComma ( ) ;
2699+ emit ( tempVar ) ;
2700+
2701+ if ( multiLine ) {
2702+ decreaseIndent ( ) ;
2703+ writeLine ( ) ;
2704+ }
2705+
2706+ write ( ")" ) ;
2707+
2708+ function writeComma ( ) {
2709+ if ( multiLine ) {
2710+ write ( "," ) ;
2711+ writeLine ( ) ;
2712+ }
2713+ else {
2714+ write ( ", " ) ;
2715+ }
2716+ }
2717+ }
2718+
2719+ function emitObjectLiteral ( node : ObjectLiteralExpression ) : void {
2720+ var properties = node . properties ;
2721+
2722+ if ( languageVersion < ScriptTarget . ES6 ) {
2723+ var numProperties = properties . length ;
2724+
2725+ // Find the first computed property.
2726+ // Everything until that point can be emitted as part of the initial object literal.
2727+ var numInitialNonComputedProperties = numProperties ;
2728+ for ( var i = 0 , n = properties . length ; i < n ; i ++ ) {
2729+ if ( properties [ i ] . name . kind === SyntaxKind . ComputedPropertyName ) {
2730+ numInitialNonComputedProperties = i ;
2731+ break ;
2732+ }
2733+ }
2734+
2735+ var hasComputedProperty = numInitialNonComputedProperties !== properties . length ;
2736+ if ( hasComputedProperty ) {
2737+ emitDownlevelObjectLiteralWithComputedProperties ( node , numInitialNonComputedProperties ) ;
2738+ return ;
2739+ }
2740+ }
2741+
2742+ // Ordinary case: either the object has no computed properties
2743+ // or we're compiling with an ES6+ target.
2744+ emitObjectLiteralBody ( node , properties . length ) ;
2745+ }
2746+
25582747 function createSynthesizedNode ( kind : SyntaxKind ) : Node {
25592748 var node = createNode ( kind ) ;
25602749 node . pos = - 1 ;
@@ -2563,7 +2752,7 @@ module ts {
25632752 return node ;
25642753 }
25652754
2566- function emitDownlevelObjectLiteralWithComputedProperties ( node : ObjectLiteralExpression , firstComputedPropertyIndex : number ) : void {
2755+ function emitDownlevelObjectLiteralWithComputedPropertiesThroughRewrite ( node : ObjectLiteralExpression , firstComputedPropertyIndex : number ) : void {
25672756 var parenthesizedObjectLiteral = createDownlevelObjectLiteralWithComputedProperties ( node , firstComputedPropertyIndex ) ;
25682757 return emit ( parenthesizedObjectLiteral ) ;
25692758 }
@@ -2763,7 +2952,7 @@ module ts {
27632952 return result ;
27642953 }
27652954
2766- function emitObjectLiteral ( node : ObjectLiteralExpression ) : void {
2955+ function emitObjectLiteralThroughRewrite ( node : ObjectLiteralExpression ) : void {
27672956 var properties = node . properties ;
27682957
27692958 if ( languageVersion < ScriptTarget . ES6 ) {
@@ -2781,7 +2970,7 @@ module ts {
27812970
27822971 var hasComputedProperty = numInitialNonComputedProperties !== properties . length ;
27832972 if ( hasComputedProperty ) {
2784- emitDownlevelObjectLiteralWithComputedProperties ( node , numInitialNonComputedProperties ) ;
2973+ emitDownlevelObjectLiteralWithComputedPropertiesThroughRewrite ( node , numInitialNonComputedProperties ) ;
27852974 return ;
27862975 }
27872976 }
0 commit comments