[AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression
Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html
Now we need to think about functionExpression and ArrowFunction:
function add(a, b) {
console.log(a, b)
return a + b
}
function subtract(a, b) {
console.log(a, b)
return a - b
}
const multiply = (a, b) => {
console.log(a, b)
return a * b;
}
const divide = function(a, b) {
console.log(a, b)
return a / b;
}
add(, )
subtract(, )
multiply(, )
divide(, )
console.log('sup dawg')
Transform code:
export default function(babel) {
const { types: t } = babel;
function getFnName(path) {
const expOrArwFn = path.findParent(t.isVariableDeclaration);
const parentFn = path.findParent(t.isFunctionDeclaration);
let faName = "";
if (parentFn) {
faName = `${parentFn.node.id.name} `;
} else if (expOrArwFn) {
faName = `${expOrArwFn.node.declarations[].id.name} `;
} else {
faName = "";
}
return faName;
}
return {
name: "ast-transform", // not required
visitor: {
CallExpression(path) {
if (
!looksLike(path.node, {
callee: {
type: "MemberExpression",
object: {
name: "console"
},
property: {
name: "log"
}
}
})
) {
return;
}
// insert string into console.log('instread here', a,b)
const { line, column } = path.node.loc.start;
const fnName = getFnName(path);
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);
}
Output:
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
}
const multiply = (a, b) => {
console.log("multiply 12:4", a, b)
return a * b;
}
const divide = function(a, b) {
console.log("divide 17:4", a, b)
return a / b;
}
add(, )
subtract(, )
multiply(, )
divide()
console.log("25:0", 'sup dawg')
Other solutions;
export default function(babel) {
const {types: t} = babel
return {
name: 'captains-log',
visitor: {
CallExpression(path) {
if (
!looksLike(path, {
node: {
callee: {
type: 'MemberExpression',
object: {
name: 'console',
},
},
},
})
) {
return
}
let prefix = ''
const functionName = getFunctionName(path)
if (functionName) {
prefix += functionName
}
const start = path.node.loc.start
prefix += ` ${start.line}:${start.column}`
path.node.arguments.unshift(t.stringLiteral(prefix.trim()))
},
},
}
function getFunctionName(path) {
const parentFunction = path.findParent(parent => {
return (
t.isFunctionDeclaration(parent) ||
t.isArrowFunctionExpression(parent) ||
t.isFunctionExpression(parent)
)
})
if (!parentFunction) {
return null
}
if (looksLike(parentFunction, {node: {id: t.isIdentifier}})) {
return parentFunction.node.id.name
} else if (
t.isVariableDeclarator(parentFunction.parent) ||
t.isFunctionExpression(parentFunction.parent)
) {
return parentFunction.parent.id.name
}
return null
}
}
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 Plugin] Hanlde ArrowFunction && FunctionExpression的更多相关文章
- [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 ...
- [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: ...
- [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 ...
- babel plugin和presets是什么,怎么用?
https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets/ 当开发react或者vuejs app时,开发者 ...
- babel plugin
a = () => {}, // Support for the experimental syntax 'classProperties' isn't currently enabled ya ...
- [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] 0. Introduction: Write a simple BabelJS plugin
To write a simple Babel plugin, we can use http://astexplorer.net/ to help us. The plugin we want to ...
- 前端利器躬行记(2)——Babel
Babel是一个JavaScript编译器,不仅能将当前运行环境不支持的JavaScript语法(例如ES6.ES7等)编译成向下兼容的可用语法(例如ES3或ES5),这其中会涉及新语法的转换和缺失特 ...
随机推荐
- 【37】池化层讲解(Pooling layers)
池化层(Pooling layers) 除了卷积层,卷积网络也经常使用池化层来缩减模型的大小,提高计算速度,同时提高所提取特征的鲁棒性,我们来看一下. 先举一个池化层的例子,然后我们再讨论池化层的 ...
- JavaDay11
Java learning_Day11 本人学习视频用的是马士兵的,也在这里献上 <链接:https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA> ...
- 报表生成(POI,jquery.table2excel.js,Echarts)
最近公司要弄个报表相关的功能,话不多说,先上图 前一种是POI 生成的,后一种是Echarts生成的.报表我想大家都不陌生,基本上在公司业务中都会使用到.先说说POI,jquery.table2exc ...
- Magento 2 中请求( Request ) 后执行的基本过程
入口 :index.php 引导 Bootstrap::run() 定义于 ./vendor/magento/framework/App/Bootstrap.php 用于 index.php 应用 ...
- python报错bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.
qpython运行 原代码: soup = BeautifulSoup(r.text,'lxml') 报错:bs4.FeatureNotFound: Couldn't find a tree b ...
- Yarn报错:Exception message: /bin/bash: line 0: fg: no job control
Exception message: /bin/bash: line 0: fg: no job control 这个错误是 本地idea跨平台远程调试hadoop集群出现的,在使用windows调用 ...
- c#枚举转字典或表格
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- openlayers轨迹匀速播放
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- liner-classifiers-SVM
1支持向量机 参考看了这篇文章你还不懂SVM你就来打我 第一遍看完确实有想打死作者的冲动,但是多看几遍之后,真香~ [SVM---这可能是最直白的推导了] 个人觉得这篇文章讲的很清楚,条理清晰,数学推 ...
- JNA 使用总结
JNA 是基于 JNI(Java Native Interface) 技术的开源工具,能够实现单方向的 Java 调用本地方法(通常是 C/C++ 编写的动态链接库中的函数),在 Windows 中是 ...