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的更多相关文章

  1. [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 ...

  2. [AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression

    Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html Now we need to think ...

  3. [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 ...

  4. 简单 babel plugin 开发-使用lerna 工具

    babel在现在的web 应用开发上具有很重要的作用,帮助我们做了好多事情,同时又有 比较多的babel plugin 可以解决我们实际开发上的问题. 以下只是学习下如果编写一个简单的babel pl ...

  5. babel plugin和presets是什么,怎么用?

    https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets/ 当开发react或者vuejs app时,开发者 ...

  6. [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 ...

  7. babel plugin

    a = () => {}, // Support for the experimental syntax 'classProperties' isn't currently enabled ya ...

  8. [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 ...

  9. 前端工程师需要掌握的 Babel 知识

    在前端圈子里,对于 Babel,大家肯定都比较熟悉了.如果哪天少了它,对于前端工程师来说肯定是个噩梦.Babel 的工作原理是怎样的可能了解的人就不太多了.本文将主要介绍 Babel 的工作原理以及怎 ...

随机推荐

  1. Linux 内核源码(kernel source)

    查看内核的发行版:uname -r(--kernel-release) $ uname -r 4.4.0-78-generic 内核源码所在的位置:/usr/src $ cd /usr/src $ l ...

  2. 19.浏览器Window服务($window)

    转自:https://www.cnblogs.com/best/tag/Angular/ 引用浏览器的window对象.默认浏览器的window是全局的根对象. 示例代码: <!DOCTYPE ...

  3. 8.不绑定(ngNonBindable)

    转自:https://www.cnblogs.com/best/tag/Angular/ ngNonBindable指令告诉Angular编译或绑定当前DOM元素的内容.这对于要求Angular忽略那 ...

  4. 程序发布出现: 服务器无法处理请求--->无法生成临时类(result = 1)。 错误CS2001:未能找到源文件“C:\ Windows \ TEMP \ lph54vwf.0.cs”

    服务器上发布的web服务程序出错: 服务器无法处理请求--->无法生成临时类(result = 1).错误CS2001:未能找到源文件“C:\ Windows \ TEMP \ lph54vwf ...

  5. SQL中NUMERIC和DECIMAL的区别

    numeric 和 decimal 数据类型的默认最大精度值是 38.在 Transact-SQL 中,numeric 与 decimal 数据类型在功能上等效. decimal(numeric ) ...

  6. Android开发当中Parcelable接口的使用

    本文转载于:http://www.2cto.com/kf/201205/132814.html 本文稍微做了些修改 android提供了一种新的类型:Parcel.本类被用作封装数据的容器,封装后的数 ...

  7. windows 手动添加服务

    windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...

  8. su su- sudo区别概述

    在Linux的操作中经常会用到su 命令进行用户的切换和sudo命令获取root权限,su su- sudo三个命令经常弄混,下面简单的讲解下. 一.查看su的命令帮助信息: pipci@openSU ...

  9. 初尝Perl -- 使用aapt给apk软件包批量重命名

    不知道什么是Perl猛戳这个链接 http://zh.wikipedia.org/wiki/Perl     任务:                 随着手机/平板的各方面性能的不断发展(CPU,内存 ...

  10. 不仅仅是MVC

    MVC Smart MV Three tier 等等