Skip to content

Commit c3faf0f

Browse files
committed
Added visitor API
1 parent 98b8af6 commit c3faf0f

7 files changed

Lines changed: 1192 additions & 17 deletions

File tree

Jakefile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var compilerSources = [
4040
"utilities.ts",
4141
"binder.ts",
4242
"checker.ts",
43+
"factory.ts",
44+
"visitor.ts",
4345
"sourcemap.ts",
4446
"declarationEmitter.ts",
4547
"emitter.ts",
@@ -60,6 +62,8 @@ var servicesSources = [
6062
"utilities.ts",
6163
"binder.ts",
6264
"checker.ts",
65+
"factory.ts",
66+
"visitor.ts",
6367
"sourcemap.ts",
6468
"declarationEmitter.ts",
6569
"emitter.ts",

src/compiler/core.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ namespace ts {
9191
return undefined;
9292
}
9393

94+
/**
95+
* Iterates through `array` by index and performs the callback on each element of array until the callback
96+
* returns a falsey value, then returns false.
97+
* If no such value is found, the callback is applied to each element of array and `true` is returned.
98+
*/
99+
export function trueForAll<T>(array: T[], callback: (element: T, index: number) => boolean): boolean {
100+
if (array) {
101+
for (let i = 0, len = array.length; i < len; i++) {
102+
if (!callback(array[i], i)) {
103+
return false;
104+
}
105+
}
106+
}
107+
108+
return true;
109+
}
110+
94111
export function contains<T>(array: T[], value: T): boolean {
95112
if (array) {
96113
for (const v of array) {
@@ -242,8 +259,14 @@ namespace ts {
242259
const count = array.length;
243260
if (count > 0) {
244261
let pos = 0;
245-
let result = arguments.length <= 2 ? array[pos] : initial;
246-
pos++;
262+
let result: T | U;
263+
if (arguments.length <= 2) {
264+
result = array[pos];
265+
pos++;
266+
}
267+
else {
268+
result = initial;
269+
}
247270
while (pos < count) {
248271
result = f(<U>result, array[pos]);
249272
pos++;
@@ -260,8 +283,14 @@ namespace ts {
260283
if (array) {
261284
let pos = array.length - 1;
262285
if (pos >= 0) {
263-
let result = arguments.length <= 2 ? array[pos] : initial;
264-
pos--;
286+
let result: T | U;
287+
if (arguments.length <= 2) {
288+
result = array[pos];
289+
pos--;
290+
}
291+
else {
292+
result = initial;
293+
}
265294
while (pos >= 0) {
266295
result = f(<U>result, array[pos]);
267296
pos--;

src/compiler/factory.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="types.ts"/>
2+
/// <reference path="utilities.ts"/>
3+
/* @internal */
4+
namespace 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;
12+
}
13+
else if (array.pos === undefined || array.end === undefined) {
14+
array.pos = -1;
15+
array.end = -1;
16+
}
17+
18+
return array;
19+
}
20+
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;
24+
return array;
25+
}
26+
27+
export function createReturn(expression?: Expression): ReturnStatement {
28+
const node = <ReturnStatement>createSynthesizedNode(SyntaxKind.ReturnStatement);
29+
node.expression = expression;
30+
return node;
31+
}
32+
33+
export function createStatement(expression: Expression): ExpressionStatement {
34+
const node = <ExpressionStatement>createSynthesizedNode(SyntaxKind.ExpressionStatement);
35+
node.expression = expression;
36+
return node;
37+
}
38+
39+
export function createVariableStatement(declarationList: VariableDeclarationList): VariableStatement {
40+
const node = <VariableStatement>createSynthesizedNode(SyntaxKind.VariableStatement);
41+
node.declarationList = declarationList;
42+
return node;
43+
}
44+
45+
export function createVariableDeclarationList(declarations: VariableDeclaration[]): VariableDeclarationList {
46+
const node = <VariableDeclarationList>createSynthesizedNode(SyntaxKind.VariableDeclarationList);
47+
node.declarations = createNodeArray(declarations);
48+
return node;
49+
}
50+
51+
export function createBlock(statements: Statement[]): Block {
52+
const block = <Block>createSynthesizedNode(SyntaxKind.Block);
53+
block.statements = createNodeArray(statements);
54+
return block;
55+
}
56+
}

src/compiler/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"utilities.ts",
1818
"binder.ts",
1919
"checker.ts",
20+
"factory.ts",
21+
"visitor.ts",
2022
"emitter.ts",
2123
"program.ts",
2224
"commandLineParser.ts",

src/compiler/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ namespace ts {
341341

342342
// Synthesized list
343343
SyntaxList,
344+
NodeArrayNode,
344345
// Enum value count
345346
Count,
346347
// Markers
@@ -444,7 +445,8 @@ namespace ts {
444445
decorators?: NodeArray<Decorator>; // Array of decorators (in document order)
445446
modifiers?: ModifiersArray; // Array of modifiers
446447
/* @internal */ id?: number; // Unique id (used to look up NodeLinks)
447-
parent?: Node; // Parent node (initialized by binding
448+
parent?: Node; // Parent node (initialized by binding)
449+
/* @internal */ original?: Node; // The original node if this is an updated node.
448450
/* @internal */ jsDocComment?: JSDocComment; // JSDoc for the node, if it has any. Only for .js files.
449451
/* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding)
450452
/* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding)
@@ -456,6 +458,10 @@ namespace ts {
456458
hasTrailingComma?: boolean;
457459
}
458460

461+
// @kind(SyntaxKind.NodeArrayNode)
462+
export interface NodeArrayNode<T> extends Node, NodeArray<T | NodeArrayNode<T>> {
463+
}
464+
459465
export interface ModifiersArray extends NodeArray<Modifier> {
460466
flags: number;
461467
}
@@ -1287,13 +1293,15 @@ namespace ts {
12871293
statements: NodeArray<Statement>;
12881294
}
12891295

1296+
export type ModuleReference = EntityName | ExternalModuleReference;
1297+
12901298
// @kind(SyntaxKind.ImportEqualsDeclaration)
12911299
export interface ImportEqualsDeclaration extends DeclarationStatement {
12921300
name: Identifier;
12931301

12941302
// 'EntityName' for an internal module reference, 'ExternalModuleReference' for an external
12951303
// module reference.
1296-
moduleReference: EntityName | ExternalModuleReference;
1304+
moduleReference: ModuleReference;
12971305
}
12981306

12991307
// @kind(SyntaxKind.ExternalModuleReference)

0 commit comments

Comments
 (0)