question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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?

@babel/cli

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

Possible solution

No response

Additional context

I run babel use cmd as follows

npx babel poc.js -o res/poc.js

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Dec 7, 2021

@tony-go Yes! Do you have any idea about how it could be transpiled?

1reaction
JLHwungcommented, Dec 7, 2021

As a workaround, you can enable the constantSuper assumption:

{
    sourceType,
    presets,
    plugins,
    assumptions: { constantSuper: true }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found