[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 这道题涉及了异步.作用域.闭包 ...
随机推荐
- vue-routerV3.1版本报错:message: "Navigating to current location ("/home") is not allowed",
出现这个错误的原因是,在路由跳转的时候两次push的path地址相同 解决方法两种: 1.切换版本回3.0版本 2.在你引了vue-router的js文件里加上如下代码即可 import VueRou ...
- vue router的其他属性、 值的传递 、 懒加载
路由的router-link标签有几个其他属性: 路由可以传递值(一般用作条目的id传递,之后用这个id从axios获取页面显示的数据 第一步: 定义路由以及值的属性名称(之后在跳转路由后页面里面获取 ...
- Spark学习之路 (三)Spark之RDD[转]
RDD的概述 什么是RDD? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的 ...
- SpringBoot之spring.factories
组件提供者如何编写出仅需系统开发者进行包引入就可以对spring进行bean注入等操作? 其实在spring库中有提供自动化配置的库spring-boot-autoconfigure,我们只需要引 ...
- centos 安装桌面
centos7.*安装 1,安装 yum groupinstall "KDE Plasma Workspaces" 2.启动 startx
- Python 高维数组“稀疏矩阵”scipy sparse学习笔记
scipy 里面的sparse函数进行的矩阵存储 可以节省内存 主要是scipy包里面的 sparse 这里目前只用到两个 稀疏矩阵的读取 sparse.load() 转稀疏矩阵为普通矩阵 spars ...
- 假期学习【九】首都之窗百姓信件爬取代码优化以及处理 2020.2.7 Python
今天对爬取程序进行了优化,修正了错误. 遇到了两个问题与错误: 1.忘记了对文件的读写设置格式,导致导入Hive数据库无法正常显示以及写入. 2.文件的读写操作有误导致数据量仅有应该有的1/2 不完整 ...
- CSS一些特殊图形
CSS一些特殊图形 CSS绘制三角形 通过控制元素的border属性可以实现三角形效果; 首先来设置4个边框, 为50px solid [color] color设置成不同的颜色值看一下效果 < ...
- PP: Modeling extreme events in time series prediction
KDD: Knowledge Discovery and Data Mining (KDD) Insititute: 复旦大学,中科大 Problem: time series prediction; ...
- 一步步教你如何在ubuntu虚拟机中安装QEMU并模拟arm 开发环境(二)rootfs制作
过了,一天,周又到了,博主终于可以拿出时间来把上一次没有给大家展示完了的内容今天在这里一并展示给大家,希望和大家共同进步,共同学习,同时我也虔诚的希望各位业界的朋友把自己的工作经验拿出来大家一起分享, ...