1

我正在尝试将 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,但如果我可以避免的话,我真的不想投入这项工作。

我觉得我在这里遗漏了一些明显的东西。任何人都知道如何做到这一点?

4

1 回答 1

2

for each...in从来都不是规范的官方部分,并且在 Babel 出现时并不存在,因此 Babel 不支持它。恐怕您必须先更新该语法的所有用法,然后 Babel 才能处理它。

于 2018-05-25T19:09:03.603 回答