Skip to content

Commit

Permalink
fix: Throw correct error types from sed ant class TDZ helpers (#15241)
Browse files Browse the repository at this point in the history
Fixes #15105
  • Loading branch information
SuperSodaSea committed Dec 5, 2022
1 parent 859daa3 commit 543c15a
Show file tree
Hide file tree
Showing 41 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers.ts
Expand Up @@ -612,7 +612,7 @@ helpers.set = helper("7.0.0-beta.0")`
export default function _set(target, property, value, receiver, isStrict) {
var s = set(target, property, value, receiver || target);
if (!s && isStrict) {
throw new Error('failed to set property');
throw new TypeError('failed to set property');
}
return value;
Expand Down Expand Up @@ -650,7 +650,7 @@ helpers.writeOnlyError = helper("7.12.13")`

helpers.classNameTDZError = helper("7.0.0-beta.0")`
export default function _classNameTDZError(name) {
throw new Error("Class \\"" + name + "\\" cannot be referenced in computed property keys.");
throw new ReferenceError("Class \\"" + name + "\\" cannot be referenced in computed property keys.");
}
`;

Expand Down
@@ -0,0 +1,5 @@
expect(() => {
class Foo {
[Foo + 1] = 2;
};
}).toThrow(ReferenceError);
@@ -0,0 +1,3 @@
class Foo {
[Foo + 1] = 2;
};
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-class-properties"]
}
@@ -0,0 +1,8 @@
let _ref;
_ref = (babelHelpers.classNameTDZError("Foo"), Foo) + 1;
class Foo {
constructor() {
babelHelpers.defineProperty(this, _ref, 2);
}
}
;
Expand Up @@ -2,4 +2,4 @@ expect(() => {
class A {
static [{ x: A || 0 }.x];
}
}).toThrow();
}).toThrow(ReferenceError);
Expand Up @@ -2,4 +2,4 @@ expect(() => {
class C {
static [C + 3] = 3;
}
}).toThrow();
}).toThrow(ReferenceError);
Expand Up @@ -2,4 +2,4 @@ expect(() => {
class C {
static [C + 3] = 3;
}
}).toThrow();
}).toThrow(ReferenceError);
Expand Up @@ -24,7 +24,7 @@ const obj = new Obj();
// but close enough for loose.
// expect(() => {
expect(obj.set()).toBe(3);
// }).toThrow();
// }).toThrow(TypeError);

expect(called).toBe(false);
expect(Base.prototype.test).toBe(1);
Expand Down
Expand Up @@ -24,7 +24,7 @@ const obj = new Obj();
// but close enough for loose.
// expect(() => {
expect(obj.set()).toBe(3);
// }).toThrow();
// }).toThrow(TypeError);

expect(called).toBe(false);
expect(Base.prototype.test).toBe(1);
Expand Down
Expand Up @@ -35,7 +35,7 @@ const obj = new Obj();
// but close enough for loose.
// expect(() => {
expect(obj.set()).toBe(3);
// }).toThrow();
// }).toThrow(TypeError);

expect(called).toBe(false);
expect(Base.prototype.test).toBe(1);
Expand Down
Expand Up @@ -19,7 +19,7 @@ const obj = new Obj();
// expect(obj.set()).toBe(3);
expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);

expect(called).toBe(false);
expect(Base.prototype.test).toBeUndefined();
Expand Down
Expand Up @@ -19,7 +19,7 @@ const obj = new Obj();
// expect(obj.set()).toBe(3);
expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);

expect(called).toBe(false);
expect(Base.prototype.test).toBeUndefined();
Expand Down
Expand Up @@ -25,7 +25,7 @@ const obj = new Obj();
// expect(obj.set()).toBe(3);
expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(called).toBe(false);
expect(Base.prototype.test).toBeUndefined();
expect(Obj.prototype.test).toBeUndefined();
Expand Down
Expand Up @@ -21,7 +21,7 @@ Object.defineProperty(Obj.prototype, 'test', {
const obj = new Obj();
expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(called).toBe(false);
expect(Base.prototype.test).toBe(1);
expect(Obj.prototype.test).toBe(2);
Expand Down
Expand Up @@ -21,7 +21,7 @@ expect(() => {
// this requires helpers to be in file (not external), so they
// are in "strict" mode code.
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.prototype.test).toBe(1);
expect(Obj.prototype.test).toBe(2);
expect(obj.test).toBe(2);
Expand Up @@ -38,7 +38,7 @@ expect(() => {
// this requires helpers to be in file (not external), so they
// are in "strict" mode code.
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.prototype.test).toBe(1);
expect(Obj.prototype.test).toBe(2);
expect(obj.test).toBe(2);
Expand Up @@ -14,5 +14,5 @@ class B extends A {

expect(() => {
new B();
}).toThrow();
}).toThrow(ReferenceError);
expect(called).toBe(false);
Expand Up @@ -18,6 +18,6 @@ Object.setPrototypeOf(obj, Base);

expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBe(1);
expect(obj.test).toBe(2);
Expand Up @@ -18,7 +18,7 @@ Object.setPrototypeOf(obj, Base);

expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(called).toBe(false);
expect(Base.test).toBe(1);
expect(obj.test).toBe(2);
Expand Up @@ -18,6 +18,6 @@ expect(() => {
// this requires helpers to be in file (not external), so they
// are in "strict" mode code.
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBe(1);
expect(obj.test).toBe(2);
Expand Up @@ -17,6 +17,6 @@ expect(() => {
// this requires helpers to be in file (not external), so they
// are in "strict" mode code.
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBe(1);
expect(obj.test).toBe(2);
Expand Up @@ -17,7 +17,7 @@ Object.setPrototypeOf(obj, Base);

expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBeUndefined();
expect(obj.test).toBe(2);

Expand Down
Expand Up @@ -17,7 +17,7 @@ Object.setPrototypeOf(obj, Base);

expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBeUndefined();
expect(obj.test).toBe(2);

Expand Down
Expand Up @@ -16,7 +16,7 @@ Object.defineProperty(obj, 'test', {
Object.setPrototypeOf(obj, Base);
expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBeUndefined();
expect(obj.test).toBe(2);
const desc = Object.getOwnPropertyDescriptor(obj, 'test');
Expand Down
Expand Up @@ -16,7 +16,7 @@ Object.setPrototypeOf(obj, Base);

expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(called).toBe(false);
expect(Base.test).toBeUndefined();
expect(obj.test).toBeUndefined();
Expand Up @@ -15,6 +15,6 @@ expect(() => {
// this requires helpers to be in file (not external), so they
// are in "strict" mode code.
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBeUndefined();
expect(obj.test).toBeUndefined();
Expand Up @@ -13,6 +13,6 @@ expect(() => {
// this requires helpers to be in file (not external), so they
// are in "strict" mode code.
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(Base.test).toBeUndefined();
expect(obj.test).toBeUndefined();
Expand Up @@ -16,7 +16,7 @@ Object.setPrototypeOf(obj, Base);

expect(() => {
obj.set();
}).toThrow();
}).toThrow(TypeError);
expect(value).toBe(2);
expect(Base.test).toBeUndefined();
expect(obj.test).toBeUndefined();

0 comments on commit 543c15a

Please sign in to comment.