Skip to content

Commit cbb910a

Browse files
committed
Merge branch 'transforms-flags' into transforms-transformer
2 parents 1e35593 + 34489a4 commit cbb910a

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

src/compiler/factory.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ namespace ts {
66
let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
77
let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
88

9-
function createNode(kind: SyntaxKind, pos?: number, end?: number): Node {
10-
if (kind === SyntaxKind.SourceFile) {
11-
return new (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))(kind, pos, end);
12-
}
13-
else {
14-
return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, pos, end);
15-
}
16-
}
9+
function createNode(kind: SyntaxKind, location?: TextRange): Node {
10+
const ConstructorForKind = kind === SyntaxKind.SourceFile
11+
? (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))
12+
: (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()));
1713

18-
function allocateNode(kind: SyntaxKind, location?: TextRange) {
19-
return location ? createNode(kind, location.pos, location.end) : createSynthesizedNode(kind);
14+
return location
15+
? new ConstructorForKind(kind, location.pos, location.end)
16+
: new ConstructorForKind(kind, /*pos*/ -1, /*end*/ -1);
2017
}
2118

2219
export function createNodeArray<T extends Node>(elements?: T[], pos?: number, end?: number): NodeArray<T> {
@@ -37,7 +34,7 @@ namespace ts {
3734
}
3835

3936
export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
40-
const node = <SynthesizedNode>createNode(kind, /*pos*/ -1, /*end*/ -1);
37+
const node = <SynthesizedNode>createNode(kind, /*location*/ undefined);
4138
node.startsOnNewLine = startsOnNewLine;
4239
return node;
4340
}
@@ -65,9 +62,7 @@ namespace ts {
6562
// We don't use "clone" from core.ts here, as we need to preserve the prototype chain of
6663
// the original node. We also need to exclude specific properties and only include own-
6764
// properties (to skip members already defined on the shared prototype).
68-
const clone = location !== undefined
69-
? <T>createNode(node.kind, location.pos, location.end)
70-
: <T>createSynthesizedNode(node.kind);
65+
const clone = <T>createNode(node.kind, location);
7166

7267
for (const key in node) {
7368
if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) {
@@ -127,22 +122,21 @@ namespace ts {
127122
block.statements = createNodeArray(statements);
128123
return block;
129124
}
130-
131125
export function createVariableDeclaration(name: BindingPattern | Identifier, initializer?: Expression, location?: TextRange): VariableDeclaration {
132-
const node = <VariableDeclaration>allocateNode(SyntaxKind.VariableDeclaration, location);
126+
const node = <VariableDeclaration>createNode(SyntaxKind.VariableDeclaration, location);
133127
node.name = name;
134128
node.initializer = initializer;
135129
return node;
136130
}
137131

138132
export function createIdentifier(text: string): Identifier {
139-
const node = <Identifier>allocateNode(SyntaxKind.Identifier);
133+
const node = <Identifier>createNode(SyntaxKind.Identifier);
140134
node.text = text;
141135
return node;
142136
}
143137

144138
export function createTempVariable(tempKind: TempVariableKind): Identifier {
145-
const name = <Identifier>allocateNode(SyntaxKind.Identifier);
139+
const name = <Identifier>createNode(SyntaxKind.Identifier);
146140
name.tempKind = tempKind;
147141
getNodeId(name);
148142
return name;
@@ -153,25 +147,25 @@ namespace ts {
153147
export function createLiteral(value: string | number | boolean | void): PrimaryExpression;
154148
export function createLiteral<T extends PrimaryExpression>(value: string | number | boolean | void): T {
155149
if (typeof value === "string") {
156-
const node = <T & StringLiteral>allocateNode(SyntaxKind.StringLiteral);
150+
const node = <T & StringLiteral>createNode(SyntaxKind.StringLiteral);
157151
node.text = value;
158152
return node;
159153
}
160154
else if (typeof value === "number") {
161-
const node = <T & LiteralExpression>allocateNode(SyntaxKind.NumericLiteral);
155+
const node = <T & LiteralExpression>createNode(SyntaxKind.NumericLiteral);
162156
node.text = value.toString();
163157
return node;
164158
}
165159
else if (typeof value === "boolean") {
166-
return <T>allocateNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword);
160+
return <T>createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword);
167161
}
168162
else if (value === null) {
169-
return <T>allocateNode(SyntaxKind.NullKeyword);
163+
return <T>createNode(SyntaxKind.NullKeyword);
170164
}
171165
}
172166

173167
export function createVoid(expression: UnaryExpression) {
174-
const node = <VoidExpression>allocateNode(SyntaxKind.VoidExpression);
168+
const node = <VoidExpression>createNode(SyntaxKind.VoidExpression);
175169
node.expression = expression;
176170
return node;
177171
}
@@ -181,22 +175,22 @@ namespace ts {
181175
}
182176

183177
export function createPropertyAccess(expression: Expression, name: string | Identifier) {
184-
const node = <PropertyAccessExpression>allocateNode(SyntaxKind.PropertyAccessExpression);
178+
const node = <PropertyAccessExpression>createNode(SyntaxKind.PropertyAccessExpression);
185179
node.expression = parenthesizeForAccess(expression);
186180
node.dotToken = createSynthesizedNode(SyntaxKind.DotToken);
187181
node.name = coerceIdentifier(name);
188182
return node;
189183
}
190184

191185
export function createElementAccess(expression: Expression, index: string | number | Expression) {
192-
const node = <ElementAccessExpression>allocateNode(SyntaxKind.ElementAccessExpression);
186+
const node = <ElementAccessExpression>createNode(SyntaxKind.ElementAccessExpression);
193187
node.expression = parenthesizeForAccess(expression);
194188
node.argumentExpression = coerceExpression(index);
195189
return node;
196190
}
197191

198192
export function createConditional(condition: Expression, whenTrue: Expression, whenFalse: Expression) {
199-
const node = <ConditionalExpression>allocateNode(SyntaxKind.ConditionalExpression);
193+
const node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression);
200194
node.condition = condition;
201195
node.questionToken = createSynthesizedNode(SyntaxKind.QualifiedName);
202196
node.whenTrue = whenTrue;
@@ -206,7 +200,7 @@ namespace ts {
206200
}
207201

208202
export function createBinary(left: Expression, operator: SyntaxKind, right: Expression, location?: TextRange) {
209-
const node = <BinaryExpression>allocateNode(SyntaxKind.BinaryExpression, location);
203+
const node = <BinaryExpression>createNode(SyntaxKind.BinaryExpression, location);
210204
node.left = parenthesizeForBinary(left, operator, BinaryOperand.Left);
211205
node.operatorToken = createSynthesizedNode(operator);
212206
node.right = parenthesizeForBinary(right, operator, BinaryOperand.Right);
@@ -226,7 +220,7 @@ namespace ts {
226220
}
227221

228222
export function createCall(expression: Expression, argumentsArray: Expression[]) {
229-
const node = <CallExpression>allocateNode(SyntaxKind.CallExpression);
223+
const node = <CallExpression>createNode(SyntaxKind.CallExpression);
230224
node.expression = parenthesizeForAccess(expression);
231225
node.arguments = createNodeArray(argumentsArray);
232226
return node;
@@ -238,7 +232,7 @@ namespace ts {
238232
}
239233

240234
export function parenthesizeExpression(expression: Expression) {
241-
const node = <ParenthesizedExpression>allocateNode(SyntaxKind.ParenthesizedExpression);
235+
const node = <ParenthesizedExpression>createNode(SyntaxKind.ParenthesizedExpression);
242236
node.expression = expression;
243237
return node;
244238
}

0 commit comments

Comments
 (0)