JSPM

  • Created
  • Published
  • Downloads 2552410
  • Score
    100M100P100Q222103F
  • License MIT

Query TypeScript ASTs with the esquery!

Package Exports

  • @phenomnomnominal/tsquery

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@phenomnomnominal/tsquery) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

TSQuery

npm version

TSQuery is a port of the ESQuery API for TypeScript! Which allows you to query a TypeScript AST for patterns of syntax using a CSS style selector system. Check out the ESQuery demo (note that the demo requires JavaScript code, not TypeScript):

demo

Installation:

npm install @phenomnomnominal/tsquery --save-dev

Examples:

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:


import { tsquery } from '@phenomnomnominal/tsquery';

const typescript = `

class Animal {
    constructor(public name: string) { }
    move(distanceInMeters: number = 0) {
        console.log(\`\${this.name} moved \${distanceInMeters}m.\`);
    }
}

class Snake extends Animal {
    constructor(name: string) { super(name); }
    move(distanceInMeters = 5) {
        console.log("Slithering...");
        super.move(distanceInMeters);
    }
}

`;

const ast = tsquery.ast(typescript);
const nodes = tsquery(ast, 'Identifier[name="Animal"]');
console.log(nodes.length); // 2

The following selectors are supported: