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的更多相关文章

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

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

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

  5. 简单 babel plugin 开发-使用lerna 工具

    babel在现在的web 应用开发上具有很重要的作用,帮助我们做了好多事情,同时又有 比较多的babel plugin 可以解决我们实际开发上的问题. 以下只是学习下如果编写一个简单的babel pl ...

  6. 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' ...

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

  8. babel plugin和presets是什么,怎么用?

    https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets/ 当开发react或者vuejs app时,开发者 ...

  9. Error Code: 1054. Unknown column '字段名' in 'field list'

    问题描述: j博主在java开发过程中,通过读取excel中表名和字段名,动态创建insert的SQL语句,在mysql可视化工具中执行此SQL语句时,一直提示"Error Code: 10 ...

随机推荐

  1. 【Linux】iptables相关实践,原理及参数解释

    1.禁止指定IP地址的主机进行连接 iptables -I INPUT -s .***.***. -j DROP 2.解除禁止指定IP地址的主机进行连接 iptables -D INPUT -s .* ...

  2. 白面系列 redis

    redis是Key-Value数据库,和Memcached类似.value可以是多种类型,如Strings, Lists, Hashes, Sets 及 Ordered Sets等. redis一个牛 ...

  3. tp3.2框架关闭日志记录

    在config.php中阿计入如下配置: 'LOG_RECORD' => false, // 默认不记录日志 'LOG_TYPE' => 'File', // 日志记录类型 默认为文件方式 ...

  4. jdk8-》stream⾥的map和filter函数使⽤

    map函数 将流中的每⼀个元素 T(入参) 映射为 R(返回值)(类似类型转换)    类似遍历集合,对集合的每个对象做处理.场景:转换对象,如javaweb开发中集合⾥⾯的DO对象转换为DTO对象 ...

  5. 并查集路径减半优化 UnionFind PathHalving (C++)

    /* * UnionFind.h * 有两种实现方式,QuickFind和QuickUnion * QuickFind: * 查找O(1) * 合并O(n) * QuickUnion:(建议使用) * ...

  6. JMeter CSRFToken认证登陆(正则提取器的使用)

    转自:http://blog.csdn.net/lion19930924/article/details/51189210 前几天用JMeter模拟登陆,但是这个网站开启了csrf认证,因此在post ...

  7. 后台异常 - sql语句查询出的结果与dao层返回的结果不一致

    问题描述 sql语句查询出的结果与dao层返回的结果不一致 问题原因 (1)select 中,查询的列名称重复,数据出现错乱 (2)使用不等号,不等号(!=,<>),查询出来的结果集不包含 ...

  8. 修改oracle数据库用户名和密码

    第一步:连接数据库 使用ssh工具以root身份连接服务器, 然后切换到oracle用户:su - oracle(回车) 使用sqlplus连接数据库:sqlplus /nolog(回车) 以管理员身 ...

  9. Mapper-元素和属性

    Mapper.xml文件内部的元素和属性     parameterType(输入类型) §  传递简单类型 §  使用#{}占位符,或者${}进行sql拼接, #{}括号中的值可以任意, ${}括号 ...

  10. Python—网络通信编程之tcp非阻塞通信(socketserver)

    服务端代码 import socketserver # 定义一个类 class MyServer(socketserver.BaseRequestHandler): # 如果handle方法出现报错, ...