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 | 17x 17x 17x 17x 1679x 1679x 1679x 1679x 17x 171x 2072x 1901x 1901x 17x 2034x 1475x 2034x 972x 2034x 293x 2034x 203x 17x 203x 45x 158x 22x 136x 22x 114x 1x 113x 113x 112x 1x 1x 6525x | // Dependencies:
import { Node, SyntaxKind } from 'typescript';
import { TSQueryNode, TSQueryTraverseOptions } from './tsquery-types';
// Constants:
const FILTERED_KEYS: Array<string> = ['parent'];
const LITERAL_KINDS: Array<SyntaxKind> = [
SyntaxKind.FirstLiteralToken,
SyntaxKind.FalseKeyword,
SyntaxKind.LastLiteralToken,
SyntaxKind.NullKeyword,
SyntaxKind.NumericLiteral,
SyntaxKind.RegularExpressionLiteral,
SyntaxKind.StringLiteral,
SyntaxKind.TrueKeyword
];
export function traverse<T extends Node = Node> (node: Node | TSQueryNode<T>, options: TSQueryTraverseOptions<T>): void {
addProperties(node as TSQueryNode<T>);
options.enter(node as TSQueryNode<T>, node.parent as TSQueryNode<T> || null);
node.forEachChild(child => traverse(child as TSQueryNode<T>, options));
options.leave(node as TSQueryNode<T>, node.parent as TSQueryNode<T> || null);
}
export function getVisitorKeys<T extends Node = Node> (node: TSQueryNode<T> | null): Array<string> {
return !!node ? Object.keys(node)
.filter(key => !FILTERED_KEYS.includes(key))
.filter(key => {
const value = (node as any)[key];
return Array.isArray(value) || typeof value === 'object';
}) : [];
}
export function addProperties (node: TSQueryNode): void {
if (isNotSet(node, 'kindName')) {
node.kindName = SyntaxKind[node.kind];
}
if (isNotSet(node, 'text')) {
node.text = node.getText();
}
if (node.kind === SyntaxKind.Identifier && isNotSet(node, 'name')) {
node.name = node.text;
}
if (isNotSet(node, 'value') && LITERAL_KINDS.includes(node.kind)) {
node.value = parseType(node);
}
}
export function parseType (node: TSQueryNode): any {
// String:
if (node.kind === SyntaxKind.StringLiteral) {
return node.text;
}
// Boolean:
if (node.text === 'true') {
return true;
}
if (node.text === 'false') {
return false;
}
// Null:
if (node.text === 'null') {
return null;
}
// Number:
const maybeNumber = +node.text;
if (!isNaN(maybeNumber)) {
return maybeNumber;
}
// RegExp:
Eif (node.text.startsWith('/') && node.text.endsWith('/')) {
return new RegExp(node.text);
}
return node.text;
}
function isNotSet (object: any, property: string): boolean {
return object[property] == null;
}
|