@@ -2130,6 +2130,24 @@ namespace ts {
21302130
21312131 // Compound nodes
21322132
2133+ export function createImmediatelyInvokedFunctionExpression ( statements : Statement [ ] ) : CallExpression ;
2134+ export function createImmediatelyInvokedFunctionExpression ( statements : Statement [ ] , param : ParameterDeclaration , paramValue : Expression ) : CallExpression ;
2135+ export function createImmediatelyInvokedFunctionExpression ( statements : Statement [ ] , param ?: ParameterDeclaration , paramValue ?: Expression ) {
2136+ return createCall (
2137+ createFunctionExpression (
2138+ /*modifiers*/ undefined ,
2139+ /*asteriskToken*/ undefined ,
2140+ /*name*/ undefined ,
2141+ /*typeParameters*/ undefined ,
2142+ /*parameters*/ param ? [ param ] : [ ] ,
2143+ /*type*/ undefined ,
2144+ createBlock ( statements , /*multiLine*/ true )
2145+ ) ,
2146+ /*typeArguments*/ undefined ,
2147+ /*argumentsArray*/ paramValue ? [ paramValue ] : [ ]
2148+ ) ;
2149+ }
2150+
21332151 export function createComma ( left : Expression , right : Expression ) {
21342152 return < Expression > createBinary ( left , SyntaxKind . CommaToken , right ) ;
21352153 }
@@ -3212,6 +3230,26 @@ namespace ts {
32123230 return isBlock ( node ) ? node : setTextRange ( createBlock ( [ setTextRange ( createReturn ( node ) , node ) ] , multiLine ) , node ) ;
32133231 }
32143232
3233+ export function convertFunctionDeclarationToExpression ( node : FunctionDeclaration ) {
3234+ Debug . assert ( ! ! node . body ) ;
3235+ const updated = createFunctionExpression (
3236+ node . modifiers ,
3237+ node . asteriskToken ,
3238+ node . name ,
3239+ node . typeParameters ,
3240+ node . parameters ,
3241+ node . type ,
3242+ node . body
3243+ ) ;
3244+ setOriginalNode ( updated , node ) ;
3245+ setTextRange ( updated , node ) ;
3246+ if ( node . startsOnNewLine ) {
3247+ updated . startsOnNewLine = true ;
3248+ }
3249+ aggregateTransformFlags ( updated ) ;
3250+ return updated ;
3251+ }
3252+
32153253 function isUseStrictPrologue ( node : ExpressionStatement ) : boolean {
32163254 return ( node . expression as StringLiteral ) . text === "use strict" ;
32173255 }
@@ -3610,7 +3648,7 @@ namespace ts {
36103648 if ( kind === SyntaxKind . FunctionExpression || kind === SyntaxKind . ArrowFunction ) {
36113649 const mutableCall = getMutableClone ( emittedExpression ) ;
36123650 mutableCall . expression = setTextRange ( createParen ( callee ) , callee ) ;
3613- return recreatePartiallyEmittedExpressions ( expression , mutableCall ) ;
3651+ return recreateOuterExpressions ( expression , mutableCall , OuterExpressionKinds . PartiallyEmittedExpressions ) ;
36143652 }
36153653 }
36163654 else {
@@ -3652,22 +3690,6 @@ namespace ts {
36523690 }
36533691 }
36543692
3655- /**
3656- * Clones a series of not-emitted expressions with a new inner expression.
3657- *
3658- * @param originalOuterExpression The original outer expression.
3659- * @param newInnerExpression The new inner expression.
3660- */
3661- function recreatePartiallyEmittedExpressions ( originalOuterExpression : Expression , newInnerExpression : Expression ) {
3662- if ( isPartiallyEmittedExpression ( originalOuterExpression ) ) {
3663- const clone = getMutableClone ( originalOuterExpression ) ;
3664- clone . expression = recreatePartiallyEmittedExpressions ( clone . expression , newInnerExpression ) ;
3665- return clone ;
3666- }
3667-
3668- return newInnerExpression ;
3669- }
3670-
36713693 function getLeftmostExpression ( node : Expression ) : Expression {
36723694 while ( true ) {
36733695 switch ( node . kind ) {
@@ -3714,6 +3736,21 @@ namespace ts {
37143736 All = Parentheses | Assertions | PartiallyEmittedExpressions
37153737 }
37163738
3739+ export type OuterExpression = ParenthesizedExpression | TypeAssertion | AsExpression | PartiallyEmittedExpression ;
3740+
3741+ export function isOuterExpression ( node : Node , kinds = OuterExpressionKinds . All ) : node is OuterExpression {
3742+ switch ( node . kind ) {
3743+ case SyntaxKind . ParenthesizedExpression :
3744+ return ( kinds & OuterExpressionKinds . Parentheses ) !== 0 ;
3745+ case SyntaxKind . TypeAssertionExpression :
3746+ case SyntaxKind . AsExpression :
3747+ return ( kinds & OuterExpressionKinds . Assertions ) !== 0 ;
3748+ case SyntaxKind . PartiallyEmittedExpression :
3749+ return ( kinds & OuterExpressionKinds . PartiallyEmittedExpressions ) !== 0 ;
3750+ }
3751+ return false ;
3752+ }
3753+
37173754 export function skipOuterExpressions ( node : Expression , kinds ?: OuterExpressionKinds ) : Expression ;
37183755 export function skipOuterExpressions ( node : Node , kinds ?: OuterExpressionKinds ) : Node ;
37193756 export function skipOuterExpressions ( node : Node , kinds = OuterExpressionKinds . All ) {
@@ -3767,6 +3804,25 @@ namespace ts {
37673804 return node ;
37683805 }
37693806
3807+ function updateOuterExpression ( outerExpression : OuterExpression , expression : Expression ) {
3808+ switch ( outerExpression . kind ) {
3809+ case SyntaxKind . ParenthesizedExpression : return updateParen ( outerExpression , expression ) ;
3810+ case SyntaxKind . TypeAssertionExpression : return updateTypeAssertion ( outerExpression , outerExpression . type , expression ) ;
3811+ case SyntaxKind . AsExpression : return updateAsExpression ( outerExpression , expression , outerExpression . type ) ;
3812+ case SyntaxKind . PartiallyEmittedExpression : return updatePartiallyEmittedExpression ( outerExpression , expression ) ;
3813+ }
3814+ }
3815+
3816+ export function recreateOuterExpressions ( outerExpression : Expression | undefined , innerExpression : Expression , kinds = OuterExpressionKinds . All ) : Expression {
3817+ if ( outerExpression && isOuterExpression ( outerExpression , kinds ) ) {
3818+ return updateOuterExpression (
3819+ outerExpression ,
3820+ recreateOuterExpressions ( outerExpression . expression , innerExpression )
3821+ ) ;
3822+ }
3823+ return innerExpression ;
3824+ }
3825+
37703826 export function startOnNewLine < T extends Node > ( node : T ) : T {
37713827 node . startsOnNewLine = true ;
37723828 return node ;
0 commit comments