Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 7x 7x 529x 199x 199x 240x 240x 217x 217x 217x 529x 312x 312x 217x 217x 2387x 312x 1872x 217x | import { Node, SyntaxKind, ts } from 'ts-morph';
import { tryUnwrapParentheses } from '.';
// True, false, null, undefined
// Number, -Number, BigInt, NaN, Infinity, -Infinity
// String(including `no substitution`)
// {}
// []
const CONSTANT_CHECKERS = [
Node.isTrueLiteral,
Node.isFalseLiteral,
Node.isNullLiteral,
Node.isStringLiteral,
Node.isNoSubstitutionTemplateLiteral,
isUndefined,
isNaN,
isNumber,
isInfinity,
isEmptyObject,
isEmptyArray
];
const FALSY_CHECKERS = [Node.isFalseLiteral, Node.isNullLiteral, isUndefined, isNaN, isZero, isEmptyString];
function isUndefined(node: Node<ts.Node>): boolean {
return node.asKind(SyntaxKind.Identifier)?.getText() === 'undefined';
}
function isNegativeNumericalLiteral(node: Node<ts.Node>): boolean {
const unaryExpr = node.asKind(SyntaxKind.PrefixUnaryExpression);
return unaryExpr?.getOperatorToken() === SyntaxKind.MinusToken && isNumber(unaryExpr.getOperand());
}
function isNumber(node: Node<ts.Node>): boolean {
return isNumericalLiteral(node) || isNegativeNumericalLiteral(node);
}
function isNumericalLiteral(node: Node<ts.Node>): boolean {
return Node.isNumericLiteral(node) || Node.isBigIntLiteral(node);
}
function isInfinity(node: Node<ts.Node>): boolean {
return (
node.asKind(SyntaxKind.Identifier)?.getText() === 'Infinity' ||
node.asKind(SyntaxKind.PrefixUnaryExpression)?.getText() === '-Infinity'
);
}
function isEmptyObject(node: Node<ts.Node>): boolean {
return node.asKind(SyntaxKind.ObjectLiteralExpression)?.getProperties().length === 0;
}
function isEmptyArray(node: Node<ts.Node>): boolean {
return node.asKind(SyntaxKind.ArrayLiteralExpression)?.getElements().length === 0;
}
function isNaN(node: Node<ts.Node>): boolean {
return node.asKind(SyntaxKind.Identifier)?.getText() === 'NaN';
}
function isZero(node: Node<ts.Node>): boolean {
return (
node.asKind(SyntaxKind.NumericLiteral)?.getLiteralValue() === 0 || //0
node.asKind(SyntaxKind.PrefixUnaryExpression)?.getText() === '-0' || // -0
node.asKind(SyntaxKind.BigIntLiteral)?.getText() === '0n' // 0n
);
}
function isEmptyString(node: Node<ts.Node>): boolean {
return (
node.asKind(SyntaxKind.StringLiteral)?.getLiteralText().length === 0 || // "" or ''
node.asKind(SyntaxKind.NoSubstitutionTemplateLiteral)?.getLiteralText().length === 0 // ``
);
}
export function isConstantExpr(node: Node<ts.Node> | undefined): boolean {
Iif (!node) return false;
node = tryUnwrapParentheses(node);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return CONSTANT_CHECKERS.map((checker) => checker(node!)).some((res) => res);
}
export function isFalsy(node: Node<ts.Node> | undefined): boolean {
Iif (!node) return false;
return FALSY_CHECKERS.map((checker) => checker(node)).some((res) => res);
}
export function isTruthyConstExpr(node: Node<ts.Node> | undefined) {
return isConstantExpr(node) && !isFalsy(node);
}
|