[Bug]: Destructuring to a Super Field is not Supported yet.
See original GitHub issue💻
- Would you like to work on a fix?
How are you using Babel?
Input code
class Foo {
constructor(props) {
;([x, ...super.client] = props);
}
}
Here is the Babel REPL
Configuration file name
babel.config.js
Configuration
module.exports = function (api) {
api.cache(true);
const sourceType= "unambiguous";
const presets = [
["@babel/preset-env", { "modules": false}]
];
const plugins = [ ];
return {
sourceType,
presets,
plugins
};
}
Current and expected behavior
Current
I use babel to trans, get a SyntaxError:
SyntaxError: /D/N2ttFuzz/corpus/DiffTest/es6-pre-tests/11674.js:
Destructuring to a super field is not supported yet.
1 | class Foo {
2 | constructor(props) {
> 3 | ;([x, ...super.client] = props);
| ^^^^^^^^^^^^
4 | }
5 | }
6 |
at File.buildCodeFrameError (/D/N2ttFuzz/src/node_modules/@babel/core/lib/transformation/file/file.js:249:12)
at NodePath.buildCodeFrameError (/D/N2ttFuzz/src/node_modules/@babel/traverse/lib/path/index.js:139:21)
at Object.destructureSet (/D/N2ttFuzz/src/node_modules/@babel/helper-replace-supers/lib/index.js:167:23)
at Object.handle (/D/N2ttFuzz/src/node_modules/@babel/helper-member-expression-to-functions/lib/index.js:434:31)
at Object.Super (/D/N2ttFuzz/src/node_modules/@babel/helper-replace-supers/lib/index.js:77:11)
at NodePath._call (/D/N2ttFuzz/src/node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (/D/N2ttFuzz/src/node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (/D/N2ttFuzz/src/node_modules/@babel/traverse/lib/path/context.js:100:31)
at TraversalContext.visitQueue (/D/N2ttFuzz/src/node_modules/@babel/traverse/lib/context.js:103:16)
at TraversalContext.visitSingle (/D/N2ttFuzz/src/node_modules/@babel/traverse/lib/context.js:77:19) {
code: 'BABEL_TRANSFORM_ERROR'
}
Expected Behavior
The following ss5 compliant code is converted from SWC and can run correctly.
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _get(target, property, receiver) {
if (typeof Reflect !== "undefined" && Reflect.get) {
_get = Reflect.get;
} else {
_get = function _get(target, property, receiver) {
var base = _superPropBase(target, property);
if (!base) return;
var desc = Object.getOwnPropertyDescriptor(base, property);
if (desc.get) {
return desc.get.call(receiver);
}
return desc.value;
};
}
return _get(target, property, receiver || target);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _superPropBase(object, property) {
while(!Object.prototype.hasOwnProperty.call(object, property)){
object = _getPrototypeOf(object);
if (object === null) break;
}
return object;
}
var Foo = function Foo(props) {
"use strict";
_classCallCheck(this, Foo);
;
var ref;
ref = props, x = ref[0], _get(_getPrototypeOf(Foo.prototype), "client", this) = ref.slice(1), ref;
};
Environment
- babel
-
- ├── @babel/cli@7.16.0
-
- ├── @babel/preset-env@7.16.0
- Node: [v16.11.1]
- npm version [8.0.0]
- os [ubuntu 20.04]
Possible solution
No response
Additional context
I run babel use cmd as follows
npx babel poc.js -o res/poc.js
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
proposal: Go 2: syntax to express destructuring and structuring ...
I go into depth below, but the chief goal is a capacity to assert that a given slice of memory referenced by an...
Read more >Node JS / V8 destructuring bug? - javascript - Stack Overflow
The proper syntax to destructure an object to existing variables is ({x, y} = {x: 1, y: 2}); ... It is not a...
Read more >Destructuring assignment - JavaScript - MDN Web Docs
It's always been possible to return an array from a function. Destructuring can make working with an array return value more concise. In...
Read more >JavaScript object destructuring usages you must know
In all practicality, your data object may not be as simple as the employee ... that is still not defined. console.log(dept);. Output,. error....
Read more >Destructuring assignment - The Modern JavaScript Tutorial
Arrays allow us to gather data items into an ordered list. Although, when we pass those to a function, it may need not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@tony-go Yes! Do you have any idea about how it could be transpiled?
As a workaround, you can enable the
constantSuper
assumption: