Type level ESQuery selector parser and matcher!
β¨ Try it out in the TypeScript Playground
npm i magic-esquery
Currently the library exports only Query
, Match
and Parse
types.
Parse
- parse selector into Selector AST
import type { Parse } from 'magic-esquery'
type res = Parse<'CallExpression'>
// ^? type res = { type: "identifier"; value: "CallExpression" }
Match
- infer the AST Node type based on Selector AST
import type { TSESTree } from '@typescript-eslint/typescript-estree'
import type { Match } from 'magic-esquery'
type res = Match<{ type: 'identifier'; value: 'CallExpression' }, TSESTree.Node>
// ^? type res = TSESTree.CallExpression
Query
- parse selector and infer AST Node type (basicallyParse
+Match
)
import type { TSESTree } from '@typescript-eslint/typescript-estree'
import type { Query } from 'magic-esquery'
type res = Query<'CallExpression', TSESTree.Node>
// ^? type res = TSESTree.CallExpression
This package is tested on selectors used in @typescript-eslint/eslint-plugin
, @stylistic/eslint-plugin
, eslint-plugin-jest
.
Check out the current ecosystem test suites here.
You can also check out additional tests for matcher and parser.
- All ESQuery grammars are supported except
:first-child
,:last-child
,:nth-child
and:nth-last-child
. They're not widely used. But if anyone wantsmagic-esquery
to support them, issues/prs are welcome! - Enhanced child type inference (
CallExpression > .callee
) - Any combination of
:matches
and:not
should work correctly, regardless of nesting combinations