const { types: t } = babel; return { name: "ast-transform", // not required visitor: { Identifier(path) { path.node.name = `_${path.node.name}`; }, ArrowFunctionExpression(path: NodePath<BabelNodeArrowFunctionExpression>, state: Object) { // In some conversion cases, it may have already been converted to a function while this callback // was queued up. if (!path.isArrowFunctionExpression()) return; path.arrowFunctionToExpression({ // While other utils may be fine inserting other arrows to make more transforms possible, // the arrow transform itself absolutely cannot insert new arrow functions. allowInsertArrow: false, specCompliant: !!state.opts.spec }); } } }; } // babel.js 应用插件 var babel = require('babel-core'); var plugin= require('./plugin'); var out = babel.transform(src, { plugins: [plugin] }); 常用转化操作
遍历
- // the BinaryExpression AST node has properties: `left`, `right`, `operator`
- BinaryExpression(path) {
- path.node.left;
- path.node.right;
- path.node.operator;
- }
我们也可以应用某个路径对象的 get 办法,经由过程传入子路径的字符串表示来拜访某个属性:
- BinaryExpression(path) {
- path.get('left');
- }
- Program(path) {
推荐阅读
海南常见罪名量刑细则实施 人工智能助力司法改革
2009年6月,我省开端量刑规范化改革,是全国第二批试点省份。据介绍,海南最新版的量刑细则涵盖了23个常见着绫躯和罚金刑、缓刑实用,所涉的刑事犯法案件占到我省刑事案件的95%以上。此次>>>详细阅读
本文标题:JavaScript语法树与代码转化实践
地址:http://www.17bianji.com/lsqh/36429.html
1/2 1