Skip to content

Commit

Permalink
feat: Expand TypeGuards.is implementation to include all node kinds.
Browse files Browse the repository at this point in the history
Realized this after merging. Also do a tiny bit of clean up.
  • Loading branch information
dsherret committed Sep 21, 2019
1 parent d9cba17 commit ae82879
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ export declare class TypeGuards {
/**
* Creates a type guard for syntax kinds.
*/
static is<TKind extends keyof ImplementedKindToNodeMappings>(kind: TKind): (node: Node) => node is ImplementedKindToNodeMappings[TKind];
static is<TKind extends keyof KindToNodeMappings>(kind: TKind): (node: Node) => node is KindToNodeMappings[TKind];
/**
* Gets if the provided value is a Node.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"diff": "^4.0.1",
"dprint": "^0.7.3",
"dprint-plugin-jsonc": "^0.2.4",
"dprint-plugin-typescript": "^0.7.2",
"dprint-plugin-typescript": "^0.7.4",
"mocha": "6.2.0",
"nyc": "14.1.1",
"rimraf": "^3.0.0",
Expand Down
64 changes: 29 additions & 35 deletions scripts/generation/createTypeGuardsUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
const implementedNodeNames = inspector.getImplementedKindToNodeMappingsNames();

// remove all the static methods/properties that start with "is"
[...typeGuardsClass.getStaticMethods(), ...typeGuardsClass.getStaticProperties()]
.filter(m => m.getName().startsWith("is"))
.forEach(m => m.remove());
[
...typeGuardsClass.getStaticMethods(),
...typeGuardsClass.getStaticProperties()
].filter(m => m.getName().startsWith("is")).forEach(m => m.remove());

createIs();
createIsNode();
Expand Down Expand Up @@ -65,12 +66,12 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
typeParameters: method.isMixin ? [{ name: "T", constraint: "compiler.Node" }] : [],
parameters: [{ name: "node", type: method.isMixin ? "T" : "compiler.Node" }],
returnType: `node is compiler.${method.wrapperName}` + (method.isMixin ? ` & compiler.${method.name}ExtensionType & T` : ""),
statements: [writer => {
statements: writer => {
if (method.syntaxKinds.length === 0)
throw new Error(`For some reason ${method.name} had no syntax kinds.`);

writeSyntaxKinds(writer, method.syntaxKinds);
}]
}
};
methodsAndProperties.push(methodStructure);
}
Expand Down Expand Up @@ -181,9 +182,9 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
name: "_hasStructure",
parameters: [{ name: "node", type: "compiler.Node" }],
returnType: `node is compiler.Node & { getStructure(): Structure; }`,
statements: [writer => {
statements: writer => {
writeSyntaxKinds(writer, nodesWithGetStructure.map(n => kindToWrapperMappings.find(m => m.wrapperName === n.getName())!.syntaxKindNames[0]));
}]
}
});
}

Expand All @@ -196,18 +197,13 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
isStatic: true,
name: "is",
typeParameters: [
{ name: `TKind`, constraint: `keyof ImplementedKindToNodeMappings` }
{ name: `TKind`, constraint: `keyof KindToNodeMappings` }
],
parameters: [{ name: `kind`, type: `TKind` }],
returnType: `(node: compiler.Node) => node is ImplementedKindToNodeMappings[TKind]`,
statements: [
writer => writer
.writeLine(`return (node: compiler.Node): node is ImplementedKindToNodeMappings[TKind] => {`)
.indent(() => {
writer.writeLine(`return node.getKind() == kind`);
})
.writeLine(`}`)
]
returnType: `(node: compiler.Node) => node is KindToNodeMappings[TKind]`,
statements: writer => writer.write(`return (node: compiler.Node): node is KindToNodeMappings[TKind] => `).inlineBlock(() => {
writer.writeLine(`return node.getKind() == kind;`);
}).write(";")
});
}

Expand All @@ -218,9 +214,9 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
name: "isNode",
returnType: `value is compiler.Node`,
parameters: [{ name: "value", type: "unknown" }],
statements: [writer => {
statements: writer => {
writer.writeLine("return value != null && (value as any).compilerNode != null");
}]
}
});
}

Expand All @@ -231,47 +227,45 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
name: "isCommentStatement",
returnType: `node is compiler.CommentStatement`,
parameters: [{ name: "node", type: "compiler.Node" }],
statements: [writer => {
statements: writer => {
writer.writeLine(`return (node.compilerNode as compiler.CompilerCommentStatement)._commentKind === compiler.CommentNodeKind.Statement;`);
}]
}
}, {
docs: ["Gets if the provided node is a CommentClassElement."],
isStatic: true,
name: "isCommentClassElement",
returnType: `node is compiler.CommentClassElement`,
parameters: [{ name: "node", type: "compiler.Node" }],
statements: [writer => {
statements: writer => {
writer.writeLine(`return (node.compilerNode as compiler.CompilerCommentClassElement)._commentKind === compiler.CommentNodeKind.ClassElement;`);
}]
}
}, {
docs: ["Gets if the provided value is a CommentTypeElement."],
isStatic: true,
name: "isCommentTypeElement",
returnType: `node is compiler.CommentTypeElement`,
parameters: [{ name: "node", type: "compiler.Node" }],
statements: [writer => {
statements: writer => {
writer.writeLine(`return (node.compilerNode as compiler.CompilerCommentTypeElement)._commentKind === compiler.CommentNodeKind.TypeElement;`);
}]
}
}, {
docs: ["Gets if the provided node is a CommentObjectLiteralElement."],
isStatic: true,
name: "isCommentObjectLiteralElement",
returnType: `node is compiler.CommentObjectLiteralElement`,
parameters: [{ name: "node", type: "compiler.Node" }],
statements: [writer => {
writer.writeLine(
`return (node.compilerNode as compiler.CompilerCommentObjectLiteralElement)._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;`
);
}]
statements: writer => writer.writeLine(
`return (node.compilerNode as compiler.CompilerCommentObjectLiteralElement)._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;`
)
}, {
docs: ["Gets if the provided node is a CommentEnumMember."],
isStatic: true,
name: "isCommentEnumMember",
returnType: `node is compiler.CommentEnumMember`,
parameters: [{ name: "node", type: "compiler.Node" }],
statements: [writer => {
writer.writeLine(`return (node.compilerNode as compiler.CompilerCommentEnumMember)._commentKind == compiler.CommentNodeKind.EnumMember;`);
}]
statements: writer => writer.writeLine(
`return (node.compilerNode as compiler.CompilerCommentEnumMember)._commentKind == compiler.CommentNodeKind.EnumMember;`
)
}];

typeGuardsClass.addMethods([{
Expand All @@ -280,10 +274,10 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
name: "isCommentNode",
returnType: `node is ${commentMethods.map(c => "compiler." + c.name.replace("is", "")).join(" | ")}`,
parameters: [{ name: "node", type: "compiler.Node" }],
statements: [writer => {
statements: writer => {
writer.writeLine("const kind = node.getKind();");
writer.writeLine(`return kind === SyntaxKind.SingleLineCommentTrivia || kind === SyntaxKind.MultiLineCommentTrivia;`);
}]
}
}, ...commentMethods]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast/base/TypeElementMemberedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ export function TypeElementMemberedNode<T extends Constructor<TypeElementMembere
return this.addMembers([member])[0];
}

addMembers(members: string | WriterFunction | ReadonlyArray<string | WriterFunction | TypeElementMemberStructures>
): (TypeElementTypes | CommentTypeElement)[] {
addMembers(members: string | WriterFunction | ReadonlyArray<string | WriterFunction | TypeElementMemberStructures>): (TypeElementTypes
| CommentTypeElement)[] {
return this.insertMembers(getEndIndexFromArray(this.getMembersWithComments()), members);
}

Expand Down
10 changes: 5 additions & 5 deletions src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as compiler from "../compiler";
import { SyntaxKind } from "../typescript";
import { Structure } from "../structures";
import { ImplementedKindToNodeMappings } from '../compiler';
import { KindToNodeMappings } from "../compiler";

/**
* Type guards for checking the type of a node.
Expand Down Expand Up @@ -53,10 +53,10 @@ export class TypeGuards {
/**
* Creates a type guard for syntax kinds.
*/
static is<TKind extends keyof ImplementedKindToNodeMappings>(kind: TKind): (node: compiler.Node) => node is ImplementedKindToNodeMappings[TKind] {
return (node: compiler.Node): node is ImplementedKindToNodeMappings[TKind] => {
return node.getKind() == kind
}
static is<TKind extends keyof KindToNodeMappings>(kind: TKind): (node: compiler.Node) => node is KindToNodeMappings[TKind] {
return (node: compiler.Node): node is KindToNodeMappings[TKind] => {
return node.getKind() == kind;
};
}

/**
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ dprint-plugin-jsonc@^0.2.4:
dependencies:
jsonc-parser "^2.1.1"

dprint-plugin-typescript@^0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/dprint-plugin-typescript/-/dprint-plugin-typescript-0.7.2.tgz#88d997e6654fb163ac101b1c4ce84d4460ae908a"
dprint-plugin-typescript@^0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/dprint-plugin-typescript/-/dprint-plugin-typescript-0.7.4.tgz#c061a032770e48555a573a6ad629c30f15e29b75"
dependencies:
"@babel/core" "^7.6.0"
"@babel/parser" "^7.6.0"
Expand Down

0 comments on commit ae82879

Please sign in to comment.