Skip to content

🪄 Type level ESQuery selector parser and matcher!

License

Notifications You must be signed in to change notification settings

auvred/magic-esquery

Repository files navigation

🪄 magic-esquery

NPM Version License: MIT Install size

Type level ESQuery selector parser and matcher!

✨ Try it out in the TypeScript Playground

magic-esquery

Installation

npm i magic-esquery

Usage

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 (basically Parse + 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

Features

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.

Highlights

  • All ESQuery grammars are supported except :first-child, :last-child, :nth-child and :nth-last-child. They're not widely used. But if anyone wants magic-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

License

MIT

About

🪄 Type level ESQuery selector parser and matcher!

Resources

License

Stars

Watchers

Forks