Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Destructuring to a Super Field is not Supported yet. #13952

Open
1 task
clhiker opened this issue Nov 11, 2021 · 6 comments
Open
1 task

[Bug]: Destructuring to a Super Field is not Supported yet. #13952

clhiker opened this issue Nov 11, 2021 · 6 comments
Labels

Comments

@clhiker
Copy link

clhiker commented Nov 11, 2021

💻

  • 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
@babel-bot
Copy link
Collaborator

Hey @clhiker! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo
Copy link
Member

SWC's output is wrong: _get(_getPrototypeOf(Foo.prototype), "client", this) = ref.slice(1) is not valid JavaScript (could you report this bug to them 🙏).

@tony-go
Copy link
Contributor

tony-go commented Dec 6, 2021

Hi 👋 !

Need help there? Let me know if I could help. ^^

@JLHwung
Copy link
Contributor

JLHwung commented Dec 7, 2021

As a workaround, you can enable the constantSuper assumption:

{
    sourceType,
    presets,
    plugins,
    assumptions: { constantSuper: true }
}

@nicolo-ribaudo
Copy link
Member

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

@tony-go
Copy link
Contributor

tony-go commented Dec 7, 2021

Hi @JLHwung @nicolo-ribaudo :)

Hope you're doing well and safe 🙌 ?

I understand the intent of this code from a user's point of view.

From a technical aspect, I understand that we have to look into the __proto__ in a recursive way to find the property back. (Tell me If I'm wrong 🙈)

Foo.__proto__.client

It's all that I know, from my naive POV ahahaha.

It's why I found that this issue could be a good challenge (with a bit of guidance)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants