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

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

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

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

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

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

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

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

  6. babel plugin

    a = () => {}, // Support for the experimental syntax 'classProperties' isn't currently enabled ya ...

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

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

  9. 前端利器躬行记(2)——Babel

    Babel是一个JavaScript编译器,不仅能将当前运行环境不支持的JavaScript语法(例如ES6.ES7等)编译成向下兼容的可用语法(例如ES3或ES5),这其中会涉及新语法的转换和缺失特 ...

随机推荐

  1. Hibernate注释

    Hibernate注释映射一.PO类的基本注释1.@Entity:将pojo类标记成实体,可以指定一个name属性,指定实体类的名称.默认一该类的类名作为实体类的名称 2.@Table:注释改持久化类 ...

  2. iframe中子父页面跨域通讯

    目录 #跨域发送信息 #跨域接收信息 #示例Demo 在非跨域的情况下,iframe中的子父页面可以很方便的通讯,但是在跨域的情况下,只能通过window.postMessage()方法来向其他页面发 ...

  3. HTML5音频(自定义mp3播放器源码)

    audio对象 src兼容.ogg .wav .mp3 <audio controls src='data/imooc.wav'></audio> width autoplay ...

  4. 洛谷 P4708 画画

    题意 在所以置换下,本质不同的各个极大连通子图均含有欧拉闭迹的\(n\)阶图个数 做法 务必先做完这题再看此题解,因为会省略大部分分析了 仍是从边入手,隔外限制:各个点度数是偶数 某个因子内\((m= ...

  5. 获取URL地址参数方法

    //获取url参数 getQueryVariable(variable){ var query =decodeURIComponent(window.location.search.substring ...

  6. Linux 编译安装python3

    编译安装python3的步骤 1.很重要,必须执行此操作,安装好编译环境,c语言也是编译后运行,需要gcc编译器golang,对代码先编译,再运行,python是直接运行yum install gcc ...

  7. 【转载】Cadence验证仿真工具IUS和IES

    本博客转自: cadence验证仿真工具IUS和IES | 骏的世界http://www.lujun.org.cn/?p=3714 cadence验证仿真工具IUS和IES cadence,有两大验证 ...

  8. apache poi下载流程

    apache poi下载官网:http://poi.apache.org/apidocs/4.1/ apache poi下载流程:https://blog.csdn.net/qq_31065001/a ...

  9. 在vue项目中使用MD5.js

    1.安装 npm install --save js-md5 2.引入 import md5 from 'js-md5'; 3.使用 md5('holle') // bcecb35d0a12baad4 ...

  10. python3.6安装lxml库

    好像是在python3.5之后,安装了lxml也无法使用etree 为了就解决这个问题使用如下方法: 1.下载lxml的wheel文件,下载地址:https://www.lfd.uci.edu/~go ...