Replace nodes with pattern-matching selectors in aster.
Allows to use pattern-matching (JavaScript code examples with wildcards and some other special syntax) for finding nodes and replacing them with results of corresponding handlers.
Uses grasp-equery behind the scenes, so check out official documentation for syntax details.
First, install aster-equery
as a development dependency:
npm install --save-dev aster-equery
Then, add it to your build script:
var aster = require('aster');
var equery = require('aster-equery');
aster.src('src/**/*.js')
.map(equery({
'if ($cond) return $expr1; else return $expr2;': function (node, named) {
return {
type: 'ReturnStatement',
argument: {
type: 'ConditionalExpression',
test: named.cond,
consequent: named.expr1,
alternate: named.expr2
}
};
}
// , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);
can be also written as:
var aster = require('aster');
var equery = require('aster-equery');
aster.src('src/**/*.js')
.map(equery({
'if ($cond) return $expr1; else return $expr2;': 'return <%= cond %> ? <%= expr1 %> : <%= expr2 %>'
// , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);
Type: {pattern: handler}
Replacement mappings.
Type: String
Type: Function(node, named)
Callback to be called on each found match. It will get two arguments - matched node object and hashmap of named subpatterns.
Type: String
estemplate string to be used for generating AST.