Continue with the previous post: https://www.cnblogs.com/Answer1215/p/12337243.html

What we want to do in this post, is adding parent function name into the console log as well:

Previous output is :

function add(a, b) {
console.log("2:4", a, b)
return a + b
} function subtract(a, b) {
console.log("7:4", a, b)
return a - b
} add(, )
subtract(, )
console.log("13:0", 'sup dawg')

Now we want:

function add(a, b) {
console.log("add 2:4", a, b)
return a + b
} function subtract(a, b) {
console.log("subtract 7:4", a, b)
return a - b
} add(, )
subtract(, )
console.log("13:0", 'sup dawg')

The key is using

path.findParent()

+

t.isFunctionDeclaration

To find its parent function.

Code:

export default function (babel) {
const { types: t } = babel; return {
name: "ast-transform", // not required
visitor: {
CallExpression(path) {
if (!looksLike(path.node, {
callee: {
type: 'MemberExpression',
object: {
name: 'console'
},
property: {
name: 'log'
}
}
})) {
return
} const parentFn = path.findParent(t.isFunctionDeclaration);
const fnName = parentFn?
`${parentFn.node.id.name} `:
'';
// insert string into console.log('instread here', a,b)
const {line, column} = path.node.loc.start;
const prefix = fnName + `${line}:${column}`;
path.node.arguments.unshift(t.stringLiteral(prefix))
}
}
};
} function looksLike(a, b) {
return (
a &&
b &&
Object.keys(b).every(bKey => {
const bVal = b[bKey]
const aVal = a[bKey]
if (typeof bVal === 'function') {
return bVal(aVal)
}
return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal)
})
)
} function isPrimitive(val) {
return val == null || /^[sbn]/.test(typeof val)
}

[AST Babel] Add function name into the console log 'path.findParent(t.isFunctionDeclaration)'的更多相关文章

  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精雕细琢(四):认亲大戏——通过console.log彻底搞清this

    目录 引言 代码在前 1.function下的this 2.箭头函数下的this 结语 引言        JS中的this指向一直是个老生常谈,但是新手又容易晕的地方.我在网上浏览了很多帖子,但是发 ...

  4. IE6789浏览器使用console.log类似的方法输出调试内容但又不影响页面正常运行

    问题来源:外网IE下,触发js报错.经检测,未清除console造成.清除console后,解决. 问题原因:console.log 原先是 Firefox 的“专利”,严格说是安装了 Firebug ...

  5. return console.log()结果为undefined现象的解答

    console.log总是出现undefined--麻烦的console //本文为作者自己思考后总结出的一些理论知识,若有错误,欢迎指出 bug出现 ​ 需求如下:新建一个car对象,调用其中的de ...

  6. for(var i=1;i<=3;i++){ setTimeout(function(){ console.log(i); },0); };答案:4 4 4。

    看面试题时,发现了一道较为经典的面试题,代码如下 for(var i=1;i<=3;i++){ setTimeout(function(){ console.log(i); },0); }; / ...

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

  8. console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(5))调用解析

    console.log((function f(n){) ? n * f(n-) : n)})()); 5被传入到函数,函数内部三元计算,5 > 1成立,运算结果是5*f(4),二次运算,5*4 ...

  9. for(var i=0;i<=3;i++){ setTimeout(function() { console.log(i) }, 10);}

    for(var i=0;i<=3;i++){ setTimeout(function() {  console.log(i)  }, 10);} 答案:打印4次4 这道题涉及了异步.作用域.闭包 ...

随机推荐

  1. POJ2226(二分图建图/最小点覆盖)

    题意: 给定m*n的棋盘,有若干只咕咕.希望去掉一部分咕咕使得剩下的咕咕在上下左右四个方向越过咕咕槽的情况下都看不到咕咕. 思路: 建立一个二分图的方法有很多,这里采用xy二分. 假设没有咕咕槽的情况 ...

  2. C#调用C++类库例子

    一.新建一个解决方案,并在解决方案下添加一个.netframework的项目,命名为FrameworkConsoleTest.再添加一个C++的动态链接库DLL项目,命名为EncryptBase. 二 ...

  3. css总结 -使用display:inline-block,出现元素高度错位

    在进行页面布局时发现一个问题,两个相同高度的元素显示高度不一致,发生错位.   <style>   .left{   display:inline-block;   height:110p ...

  4. Meta(其他信息)

    简介 元数据就是描述数据的数据 <meta> 元素表示那些不能由其它HTML元相关元素 (<base>, <link>, <script>, <s ...

  5. CSS一些特殊图形

    CSS一些特殊图形 CSS绘制三角形 通过控制元素的border属性可以实现三角形效果; 首先来设置4个边框, 为50px solid [color] color设置成不同的颜色值看一下效果 < ...

  6. AspxDashboardView 更新参数

    AspxDashboardView 更新参数 function SetThrendDashboardView() { console.log("就是这样被你征服"); var to ...

  7. c++中vector函数

    std::vector <cv::Point> VectorPoints 说明:首先定义一个Point(即Point2i---二维整型的点)类型的变量VectorPoints,这就是我们创 ...

  8. php中多图上传采用数组差集处理(array_diff,array_map)

    //删除旧有的图片 //新增数组 $arr2=array(); //原有数组 $old_pics = ReportPic::find()->where(['report_id' => $i ...

  9. Android开发之JDK配置,及ADT下载

    第一步: 到官网上下载jdk,记住关键的一点,首先要查看自己的windows电脑是32位的还是64位的,这里想下载相应的位数的jdk 第二步: 安装JDK,一般默认安装路径,不做自己修改安装路径,如: ...

  10. php设计模式之适配器模式实例代码

    <?php header("Content-type:text/html;charset=utf-8"); // 适配器模式 /** * 查看天气接口 */ class Ti ...