affe
v0.0.4
Published
AST querying for lazy people
Downloads
6
Readme
AST querying for lazy people.
import { js, pipe, select, all } from 'affe'
const config = await readFile('eslint.config.js', 'utf8')
//
// 👇 Let's find out which eslint rules are specified
// in the config.
//
const rules = await pipe(
js(config),
select(`
export property[name=rules]
> value > object > property > key > *
`),
all(),
)
console.log(rules)
// > semi
// > prefer-const
Contents
Installation
Node:
npm i affe
Browser / Deno:
import { js } from 'https://esm.sh/affe'
Usage
affe
simplifies ANY syntax tree into a format that can be queried with a CSS-like syntax. It also provides tooling for
further simplification of ASTs of a specific language
for more convenience.
affe
provides support for JavaScript/JSX out of the box, but you can easily add support for any other language.
import { jsx, pipe, select, pick, all } from 'affe'
const code = jsx`
export default ({ name, style }) => (
<div className={style}>Hello, {name}!</div>
)
`
//
// 👇 Let's find out the name of the properties
// of the exported component.
//
const params = pipe(
code,
select('export params property key *'),
pick(node => node.name ?? node.value),
all(),
)
console.log(params)
// > [name, style]
Contribution
You need node, NPM to start and git to start.
# clone the code
git clone [email protected]:loreanvictor/affe.git
# install stuff
npm i
Make sure all checks are successful on your PRs. This includes all tests passing, high code coverage, correct typings and abiding all the linting rules. The code is typed with TypeScript, Jest is used for testing and coverage reports, ESLint and TypeScript ESLint are used for linting. Subsequently, IDE integrations for TypeScript and ESLint would make your life much easier (for example, VSCode supports TypeScript out of the box and has this nice ESLint plugin), but you could also use the following commands:
# run tests
npm test
# check code coverage
npm run coverage
# run linter
npm run lint
# run type checker
npm run typecheck