transform an AST with source maps
npm install transform-astTransform an AST with source maps.
Basically @substack's falafel, but based on [magic-string][].
``js
var result = require('transform-ast')(
var multiply = (a, b) => {
return a * b
}
var add = (a, b) => a + b, function (node) {{ return ${node.body.getSource()} }
if (node.type === 'ArrowFunctionExpression') {
var params = node.params.map(function (param) { return param.getSource() })
if (node.body.type !== 'BlockStatement') {
node.body.edit.update()function (${params.join(', ')}) ${node.body.getSource()}
}
node.edit.update()
}
})
result.toString() ===
var multiply = function (a, b) {
return a * b
}
var add = function (a, b) { return a + b }`
fs.writeFile('output.js.map', JSON.stringify(result.map))
`bash`
npm install --save transform-ast
Parse and transform a source string.fn will be called on each node.magicString
The returned is a [magic-string][] instance, with a toString() method to get the transformed string and a .map property to access the source map.
opts.parser sets the parser module to use. This should be an object with a .parse(src, opts) function. The default is require('acorn-node').
If you already have an AST, pass it in opts.ast. This will skip the parse step inside transformAst().
`js`
transformAst(source, { ast: parsedSource }, cb)
Walk the AST again.
fn will be called on each node.
Generate and return a source map.
If the input source had an inline source map comment, this will be taken into account, and the final source map will point back to the original string.generateMap()
The source map for _only_ the changes made by transform-ast can be accessed by using [magic-string][]'s method.
In addition to the usual AST node properties, each node object also has some additional methods.
Unlike falafel, these methods live on the .edit property, to prevent name conflicts (such as the update() method and the .update property of a ForStatement).node
They're still also defined on the s themselves, but only if there is no naming conflict..edit
It's better to use the property.
Get the source string for a node, including transformations.
Replace node with the given string.
Append the source string after this node.
Prepend the source string before this node.
You can pass in a custom parser using the parser option.parse
The parser should be an object with a function that takes a string and returns an AST..start
Each AST node should have and .end properties indicating their position in the source string.
For example, parsing JSX using babylon:
`js
var babylon = require('babylon')
var transform = require('transform-ast')
var assert = require('assert')
assert.equal(transform(
var el =
, { parser: babylon, plugins: [ 'jsx' ] }, function (node) {
if (node.type === 'JSXElement') {
node.edit.update(JSON.stringify(node.source()))
}
}).toString(), )
`But parsers for other languages too, like tacoscript's parser module horchata:
`js
var horchata = require('horchata')
var transform = require('transform-ast')
var assert = require('assert')assert.equal(transform(
, { parser: horchata }, function (node) {
switch (node.type) {
case 'FunctionExpression':
node.edit.update('function () ' + node.body.getSource())
}
}).toString(), )
``[magic-string]: https://github.com/rich-harris/magic-string