[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 subtract(a, b) {
console.log(a, b)
return a - b
}
add(, )
subtract(, )
console.log('sup dawg')
We want to transform the code to:
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')
Added line and colum number in front of console log arguements
Using the utilites functions:
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)
}
Babel plugin 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
}
console.log(path.node.loc)
// insert string into console.log('instread here', a,b)
const {line, column} = path.node.loc.start;
path.node.arguments.unshift(t.stringLiteral(`${line}:${column}`))
}
}
};
}
[AST Babel Plugin] Transform code, add line:column number for console log的更多相关文章
- [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: ...
- [AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression
Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html Now we need to think ...
- [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 ...
- [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 ...
- 简单 babel plugin 开发-使用lerna 工具
babel在现在的web 应用开发上具有很重要的作用,帮助我们做了好多事情,同时又有 比较多的babel plugin 可以解决我们实际开发上的问题. 以下只是学习下如果编写一个简单的babel pl ...
- Error Code: 1054. Unknown column 'age' in 'user'
1.错误描述 10:28:20 alter table user modify age int(3) after sex Error Code: 1054. Unknown column 'age' ...
- Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists
Add a Column to a table if not exists MySQL allows you to create a table if it does not exist, but d ...
- babel plugin和presets是什么,怎么用?
https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets/ 当开发react或者vuejs app时,开发者 ...
- Error Code: 1054. Unknown column '字段名' in 'field list'
问题描述: j博主在java开发过程中,通过读取excel中表名和字段名,动态创建insert的SQL语句,在mysql可视化工具中执行此SQL语句时,一直提示"Error Code: 10 ...
随机推荐
- 自定义Nginx返回页面
1.403返回页面 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log noti ...
- set的使用
集合是Python的一种数据类型,集合是一个可变容器.常用于列表的去重. 什么是集合 集合是一个可变容器 集合中的数据对象都是唯一的(不可重复) 集合是无序的存储结构 集合是可迭代对象 集合内的元素是 ...
- bugku 想蹭网先解开密码
首先下载文件 然后 创建密码字典:使用命令 crunch 11 11 -t 1391040%%%% -o password.txt 爆破:使用命令 aircrack-ng -a2 所下载文件的地址 - ...
- 记录 shell学习过程(1) 超简单的面向过程的2个shell 分区以及创建lvm
分区 #!/usr/bin/env bash #fdisk /dev/sdb << EOF #n # # # #+600M #w #EOF 创建lvm pvcreate /dev/sdb ...
- JS高级---逆推继承看原型
逆推继承看原型 function F1(age) { this.age = age; } function F2(age) { this.age = age; } F2.prototype = new ...
- k8s集群问题记录
k8s集群问题记录 k8s学习方案 问题解决思路 主要学习路径: rancher(k8s)->rke->helm->kubectl->k8s(k8s中文api) 常见问题总结: ...
- sudo用户找不到环境变量 sudo找不到/usr/local/bin 下的执行文件,
出于安全方面的考虑,使用sudo执行命令将在一个最小化的环境中执行,环境变量都重置成默认状态. 所以PATH这个变量不包括用户自定义设置的内容,如找不到/usr/local/bin/下面的命令在sud ...
- Go_CSP并发模型
go语言的最大两个亮点,一个是goroutine,一个就是chan了.二者合体的典型应用CSP,基本就是大家认可的并行开发神器,简化了并行程序的开发难度,我们来看一下CSP. 11.1.CSP是什么 ...
- MySQL表的操作01
表在数据库中主要用来实现存储数据记录,其基本操作包括创建表.查看表.删除表和修改表. 表中的数据库对象包括: 1.列(COLUMNS):也称属性列,在具体创建表时,必须指定列的名字和它的数据类型. 2 ...
- java基础(十一)之抽象类和抽象函数
1.抽象函数的语法特征2.抽象类的语法特征3.抽象类的作用 抽象函数 只有函数的定义,没有函数体的函数被称为抽象函数: abstract void func(); 抽象类 使用abstract定义的类 ...