Skip to content

Commit 9afc2b3

Browse files
committed
Merge pull request #5355 from Microsoft/initFieldsInConstructor
pre-initialize Node fields in constructor
2 parents 402baba + 7c064af commit 9afc2b3

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

src/compiler/core.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ namespace ts {
775775
};
776776

777777
export interface ObjectAllocator {
778-
getNodeConstructor(kind: SyntaxKind): new () => Node;
778+
getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node;
779779
getSymbolConstructor(): new (flags: SymbolFlags, name: string) => Symbol;
780780
getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type;
781781
getSignatureConstructor(): new (checker: TypeChecker) => Signature;
@@ -796,15 +796,13 @@ namespace ts {
796796

797797
export let objectAllocator: ObjectAllocator = {
798798
getNodeConstructor: kind => {
799-
function Node() {
799+
function Node(pos: number, end: number) {
800+
this.pos = pos;
801+
this.end = end;
802+
this.flags = NodeFlags.None;
803+
this.parent = undefined;
800804
}
801-
Node.prototype = {
802-
kind: kind,
803-
pos: -1,
804-
end: -1,
805-
flags: 0,
806-
parent: undefined,
807-
};
805+
Node.prototype = { kind };
808806
return <any>Node;
809807
},
810808
getSymbolConstructor: () => <any>Symbol,

src/compiler/parser.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
/// <reference path="utilities.ts"/>
33

44
namespace ts {
5-
let nodeConstructors = new Array<new () => Node>(SyntaxKind.Count);
5+
let nodeConstructors = new Array<new (pos: number, end: number) => Node>(SyntaxKind.Count);
66
/* @internal */ export let parseTime = 0;
77

8-
export function getNodeConstructor(kind: SyntaxKind): new () => Node {
8+
export function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node {
99
return nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind));
1010
}
1111

12-
export function createNode(kind: SyntaxKind): Node {
13-
return new (getNodeConstructor(kind))();
12+
export function createNode(kind: SyntaxKind, pos?: number, end?: number): Node {
13+
return new (getNodeConstructor(kind))(pos, end);
1414
}
1515

1616
function visitNode<T>(cbNode: (node: Node) => T, node: Node): T {
@@ -993,14 +993,10 @@ namespace ts {
993993

994994
function createNode(kind: SyntaxKind, pos?: number): Node {
995995
nodeCount++;
996-
let node = new (nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)))();
997996
if (!(pos >= 0)) {
998997
pos = scanner.getStartPos();
999998
}
1000-
1001-
node.pos = pos;
1002-
node.end = pos;
1003-
return node;
999+
return new (nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)))(pos, pos);
10041000
}
10051001

10061002
function finishNode<T extends Node>(node: T, end?: number): T {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ namespace ts {
361361
}
362362

363363
export const enum NodeFlags {
364+
None = 0,
364365
Export = 0x00000001, // Declarations
365366
Ambient = 0x00000002, // Declarations
366367
Public = 0x00000010, // Property/Method

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ namespace ts {
15051505
}
15061506

15071507
export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
1508-
let node = <SynthesizedNode>createNode(kind);
1508+
let node = <SynthesizedNode>createNode(kind, /* pos */ -1, /* end */ -1);
15091509
node.startsOnNewLine = startsOnNewLine;
15101510
return node;
15111511
}

src/harness/harness.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ namespace Utils {
274274

275275
case "flags":
276276
// Print out flags with their enum names.
277-
o[propertyName] = getNodeFlagName(n.flags);
277+
if (n.flags) {
278+
o[propertyName] = getNodeFlagName(n.flags);
279+
}
278280
break;
279281

280282
case "parserContextFlags":

src/services/services.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ namespace ts {
174174
let jsDocCompletionEntries: CompletionEntry[];
175175

176176
function createNode(kind: SyntaxKind, pos: number, end: number, flags: NodeFlags, parent?: Node): NodeObject {
177-
let node = <NodeObject> new (getNodeConstructor(kind))();
178-
node.pos = pos;
179-
node.end = end;
177+
let node = <NodeObject> new (getNodeConstructor(kind))(pos, end);
180178
node.flags = flags;
181179
node.parent = parent;
182180
return node;
@@ -7967,14 +7965,14 @@ namespace ts {
79677965
function initializeServices() {
79687966
objectAllocator = {
79697967
getNodeConstructor: kind => {
7970-
function Node() {
7968+
function Node(pos: number, end: number) {
7969+
this.pos = pos;
7970+
this.end = end;
7971+
this.flags = NodeFlags.None;
7972+
this.parent = undefined;
79717973
}
79727974
let proto = kind === SyntaxKind.SourceFile ? new SourceFileObject() : new NodeObject();
79737975
proto.kind = kind;
7974-
proto.pos = -1;
7975-
proto.end = -1;
7976-
proto.flags = 0;
7977-
proto.parent = undefined;
79787976
Node.prototype = proto;
79797977
return <any>Node;
79807978
},

0 commit comments

Comments
 (0)