Skip to content

Commit bd98bc9

Browse files
committed
Add factory functions for rest of NodeEdgeTraversal
1 parent 7a539d0 commit bd98bc9

File tree

4 files changed

+315
-172
lines changed

4 files changed

+315
-172
lines changed

src/compiler/factory.ts

Lines changed: 163 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ namespace ts {
187187

188188
// Names
189189

190+
export function createQualifiedName(left: EntityName, right: string | Identifier) {
191+
const node = <QualifiedName>createSynthesizedNode(SyntaxKind.QualifiedName);
192+
node.left = left;
193+
node.right = asName(right);
194+
return node;
195+
}
196+
197+
export function updateQualifiedName(node: QualifiedName, left: EntityName, right: Identifier) {
198+
return node.left !== left
199+
|| node.right !== right
200+
? updateNode(createQualifiedName(left, right), node)
201+
: node;
202+
}
203+
190204
export function createComputedPropertyName(expression: Expression) {
191205
const node = <ComputedPropertyName>createSynthesizedNode(SyntaxKind.ComputedPropertyName);
192206
node.expression = expression;
@@ -502,6 +516,20 @@ namespace ts {
502516
: node;
503517
}
504518

519+
export function createTypeAssertion(type: TypeNode, expression: Expression) {
520+
const node = <TypeAssertion>createSynthesizedNode(SyntaxKind.TypeAssertionExpression);
521+
node.type = type;
522+
node.expression = parenthesizePrefixOperand(expression);
523+
return node;
524+
}
525+
526+
export function updateTypeAssertion(node: TypeAssertion, type: TypeNode, expression: Expression) {
527+
return node.type !== type
528+
|| node.expression !== expression
529+
? updateNode(createTypeAssertion(type, expression), node)
530+
: node;
531+
}
532+
505533
export function createParen(expression: Expression) {
506534
const node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
507535
node.expression = expression;
@@ -749,6 +777,32 @@ namespace ts {
749777
: node;
750778
}
751779

780+
export function createAsExpression(expression: Expression, type: TypeNode) {
781+
const node = <AsExpression>createSynthesizedNode(SyntaxKind.AsExpression);
782+
node.expression = expression;
783+
node.type = type;
784+
return node;
785+
}
786+
787+
export function updateAsExpression(node: AsExpression, expression: Expression, type: TypeNode) {
788+
return node.expression !== expression
789+
|| node.type !== type
790+
? updateNode(createAsExpression(expression, type), node)
791+
: node;
792+
}
793+
794+
export function createNonNullExpression(expression: Expression) {
795+
const node = <NonNullExpression>createSynthesizedNode(SyntaxKind.NonNullExpression);
796+
node.expression = parenthesizeForAccess(expression);
797+
return node;
798+
}
799+
800+
export function updateNonNullExpression(node: NonNullExpression, expression: Expression) {
801+
return node.expression !== expression
802+
? updateNode(createNonNullExpression(expression), node)
803+
: node;
804+
}
805+
752806
// Misc
753807

754808
export function createTemplateSpan(expression: Expression, literal: TemplateMiddle | TemplateTail) {
@@ -1040,18 +1094,6 @@ namespace ts {
10401094
: node;
10411095
}
10421096

1043-
export function createCaseBlock(clauses: CaseOrDefaultClause[]): CaseBlock {
1044-
const node = <CaseBlock>createSynthesizedNode(SyntaxKind.CaseBlock);
1045-
node.clauses = createNodeArray(clauses);
1046-
return node;
1047-
}
1048-
1049-
export function updateCaseBlock(node: CaseBlock, clauses: CaseOrDefaultClause[]) {
1050-
return node.clauses !== clauses
1051-
? updateNode(createCaseBlock(clauses), node)
1052-
: node;
1053-
}
1054-
10551097
export function createFunctionDeclaration(decorators: Decorator[], modifiers: Modifier[], asteriskToken: AsteriskToken, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block) {
10561098
const node = <FunctionDeclaration>createSynthesizedNode(SyntaxKind.FunctionDeclaration);
10571099
node.decorators = asNodeArray(decorators);
@@ -1099,6 +1141,85 @@ namespace ts {
10991141
: node;
11001142
}
11011143

1144+
export function createEnumDeclaration(decorators: Decorator[], modifiers: Modifier[], name: string | Identifier, members: EnumMember[]) {
1145+
const node = <EnumDeclaration>createSynthesizedNode(SyntaxKind.EnumDeclaration);
1146+
node.decorators = asNodeArray(decorators);
1147+
node.modifiers = asNodeArray(modifiers);
1148+
node.name = asName(name);
1149+
node.members = createNodeArray(members);
1150+
return node;
1151+
}
1152+
1153+
export function updateEnumDeclaration(node: EnumDeclaration, decorators: Decorator[], modifiers: Modifier[], name: Identifier, members: EnumMember[]) {
1154+
return node.decorators !== decorators
1155+
|| node.modifiers !== modifiers
1156+
|| node.name !== name
1157+
|| node.members !== members
1158+
? updateNode(createEnumDeclaration(decorators, modifiers, name, members), node)
1159+
: node;
1160+
}
1161+
1162+
export function createModuleDeclaration(decorators: Decorator[], modifiers: Modifier[], name: ModuleName, body: ModuleBody, flags?: NodeFlags) {
1163+
const node = <ModuleDeclaration>createSynthesizedNode(SyntaxKind.ModuleDeclaration);
1164+
node.flags |= flags;
1165+
node.decorators = asNodeArray(decorators);
1166+
node.modifiers = asNodeArray(modifiers);
1167+
node.name = name;
1168+
node.body = body;
1169+
return node;
1170+
}
1171+
1172+
export function updateModuleDeclaration(node: ModuleDeclaration, decorators: Decorator[], modifiers: Modifier[], name: ModuleName, body: ModuleBody) {
1173+
return node.decorators !== decorators
1174+
|| node.modifiers !== modifiers
1175+
|| node.name !== name
1176+
|| node.body !== body
1177+
? updateNode(createModuleDeclaration(decorators, modifiers, name, body, node.flags), node)
1178+
: node;
1179+
}
1180+
1181+
export function createModuleBlock(statements: Statement[]) {
1182+
const node = <ModuleBlock>createSynthesizedNode(SyntaxKind.CaseBlock);
1183+
node.statements = createNodeArray(statements);
1184+
return node;
1185+
}
1186+
1187+
export function updateModuleBlock(node: ModuleBlock, statements: Statement[]) {
1188+
return node.statements !== statements
1189+
? updateNode(createModuleBlock(statements), node)
1190+
: node;
1191+
}
1192+
1193+
export function createCaseBlock(clauses: CaseOrDefaultClause[]): CaseBlock {
1194+
const node = <CaseBlock>createSynthesizedNode(SyntaxKind.CaseBlock);
1195+
node.clauses = createNodeArray(clauses);
1196+
return node;
1197+
}
1198+
1199+
export function updateCaseBlock(node: CaseBlock, clauses: CaseOrDefaultClause[]) {
1200+
return node.clauses !== clauses
1201+
? updateNode(createCaseBlock(clauses), node)
1202+
: node;
1203+
}
1204+
1205+
export function createImportEqualsDeclaration(decorators: Decorator[], modifiers: Modifier[], name: string | Identifier, moduleReference: ModuleReference) {
1206+
const node = <ImportEqualsDeclaration>createSynthesizedNode(SyntaxKind.ImportEqualsDeclaration);
1207+
node.decorators = asNodeArray(decorators);
1208+
node.modifiers = asNodeArray(modifiers);
1209+
node.name = asName(name);
1210+
node.moduleReference = moduleReference;
1211+
return node;
1212+
}
1213+
1214+
export function updateImportEqualsDeclaration(node: ImportEqualsDeclaration, decorators: Decorator[], modifiers: Modifier[], name: Identifier, moduleReference: ModuleReference) {
1215+
return node.decorators !== decorators
1216+
|| node.modifiers !== modifiers
1217+
|| node.name !== name
1218+
|| node.moduleReference !== moduleReference
1219+
? updateNode(createImportEqualsDeclaration(decorators, modifiers, name, moduleReference), node)
1220+
: node;
1221+
}
1222+
11021223
export function createImportDeclaration(decorators: Decorator[], modifiers: Modifier[], importClause: ImportClause, moduleSpecifier?: Expression): ImportDeclaration {
11031224
const node = <ImportDeclaration>createSynthesizedNode(SyntaxKind.ImportDeclaration);
11041225
node.decorators = asNodeArray(decorators);
@@ -1228,6 +1349,20 @@ namespace ts {
12281349
: node;
12291350
}
12301351

1352+
// Module references
1353+
1354+
export function createExternalModuleReference(expression: Expression) {
1355+
const node = <ExternalModuleReference>createSynthesizedNode(SyntaxKind.ExternalModuleReference);
1356+
node.expression = expression;
1357+
return node;
1358+
}
1359+
1360+
export function updateExternalModuleReference(node: ExternalModuleReference, expression: Expression) {
1361+
return node.expression !== expression
1362+
? updateNode(createExternalModuleReference(expression), node)
1363+
: node;
1364+
}
1365+
12311366
// JSX
12321367

12331368
export function createJsxElement(openingElement: JsxOpeningElement, children: JsxChild[], closingElement: JsxClosingElement) {
@@ -1426,6 +1561,22 @@ namespace ts {
14261561
return node;
14271562
}
14281563

1564+
// Enum
1565+
1566+
export function createEnumMember(name: string | PropertyName, initializer?: Expression) {
1567+
const node = <EnumMember>createSynthesizedNode(SyntaxKind.EnumMember);
1568+
node.name = asName(name);
1569+
node.initializer = initializer && parenthesizeExpressionForList(initializer);
1570+
return node;
1571+
}
1572+
1573+
export function updateEnumMember(node: EnumMember, name: PropertyName, initializer: Expression | undefined) {
1574+
return node.name !== name
1575+
|| node.initializer !== initializer
1576+
? updateNode(createEnumMember(name, initializer), node)
1577+
: node;
1578+
}
1579+
14291580
// Top-level nodes
14301581

14311582
export function updateSourceFileNode(node: SourceFile, statements: Statement[]) {

src/compiler/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,24 +1771,28 @@
17711771
members: NodeArray<EnumMember>;
17721772
}
17731773

1774-
export type ModuleBody = ModuleBlock | ModuleDeclaration;
1775-
17761774
export type ModuleName = Identifier | StringLiteral;
17771775

1776+
export type ModuleBody = NamespaceBody | JSDocNamespaceBody;
1777+
17781778
export interface ModuleDeclaration extends DeclarationStatement {
17791779
kind: SyntaxKind.ModuleDeclaration;
17801780
name: Identifier | StringLiteral;
1781-
body?: ModuleBlock | NamespaceDeclaration | JSDocNamespaceDeclaration | Identifier;
1781+
body?: ModuleBody | JSDocNamespaceDeclaration | Identifier;
17821782
}
17831783

1784+
export type NamespaceBody = ModuleBlock | NamespaceDeclaration;
1785+
17841786
export interface NamespaceDeclaration extends ModuleDeclaration {
17851787
name: Identifier;
1786-
body: ModuleBlock | NamespaceDeclaration;
1788+
body: NamespaceBody;
17871789
}
17881790

1791+
export type JSDocNamespaceBody = Identifier | JSDocNamespaceDeclaration;
1792+
17891793
export interface JSDocNamespaceDeclaration extends ModuleDeclaration {
17901794
name: Identifier;
1791-
body: JSDocNamespaceDeclaration | Identifier;
1795+
body: JSDocNamespaceBody;
17921796
}
17931797

17941798
export interface ModuleBlock extends Node, Statement {

src/compiler/utilities.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,6 +3887,19 @@ namespace ts {
38873887
export function isModuleBody(node: Node): node is ModuleBody {
38883888
const kind = node.kind;
38893889
return kind === SyntaxKind.ModuleBlock
3890+
|| kind === SyntaxKind.ModuleDeclaration
3891+
|| kind === SyntaxKind.Identifier;
3892+
}
3893+
3894+
export function isNamespaceBody(node: Node): node is NamespaceBody {
3895+
const kind = node.kind;
3896+
return kind === SyntaxKind.ModuleBlock
3897+
|| kind === SyntaxKind.ModuleDeclaration;
3898+
}
3899+
3900+
export function isJSDocNamespaceBody(node: Node): node is JSDocNamespaceBody {
3901+
const kind = node.kind;
3902+
return kind === SyntaxKind.Identifier
38903903
|| kind === SyntaxKind.ModuleDeclaration;
38913904
}
38923905

0 commit comments

Comments
 (0)