🐊Putout parser
npm install @putout/engine-parser[NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-parser.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/engine-parser "npm"
🐊Putout engine that parses input.
```
npm i @putout/engine-parser
- @babel/parse
- acorn
- espree
- esprima
- tenko
Any parser should be installed before use, but you can be sure that @babel/parse always installed.
By default @putout/printer used. It formats code according to eslint-plugin-putout rules but without using ESLint.
But you can also use babel with:
`js`
const ast = parse(source);
const code = print(ast, {
printer: 'babel',
});
When you need to pass options, use:
`js
const code = print(ast, {
printer: ['putout', {
format: {
indent: ' ',
},
}],
});
print(ast, {
printer: ['babel', {
alignSpaces: false, // when you don't want to add spaces on empty lines
}],
});
`
Print code from ast
You can add default options for custom parser you use.
parse without memoization.
create node using memoization.
create node without memoization.
Extract expression node from ExpressionStatement.
#### Generate sourcemaps using Babel
To generate sourcemap using babel generator, you should use babel parser before.
This is low level transformation, because Babel doesn't preserve any formatting.
`js
import {generate} from '@putout/engine-parser';
import {parse} from '@putout/engine-parser/babel';
const ast = parse(source, {
sourceFilename: 'hello.js',
});
generate(ast, {sourceMaps: true}, {
'hello.js': source,
});
// returns
({
code,
map,
});
`
`js
import {parse} from '@putout/engine-parser';
const parser = 'acorn';
const code = parse('var t = "hello"', {
parser,
});
``
MIT