1- /// <reference path="types .ts"/>
1+ /// <reference path="core .ts"/>
22/// <reference path="utilities.ts"/>
3- /* @internal */
3+
44namespace ts {
5- export function createNodeArray < T extends Node > ( elements ?: T [ ] , location ?: TextRange ) : NodeArray < T > ;
6- export function createNodeArray < T extends Node , TArray extends NodeArray < T > > ( elements : TArray , location ?: TextRange ) : TArray ;
7- export function createNodeArray < T extends Node , TArray extends NodeArray < T > > ( elements ?: T [ ] , location ?: TextRange ) : TArray {
8- const array = < TArray > ( elements || [ ] ) ;
9- if ( location ) {
10- array . pos = location . pos ;
11- array . end = location . end ;
5+ let NodeConstructor : new ( kind : SyntaxKind , pos : number , end : number ) => Node ;
6+ let SourceFileConstructor : new ( kind : SyntaxKind , pos : number , end : number ) => Node ;
7+
8+ export function createNode ( kind : SyntaxKind , pos ?: number , end ?: number ) : Node {
9+ if ( kind === SyntaxKind . SourceFile ) {
10+ return new ( SourceFileConstructor || ( SourceFileConstructor = objectAllocator . getSourceFileConstructor ( ) ) ) ( kind , pos , end ) ;
1211 }
13- else if ( array . pos === undefined || array . end === undefined ) {
14- array . pos = - 1 ;
15- array . end = - 1 ;
12+ else {
13+ return new ( NodeConstructor || ( NodeConstructor = objectAllocator . getNodeConstructor ( ) ) ) ( kind , pos , end ) ;
1614 }
15+ }
1716
17+ /* @internal */
18+ export function createNodeArray < T extends Node > ( elements ?: T [ ] , pos ?: number , end ?: number ) : NodeArray < T > {
19+ const array = < NodeArray < T > > ( elements || [ ] ) ;
20+ array . pos = pos ;
21+ array . end = end ;
22+ array . arrayKind = ArrayKind . NodeArray ;
1823 return array ;
1924 }
2025
21- export function createNodeArrayNode < T extends Node > ( elements ?: ( T | NodeArrayNode < T > ) [ ] ) : NodeArrayNode < T > {
22- const array = < NodeArrayNode < T > > createNodeArray ( elements ) ;
23- array . kind = SyntaxKind . NodeArrayNode ;
26+ /* @internal */
27+ export function createModifiersArray ( elements ?: Modifier [ ] , pos ?: number , end ?: number ) : ModifiersArray {
28+ const array = < ModifiersArray > ( elements || [ ] ) ;
29+ array . pos = pos ;
30+ array . end = end ;
31+ array . arrayKind = ArrayKind . ModifiersArray ;
32+ array . flags = 0 ;
2433 return array ;
2534 }
2635
36+ /* @internal */
37+ export function createSynthesizedNode ( kind : SyntaxKind , startsOnNewLine ?: boolean ) : Node {
38+ const node = < SynthesizedNode > createNode ( kind , /*pos*/ - 1 , /*end*/ - 1 ) ;
39+ node . startsOnNewLine = startsOnNewLine ;
40+ return node ;
41+ }
42+
43+ /* @internal */
44+ export function createSynthesizedNodeArray < T extends Node > ( elements ?: T [ ] ) : NodeArray < T > {
45+ return createNodeArray ( elements , /*pos*/ - 1 , /*end*/ - 1 ) ;
46+ }
47+
48+ /* @internal */
49+ export function createSynthesizedModifiersArray ( elements ?: Modifier [ ] ) : ModifiersArray {
50+ return createModifiersArray ( elements , /*pos*/ - 1 , /*end*/ - 1 ) ;
51+ }
52+
53+ /**
54+ * Creates a shallow, memberwise clone of a node. The "kind", "pos", "end", "flags", and "parent"
55+ * properties are excluded by default, and can be provided via the "location", "flags", and
56+ * "parent" parameters.
57+ *
58+ * @param node The node to clone.
59+ * @param location An optional TextRange to use to supply the new position.
60+ * @param flags The NodeFlags to use for the cloned node.
61+ * @param parent The parent for the new node.
62+ * @param original An optional pointer to the original source tree node.
63+ */
64+ /* @internal */
65+ export function cloneNode < T extends Node > ( node : T , location ?: TextRange , flags ?: NodeFlags , parent ?: Node , original ?: Node ) : T {
66+ // We don't use "clone" from core.ts here, as we need to preserve the prototype chain of
67+ // the original node. We also need to exclude specific properties and only include own-
68+ // properties (to skip members already defined on the shared prototype).
69+ const clone = location !== undefined
70+ ? < T > createNode ( node . kind , location . pos , location . end )
71+ : < T > createSynthesizedNode ( node . kind ) ;
72+
73+ for ( const key in node ) {
74+ if ( clone . hasOwnProperty ( key ) || ! node . hasOwnProperty ( key ) ) {
75+ continue ;
76+ }
77+
78+ ( < any > clone ) [ key ] = ( < any > node ) [ key ] ;
79+ }
80+
81+ if ( flags !== undefined ) {
82+ clone . flags = flags ;
83+ }
84+
85+ if ( parent !== undefined ) {
86+ clone . parent = parent ;
87+ }
88+
89+ if ( original !== undefined ) {
90+ clone . original = original ;
91+ }
92+
93+ return clone ;
94+ }
95+
96+ /* @internal */
97+ export function createNodeArrayNode < T extends Node > ( elements : T [ ] ) : NodeArrayNode < T > {
98+ const node = < NodeArrayNode < T > > createSynthesizedNode ( SyntaxKind . NodeArrayNode ) ;
99+ node . nodes = createNodeArray ( elements ) ;
100+ return node ;
101+ }
102+
103+ /* @internal */
27104 export function createReturn ( expression ?: Expression ) : ReturnStatement {
28105 const node = < ReturnStatement > createSynthesizedNode ( SyntaxKind . ReturnStatement ) ;
29106 node . expression = expression ;
30107 return node ;
31108 }
32109
110+ /* @internal */
33111 export function createStatement ( expression : Expression ) : ExpressionStatement {
34112 const node = < ExpressionStatement > createSynthesizedNode ( SyntaxKind . ExpressionStatement ) ;
35113 node . expression = expression ;
36114 return node ;
37115 }
38116
117+ /* @internal */
39118 export function createVariableStatement ( declarationList : VariableDeclarationList ) : VariableStatement {
40119 const node = < VariableStatement > createSynthesizedNode ( SyntaxKind . VariableStatement ) ;
41120 node . declarationList = declarationList ;
42121 return node ;
43122 }
44123
124+ /* @internal */
45125 export function createVariableDeclarationList ( declarations : VariableDeclaration [ ] ) : VariableDeclarationList {
46126 const node = < VariableDeclarationList > createSynthesizedNode ( SyntaxKind . VariableDeclarationList ) ;
47127 node . declarations = createNodeArray ( declarations ) ;
48128 return node ;
49129 }
50130
131+ /* @internal */
51132 export function createBlock ( statements : Statement [ ] ) : Block {
52133 const block = < Block > createSynthesizedNode ( SyntaxKind . Block ) ;
53134 block . statements = createNodeArray ( statements ) ;
54135 return block ;
55136 }
137+
56138}
0 commit comments