From b3e9d262b85e00baf79d3b01d2ac1e5cf5d59c64 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 8 Feb 2016 11:04:01 -0800 Subject: [PATCH] Moved createNode back to parser, added local createNode to factory --- src/compiler/factory.ts | 35 +++++++++++------------------------ src/compiler/parser.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index c28162d8723..99f99230645 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -1,20 +1,21 @@ /// /// +/* @internal */ namespace ts { let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; - export function createNode(kind: SyntaxKind, pos?: number, end?: number): Node { - if (kind === SyntaxKind.SourceFile) { - return new (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))(kind, pos, end); - } - else { - return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, pos, end); - } + function createNode(kind: SyntaxKind, location?: TextRange): Node { + const ConstructorForKind = kind === SyntaxKind.SourceFile + ? (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor())) + : (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor())); + + return location + ? new ConstructorForKind(kind, location.pos, location.end) + : new ConstructorForKind(kind, /*pos*/ -1, /*end*/ -1); } - /* @internal */ export function createNodeArray(elements?: T[], pos?: number, end?: number): NodeArray { const array = >(elements || []); array.pos = pos; @@ -23,7 +24,6 @@ namespace ts { return array; } - /* @internal */ export function createModifiersArray(elements?: Modifier[], pos?: number, end?: number): ModifiersArray { const array = (elements || []); array.pos = pos; @@ -33,19 +33,16 @@ namespace ts { return array; } - /* @internal */ export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node { - const node = createNode(kind, /*pos*/ -1, /*end*/ -1); + const node = createNode(kind, /*location*/ undefined); node.startsOnNewLine = startsOnNewLine; return node; } - /* @internal */ export function createSynthesizedNodeArray(elements?: T[]): NodeArray { return createNodeArray(elements, /*pos*/ -1, /*end*/ -1); } - /* @internal */ export function createSynthesizedModifiersArray(elements?: Modifier[]): ModifiersArray { return createModifiersArray(elements, /*pos*/ -1, /*end*/ -1); } @@ -61,14 +58,11 @@ namespace ts { * @param parent The parent for the new node. * @param original An optional pointer to the original source tree node. */ - /* @internal */ export function cloneNode(node: T, location?: TextRange, flags?: NodeFlags, parent?: Node, original?: Node): T { // We don't use "clone" from core.ts here, as we need to preserve the prototype chain of // the original node. We also need to exclude specific properties and only include own- // properties (to skip members already defined on the shared prototype). - const clone = location !== undefined - ? createNode(node.kind, location.pos, location.end) - : createSynthesizedNode(node.kind); + const clone = createNode(node.kind, location); for (const key in node) { if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) { @@ -93,46 +87,39 @@ namespace ts { return clone; } - /* @internal */ export function createNodeArrayNode(elements: T[]): NodeArrayNode { const node = >createSynthesizedNode(SyntaxKind.NodeArrayNode); node.nodes = createNodeArray(elements); return node; } - /* @internal */ export function createReturn(expression?: Expression): ReturnStatement { const node = createSynthesizedNode(SyntaxKind.ReturnStatement); node.expression = expression; return node; } - /* @internal */ export function createStatement(expression: Expression): ExpressionStatement { const node = createSynthesizedNode(SyntaxKind.ExpressionStatement); node.expression = expression; return node; } - /* @internal */ export function createVariableStatement(declarationList: VariableDeclarationList): VariableStatement { const node = createSynthesizedNode(SyntaxKind.VariableStatement); node.declarationList = declarationList; return node; } - /* @internal */ export function createVariableDeclarationList(declarations: VariableDeclaration[]): VariableDeclarationList { const node = createSynthesizedNode(SyntaxKind.VariableDeclarationList); node.declarations = createNodeArray(declarations); return node; } - /* @internal */ export function createBlock(statements: Statement[]): Block { const block = createSynthesizedNode(SyntaxKind.Block); block.statements = createNodeArray(statements); return block; } - } \ No newline at end of file diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 1d9f9fe2305..b21f9919f45 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5,6 +5,18 @@ namespace ts { /* @internal */ export let parseTime = 0; + let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; + let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; + + export function createNode(kind: SyntaxKind, pos?: number, end?: number): Node { + if (kind === SyntaxKind.SourceFile) { + return new (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))(kind, pos, end); + } + else { + return new (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()))(kind, pos, end); + } + } + function visitNode(cbNode: (node: Node) => T, node: Node): T { if (node) { return cbNode(node);