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

esbuild won't create a TDZ error when a class is referenced in its own computed property keys #2045

Closed
SukkaW opened this issue Feb 23, 2022 · 0 comments · Fixed by #3167
Closed
Labels

Comments

@SukkaW
Copy link

SukkaW commented Feb 23, 2022

With given code:

class A {
  static a = 1;
  static [A.a] = 2;
}

Here is the output of the esbuild (with --loader=js --target=es6 flag):

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
var _a;
const _A = class {
};
let A = _A;
_a = _A.a;
__publicField(A, "a", 1);
__publicField(A, _a, 2);

And here is the output of the babel:

"use strict";

var _a;

function _classNameTDZError(name) { throw new Error("Class \"" + name + "\" cannot be referenced in computed property keys."); }

_a = (_classNameTDZError("A"), A).a;

var A = function A() {};

A.a = 1;
A[_a] = 2;

And here is the output of the swc:

function a(a, b, c) {
    return b in a ? Object.defineProperty(a, b, {
        value: c,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : a[b] = c, a;
}
var c = (function(a) {
    throw new Error("Class \"A\" cannot be referenced in computed property keys.");
}("A"), b).a, b = function b() {
    "use strict";
    !function(a, b) {
        if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function");
    }(this, b);
};
a(b, "a", 1), a(b, c, 2);

Both swc & babel output the correct result (where an error should be thrown).

@SukkaW SukkaW changed the title esbuild didn't generate TDZ error when a class is referenced in its own computed property keys esbuild won't create a TDZ error when a class is referenced in its own computed property keys Feb 23, 2022
@evanw evanw added the classes label Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants