Skip to content

Commit 34489a4

Browse files
committed
Merge branch 'transforms-visitor' into transforms-flags
2 parents ba9181c + b3e9d26 commit 34489a4

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

src/compiler/factory.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/// <reference path="core.ts"/>
22
/// <reference path="utilities.ts"/>
33

4+
/* @internal */
45
namespace ts {
56
let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
67
let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
78

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);
11-
}
12-
else {
13-
return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, pos, end);
14-
}
9+
function createNode(kind: SyntaxKind, location?: TextRange): Node {
10+
const ConstructorForKind = kind === SyntaxKind.SourceFile
11+
? (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))
12+
: (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()));
13+
14+
return location
15+
? new ConstructorForKind(kind, location.pos, location.end)
16+
: new ConstructorForKind(kind, /*pos*/ -1, /*end*/ -1);
1517
}
1618

17-
/* @internal */
1819
export function createNodeArray<T extends Node>(elements?: T[], pos?: number, end?: number): NodeArray<T> {
1920
const array = <NodeArray<T>>(elements || []);
2021
array.pos = pos;
@@ -23,7 +24,6 @@ namespace ts {
2324
return array;
2425
}
2526

26-
/* @internal */
2727
export function createModifiersArray(elements?: Modifier[], pos?: number, end?: number): ModifiersArray {
2828
const array = <ModifiersArray>(elements || []);
2929
array.pos = pos;
@@ -33,19 +33,16 @@ namespace ts {
3333
return array;
3434
}
3535

36-
/* @internal */
3736
export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
38-
const node = <SynthesizedNode>createNode(kind, /*pos*/ -1, /*end*/ -1);
37+
const node = <SynthesizedNode>createNode(kind, /*location*/ undefined);
3938
node.startsOnNewLine = startsOnNewLine;
4039
return node;
4140
}
4241

43-
/* @internal */
4442
export function createSynthesizedNodeArray<T extends Node>(elements?: T[]): NodeArray<T> {
4543
return createNodeArray(elements, /*pos*/ -1, /*end*/ -1);
4644
}
4745

48-
/* @internal */
4946
export function createSynthesizedModifiersArray(elements?: Modifier[]): ModifiersArray {
5047
return createModifiersArray(elements, /*pos*/ -1, /*end*/ -1);
5148
}
@@ -61,14 +58,11 @@ namespace ts {
6158
* @param parent The parent for the new node.
6259
* @param original An optional pointer to the original source tree node.
6360
*/
64-
/* @internal */
6561
export function cloneNode<T extends Node>(node: T, location?: TextRange, flags?: NodeFlags, parent?: Node, original?: Node): T {
6662
// We don't use "clone" from core.ts here, as we need to preserve the prototype chain of
6763
// the original node. We also need to exclude specific properties and only include own-
6864
// 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);
65+
const clone = <T>createNode(node.kind, location);
7266

7367
for (const key in node) {
7468
if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) {
@@ -93,46 +87,39 @@ namespace ts {
9387
return clone;
9488
}
9589

96-
/* @internal */
9790
export function createNodeArrayNode<T extends Node>(elements: T[]): NodeArrayNode<T> {
9891
const node = <NodeArrayNode<T>>createSynthesizedNode(SyntaxKind.NodeArrayNode);
9992
node.nodes = createNodeArray(elements);
10093
return node;
10194
}
10295

103-
/* @internal */
10496
export function createReturn(expression?: Expression): ReturnStatement {
10597
const node = <ReturnStatement>createSynthesizedNode(SyntaxKind.ReturnStatement);
10698
node.expression = expression;
10799
return node;
108100
}
109101

110-
/* @internal */
111102
export function createStatement(expression: Expression): ExpressionStatement {
112103
const node = <ExpressionStatement>createSynthesizedNode(SyntaxKind.ExpressionStatement);
113104
node.expression = expression;
114105
return node;
115106
}
116107

117-
/* @internal */
118108
export function createVariableStatement(declarationList: VariableDeclarationList): VariableStatement {
119109
const node = <VariableStatement>createSynthesizedNode(SyntaxKind.VariableStatement);
120110
node.declarationList = declarationList;
121111
return node;
122112
}
123113

124-
/* @internal */
125114
export function createVariableDeclarationList(declarations: VariableDeclaration[]): VariableDeclarationList {
126115
const node = <VariableDeclarationList>createSynthesizedNode(SyntaxKind.VariableDeclarationList);
127116
node.declarations = createNodeArray(declarations);
128117
return node;
129118
}
130119

131-
/* @internal */
132120
export function createBlock(statements: Statement[]): Block {
133121
const block = <Block>createSynthesizedNode(SyntaxKind.Block);
134122
block.statements = createNodeArray(statements);
135123
return block;
136124
}
137-
138125
}

src/compiler/parser.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
namespace ts {
66
/* @internal */ export let parseTime = 0;
77

8+
let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
9+
let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
10+
11+
export function createNode(kind: SyntaxKind, pos?: number, end?: number): Node {
12+
if (kind === SyntaxKind.SourceFile) {
13+
return new (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))(kind, pos, end);
14+
}
15+
else {
16+
return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, pos, end);
17+
}
18+
}
19+
820
function visitNode<T>(cbNode: (node: Node) => T, node: Node): T {
921
if (node) {
1022
return cbNode(node);

0 commit comments

Comments
 (0)