@putout/engine-parser
🐊Putout engine that parses input.
Install
npm i @putout/engine-parser
Supported parsers
Any parser should be installed before use, but you can be sure that @babel/parse
always installed.
API
print(ast)
Print code from ast
parse(code)
You can add default options
for custom parser
you use.
parse.fresh(code)
parse
without memoization
.
parse.ast(template)
create node using memoization
.
parse.ast.fresh(template)
create node without memoization
.
Sourcemaps
You have two ways to benefit from source map generation:
- using
Recast
print; - using
Babel
generator;
Generate sourcemaps using Recast
const source = `const hello = 'world';`;
const ast = parse(source, {
sourceFileName: 'hello.js',
});
print(ast, {sourceMapName: 'hello.map'});
`const hello = 'world';
{"version":3,"sources":["hello.js"],"names":[],"mappings":"AAAA...","file":"hello.map","sourcesContent":["const hello = 'world';"]}`;
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.
const {generate} = require('@putout/engine-parser');
const babel = require('@putout/engine-parser/babel');
const ast = babel.parse(source, {
sourceFilename: 'hello.js',
});
generate(ast, {sourceMaps: true}, {
'hello.js': source,
});
({code, map});
Example
const {parse} = require('@putout/engin-parser');
const parser = 'acorn';
const code = parse('var t = "hello"', {
parser,
});
License
MIT