转换前:

const sum = (a,b)=>a+b

转化后:

// "use strict";
// var fn = function fn(a, b) {
// return a + b;
// };
 
 

实现:

从图片的对比我们可以看出最大的不同是在 init 时,函数的不同

init
Es6 : ArrowFunctionExpression
Es5: FunctionExpression
 
 
所以我们可以利用一个插件转化
 
let t = require('@babel/types');
 
 
具体:
const babel = require('@babel/core');
let code = `let fn = (a,b) => a + b`;
let t = require('@babel/types');
//1.init
// Es6 : ArrowFunctionExpression
// Es5: FunctionExpression
/// t.functionExpression(id, params, body, generator, async)
// id: Identifier (default: null)
// params: Array<LVal> (required)
// body: BlockStatement (required)
// generator: boolean (default: false)
// async: boolean (default: false)
// returnType: TypeAnnotation | TSTypeAnnotation | Noop (default: null)
// typeParameters: TypeParameterDeclaration | TSTypeParameterDeclaration | Noop (default: null) //2. body
//
// ES5 BlockStatement
// Es6 ExpressionStatement
let transformArrowFunctions = {
visitor: {
ArrowFunctionExpression: (path, state) => {
// console.log(path.node)
// console.log(path.parent.id)
let node = path.node;
let id = path.parent.id;
let params = node.params;
let body=t.blockStatement([
t.returnStatement(node.body)
]);
//将ArrowFunctionExpression 转化为 FunctionExpression ,传入不要的参数
let functionExpression = t.functionExpression(id,params,body,false,false);
path.replaceWith(functionExpression);
}
}
}
const result = babel.transform(code, {
plugins: [transformArrowFunctions]
});
console.log(result.code); // let fn = function fn(a, b) {
// return a + b;
// };

输出:

let fn = function fn(a, b) {
return a + b;
};

AST树可视化工具的网站:  https://astexplorer.net/

babel 转换箭头函数的更多相关文章

  1. let import export React入门实例教程 connect provider combineReducers 箭头函数 30分钟掌握ES6/ES2015核心内容 Rest babel

    let与var的区别 http://www.cnblogs.com/snandy/archive/2015/05/10/4485832.html es6 导入导出 http://www.csdn.ne ...

  2. 关于ES6的箭头函数的详解

    ok  坑比函数~~箭头函数~~不自己动手写看懂也不行~~~ 当然你也可以一点一点的把函数复制到Babel里面去将ES6转换成ES5  (斗笔行为) 老谢写的笔记教程就是深入(通俗易懂)哈哈~~~ 第 ...

  3. 箭头函数 Arrow Functions/////////////////////zzz

    箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教程曾建议在HTML注释内包裹行内脚本,这样可以避免不支持JS的浏览器误将JS代码显示为文本.你会写这样的代码: < ...

  4. [译]ES6箭头函数和它的作用域

    原文来自我的前端博客: http://www.hacke2.cn/arrow-functions-and-their-scope/ 在ES6很多很棒的新特性中, 箭头函数 (或者大箭头函数)就是其中值 ...

  5. JavaScript 基础(七) 箭头函数 generator Date JSON

    ES6 标准新增了一种新的函数: Arrow Function(箭头函数). x => x *x 上面的箭头相当于: function (x){ return x*x; } 箭头函数相当于匿名函 ...

  6. 深入浅出ES6(七):箭头函数 Arrow Functions

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教 ...

  7. ES6箭头函数和它的作用域

    原文来自我的前端博客: http://www.hacke2.cn/arrow-functions-and-their-scope/ http://es6rocks.com/2014/10/arrow- ...

  8. JS中的普通函数和箭头函数

    最近被问到了一个问题: >javaScript 中的箭头函数 (=>) 和普通函数 (function) 有什么区别? 我当时想的就是:这个问题很简单啊~(flag),然后做出了错误的回答 ...

  9. es6 箭头函数(arrow function) 学习笔记

    箭头函数有两个好处. 1.他们比传统函数表达式简洁. const arr = [1, 2, 3]; const squares = arr.map(x => x * x); // 传统函数表达式 ...

随机推荐

  1. leetcode-mid-sorting and searching -347. Top K Frequent Elements

    mycode   71.43% class Solution(object): def topKFrequent(self, nums, k): """ :type nu ...

  2. 刃边法计算MTF(ESF、LSF、PSF)

    MTF 调制传递函数 评价一个成像系统目前主流的办法主要有三种TV line检测,MTF检测,和SFR检测. MTF是Modulation Transfer Function的英文简称,中文为调制传递 ...

  3. windows下eclipse打不开

    报错找不到jre等东西 因为eclipse到不到javaw.exe 将其写入eclipse.ini即可 在eclipse.ini的前面加上 -vm D:\dev_tool\java\jdk1.7.0_ ...

  4. python - property 属性函数

    Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...

  5. Python基本语法_文件操作_读写函数详解

    目录 目录 软件环境 file文件对象 open文件操作 读文件 read读取所有文件内容 readline获取一行内容 readlines读取所有文件内容 readreadlinereadlines ...

  6. 【工具安装】kali linux 安装教程

    日期:2019-07-14 16:36:21 介绍:使用最新版的 VMware 来安装 kali linux 0x01.下载镜像 首先需要安装 VMware,安装步骤点这里. VMware 安装教程 ...

  7. windows7如何用键盘模拟鼠标操作

    windows7如何用键盘模拟鼠标操作 https://jingyan.baidu.com/article/6dad5075104907a123e36e38.html 听语音 37453人看了这个视频 ...

  8. fiddler过滤机制讲解

    1.User Fiters启用 2.Action Action:Run Filterset now是否运行,Load Filterset加载,Save Filterset保存: 3.Hosts过滤 Z ...

  9. 各种开源许可 license 区别

    copy from http://www.ruanyifeng.com/blog/2011/05/how_to_choose_free_software_licenses.html

  10. jeecg项目将workbook 的Excel流添加到zip压缩包里导出

    1.直接献出代码 Map<String,List<ConfidentialInformation>> typeMap = new HashMap<>(); try ...