javascript - Babel 无法解析“for each...in”语句
问题描述
我正在尝试将 Salesforce Commerce Cloud 的 .ds 文件转换为 JavaScript,以便我们可以应用标准测试工具(Jest、Mocha 等)。SFCC 文档表明 .ds 文件是“Rhino JavaScript”,具有非标准扩展名,用于类似流的类型检查。
到目前为止,使用transform-flow-strip-types
插件去除类型注释很简单。但是 SFCC 支持Babel 令人窒息的 JavaScript 1.6 中已弃用的“for each...in”语句。
下面的所有代码都可以在 github 上找到。
这是我的源 src/index.ds 文件:
function dump(a: Array) {
for each (var x in a) {
console.log(x);
}
}
module.exports = dump;
还有我的 gulfile.js:
const gulp = require('gulp');
const babel = require('gulp-babel');
gulp.task('test', function () {
gulp.src('src/**/*.ds')
.pipe(babel())
.pipe(gulp.dest('dst'));
});
这是我的 package.json:
{
"name": "dspoc",
"version": "1.0.0",
"description": "poc",
"main": "index.ds",
"author": "cjr",
"license": "ISC",
"devDependencies": {
"babel": "^6.23.0",
"babel-core": "^6.26.3",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"gulp": "^3.9.1",
"gulp-babel": "^7.0.1"
},
"babel": {
"plugins": [
"transform-flow-strip-types"
]
}
}
当我跑步时gulp test
,我得到了这个:
%> gulp test
[11:23:06] Using gulpfile ~/dev/dspoc/gulpfile.js
[11:23:06] Starting 'test'...
[11:23:06] Finished 'test' after 9.15 ms
events.js:163
throw er; // Unhandled 'error' event
^
SyntaxError: /Users/craser/dev/dspoc/src/index.ds: Unexpected token, expected ( (2:5)
1 | function dump(a: Array) {
> 2 | for each (var x in a) {
| ^
3 | console.log(x);
4 | }
5 | }
at Parser.pp$5.raise (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp.unexpected (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:1761:8)
at Parser.pp.expect (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:1749:33)
at Parser.pp$1.parseForStatement (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:2008:8)
at Parser.pp$1.parseStatement (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:1836:19)
at Parser.parseStatement (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:5910:22)
at Parser.pp$1.parseBlockBody (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:2268:21)
at Parser.pp$1.parseBlock (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:2247:8)
at Parser.pp$3.parseFunctionBody (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:4235:22)
at Parser.parseFunctionBody (/Users/craser/dev/dspoc/node_modules/babylon/lib/index.js:5897:20)
我花了很多时间来寻找一个插件,它可以让 Babel 将其转换为类似for...of
语句的东西,但我似乎找不到任何东西。
我现在正处于挖掘for-of transform的边缘,并创建了类似于 transform的东西for each...in
,但如果我可以避免的话,我真的不想投入这项工作。
我觉得我在这里遗漏了一些明显的东西。任何人都知道如何做到这一点?
解决方案
for each...in
从来都不是规范的官方部分,并且在 Babel 出现时并不存在,因此 Babel 不支持它。恐怕您必须先更新该语法的所有用法,然后 Babel 才能处理它。
推荐阅读
- kubernetes - “幽灵”kubernetes pod 卡在终止状态
- java - 带有多个附件的 Android 电子邮件意图
- java - 在 JTextArea 中使用换行,将行换行到 JTextArea 中的特定位置
- windows-installer - 高级安装程序如何以管理员身份运行 exe
- python - Tensorflow/Keras:训练期间的模型准确率始终为 0.5,输入大小与第一个官方教程不同
- makefile - 添加规则以在缺少时生成特定标头
- jquery - stopPropagation 对内部按钮的事件不起作用
- apache-poi - POI 3.17 在克隆工作表中创建单元格注释会创建不一致的 xlsx
- laravel - 如何使用重定向发布请求登录laravel中的登录方法?
- typescript - 错误:无法通过使用数组调用其类型缺少调用签名的表达式