-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (26 loc) · 900 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var template = require('babel-template');
function getAst( t, code ) {
// we first try to see if it's a simple stringified value
try {
var val = JSON.parse( code );
return t.valueToNode( val );
} catch( e ) {}
// if we reached here, it's probably not a simple value but some code
var ast = template(code)();
return ast;
}
module.exports = function( babel ) {
var t = babel.types;
return {
visitor: {
Identifier: function( path, state ) {
if ( t.isIdentifier( path.node ) ) {
if ( state.opts.hasOwnProperty( path.node.name ) ) {
var definition = state.opts[ path.node.name ];
path.replaceWith( getAst( t, definition ) );
}
}
}
}
};
};