Skip to content

Commit

Permalink
Tests WIP 5
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 30, 2023
1 parent e9f2cd9 commit ccbb839
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 23 deletions.
23 changes: 0 additions & 23 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
# TODO

* Prevent injection of class into scope function for its methods.

* Tests for `super()` in arrow functions:

```js
class Y extends X {
constructor() {
const callSuper = () => super();
callSuper();
}
}
return Y;
```

```js
let callSuper;
class Y extends X {
constructor() {
callSuper = () => super();
}
}
try { new Y() } catch {}
return callSuper;
```
108 changes: 108 additions & 0 deletions test/classes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3391,6 +3391,114 @@ describe('Classes', () => {
});
});

describe('`super()` in arrow function', () => {
itSerializes('when class serialized', {
in() {
class X {
constructor() {
this.x = 1;
}
}
return class Y extends X {
constructor() { // eslint-disable-line constructor-super
const callSuper = () => super();
callSuper();
}
};
},
out: `(()=>{
const a=Object.setPrototypeOf,
b=class X{
constructor(){
this.x=1
}
},
c=a(
class Y extends class{}{
constructor(){
const a=()=>super();
a()
}
},
b
);
a(c.prototype,b.prototype);
return c
})()`,
validate(Klass) {
expect(Klass).toBeFunction();
expect(Klass.name).toBe('Y');
const {prototype} = Klass;
expect(prototype).toBeObject();
expect(prototype.constructor).toBe(Klass);
const proto = Object.getPrototypeOf(prototype);
expect(proto).toBeObject();
expect(proto).not.toBe(Object.prototype);
expect(proto).toHavePrototype(Object.prototype);
const SuperClass = proto.constructor;
expect(SuperClass).toBeFunction();
expect(SuperClass.name).toBe('X');
const instance = new Klass();
expect(instance).toBeInstanceOf(Klass);
expect(instance).toBeInstanceOf(SuperClass);
expect(instance).toHaveOwnPropertyNames(['x']);
expect(instance.x).toBe(1);
}
});

itSerializes('when arrow function serialized', {
in() {
class X {
constructor() {
this.x = 1;
}
}

class Y extends X {
constructor() {
return {callSuper: (0, () => super())}; // eslint-disable-line no-constructor-return
}
}

return new Y().callSuper;
},
out: `(()=>{
const a=Object.setPrototypeOf,
b=class X{
constructor(){
this.x=1
}
},
c=a(
class Y extends class{}{
constructor(){
return{callSuper:(0,()=>super())}
}
},
b
);
a(c.prototype,b.prototype);
return(a=>()=>Reflect.construct(Object.getPrototypeOf(a),[],a))(c)
})()`,
validate(fn) {
expect(fn).toBeFunction();
const instance = fn();
expect(instance).toBeObject();
expect(instance).toHaveOwnPropertyNames(['x']);
expect(instance.x).toBe(1);
const proto = Object.getPrototypeOf(instance);
expect(proto).toBeObject();
const Klass = proto.constructor;
expect(Klass).toBeFunction();
expect(Klass.name).toBe('Y');
const SuperClass = Object.getPrototypeOf(Klass);
expect(SuperClass).toBeFunction();
expect(SuperClass.name).toBe('X');
expect(Object.getPrototypeOf(proto).constructor).toBe(SuperClass);
}
});
});

itSerializes("defined in another class method's computed key", {
in() {
let Klass;
Expand Down

0 comments on commit ccbb839

Please sign in to comment.