Skip to content

Commit

Permalink
fix: handle undefined in comment node type guards (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored May 14, 2022
1 parent b5b43da commit 452cff3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
15 changes: 10 additions & 5 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -4025,19 +4025,24 @@ class Node {
return kind === SyntaxKind.SingleLineCommentTrivia || kind === SyntaxKind.MultiLineCommentTrivia;
}
static isCommentStatement(node) {
return (node === null || node === void 0 ? void 0 : node.compilerNode)._commentKind === CommentNodeKind.Statement;
var _a;
return ((_a = node === null || node === void 0 ? void 0 : node.compilerNode) === null || _a === void 0 ? void 0 : _a._commentKind) === CommentNodeKind.Statement;
}
static isCommentClassElement(node) {
return (node === null || node === void 0 ? void 0 : node.compilerNode)._commentKind === CommentNodeKind.ClassElement;
var _a;
return ((_a = node === null || node === void 0 ? void 0 : node.compilerNode) === null || _a === void 0 ? void 0 : _a._commentKind) === CommentNodeKind.ClassElement;
}
static isCommentTypeElement(node) {
return (node === null || node === void 0 ? void 0 : node.compilerNode)._commentKind === CommentNodeKind.TypeElement;
var _a;
return ((_a = node === null || node === void 0 ? void 0 : node.compilerNode) === null || _a === void 0 ? void 0 : _a._commentKind) === CommentNodeKind.TypeElement;
}
static isCommentObjectLiteralElement(node) {
return (node === null || node === void 0 ? void 0 : node.compilerNode)._commentKind === CommentNodeKind.ObjectLiteralElement;
var _a;
return ((_a = node === null || node === void 0 ? void 0 : node.compilerNode) === null || _a === void 0 ? void 0 : _a._commentKind) === CommentNodeKind.ObjectLiteralElement;
}
static isCommentEnumMember(node) {
return (node === null || node === void 0 ? void 0 : node.compilerNode)._commentKind == CommentNodeKind.EnumMember;
var _a;
return ((_a = node === null || node === void 0 ? void 0 : node.compilerNode) === null || _a === void 0 ? void 0 : _a._commentKind) == CommentNodeKind.EnumMember;
}
static isAbstractable(node) {
switch (node === null || node === void 0 ? void 0 : node.getKind()) {
Expand Down
10 changes: 5 additions & 5 deletions packages/ts-morph/scripts/generation/createNodeTypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export function createNodeTypeGuards(inspector: TsMorphInspector, tsInspector: T
returnType: `node is compiler.CommentStatement`,
parameters: [{ name: "node", type: "compiler.Node | undefined" }],
statements: writer => {
writer.writeLine(`return (node?.compilerNode as compiler.CompilerCommentStatement)._commentKind === compiler.CommentNodeKind.Statement;`);
writer.writeLine(`return (node?.compilerNode as compiler.CompilerCommentStatement)?._commentKind === compiler.CommentNodeKind.Statement;`);
},
}, {
docs: ["Gets if the provided node is a CommentClassElement."],
Expand All @@ -262,7 +262,7 @@ export function createNodeTypeGuards(inspector: TsMorphInspector, tsInspector: T
returnType: `node is compiler.CommentClassElement`,
parameters: [{ name: "node", type: "compiler.Node | undefined" }],
statements: writer => {
writer.writeLine(`return (node?.compilerNode as compiler.CompilerCommentClassElement)._commentKind === compiler.CommentNodeKind.ClassElement;`);
writer.writeLine(`return (node?.compilerNode as compiler.CompilerCommentClassElement)?._commentKind === compiler.CommentNodeKind.ClassElement;`);
},
}, {
docs: ["Gets if the provided value is a CommentTypeElement."],
Expand All @@ -271,7 +271,7 @@ export function createNodeTypeGuards(inspector: TsMorphInspector, tsInspector: T
returnType: `node is compiler.CommentTypeElement`,
parameters: [{ name: "node", type: "compiler.Node | undefined" }],
statements: writer => {
writer.writeLine(`return (node?.compilerNode as compiler.CompilerCommentTypeElement)._commentKind === compiler.CommentNodeKind.TypeElement;`);
writer.writeLine(`return (node?.compilerNode as compiler.CompilerCommentTypeElement)?._commentKind === compiler.CommentNodeKind.TypeElement;`);
},
}, {
docs: ["Gets if the provided node is a CommentObjectLiteralElement."],
Expand All @@ -281,7 +281,7 @@ export function createNodeTypeGuards(inspector: TsMorphInspector, tsInspector: T
parameters: [{ name: "node", type: "compiler.Node | undefined" }],
statements: writer =>
writer.writeLine(
`return (node?.compilerNode as compiler.CompilerCommentObjectLiteralElement)._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;`,
`return (node?.compilerNode as compiler.CompilerCommentObjectLiteralElement)?._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;`,
),
}, {
docs: ["Gets if the provided node is a CommentEnumMember."],
Expand All @@ -291,7 +291,7 @@ export function createNodeTypeGuards(inspector: TsMorphInspector, tsInspector: T
parameters: [{ name: "node", type: "compiler.Node | undefined" }],
statements: writer =>
writer.writeLine(
`return (node?.compilerNode as compiler.CompilerCommentEnumMember)._commentKind == compiler.CommentNodeKind.EnumMember;`,
`return (node?.compilerNode as compiler.CompilerCommentEnumMember)?._commentKind == compiler.CommentNodeKind.EnumMember;`,
),
}];

Expand Down
10 changes: 5 additions & 5 deletions packages/ts-morph/src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2181,27 +2181,27 @@ export class Node<NodeType extends ts.Node = ts.Node> {

/** Gets if the provided node is a CommentStatement. */
static isCommentStatement(node: compiler.Node | undefined): node is compiler.CommentStatement {
return (node?.compilerNode as compiler.CompilerCommentStatement)._commentKind === compiler.CommentNodeKind.Statement;
return (node?.compilerNode as compiler.CompilerCommentStatement)?._commentKind === compiler.CommentNodeKind.Statement;
}

/** Gets if the provided node is a CommentClassElement. */
static isCommentClassElement(node: compiler.Node | undefined): node is compiler.CommentClassElement {
return (node?.compilerNode as compiler.CompilerCommentClassElement)._commentKind === compiler.CommentNodeKind.ClassElement;
return (node?.compilerNode as compiler.CompilerCommentClassElement)?._commentKind === compiler.CommentNodeKind.ClassElement;
}

/** Gets if the provided value is a CommentTypeElement. */
static isCommentTypeElement(node: compiler.Node | undefined): node is compiler.CommentTypeElement {
return (node?.compilerNode as compiler.CompilerCommentTypeElement)._commentKind === compiler.CommentNodeKind.TypeElement;
return (node?.compilerNode as compiler.CompilerCommentTypeElement)?._commentKind === compiler.CommentNodeKind.TypeElement;
}

/** Gets if the provided node is a CommentObjectLiteralElement. */
static isCommentObjectLiteralElement(node: compiler.Node | undefined): node is compiler.CommentObjectLiteralElement {
return (node?.compilerNode as compiler.CompilerCommentObjectLiteralElement)._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;
return (node?.compilerNode as compiler.CompilerCommentObjectLiteralElement)?._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;
}

/** Gets if the provided node is a CommentEnumMember. */
static isCommentEnumMember(node: compiler.Node | undefined): node is compiler.CommentEnumMember {
return (node?.compilerNode as compiler.CompilerCommentEnumMember)._commentKind == compiler.CommentNodeKind.EnumMember;
return (node?.compilerNode as compiler.CompilerCommentEnumMember)?._commentKind == compiler.CommentNodeKind.EnumMember;
}

/**
Expand Down

0 comments on commit 452cff3

Please sign in to comment.