shift-query-cli
Command line tool to query AST nodes in JavaScript source files
Last updated 10 months ago by jsoverson .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install shift-query-cli -g
SYNC missed versions from official npm registry.

Shift query CLI

Command line tool to query AST nodes in JavaScript source files. Outputs JSON

Install

$ npm install -g shift-query-cli

Usage

Usage: shift-query [-j] file.js query-pattern

$ shift-query test.js IdentifierExpression
[ IdentifierExpression { type: 'IdentifierExpression', name: 'require' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'puppeteer' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'browser' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'page' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'page' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'page' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'page' } ]

If a script is piped to shift-query then the second argument is treated as the query pattern.

$ tail +2 shift-query | shift-query IdentifierExpression
[ IdentifierExpression { type: 'IdentifierExpression', name: 'require' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'require' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'require' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'console' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'code' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'file' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'pattern' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'printUsage' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'require' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'file' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'console' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'getStdin' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'fileContents' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'pattern' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'printUsage' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'parseScript' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'fileContents' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'console' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'e' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'process' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'query' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'ast' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'pattern' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'console' },
  IdentifierExpression { type: 'IdentifierExpression', name: 'result' } ]

Outputting JSON

Output JSON with the -j flag.

$ shift-query -j test.js "[params.items.length=0]"
$ shift-query -j test.js LiteralStringExpression | jq .[].value
"puppeteer"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
"en-US,en;q=0.9"
"1"
"https://www.google.com/"
"ss.png"

Query syntax

Sytax is from shift-query which is, in turn, from esquery. It follows CSS syntax very closely and if you are familiar writing CSS selectors you should pick this up quickly.

See shift-query

Current Tags

  • 2.0.2                                ...           latest (10 months ago)

6 Versions

  • 2.0.2                                ...           10 months ago
  • 2.0.1                                ...           10 months ago
  • 2.0.0                                ...           10 months ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |