Skip to content

Commit 4722c29

Browse files
authored
feat: wrap TypeOperator node (#1208)
1 parent ad4fdbd commit 4722c29

File tree

11 files changed

+113
-63
lines changed

11 files changed

+113
-63
lines changed

deno/ts_morph.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7618,8 +7618,19 @@ class DecoratorStructurePrinter extends NodePrinter {
76187618
}
76197619
printTextInternal(writer, structure) {
76207620
writer.write(`@${structure.name}`);
7621+
this.printTypeArguments(writer, structure);
76217622
this.printArguments(writer, structure);
76227623
}
7624+
printTypeArguments(writer, structure) {
7625+
if (structure.typeArguments == null || structure.typeArguments.length === 0)
7626+
return;
7627+
writer.write("<");
7628+
for (let i = 0; i < structure.typeArguments.length; i++) {
7629+
writer.conditionalWrite(i > 0, ", ");
7630+
writer.write(this.getTextWithQueuedChildIndentation(writer, structure.typeArguments[i]));
7631+
}
7632+
writer.write(">");
7633+
}
76237634
printArguments(writer, structure) {
76247635
if (structure.arguments == null)
76257636
return;

packages/ts-morph/src/compiler/ast/common/Node.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
27332733
*/
27342734
static isFunctionLikeDeclaration<T extends compiler.Node>(node: T | undefined): node is compiler.FunctionLikeDeclaration & compiler.FunctionLikeDeclarationExtensionType & T {
27352735
switch (node?.getKind()) {
2736-
case SyntaxKind.ClassStaticBlockDeclaration:
27372736
case SyntaxKind.Constructor:
27382737
case SyntaxKind.GetAccessor:
27392738
case SyntaxKind.MethodDeclaration:
@@ -3352,7 +3351,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
33523351
switch (node?.getKind()) {
33533352
case SyntaxKind.ClassDeclaration:
33543353
case SyntaxKind.ClassExpression:
3355-
case SyntaxKind.ClassStaticBlockDeclaration:
33563354
case SyntaxKind.Constructor:
33573355
case SyntaxKind.GetAccessor:
33583356
case SyntaxKind.MethodDeclaration:
@@ -3548,7 +3546,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
35483546
*/
35493547
static isParameteredNode<T extends compiler.Node>(node: T | undefined): node is compiler.ParameteredNode & compiler.ParameteredNodeExtensionType & T {
35503548
switch (node?.getKind()) {
3551-
case SyntaxKind.ClassStaticBlockDeclaration:
35523549
case SyntaxKind.Constructor:
35533550
case SyntaxKind.GetAccessor:
35543551
case SyntaxKind.MethodDeclaration:
@@ -3800,7 +3797,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
38003797
*/
38013798
static isReturnTypedNode<T extends compiler.Node>(node: T | undefined): node is compiler.ReturnTypedNode & compiler.ReturnTypedNodeExtensionType & T {
38023799
switch (node?.getKind()) {
3803-
case SyntaxKind.ClassStaticBlockDeclaration:
38043800
case SyntaxKind.Constructor:
38053801
case SyntaxKind.GetAccessor:
38063802
case SyntaxKind.MethodDeclaration:
@@ -3866,7 +3862,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
38663862
*/
38673863
static isSignaturedDeclaration<T extends compiler.Node>(node: T | undefined): node is compiler.SignaturedDeclaration & compiler.SignaturedDeclarationExtensionType & T {
38683864
switch (node?.getKind()) {
3869-
case SyntaxKind.ClassStaticBlockDeclaration:
38703865
case SyntaxKind.Constructor:
38713866
case SyntaxKind.GetAccessor:
38723867
case SyntaxKind.MethodDeclaration:
@@ -3968,7 +3963,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
39683963
*/
39693964
static isStaticableNode<T extends compiler.Node>(node: T | undefined): node is compiler.StaticableNode & compiler.StaticableNodeExtensionType & T {
39703965
switch (node?.getKind()) {
3971-
case SyntaxKind.ClassStaticBlockDeclaration:
39723966
case SyntaxKind.GetAccessor:
39733967
case SyntaxKind.MethodDeclaration:
39743968
case SyntaxKind.PropertyDeclaration:
@@ -4179,6 +4173,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
41794173
case SyntaxKind.ThisType:
41804174
case SyntaxKind.TupleType:
41814175
case SyntaxKind.TypeLiteral:
4176+
case SyntaxKind.TypeOperator:
41824177
case SyntaxKind.TypePredicate:
41834178
case SyntaxKind.TypeReference:
41844179
case SyntaxKind.UnionType:
@@ -4191,6 +4186,14 @@ export class Node<NodeType extends ts.Node = ts.Node> {
41914186
/** Gets if the node is a TypeOfExpression. */
41924187
static readonly isTypeOfExpression: (node: compiler.Node | undefined) => node is compiler.TypeOfExpression = Node.is(SyntaxKind.TypeOfExpression);
41934188

4189+
/**
4190+
* Gets if the node is a TypeOperatorTypeNode.
4191+
* @param node - Node to check.
4192+
*/
4193+
static isTypeOperatorTypeNode(node: compiler.Node | undefined): node is compiler.TypeOperatorTypeNode {
4194+
return node?.getKind() === SyntaxKind.TypeOperator;
4195+
}
4196+
41944197
/**
41954198
* Gets if the node is a TypeParameterDeclaration.
41964199
* @param node - Node to check.
@@ -4207,7 +4210,6 @@ export class Node<NodeType extends ts.Node = ts.Node> {
42074210
switch (node?.getKind()) {
42084211
case SyntaxKind.ClassDeclaration:
42094212
case SyntaxKind.ClassExpression:
4210-
case SyntaxKind.ClassStaticBlockDeclaration:
42114213
case SyntaxKind.Constructor:
42124214
case SyntaxKind.GetAccessor:
42134215
case SyntaxKind.MethodDeclaration:

packages/ts-morph/src/compiler/ast/kindToNodeMappings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export interface ImplementedKindToNodeMappings {
163163
[SyntaxKind.TypeAliasDeclaration]: compiler.TypeAliasDeclaration;
164164
[SyntaxKind.TypeAssertionExpression]: compiler.TypeAssertion;
165165
[SyntaxKind.TypeLiteral]: compiler.TypeLiteralNode;
166+
[SyntaxKind.TypeOperator]: compiler.TypeOperatorTypeNode;
166167
[SyntaxKind.TypeParameter]: compiler.TypeParameterDeclaration;
167168
[SyntaxKind.TypePredicate]: compiler.TypePredicateNode;
168169
[SyntaxKind.TypeReference]: compiler.TypeReferenceNode;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ts } from "@ts-morph/common";
2+
import { TypeNode } from "./TypeNode";
3+
4+
export class TypeOperatorTypeNode extends TypeNode<ts.TypeOperatorNode> {
5+
/**
6+
* Gets the node within the type operator.
7+
*/
8+
getTypeNode(): TypeNode {
9+
return this._getNodeFromCompilerNode(this.compilerNode.type);
10+
}
11+
}

packages/ts-morph/src/compiler/ast/type/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export * from "./TupleTypeNode";
1818
export * from "./TypeAliasDeclaration";
1919
export * from "./TypeLiteralNode";
2020
export * from "./TypeNode";
21+
export * from "./TypeOperatorTypeNode";
2122
export * from "./TypeParameterDeclaration";
2223
export * from "./TypePredicateNode";
2324
export * from "./TypeReferenceNode";

packages/ts-morph/src/factories/StructurePrinterFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// DO NOT EDIT - Automatically maintained by createStructurePrinterFactory.ts
22
import { Memoize } from "@ts-morph/common";
3-
import { SupportedFormatCodeSettings } from "../options";
43
import * as structurePrinters from "../structurePrinters";
4+
import { SupportedFormatCodeSettings } from "../options";
55

66
/** Cached lazy factory for StructurePrinters. */
77
export class StructurePrinterFactory {

packages/ts-morph/src/factories/kindToWrapperMappings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export const kindToWrapperMappings: { [key: number]: unknown; } = {
164164
[SyntaxKind.TypeAliasDeclaration]: compiler.TypeAliasDeclaration,
165165
[SyntaxKind.TypeAssertionExpression]: compiler.TypeAssertion,
166166
[SyntaxKind.TypeLiteral]: compiler.TypeLiteralNode,
167+
[SyntaxKind.TypeOperator]: compiler.TypeOperatorTypeNode,
167168
[SyntaxKind.TypeParameter]: compiler.TypeParameterDeclaration,
168169
[SyntaxKind.TypePredicate]: compiler.TypePredicateNode,
169170
[SyntaxKind.TypeReference]: compiler.TypeReferenceNode,

packages/ts-morph/src/structures/Structure.ts

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export const Structure = {
9090
isTypeParametered<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & TypeParameteredNodeStructure {
9191
switch (structure.kind) {
9292
case StructureKind.Class:
93-
case StructureKind.ClassStaticBlock:
9493
case StructureKind.Constructor:
9594
case StructureKind.ConstructorOverload:
9695
case StructureKind.GetAccessor:
@@ -190,10 +189,31 @@ export const Structure = {
190189
isClassStaticBlock(structure: Structure & { kind: StructureKind; }): structure is ClassStaticBlockDeclarationStructure {
191190
return structure.kind === StructureKind.ClassStaticBlock;
192191
},
193-
/** Gets if the provided structure is a StaticableNodeStructure. */
194-
isStaticable<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & StaticableNodeStructure {
192+
/** Gets if the provided structure is a StatementedNodeStructure. */
193+
isStatemented<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & StatementedNodeStructure {
195194
switch (structure.kind) {
196195
case StructureKind.ClassStaticBlock:
196+
case StructureKind.Constructor:
197+
case StructureKind.GetAccessor:
198+
case StructureKind.Method:
199+
case StructureKind.SetAccessor:
200+
case StructureKind.Function:
201+
case StructureKind.Module:
202+
case StructureKind.SourceFile:
203+
return true;
204+
default:
205+
return false;
206+
}
207+
},
208+
/** Gets if the provided structure is a ConstructorDeclarationStructure. */
209+
isConstructor(structure: Structure & { kind: StructureKind; }): structure is ConstructorDeclarationStructure {
210+
return structure.kind === StructureKind.Constructor;
211+
},
212+
/** Gets if the provided structure is a ScopedNodeStructure. */
213+
isScoped<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & ScopedNodeStructure {
214+
switch (structure.kind) {
215+
case StructureKind.Constructor:
216+
case StructureKind.ConstructorOverload:
197217
case StructureKind.GetAccessor:
198218
case StructureKind.Method:
199219
case StructureKind.MethodOverload:
@@ -207,7 +227,6 @@ export const Structure = {
207227
/** Gets if the provided structure is a FunctionLikeDeclarationStructure. */
208228
isFunctionLike<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & FunctionLikeDeclarationStructure {
209229
switch (structure.kind) {
210-
case StructureKind.ClassStaticBlock:
211230
case StructureKind.Constructor:
212231
case StructureKind.GetAccessor:
213232
case StructureKind.Method:
@@ -221,7 +240,6 @@ export const Structure = {
221240
/** Gets if the provided structure is a SignaturedDeclarationStructure. */
222241
isSignatured<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & SignaturedDeclarationStructure {
223242
switch (structure.kind) {
224-
case StructureKind.ClassStaticBlock:
225243
case StructureKind.Constructor:
226244
case StructureKind.ConstructorOverload:
227245
case StructureKind.GetAccessor:
@@ -241,7 +259,6 @@ export const Structure = {
241259
/** Gets if the provided structure is a ParameteredNodeStructure. */
242260
isParametered<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & ParameteredNodeStructure {
243261
switch (structure.kind) {
244-
case StructureKind.ClassStaticBlock:
245262
case StructureKind.Constructor:
246263
case StructureKind.ConstructorOverload:
247264
case StructureKind.GetAccessor:
@@ -261,7 +278,6 @@ export const Structure = {
261278
/** Gets if the provided structure is a ReturnTypedNodeStructure. */
262279
isReturnTyped<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & ReturnTypedNodeStructure {
263280
switch (structure.kind) {
264-
case StructureKind.ClassStaticBlock:
265281
case StructureKind.Constructor:
266282
case StructureKind.ConstructorOverload:
267283
case StructureKind.GetAccessor:
@@ -279,41 +295,6 @@ export const Structure = {
279295
return false;
280296
}
281297
},
282-
/** Gets if the provided structure is a StatementedNodeStructure. */
283-
isStatemented<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & StatementedNodeStructure {
284-
switch (structure.kind) {
285-
case StructureKind.ClassStaticBlock:
286-
case StructureKind.Constructor:
287-
case StructureKind.GetAccessor:
288-
case StructureKind.Method:
289-
case StructureKind.SetAccessor:
290-
case StructureKind.Function:
291-
case StructureKind.Module:
292-
case StructureKind.SourceFile:
293-
return true;
294-
default:
295-
return false;
296-
}
297-
},
298-
/** Gets if the provided structure is a ConstructorDeclarationStructure. */
299-
isConstructor(structure: Structure & { kind: StructureKind; }): structure is ConstructorDeclarationStructure {
300-
return structure.kind === StructureKind.Constructor;
301-
},
302-
/** Gets if the provided structure is a ScopedNodeStructure. */
303-
isScoped<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & ScopedNodeStructure {
304-
switch (structure.kind) {
305-
case StructureKind.Constructor:
306-
case StructureKind.ConstructorOverload:
307-
case StructureKind.GetAccessor:
308-
case StructureKind.Method:
309-
case StructureKind.MethodOverload:
310-
case StructureKind.Property:
311-
case StructureKind.SetAccessor:
312-
return true;
313-
default:
314-
return false;
315-
}
316-
},
317298
/** Gets if the provided structure is a ConstructorDeclarationOverloadStructure. */
318299
isConstructorDeclarationOverload(structure: Structure & { kind: StructureKind; }): structure is ConstructorDeclarationOverloadStructure {
319300
return structure.kind === StructureKind.ConstructorOverload;
@@ -338,6 +319,19 @@ export const Structure = {
338319
return false;
339320
}
340321
},
322+
/** Gets if the provided structure is a StaticableNodeStructure. */
323+
isStaticable<T extends Structure & { kind: StructureKind; }>(structure: T): structure is T & StaticableNodeStructure {
324+
switch (structure.kind) {
325+
case StructureKind.GetAccessor:
326+
case StructureKind.Method:
327+
case StructureKind.MethodOverload:
328+
case StructureKind.Property:
329+
case StructureKind.SetAccessor:
330+
return true;
331+
default:
332+
return false;
333+
}
334+
},
341335
/** Gets if the provided structure is a MethodDeclarationStructure. */
342336
isMethod(structure: Structure & { kind: StructureKind; }): structure is MethodDeclarationStructure {
343337
return structure.kind === StructureKind.Method;

packages/ts-morph/src/structures/utils/forEachStructureChild.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,19 @@ function forJSDocableNode<TStructure>(structure: JSDocableNodeStructure, callbac
139139

140140
/** @generated */
141141
function forClassStaticBlockDeclaration<TStructure>(structure: ClassStaticBlockDeclarationStructure, callback: (structure: Structures) => TStructure | void): TStructure | undefined {
142-
return forFunctionLikeDeclaration(structure, callback);
142+
return forJSDocableNode(structure, callback)
143+
|| forStatementedNode(structure, callback);
144+
}
145+
146+
/** @generated */
147+
function forStatementedNode<TStructure>(structure: StatementedNodeStructure, callback: (structure: Structures) => TStructure | void): TStructure | undefined {
148+
return forAllUnknownKindIfStructure(structure.statements, callback);
149+
}
150+
151+
/** @generated */
152+
function forConstructorDeclaration<TStructure>(structure: ConstructorDeclarationStructure, callback: (structure: Structures) => TStructure | void): TStructure | undefined {
153+
return forFunctionLikeDeclaration(structure, callback)
154+
|| forAll(structure.overloads, callback, StructureKind.ConstructorOverload);
143155
}
144156

145157
/** @generated */
@@ -160,17 +172,6 @@ function forParameteredNode<TStructure>(structure: ParameteredNodeStructure, cal
160172
return forAll(structure.parameters, callback, StructureKind.Parameter);
161173
}
162174

163-
/** @generated */
164-
function forStatementedNode<TStructure>(structure: StatementedNodeStructure, callback: (structure: Structures) => TStructure | void): TStructure | undefined {
165-
return forAllUnknownKindIfStructure(structure.statements, callback);
166-
}
167-
168-
/** @generated */
169-
function forConstructorDeclaration<TStructure>(structure: ConstructorDeclarationStructure, callback: (structure: Structures) => TStructure | void): TStructure | undefined {
170-
return forFunctionLikeDeclaration(structure, callback)
171-
|| forAll(structure.overloads, callback, StructureKind.ConstructorOverload);
172-
}
173-
174175
/** @generated */
175176
function forConstructorDeclarationOverload<TStructure>(structure: ConstructorDeclarationOverloadStructure, callback: (structure: Structures) => TStructure | void): TStructure | undefined {
176177
return forSignaturedDeclaration(structure, callback)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SyntaxKind } from "@ts-morph/common";
2+
import { expect } from "chai";
3+
import { TypeOperatorTypeNode } from "../../../../compiler";
4+
import { getInfoFromTextWithDescendant } from "../../testHelpers";
5+
6+
describe(nameof(TypeOperatorTypeNode), () => {
7+
function getNode(text: string) {
8+
return getInfoFromTextWithDescendant<TypeOperatorTypeNode>(text, SyntaxKind.TypeOperator);
9+
}
10+
11+
describe(nameof<TypeOperatorTypeNode>(d => d.getType), () => {
12+
it("should get the type", () => {
13+
const { descendant } = getNode("var t: readonly string;");
14+
expect(descendant.getTypeNode().getText()).to.equal("string");
15+
});
16+
});
17+
});

0 commit comments

Comments
 (0)