Repository navigation
Compile object-literal shorthand of class declarations #1557
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
Open
cramforce
wants to merge
5
commits into
main
Choose a base branch
from
opt/class-value-shorthand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−100
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...compiler/test/ts7/baselines/order-parity/tests/corpus/6585-class-value-shorthand.cjs.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6585-class-value-shorthand.cjs" | ||
| ], | ||
| "diags": [] | ||
| } |
6 changes: 6 additions & 0 deletions
6
.../compiler/test/ts7/baselines/order-parity/tests/corpus/6586-class-value-shorthand.ts.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6586-class-value-shorthand.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
7 changes: 7 additions & 0 deletions
7
.../ts7/baselines/order-parity/tests/corpus/6587-class-value-shorthand-exports/main.cjs.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6587-class-value-shorthand-exports/channel.cjs", | ||
| "<repo>/tests/corpus/6587-class-value-shorthand-exports/main.cjs" | ||
| ], | ||
| "diags": [] | ||
| } |
6 changes: 6 additions & 0 deletions
6
...st/ts7/baselines/order-parity/tests/corpus/6588-class-value-shorthand-early-reads.ts.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "order": [ | ||
| "<repo>/tests/corpus/6588-class-value-shorthand-early-reads.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Object-literal shorthand of class values: class declarations (plain and | ||
| // extending EventEmitter) and class expressions stored as properties. The | ||
| // property holds the class itself: construction through it, its name, | ||
| // identity with the binding, instanceof, and the emitter surface of its | ||
| // instances. | ||
| "use strict"; | ||
| const { EventEmitter } = require("node:events"); | ||
|
|
||
| class Base { | ||
| hi() { | ||
| return "base"; | ||
| } | ||
| } | ||
| class Bus extends EventEmitter { | ||
| constructor(name) { | ||
| super(); | ||
| this.name = name; | ||
| } | ||
| } | ||
| const Anon = class extends EventEmitter {}; | ||
| const api = { Base, Bus, Anon, version: 1 }; | ||
| const bus = new api.Bus("main"); | ||
| console.log(api.Base.name, api.Bus.name, api.Anon.name, api.version); | ||
| console.log(bus.name, bus instanceof Bus, bus instanceof EventEmitter, bus instanceof api.Bus); | ||
| console.log(new api.Base().hi(), new api.Base() instanceof Base, new api.Bus("x") instanceof Base); | ||
| bus.on("x", (v) => console.log("x", v)); | ||
| console.log(bus.emit("x", 1), bus.listenerCount("x")); | ||
| const anon = new api.Anon(); | ||
| console.log(anon instanceof EventEmitter, anon instanceof Anon, typeof api.Anon); | ||
| console.log(api.Bus === Bus, typeof api.Bus, Object.keys(api)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Object-literal shorthand of class values in TypeScript, including a | ||
| // class extending EventEmitter, and statically decided instanceof whose | ||
| // operands are computed: both operands still evaluate, left first. | ||
| import { EventEmitter } from "node:events"; | ||
|
|
||
| const A = class { | ||
| x = 1; | ||
| }; | ||
| class B extends EventEmitter { | ||
| n = 2; | ||
| } | ||
| class C { | ||
| label(): string { | ||
| return "c"; | ||
| } | ||
| } | ||
| const registry = { A, B, C }; | ||
| console.log(registry.A.name, registry.B.name, registry.C.name); | ||
| console.log(new registry.A().x, new registry.B().n, new registry.C().label()); | ||
| const order: string[] = []; | ||
| function make(): C { | ||
| order.push("make"); | ||
| return new C(); | ||
| } | ||
| function target(): typeof C { | ||
| order.push("target"); | ||
| return registry.C; | ||
| } | ||
| console.log(make() instanceof target(), new registry.B() instanceof EventEmitter); | ||
| console.log(new registry.B() instanceof B, make() instanceof registry.C, order.join(",")); |
17 changes: 17 additions & 0 deletions
17
tests/corpus/6587-class-value-shorthand-exports/channel.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| const { EventEmitter } = require("node:events"); | ||
|
|
||
| class Channel extends EventEmitter { | ||
| constructor(name) { | ||
| super(); | ||
| this.name = name; | ||
| } | ||
| send(message) { | ||
| this.emit("message", message, this.name); | ||
| } | ||
| } | ||
| const Plain = class { | ||
| constructor() { | ||
| this.kind = "plain"; | ||
| } | ||
| }; | ||
| module.exports = { Channel, Plain }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // A module exporting classes through object-literal shorthand | ||
| // (`module.exports = { Channel, Plain }`), one of them extending | ||
| // EventEmitter; the importer constructs, compares and tests instances | ||
| // through the exported values. | ||
| const { EventEmitter } = require("node:events"); | ||
| const lib = require("./channel.cjs"); | ||
| const { Channel, Plain } = lib; | ||
|
|
||
| const ch = new lib.Channel("alpha"); | ||
| ch.on("message", (message, from) => console.log("message", message, from)); | ||
| ch.send("hello"); | ||
| console.log(lib.Channel.name, Channel === lib.Channel, ch instanceof Channel, ch instanceof EventEmitter); | ||
| const p = new Plain(); | ||
| console.log(p.kind, p instanceof lib.Plain, lib.Plain.name, new lib.Plain() instanceof Plain); | ||
| console.log(ch.listenerCount("message"), ch.eventNames()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Reads that run before their binding is initialized throw Node's | ||
| // ReferenceError: a class read through object-literal shorthand from a | ||
| // function called before the class statement, and the left operand of a | ||
| // statically decided instanceof whose constructor operand is computed. | ||
| class Plain {} | ||
| const lib = { Plain }; | ||
|
|
||
| function early(): { Late: typeof Late } { | ||
| return { Late }; | ||
| } | ||
| function earlyInstance(): boolean { | ||
| return pending instanceof lib.Plain; | ||
| } | ||
|
|
||
| function attempt(label: string, run: () => unknown): void { | ||
| try { | ||
| run(); | ||
| console.log(label, "returned"); | ||
| } catch (error) { | ||
| console.log(label, (error as Error).name, (error as Error).message); | ||
| } | ||
| } | ||
| attempt("shorthand", early); | ||
| attempt("instanceof", earlyInstance); | ||
|
|
||
| class Late { | ||
| static tag = "late"; | ||
| } | ||
| let pending = new Plain(); | ||
| const late = early().Late; | ||
| console.log(late.tag, late === Late, earlyInstance()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.