Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 12, 2024
1 parent 128fc28 commit a689bee
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 110 deletions.
89 changes: 88 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,94 @@ You can use `node run.mjs` after `npm install` to run and update the tests below

### esbuild (`esbuild@0.21.0`)

✅ All checks passed
<details>
<summary>❌ 16 checks failed (click for details)</summary>

```
❌ Field decorators: Shim (instance field)
Code: this instanceof Foo2
Expected: true
Observed: false
❌ Field decorators: Shim (instance field)
Code: this instanceof Foo2
Expected: true
Observed: false
❌ Field decorators: Shim (instance field)
Code: log + ""
Expected: "false,false,123,true,false,"
Observed: "false,false,123,false,false,"
❌ Field decorators: Shim (static field)
Code: this
Expected: class
Observed: [object global]
❌ Field decorators: Shim (static field)
Code: this
Expected: class
Observed: [object global]
❌ Field decorators: Shim (static field)
Code: log + ""
Expected: "false,false,123,true,false,"
Observed: "false,false,123,false,false,"
❌ Field decorators: Shim (private instance field)
Code: this instanceof Foo2
Expected: true
Observed: false
❌ Field decorators: Shim (private instance field)
Code: this instanceof Foo2
Expected: true
Observed: false
❌ Field decorators: Shim (private instance field)
Code: log + ""
Expected: "false,false,123,true,false,"
Observed: "false,false,123,false,false,"
❌ Field decorators: Shim (private static field)
Code: this
Expected: class
Observed: [object global]
❌ Field decorators: Shim (private static field)
Code: this
Expected: class
Observed: [object global]
❌ Field decorators: Shim (private static field)
Code: log + ""
Expected: "false,false,123,true,false,"
Observed: "false,false,123,false,false,"
❌ Auto-accessor decorators: Shim (instance auto-accessor)
Code: this instanceof Foo2
Expected: true
Observed: false
❌ Auto-accessor decorators: Shim (static auto-accessor)
Code: this
Expected: class
Observed: [object global]
❌ Auto-accessor decorators: Shim (private instance auto-accessor)
Code: this instanceof Foo2
Expected: true
Observed: false
❌ Auto-accessor decorators: Shim (private static auto-accessor)
Code: this
Expected: class
Observed: [object global]
❌ 16 checks failed
```

</details>

### Babel (`@babel/plugin-proposal-decorators@7.24.1`)

Expand Down
138 changes: 82 additions & 56 deletions decorator-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,10 @@ const tests = {
'Field decorators: Shim (instance field)': () => {
let log = [];
const dec = (value, ctx) => {
return (x) => log.push(x);
return function (x) {
assertEq(() => this instanceof Foo, true);
return log.push('foo' in this, 'bar' in this, x);
};
};
class Foo {
@dec
Expand All @@ -926,40 +929,43 @@ const tests = {
}
assertEq(() => log + '', '');
var obj = new Foo;
assertEq(() => obj.foo, 1);
assertEq(() => obj.bar, 2);
assertEq(() => log + '', '123,');
var obj = new Foo;
assertEq(() => obj.foo, 3);
assertEq(() => obj.bar, 4);
assertEq(() => log + '', '123,,123,');
assertEq(() => obj.bar, 6);
assertEq(() => log + '', 'false,false,123,true,false,');
},
'Field decorators: Shim (static field)': () => {
let foo;
let log = [];
const dec = (value, ctx) => {
return (x) => log.push(x);
};
const fn = (foo, bar) => {
class Foo {
@dec
static foo = 123;
@dec
static bar;
}
assertEq(() => Foo.foo, foo);
assertEq(() => Foo.bar, bar);
return function (x) {
assertEq(() => this, foo);
return log.push('foo' in this, 'bar' in this, x);
};
};
assertEq(() => log + '', '');
fn(1, 2);
assertEq(() => log + '', '123,');
fn(3, 4);
assertEq(() => log + '', '123,,123,');
class Foo {
static {
foo = Foo;
}
@dec
static foo = 123;
@dec
static bar;
}
assertEq(() => Foo.foo, 3);
assertEq(() => Foo.bar, 6);
assertEq(() => log + '', 'false,false,123,true,false,');
},
'Field decorators: Shim (private instance field)': () => {
let log = [];
const dec = (value, ctx) => {
return (x) => log.push(x);
return function (x) {
assertEq(() => this instanceof Foo, true);
return log.push(has$foo(this), has$bar(this), x);
};
};
let has$foo;
let has$bar;
let get$foo;
let get$bar;
class Foo {
Expand All @@ -968,46 +974,48 @@ const tests = {
@dec
#bar;
static {
has$foo = x => #foo in x;
has$bar = x => #bar in x;
get$foo = x => x.#foo;
get$bar = x => x.#bar;
}
}
assertEq(() => log + '', '');
var obj = new Foo;
assertEq(() => get$foo(obj), 1);
assertEq(() => get$bar(obj), 2);
assertEq(() => log + '', '123,');
var obj = new Foo;
assertEq(() => get$foo(obj), 3);
assertEq(() => get$bar(obj), 4);
assertEq(() => log + '', '123,,123,');
assertEq(() => get$bar(obj), 6);
assertEq(() => log + '', 'false,false,123,true,false,');
},
'Field decorators: Shim (private static field)': () => {
let foo;
let log = [];
const dec = (value, ctx) => {
return (x) => log.push(x);
};
const fn = (foo, bar) => {
let get$foo;
let get$bar;
class Foo {
@dec
static #foo = 123;
@dec
static #bar;
static {
get$foo = x => x.#foo;
get$bar = x => x.#bar;
}
}
assertEq(() => get$foo(Foo), foo);
assertEq(() => get$bar(Foo), bar);
return function (x) {
assertEq(() => this, foo);
return log.push(has$foo(this), has$bar(this), x);
};
};
assertEq(() => log + '', '');
fn(1, 2);
assertEq(() => log + '', '123,');
fn(3, 4);
assertEq(() => log + '', '123,,123,');
let has$foo;
let has$bar;
let get$foo;
let get$bar;
class Foo {
static {
foo = Foo;
has$foo = x => #foo in x;
has$bar = x => #bar in x;
get$foo = x => x.#foo;
get$bar = x => x.#bar;
}
@dec
static #foo = 123;
@dec
static #bar;
}
assertEq(() => get$foo(Foo), 3);
assertEq(() => get$bar(Foo), 6);
assertEq(() => log + '', 'false,false,123,true,false,');
},
'Field decorators: Order (instance field)': () => {
const log = [];
Expand Down Expand Up @@ -2386,7 +2394,10 @@ const tests = {
let get;
let set;
const dec = (target, ctx) => {
const init = (x) => x + 1;
function init(x) {
assertEq(() => this instanceof Foo, true);
return x + 1;
}
get = function () { return target.get.call(this) * 10; };
set = function (x) { target.set.call(this, x * 2); };
return { get, set, init };
Expand All @@ -2403,15 +2414,22 @@ const tests = {
assertEq(() => obj.foo, (321 * 2) * 10);
},
'Auto-accessor decorators: Shim (static auto-accessor)': () => {
let foo;
let get;
let set;
const dec = (target, ctx) => {
const init = (x) => x + 1;
function init(x) {
assertEq(() => this, foo);
return x + 1;
}
get = function () { return target.get.call(this) * 10; };
set = function (x) { target.set.call(this, x * 2); };
return { get, set, init };
};
class Foo {
static {
foo = Foo;
}
@dec
static accessor foo = 123;
}
Expand All @@ -2425,7 +2443,10 @@ const tests = {
let get;
let set;
const dec = (target, ctx) => {
const init = (x) => x + 1;
function init(x) {
assertEq(() => this instanceof Foo, true);
return x + 1;
}
get = function () { return target.get.call(this) * 10; };
set = function (x) { target.set.call(this, x * 2); };
return { get, set, init };
Expand All @@ -2446,23 +2467,28 @@ const tests = {
assertEq(() => get$foo(obj), (321 * 2) * 10);
},
'Auto-accessor decorators: Shim (private static auto-accessor)': () => {
let foo;
let get;
let set;
const dec = (target, ctx) => {
const init = (x) => x + 1;
function init(x) {
assertEq(() => this, foo);
return x + 1;
}
get = function () { return target.get.call(this) * 10; };
set = function (x) { target.set.call(this, x * 2); };
return { get, set, init };
};
let get$foo;
let set$foo;
class Foo {
@dec
static accessor #foo = 123;
static {
foo = Foo;
get$foo = x => x.#foo;
set$foo = (x, y) => { x.#foo = y; };
}
@dec
static accessor #foo = 123;
}
assertEq(() => get$foo(Foo), (123 + 1) * 10);
assertEq(() => set$foo(Foo, 321), undefined);
Expand Down
Loading

0 comments on commit a689bee

Please sign in to comment.