@@ -110,21 +110,15 @@ namespace ts {
110110 }
111111
112112 /**
113- * Creates a shallow, memberwise clone of a node. The "kind", "pos", "end", "flags", and "parent"
114- * properties are excluded by default, and can be provided via the "location", "flags", and
115- * "parent" parameters.
116- *
117- * @param node The node to clone.
118- * @param location An optional TextRange to use to supply the new position.
119- * @param flags The NodeFlags to use for the cloned node.
120- * @param parent The parent for the new node.
121- * @param original An optional pointer to the original source tree node.
113+ * Creates a shallow, memberwise clone of a node with no source map location.
122114 */
123- export function cloneNode < T extends Node > ( node : T , location ?: TextRange , flags ?: NodeFlags , parent ?: Node , original ?: Node ) : T {
115+ export function getSynthesizedClone < T extends Node > ( node : T ) : T {
124116 // We don't use "clone" from core.ts here, as we need to preserve the prototype chain of
125117 // the original node. We also need to exclude specific properties and only include own-
126118 // properties (to skip members already defined on the shared prototype).
127- const clone = < T > createNode ( node . kind , location ) ;
119+ const clone = < T > createSynthesizedNode ( node . kind ) ;
120+ clone . flags = node . flags ;
121+ clone . original = node ;
128122
129123 for ( const key in node ) {
130124 if ( clone . hasOwnProperty ( key ) || ! node . hasOwnProperty ( key ) ) {
@@ -134,40 +128,28 @@ namespace ts {
134128 ( < any > clone ) [ key ] = ( < any > node ) [ key ] ;
135129 }
136130
137- if ( flags !== undefined ) {
138- clone . flags = flags ;
139- }
140-
141- if ( parent !== undefined ) {
142- clone . parent = parent ;
143- }
144-
145- if ( original !== undefined ) {
146- clone . original = original ;
147- }
148-
149131 return clone ;
150132 }
151133
152134 /**
153135 * Creates a shallow, memberwise clone of a node for mutation.
154136 */
155137 export function getMutableClone < T extends Node > ( node : T ) : T {
156- return cloneNode ( node , /*location*/ node , node . flags , /*parent*/ undefined , /*original*/ node ) ;
157- }
158-
159- /**
160- * Creates a shallow, memberwise clone of a node with no source map location.
161- */
162- export function getSynthesizedClone < T extends Node > ( node : T ) : T {
163- return nodeIsSynthesized ( node ) ? node : cloneNode ( node , /*location*/ undefined , node . flags , /*parent*/ undefined , /*original*/ node ) ;
138+ const clone = getSynthesizedClone ( node ) ;
139+ clone . pos = node . pos ;
140+ clone . end = node . end ;
141+ clone . parent = node . parent ;
142+ return clone ;
164143 }
165144
166145 /**
167146 * Creates a shallow, memberwise clone of a node at the specified source map location.
168147 */
169148 export function getRelocatedClone < T extends Node > ( node : T , location : TextRange ) : T {
170- return cloneNode ( node , location , node . flags , /*parent*/ undefined , /*original*/ node ) ;
149+ const clone = getSynthesizedClone ( node ) ;
150+ clone . pos = location . pos ;
151+ clone . end = location . end ;
152+ return clone ;
171153 }
172154
173155 export function createNodeArrayNode < T extends Node > ( elements : T [ ] ) : NodeArrayNode < T > {
@@ -718,8 +700,8 @@ namespace ts {
718700
719701 export function createMemberAccessForPropertyName ( target : Expression , memberName : PropertyName , location ?: TextRange ) : MemberExpression {
720702 return isIdentifier ( memberName )
721- ? createPropertyAccess ( target , cloneNode ( memberName ) , location )
722- : createElementAccess ( target , cloneNode ( isComputedPropertyName ( memberName ) ? memberName . expression : memberName ) , location ) ;
703+ ? createPropertyAccess ( target , getSynthesizedClone ( memberName ) , location )
704+ : createElementAccess ( target , getSynthesizedClone ( isComputedPropertyName ( memberName ) ? memberName . expression : memberName ) , location ) ;
723705 }
724706
725707 export function createRestParameter ( name : string | Identifier ) {
@@ -1154,15 +1136,15 @@ namespace ts {
11541136 return isQualifiedName ( node )
11551137 ? createPropertyAccess (
11561138 createExpressionFromEntityName ( node . left ) ,
1157- cloneNode ( node . right )
1139+ getSynthesizedClone ( node . right )
11581140 )
1159- : cloneNode ( node ) ;
1141+ : getSynthesizedClone ( node ) ;
11601142 }
11611143
11621144 export function createExpressionForPropertyName ( memberName : PropertyName , location ?: TextRange ) : Expression {
11631145 return isIdentifier ( memberName ) ? createLiteral ( memberName . text , location )
1164- : isComputedPropertyName ( memberName ) ? cloneNode ( memberName . expression , location )
1165- : cloneNode ( memberName , location ) ;
1146+ : isComputedPropertyName ( memberName ) ? getRelocatedClone ( memberName . expression , location )
1147+ : getRelocatedClone ( memberName , location ) ;
11661148 }
11671149
11681150 // Utilities
@@ -1370,7 +1352,7 @@ namespace ts {
13701352 const callee = expression . expression ;
13711353 if ( callee . kind === SyntaxKind . FunctionExpression
13721354 || callee . kind === SyntaxKind . ArrowFunction ) {
1373- const clone = cloneNode ( expression , expression , expression . flags , expression . parent , expression ) ;
1355+ const clone = getMutableClone ( expression ) ;
13741356 clone . expression = createParen ( callee , /*location*/ callee ) ;
13751357 return clone ;
13761358 }
0 commit comments