[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 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)'的更多相关文章
- [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精雕细琢(四):认亲大戏——通过console.log彻底搞清this
目录 引言 代码在前 1.function下的this 2.箭头函数下的this 结语 引言 JS中的this指向一直是个老生常谈,但是新手又容易晕的地方.我在网上浏览了很多帖子,但是发 ...
- IE6789浏览器使用console.log类似的方法输出调试内容但又不影响页面正常运行
问题来源:外网IE下,触发js报错.经检测,未清除console造成.清除console后,解决. 问题原因:console.log 原先是 Firefox 的“专利”,严格说是安装了 Firebug ...
- return console.log()结果为undefined现象的解答
console.log总是出现undefined--麻烦的console //本文为作者自己思考后总结出的一些理论知识,若有错误,欢迎指出 bug出现 需求如下:新建一个car对象,调用其中的de ...
- 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); }; / ...
- [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: ...
- 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 ...
- 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 这道题涉及了异步.作用域.闭包 ...
随机推荐
- 字符串积累ing
明天就要上网课拉拉啦啦! 数据库先在手机端登录然后转战客户端试之! 操作系统在客户端登录试一试! 马原用学习通试试啦! 首先,介绍一下strlen,strcpy,strcmp函数! 参考:https: ...
- C++ fstream文件操作问题记录
今天测试阿里云OSS文件上传接口,用fstream获取文件指针,代码如下 std::shared_ptr<std::iostream> content = std::make_shared ...
- Dev 控件笔记1 repositoryItemLookUpEdit 控件
repositoryItemLookUpEdit 嵌套在 gridcontrol 中 作为列下拉 效果就是多列的 combox 代码如下 var y = userinfo.Select ...
- Visual Studio Code 搭建 C/C++开发环境方法总结
之前看错资料踩了不少坑,先将可行经验总结如下: 1. 下载 Visual Studio Code 并安装.地址:https://code.visualstudio.com 2. 安装 C/C++插件与 ...
- substring && substr
let string = '0123456';string.substring(0,3);//start: number, end?: number 012(而非 0123)string.substr ...
- 【剑指Offer】47、求1+2+3+...+n
题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 题解:递归实现/利用Math //利用短路 ...
- linux基础安全
一.用户防护 Chage -l 用户名 查看用户账号有效期信息 Chage -E 时间 用户名 给用户设置过期时间 /etc/login.defs 文件保存许多默认设置 Pas ...
- C语言-基本数据类型
一.C语言数据类型深度剖析 1.什么是数据类型? -可以理解为固定内存大小的别名 -数据类型是创建变量的模子 -数据类型是对内存的格式化操作 上面三句话如何理解? 在以前的汇编语言时代,我们要存储一些 ...
- 搭建 Review Board - SVN 审核工具
一.安装环境 CentOS-6.7,ReviewBoard-2.5.1.1 二.安装环境的配置 1.确认当前系统中有如下包,若没有,使用yum安装 httpd-2.2.15:httpd 指的是apac ...
- 跨域 node git
promise 异步回调地狱:就是多个异步请求嵌套的表现 瑕疵:后期维护难 解决:通过promise技术 什么是promise:就是一种异步编程的解决方案 有三个状态:进行中.成功了,失败了 var ...