Static map property in a class causing Babylon error

38 views Asked by At

How can I write the following static property so Babylon doesn't throw an error in JavaScript?

Looking for vanilla JS solution.

The code works in the browser. The problem is with Nightwatch that I think uses the Babylon under the hood and has a problem with this line.

I'm using ESM standard.

class Foo {
    static dictionary = new Map();
}

Error

✖ SyntaxError mylib.js: Unexpected token

 Stack Trace :
    at Parser.pp$5.raise (path/node_modules/babylon/lib/index.js:4454:13)
    at Parser.pp.unexpected (path/node_modules/babylon/lib/index.js:1761:8)
    at Parser.pp$1.parseClassProperty (path/node_modules/babylon/lib/index.js:2571:50)
    at Parser.pp$1.parseClassBody (path/node_modules/babylon/lib/index.js:2516:34)

package.json

"type": "module",
"scripts": {
  "test:safari": "nightwatch --env default",
}
"babel": {
  "presets": ["es2015"],
  "plugins": ["add-module-exports"]
},
"devDependencies": {
  "@rollup/plugin-terser": "^0.4.4",
  "babel-core": "^6.26.3",
  "babel-plugin-add-module-exports": "^1.0.4",
  "babel-preset-es2015": "^6.24.1",
  "chai": "^4.2.0",
  "chromedriver": "^119.0.1",
  "dotenv": "^16.0.3",
  "edgedriver": "^5.3.8",
  "geckodriver": "^4.2.1",
  "jsdoc": "^3.6.5",
  "minami": "^1.2.3",
  "mocha": "10.1.0",
  "nightwatch": "^3.3.2",
  "rollup": "^4.6.1"
}

Attempt

Turns out the babylon doesn't like static variables.

I learned about static block transformations and applied to the code without any success.

https://babeljs.io/docs/babel-plugin-transform-class-static-block

static dictionary = (() => {
    const map = new Map();
    return map;
})();
0

There are 0 answers