Gathering detailed insights and metrics for @phenomnomnominal/tsquery
Gathering detailed insights and metrics for @phenomnomnominal/tsquery
Gathering detailed insights and metrics for @phenomnomnominal/tsquery
Gathering detailed insights and metrics for @phenomnomnominal/tsquery
npm install @phenomnomnominal/tsquery
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
992 Stars
144 Commits
29 Forks
16 Watching
2 Branches
9 Contributors
Updated on 23 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-9%
367,159
Compared to previous day
Last week
0.9%
2,067,014
Compared to previous week
Last month
6.5%
8,693,501
Compared to previous month
Last year
9.1%
96,188,394
Compared to previous year
2
1
TSQuery is a port of the ESQuery API for TypeScript! TSQuery allows you to query a TypeScript AST for patterns of syntax using a CSS style selector system.
ESQuery demo - note that the demo requires JavaScript code, not TypeScript TSQuery demo by Uri Shaked
1npm install @phenomnomnominal/tsquery --save-dev
Say we want to select all instances of an identifier with name "Animal", e.g. the identifier in the class
declaration, and the identifier in the extends
declaration.
We would do something like the following:
1import { ast, query } from '@phenomnomnominal/tsquery'; 2 3const typescript = ` 4 5class Animal { 6 constructor(public name: string) { } 7 move(distanceInMeters: number = 0) { 8 console.log(\`\${this.name} moved \${distanceInMeters}m.\`); 9 } 10} 11 12class Snake extends Animal { 13 constructor(name: string) { super(name); } 14 move(distanceInMeters = 5) { 15 console.log("Slithering..."); 16 super.move(distanceInMeters); 17 } 18} 19 20`; 21 22const ast = ast(typescript); 23const nodes = query(ast, 'Identifier[name="Animal"]'); 24console.log(nodes.length); // 2
The following selectors are supported:
ForStatement
(see common node types)*
[attr]
[attr="foo"]
or [attr=123]
[attr=/foo.*/]
[attr!="foo"]
, [attr>2]
, [attr<3]
, [attr>=2]
, or [attr<=3]
[attr.level2="foo"]
FunctionDeclaration > Identifier.id
:first-child
or :last-child
:nth-child(2)
:nth-last-child(1)
ancestor descendant
parent > child
node ~ sibling
node + adjacent
:not(ForStatement)
:matches([attr] > :first-child, :last-child)
IfStatement:has([name="foo"])
:statement
, :expression
, :declaration
, :function
, or :pattern
Identifier
- any identifier (name of a function, class, variable, etc)IfStatement
, ForStatement
, WhileStatement
, DoStatement
- control flowFunctionDeclaration
, ClassDeclaration
, ArrowFunction
- declarationsVariableStatement
- var, const, let.ImportDeclaration
- any import
statementStringLiteral
- any stringTrueKeyword
, FalseKeyword
, NullKeyword
, AnyKeyword
- various keywordsCallExpression
- function callNumericLiteral
- any numeric constantNoSubstitutionTemplateLiteral
, TemplateExpression
- template strings and expressionsast
:Parse a string of code into an Abstract Syntax Tree which can then be queried with TSQuery Selectors.
1import { ast } from '@phenomnomnominal/tsquery'; 2 3const sourceFile = ast('const x = 1;');
includes
:Check for Nodes
within a given string
of code or AST Node
matching a Selector
.
1import { includes } from '@phenomnomnominal/tsquery'; 2 3const hasIdentifier = includes('const x = 1;', 'Identifier');
map
:Transform AST Nodes
within a given Node
matching a Selector
. Can be used to do Node
-based replacement or removal of parts of the input AST.
1import { factory } from 'typescript'; 2import { map } from '@phenomnomnominal/tsquery'; 3 4const tree = ast('const x = 1;') 5const updatedTree = map(tree, 'Identifier', () => factory.createIdentifier('y'));
match
:Find AST Nodes
within a given AST Node
matching a Selector
.
1import { ast, match } from '@phenomnomnominal/tsquery'; 2 3const tree = ast('const x = 1;') 4const [xNode] = match(tree, 'Identifier');
parse
:Parse a string
into an ESQuery Selector
.
1import { parse } from '@phenomnomnominal/tsquery'; 2 3const selector = parse(':matches([attr] > :first-child, :last-child)');
print
:Print a given Node
or SourceFile
to a string, using the default TypeScript printer.
1import { print } from '@phenomnomnominal/tsquery'; 2import { factory } from 'typescript'; 3 4 // create synthetic node: 5const node = factory.createArrowFunction( 6 // ... 7); 8const code = print(node);
project
:Get all the SourceFiles
included in a the TypeScript project described by a given config file.
1import { project } from '@phenomnomnominal/tsquery'; 2 3const files = project('./tsconfig.json');
files
:Get all the file paths included ina the TypeScript project described by a given config file.
1import { files } from '@phenomnomnominal/tsquery'; 2 3const filePaths = files('./tsconfig.json');
match
:Find AST Nodes
within a given string
of code or AST Node
matching a Selector
.
1import {query } from '@phenomnomnominal/tsquery'; 2 3const [xNode] = query('const x = 1;', 'Identifier');
replace
:Transform AST Nodes
within a given Node
matching a Selector
. Can be used to do string-based replacement or removal of parts of the input AST. The updated code will be printed with the TypeScript Printer
, so you may need to run your own formatter on any output code.
1import { replace } from '@phenomnomnominal/tsquery'; 2 3const updatedCode = replace('const x = 1;', 'Identifier', () => 'y'));
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/16 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More