[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 write is:
var foo = 'o'
var bar = 'o'
foo === bar
function foo(foo, bar) {
foo === bar;
}
We want to trasnform the code which highlighted in foo() function to:
_foo === _bar
Notice that, we also have 'foo === bar' which is not inside foo() function. But we don't want to touch that.
To get started, we have code below:
export default function(babel) {
const { types: t } = babel;
return {
name: "add_underscore",
visitor: {
// your code here
}
};
}
All the code is under 'visitor' prop.
By hightlight the code we can see, it is a 'BinaryExpression':

So we focus on 'BinaryExpression':
export default function(babel) {
const { types: t } = babel;
return {
name: "add_underscore",
visitor: {
BinaryExpression(path) {
}
}
};
}
'path' param contains all the information we need.
If add a console.log() inside BinarayExpression() function, we can see two logs, one is from global scope, another one is from 'foo()' scope. We only want to get one from foo() function scope:
The way to do is check 'path.scope.block.type', which is current code' block scope.
'foo === bar' exisits on global belongs to 'Program':
the one exists in foo() belongs to 'BlockStatement':
BinaryExpression(path) {
// remove global one
if (path.scope.block.type === "Program") {
return;
}
}
And the opreator we want to check is '===':
BinaryExpression(path) {
if (path.scope.block.type === "Program") {
return;
}
if (path.node.operator !== "===") {
return;
}
}
Now we have located the one 'foo === bar' we want, we can start to replace the name:
export default function(babel) {
const { types: t } = babel;
return {
name: "add_underscore",
visitor: {
BinaryExpression(path) {
if (path.scope.block.type === "Program") {
return;
}
if (path.node.operator !== "===") {
return;
}
// locate the 'foo' and 'bar'
// as left and right Identifier
const leftIdentifier = path.node.left;
const rightIndentifier = path.node.right;
// generate a new identifier
const newLeftIdentifier = path.scope.generateUidIdentifier(leftIdentifier.name);
const newRightIdentifier = path.scope.generateUidIdentifier(
rightIndentifier.name
);
// replace the old with new one
path.node.left = t.identifier(newLeftIdentifier.name);
path.node.right = t.identifier(newRightIdentifier.name);
}
}
};
}
Now the generate code looks like:
var foo = 'o'
var bar = 'o'
foo === bar
function foo(foo, bar) {
_foo === _bar;
}
The code have successfully transform to '_foo === _bar'.
But clearly the code won't work, because _foo and _bar is undefined.
We need to update the params in the function as well.
// update params in the function
const [fooParam, barParam] = path.scope.block.params;
fooParam.name = t.identifier(newLeftIdentifier.name).name;
barParam.name = t.identifier(newRightIdentifier.name).name;
All Code:
export default function(babel) {
const { types: t } = babel;
return {
name: "add_underscore",
visitor: {
BinaryExpression(path) {
if (path.scope.block.type === "Program") {
return;
}
if (path.node.operator !== "===") {
return;
}
// locate the 'foo' and 'bar'
// as left and right Identifier
const leftIdentifier = path.node.left;
const rightIndentifier = path.node.right;
// generate a new identifier
const newLeftIdentifier = path.scope.generateUidIdentifier(leftIdentifier.name);
const newRightIdentifier = path.scope.generateUidIdentifier(
rightIndentifier.name
);
// replace the old with new one
path.node.left = t.identifier(newLeftIdentifier.name);
path.node.right = t.identifier(newRightIdentifier.name);
// update params in the function
const [fooParam, barParam] = path.scope.block.params;
fooParam.name = t.identifier(newLeftIdentifier.name).name;
barParam.name = t.identifier(newRightIdentifier.name).name;
}
}
};
}
[Notice]: this is a just learning note for myself. The approache might not be optimal.
[Javascript AST] 0. Introduction: Write a simple BabelJS plugin的更多相关文章
- [Javascript AST] 2. Introduction: Write a simple ESLint rule
What we want to do is checking if user write nested if statements which actually can combine to one: ...
- [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 ...
- js中 javascript:void(0) 用法详解
点击链接不做任何事情: <a href="#" onclick="return false">test</a> <a href=& ...
- html 空链接 href="#"与href="javascript:void(0)"的区别
#包含了一个位置信息 默认的锚是#top 也就是网页的上端 而javascript:void(0) 仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#但跳动到了页首 而javascrip ...
- a href=#与 a href=javascript:void(0) 的区别
a href="#"> 点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP <a href="javascript:void(0)" onCl ...
- href使用 javascript:;与javascript:void(0)防跳到顶部
有时候我们在编写js过程中,需要触发事件而不需要返回值,那么就可能需要这样的写法 href=”#”,包含了一个位置信息.默认的锚是#top,也就是网页的上端,当连续快速点击此链接时会导致浏览器巨慢 ...
- javascript:void(0)
这是不是一个设计缺陷呢 void(0)这种用法巧妙利用void关键字的特性返回undefined(且没有副作用).因为不是关键字,比如直接使用undefined,内容可能被改写. 再来看为啥使用0,而 ...
- <a href=”#”>与 <a href=”javascript:void(0)” 的区别
<a href=”#”>中的“#”其实是锚点的意思,默认为#top,所以当页面比较长的时候,使用这种方式会让页面刷新到页首(页面的最上部) javascript:void(0)其实是一个死 ...
- 超级链接a中javascript:void(0)弹出另外一个框问题
转字:http://my.oschina.net/castusz/blog/68186 结果在IE.Firefox.Chrome都是先执行的onclick事件,在项目中我们尽量不要同时使用这两种方式. ...
随机推荐
- 用Struts2搭建一个登录例子【本人亲测好用】
今天尝试struts2的搭建,遇到不少的问题,终于一一解决,逛了很多地方,最终完成搭建 1.首先要下载struts2的一些组件,我下载的是版本2.3.4.1,Eclipse是4.6版本的.由于版本的不 ...
- Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...
- noip 2018 day1 T2 货币系统 完全背包
Code: #include<cstdio> #include<string> #include<cstring> #include<algorithm> ...
- 利用canvas画一个实时时钟
先放一张效果图: 下面是源代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- JAVA基础数据类型
JAVA的数据类型粗略分两种 1.基本数据类型 整数类型: byte,short,int,long 浮点类型: float,double 字符类型: char 布尔类型: boolean 基本语法格式 ...
- TCP简单说|(上)
本文在Creative Commons许可证下发布 TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人 ...
- Maven学习总结(20)——Maven pom.xml配置再体会
Maven的pom.xml配置文件详解 <!--父项目的坐标.如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值. 坐标包括group ID,artifact ID和 vers ...
- CCF模拟题 最大的矩形
最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方 ...
- NOIP2009 最优贸易(BFS)
本题正解是tarjan.我没有去写 之前的代码是错误的不好意思,因为数据太弱一直没有发现. 相同还是两遍bfs,一次正向,一次反向.在正向的时候我们求出从起点走到各个点的最小值.在反向的时候求出从终点 ...
- 第一个ASP.NET
1.新建 2.发布 3.访问