UNPKG

1.9 kBPlain TextView Raw
1import * as ts from 'typescript';
2
3/**
4 * TypeScript 2.0 will have more features to support type guard.
5 * https://www.typescriptlang.org/docs/handbook/advanced-types.html
6 * We could avoid 'as' cast if switching to 2.0
7 */
8
9export function isJsxAttribute(node: ts.Node): node is ts.JsxAttribute {
10 return node && node.kind === ts.SyntaxKind.JsxAttribute;
11}
12
13export function isJsxSpreadAttribute(node: ts.Node): node is ts.JsxSpreadAttribute {
14 return node && node.kind === ts.SyntaxKind.JsxSpreadAttribute;
15}
16
17export function isJsxExpression(node: ts.Node): node is ts.JsxExpression {
18 return node && node.kind === ts.SyntaxKind.JsxExpression;
19}
20
21/**
22 * There is no type of NumericLiteral in typescript, guarded as LiteralExpression.
23 */
24export function isNumericLiteral(node: ts.Node): node is ts.LiteralExpression {
25 return node && node.kind === ts.SyntaxKind.NumericLiteral;
26}
27
28export function isStringLiteral(node: ts.Node): node is ts.StringLiteral {
29 return node && node.kind === ts.SyntaxKind.StringLiteral;
30}
31
32export function isJsxElement(node: ts.Node): node is ts.JsxElement {
33 return node && node.kind === ts.SyntaxKind.JsxElement;
34}
35
36export function isJsxSelfClosingElement(node: ts.Node): node is ts.JsxSelfClosingElement {
37 return node && node.kind === ts.SyntaxKind.JsxSelfClosingElement;
38}
39
40export function isJsxOpeningElement(node: ts.Node): node is ts.JsxOpeningElement {
41 return node && node.kind === ts.SyntaxKind.JsxOpeningElement;
42}
43
44export function isTrueKeyword(node: ts.Node): node is ts.LiteralExpression {
45 return node && node.kind === ts.SyntaxKind.TrueKeyword;
46}
47export function isFalseKeyword(node: ts.Node): node is ts.LiteralExpression {
48 return node && node.kind === ts.SyntaxKind.FalseKeyword;
49}
50export function isNullKeyword(node: ts.Node): node is ts.LiteralExpression {
51 return node && node.kind === ts.SyntaxKind.NullKeyword;
52}