首页 > 解决方案 > 从 ARROW 函数 (es6) 到 ES5

问题描述

在我的 angularjs 应用程序中,我使用 esprima.js 验证和 gulp。现在,esprima 给我这两个 js 的错误。

internal/streams/legacy.js:59
  throw er; // Unhandled stream error in pipe.
  ^

错误:第 38843 行:意外的标识符

这是第一块

var filteredCampaignItems = campaignItems.filter((thing, index, self) => index === self.findIndex((t) => (
              t.expectedAdvertisementDisplayCount === thing.expectedAdvertisementDisplayCount && t.smartId === thing.smartId
            ))
          )

第二个是求和的过滤器

app.filter('sumProduct', function() {
return function (input) {
var i = input instanceof Array ? input.length : 0;
var a = arguments.length;
if (i === 0)
  return i;
var total = 0;
for(var x of input){
  var duration = parseFloat(x.meta_duration);
  if(isNaN(duration)){
      throw 'filter sumProduct can count only numeric values';
  }
  total += duration;
}
return total;
}
});

Esprima 版本是"version": "4.0.0",我发现我需要获取版本的instanbul 依赖"istanbul": "^1.0.0-alpha.2",我手动更改为版本1.0.0-alpha.2并调用npm i esprima。我这样做,但同样的错误被抛出。

有人知道如何解决这个问题吗?

标签: javascriptgulpesprima

解决方案


我推荐使用 BabelJS ( https://babeljs.io/ )。

Babel 是一个编译器,可以将每个“新”功能转换为“旧”ES5 代码(目标级别是可配置的,但我想在大多数情况下 ES5 都可以)

他们甚至有一个在线工具可以现场试用(https://babeljs.io/repl/


推荐阅读