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.CallExpressionQuery- 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.CallExpressionThis 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-childand:nth-last-child. They're not widely used. But if anyone wantsmagic-esqueryto support them, issues/prs are welcome! - Enhanced child type inference (
CallExpression > .callee) - Any combination of
:matchesand:notshould work correctly, regardless of nesting combinations
