Skip to content

Commit fd320b0

Browse files
committed
Support strictNullChecks mode in visitors
1 parent 0f495fb commit fd320b0

File tree

1 file changed

+98
-4
lines changed

1 file changed

+98
-4
lines changed

src/compiler/visitor.ts

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,42 @@ namespace ts {
1212
* @param optional An optional value indicating whether the Node is itself optional.
1313
* @param lift An optional callback to execute to lift a NodeArray into a valid Node.
1414
*/
15-
export function visitNode<T extends Node>(node: T, visitor: Visitor, test: (node: Node) => boolean, optional?: boolean, lift?: (node: NodeArray<Node>) => T): T {
15+
export function visitNode<T extends Node>(node: T, visitor: Visitor, test: (node: Node) => node is T, optional?: boolean, lift?: (node: NodeArray<Node>) => T): T;
16+
17+
/**
18+
* Visits a Node using the supplied visitor, possibly returning a new Node in its place.
19+
*
20+
* @param node The Node to visit.
21+
* @param visitor The callback used to visit the Node.
22+
* @param test A callback to execute to verify the Node is valid.
23+
* @param optional An optional value indicating whether the Node is itself optional.
24+
* @param lift An optional callback to execute to lift a NodeArray into a valid Node.
25+
*/
26+
export function visitNode<T extends Node>(node: T, visitor: Visitor, test?: (node: Node) => boolean, optional?: boolean, lift?: (node: NodeArray<Node>) => T): T;
27+
28+
/**
29+
* Visits a Node using the supplied visitor, possibly returning a new Node in its place.
30+
*
31+
* @param node The Node to visit.
32+
* @param visitor The callback used to visit the Node.
33+
* @param test A callback to execute to verify the Node is valid.
34+
* @param optional An optional value indicating whether the Node is itself optional.
35+
* @param lift An optional callback to execute to lift a NodeArray into a valid Node.
36+
*/
37+
export function visitNode<T extends Node>(node: T | undefined, visitor: Visitor, test: (node: Node) => node is T, optional?: boolean, lift?: (node: NodeArray<Node>) => T): T | undefined;
38+
39+
/**
40+
* Visits a Node using the supplied visitor, possibly returning a new Node in its place.
41+
*
42+
* @param node The Node to visit.
43+
* @param visitor The callback used to visit the Node.
44+
* @param test A callback to execute to verify the Node is valid.
45+
* @param optional An optional value indicating whether the Node is itself optional.
46+
* @param lift An optional callback to execute to lift a NodeArray into a valid Node.
47+
*/
48+
export function visitNode<T extends Node>(node: T | undefined, visitor: Visitor, test?: (node: Node) => boolean, optional?: boolean, lift?: (node: NodeArray<Node>) => T): T | undefined;
49+
50+
export function visitNode<T extends Node | undefined>(node: T, visitor: Visitor, test?: (node: Node) => boolean, optional?: boolean, lift?: (node: NodeArray<Node>) => T): T {
1651
if (node === undefined || visitor === undefined) {
1752
return node;
1853
}
@@ -52,7 +87,51 @@ namespace ts {
5287
* @param start An optional value indicating the starting offset at which to start visiting.
5388
* @param count An optional value indicating the maximum number of nodes to visit.
5489
*/
55-
export function visitNodes<T extends Node>(nodes: NodeArray<T>, visitor: Visitor, test: (node: Node) => boolean, start?: number, count?: number): NodeArray<T> {
90+
export function visitNodes<T extends Node>(nodes: NodeArray<T>, visitor: Visitor, test: (node: Node) => node is T, start?: number, count?: number): NodeArray<T>;
91+
92+
/**
93+
* Visits a NodeArray using the supplied visitor, possibly returning a new NodeArray in its place.
94+
*
95+
* @param nodes The NodeArray to visit.
96+
* @param visitor The callback used to visit a Node.
97+
* @param test A node test to execute for each node.
98+
* @param start An optional value indicating the starting offset at which to start visiting.
99+
* @param count An optional value indicating the maximum number of nodes to visit.
100+
*/
101+
export function visitNodes<T extends Node>(nodes: NodeArray<T>, visitor: Visitor, test?: (node: Node) => boolean, start?: number, count?: number): NodeArray<T>;
102+
103+
/**
104+
* Visits a NodeArray using the supplied visitor, possibly returning a new NodeArray in its place.
105+
*
106+
* @param nodes The NodeArray to visit.
107+
* @param visitor The callback used to visit a Node.
108+
* @param test A node test to execute for each node.
109+
* @param start An optional value indicating the starting offset at which to start visiting.
110+
* @param count An optional value indicating the maximum number of nodes to visit.
111+
*/
112+
export function visitNodes<T extends Node>(nodes: NodeArray<T> | undefined, visitor: Visitor, test: (node: Node) => node is T, start?: number, count?: number): NodeArray<T> | undefined;
113+
114+
/**
115+
* Visits a NodeArray using the supplied visitor, possibly returning a new NodeArray in its place.
116+
*
117+
* @param nodes The NodeArray to visit.
118+
* @param visitor The callback used to visit a Node.
119+
* @param test A node test to execute for each node.
120+
* @param start An optional value indicating the starting offset at which to start visiting.
121+
* @param count An optional value indicating the maximum number of nodes to visit.
122+
*/
123+
export function visitNodes<T extends Node>(nodes: NodeArray<T> | undefined, visitor: Visitor, test?: (node: Node) => boolean, start?: number, count?: number): NodeArray<T> | undefined;
124+
125+
/**
126+
* Visits a NodeArray using the supplied visitor, possibly returning a new NodeArray in its place.
127+
*
128+
* @param nodes The NodeArray to visit.
129+
* @param visitor The callback used to visit a Node.
130+
* @param test A node test to execute for each node.
131+
* @param start An optional value indicating the starting offset at which to start visiting.
132+
* @param count An optional value indicating the maximum number of nodes to visit.
133+
*/
134+
export function visitNodes<T extends Node>(nodes: NodeArray<T> | undefined, visitor: Visitor, test?: (node: Node) => boolean, start?: number, count?: number): NodeArray<T> | undefined {
56135
if (nodes === undefined || visitor === undefined) {
57136
return nodes;
58137
}
@@ -137,6 +216,11 @@ namespace ts {
137216
* environment and merging hoisted declarations upon completion.
138217
*/
139218
export function visitFunctionBody(node: FunctionBody, visitor: Visitor, context: TransformationContext): FunctionBody;
219+
/**
220+
* Resumes a suspended lexical environment and visits a function body, ending the lexical
221+
* environment and merging hoisted declarations upon completion.
222+
*/
223+
export function visitFunctionBody(node: FunctionBody | undefined, visitor: Visitor, context: TransformationContext): FunctionBody | undefined;
140224
/**
141225
* Resumes a suspended lexical environment and visits a concise body, ending the lexical
142226
* environment and merging hoisted declarations upon completion.
@@ -162,6 +246,16 @@ namespace ts {
162246
* @param context A lexical environment context for the visitor.
163247
*/
164248
export function visitEachChild<T extends Node>(node: T, visitor: Visitor, context: TransformationContext): T;
249+
250+
/**
251+
* Visits each child of a Node using the supplied visitor, possibly returning a new Node of the same kind in its place.
252+
*
253+
* @param node The Node whose children will be visited.
254+
* @param visitor The callback used to visit each child.
255+
* @param context A lexical environment context for the visitor.
256+
*/
257+
export function visitEachChild<T extends Node>(node: T | undefined, visitor: Visitor, context: TransformationContext): T | undefined;
258+
165259
export function visitEachChild(node: Node, visitor: Visitor, context: TransformationContext): Node {
166260
if (node === undefined) {
167261
return undefined;
@@ -317,7 +411,7 @@ namespace ts {
317411
case SyntaxKind.FunctionExpression:
318412
return updateFunctionExpression(<FunctionExpression>node,
319413
visitNodes((<FunctionExpression>node).modifiers, visitor, isModifier),
320-
visitNode((<FunctionExpression>node).name, visitor, isPropertyName),
414+
visitNode((<FunctionExpression>node).name, visitor, isIdentifier, /*optional*/ true),
321415
visitNodes((<FunctionExpression>node).typeParameters, visitor, isTypeParameter),
322416
visitParameterList((<FunctionExpression>node).parameters, visitor, context),
323417
visitNode((<FunctionExpression>node).type, visitor, isTypeNode, /*optional*/ true),
@@ -508,7 +602,7 @@ namespace ts {
508602
return updateFunctionDeclaration(<FunctionDeclaration>node,
509603
visitNodes((<FunctionDeclaration>node).decorators, visitor, isDecorator),
510604
visitNodes((<FunctionDeclaration>node).modifiers, visitor, isModifier),
511-
visitNode((<FunctionDeclaration>node).name, visitor, isPropertyName),
605+
visitNode((<FunctionDeclaration>node).name, visitor, isIdentifier, /*optional*/ true),
512606
visitNodes((<FunctionDeclaration>node).typeParameters, visitor, isTypeParameter),
513607
visitParameterList((<FunctionDeclaration>node).parameters, visitor, context),
514608
visitNode((<FunctionDeclaration>node).type, visitor, isTypeNode, /*optional*/ true),

0 commit comments

Comments
 (0)