[Javascirpt AST] Babel Plugin -- create new CallExpression
The code we want to trasform:
2 ** 3;
a ** b;
a **b * c;
a ** b ** c;
(a+1) ** (b+1);
transform to:
Math.pow(2, 3);
Math.pow(a, b);
Math.pow(a, b) * c;
Math.pow(a, Math.pow(b, c));
Math.pow(a+1, b+1);
Code:
export default function (babel) {
const { types: t } = babel;
return {
name: "ast-transform", // not required
visitor: {
BinaryExpression (path) {
console.log(t )
const isPowOpreator = path.node.operator === '**';
if(!isPowOpreator) {
return;
}
const {left, right} = path.node;
path.replaceWith(t.callExpression( // create Math.pow() node
t.memberExpression(
t.identifier('Math'),
t.identifier('pow')
),
[left, right] // pass in Math.pow(left, right)
))
}
}
};
}
[Javascirpt AST] Babel Plugin -- create new CallExpression的更多相关文章
- [AST Babel Plugin] Transform code, add line:column number for console log
For example we have current code: function add(a, b) { console.log(a, b) return a + b } function sub ...
- [AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression
Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html Now we need to think ...
- [Javascript AST] 1. Continue: Write a simple Babel plugin
We want to write a Babel Plugin, which move 'const versionRegex = /(/d+)\.(/d+)\.(/d+)/gi' out of fu ...
- 简单 babel plugin 开发-使用lerna 工具
babel在现在的web 应用开发上具有很重要的作用,帮助我们做了好多事情,同时又有 比较多的babel plugin 可以解决我们实际开发上的问题. 以下只是学习下如果编写一个简单的babel pl ...
- babel plugin和presets是什么,怎么用?
https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets/ 当开发react或者vuejs app时,开发者 ...
- [AST Babel] Add function name into the console log 'path.findParent(t.isFunctionDeclaration)'
Continue with the previous post: https://www.cnblogs.com/Answer1215/p/12337243.html What we want to ...
- babel plugin
a = () => {}, // Support for the experimental syntax 'classProperties' isn't currently enabled ya ...
- [Javascript AST] 0. Introduction: Write a simple BabelJS plugin
To write a simple Babel plugin, we can use http://astexplorer.net/ to help us. The plugin we want to ...
- 前端工程师需要掌握的 Babel 知识
在前端圈子里,对于 Babel,大家肯定都比较熟悉了.如果哪天少了它,对于前端工程师来说肯定是个噩梦.Babel 的工作原理是怎样的可能了解的人就不太多了.本文将主要介绍 Babel 的工作原理以及怎 ...
随机推荐
- BZOJ 3544 treap (set)
我只是想找个treap的练习题-- 每回找到lower_bound 就好啦 //By SiriusRen #include <cstdio> #include <cstring> ...
- 关于Hive在主节点上与不在主节点上搭建的区别之谈
Hive不在主节点上搭建,我这里是在HadoopSlave1上.
- Codefroces 852 G. Bathroom terminal
G. Bathroom terminal Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to p ...
- [Python] Understand Mutable vs. Immutable objects in Python
In this lesson, you will learn what mutable and immutable objects are, and the difference between th ...
- 负载均衡(LB)具体解释
二.LB LoadBalance就是把负载均衡分配到集群的各个节点,从而提高总体的吞吐能力.Oracle 10g RAC提供了两种手段来实现负载,其一是通过Connection Balancing.依 ...
- 1.6 INSERT语句
1.6 INSERT语句正在更新内容,请稍后
- js数组详解:
一. 数组的浅复制与深复制: 数组之间的复制,由于数组是引用类型,如果是字面量式复制,导致只要是改变其中一个数组的值两者都会发生变化,这种复制叫做浅复制.如果要想复制后不收影响,则需要深复制.深复制就 ...
- ssh-keygen && ssh-copy-id 生成管理传输秘钥
- Pig源代码分析: 简析运行计划的生成
摘要 本文通过跟代码的方式,分析从输入一批Pig-latin到输出物理运行计划(与launcher引擎有关,通常是MR运行计划.也能够是Spark RDD的运行算子)的总体流程. 不会详细涉及AST怎 ...
- Android 上的 制表符(tab) —— 一个奇妙的字符 (二)
接到上回的说,主要是上回那个问题,我认为是android的bug,黎叔认为是cocos2dx的bug,叫我去提交bug.所以我又继续研究了下. 上回说到会调用java层的函数去创建一个image,然后 ...