Skip to content

Instantly share code, notes, and snippets.

@kinka
Created December 10, 2017 04:17
Show Gist options
  • Save kinka/fd2b8d598168b9a707ce75673df5b43c to your computer and use it in GitHub Desktop.
Save kinka/fd2b8d598168b9a707ce75673df5b43c to your computer and use it in GitHub Desktop.
module.exports = function ({types: t}) {
return {
// name: "ast-transform", // not required
visitor: {
Identifier(path, state) {
//path.node.name = path.node.name.split('').reverse().join('');
//console.log('test', path.node)
//path.node.name = path.node.name + '_' + loc.start.line + '_' + loc.start.column
},
FunctionDeclaration: {
exit(path, state) {
const filename = state.file.opts.filename;
const loc = path.node.loc
const body = path.get('body')
body.unshiftContainer('body',
t.expressionStatement(
t.callExpression(
t.memberExpression(t.identifier('console'), t.identifier('log')),
[t.stringLiteral(filename), t.stringLiteral(path.node.id.name), t.identifier('loc')]
)
)
);
body.unshiftContainer('body',
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('loc'), t.identifier(JSON.stringify(loc)))
])
);
}
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment