Giter Club home page Giter Club logo

tsquery's Introduction

TSQuery

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.

Installation

npm install @aztack/tsquery --save-dev
# or install tsquery cli to global
npm install -g @aztack/tsquery

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 '@aztack/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

CLI

# Usage:
$ tsquery <glob-pattern> <ts-query-string> [inline-script | js-file-path]
// ./src/a.ts
class ClassA {}

// ./src/b.ts
class ClassB {}

// ./process.tsq.js
module.exprots = function ($, $filepath, $filename, $files, $tsq) {
    // matched: files matched with glob-pattern
    // index: current file index
    // $files = {matched, index}
    
    // ast: ast of current ts code
    // tsquery: the tsquery instance
    // code: code string read file current file
    // ts: the typescript library
    // $tsq = {ast, tsquery, code, ts}
    return $.name.escapedText;
}
$ tsquery "./src/*.ts" "ClassDeclaration" "return $.name.escapedText"
# or
$ tsquery "./src/*.ts" "ClassDeclaration" "./process.tsq.js"

will output

{
  query: "ClassDeclaration",
  result: [{
    file: './a.ts',
    basename: 'a',
    count: 1,
    result: [ 'ClassA' ]
  },{
    file: './b.ts',
    basename: 'b',
    count: 1,
    result: [ 'ClassB' ]
  }],
  count: 2
  script: "inline",
}

Selectors

The following selectors are supported:

Common AST node types

  • Identifier - any identifier (name of a function, class, variable, etc)
  • IfStatement, ForStatement, WhileStatement, DoStatement - control flow
  • FunctionDeclaration, ClassDeclaration, ArrowFunction - declarations
  • VariableStatement - var, const, let.
  • ImportDeclaration - any import statement
  • StringLiteral - any string
  • TrueKeyword, FalseKeyword, NullKeyword, AnyKeyword - various keywords
  • CallExpression - function call
  • NumericLiteral - any numeric constant
  • NoSubstitutionTemplateLiteral, TemplateExpression - template strings and expressions

tsquery's People

Contributors

phenomnomnominal avatar urish avatar dependabot[bot] avatar cartant avatar mohsen1 avatar vogloblinsky avatar felixfbecker avatar kroeder avatar virkt25 avatar

Stargazers

Roman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.