Repository navigation
Keep a non-enumerable key of a checked object non-enumerable in its record #3235
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
6 changes: 6 additions & 0 deletions
6
...ages/compiler/test/ts7/baselines/order-parity/tests/corpus/8854-record-hidden-key.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/8854-record-hidden-key.ts" | ||
| ], | ||
| "diags": [] | ||
| } |
6 changes: 6 additions & 0 deletions
6
...test/ts7/baselines/order-parity/tests/fixtures/npm/cases/hidden-key-boundary/main.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/fixtures/npm/cases/hidden-key-boundary/main.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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // A checked record keeps a declared key non-enumerable when the object it | ||
| // was converted from held the key that way. The record has no place for | ||
| // the attribute, so its kept-source view holds it: Object.keys, values, | ||
| // entries, for...in, JSON.stringify, console.log and untyped views leave | ||
| // the key out, while reads, writes, `in`, hasOwn and getOwnPropertyNames | ||
| // still see it, and its descriptor reports the attribute. | ||
|
|
||
| interface Schema { | ||
| kind: string; | ||
| type: string; | ||
| note?: string; | ||
| } | ||
|
|
||
| function hide(target: any, key: string, value: unknown): void { | ||
| Object.defineProperty(target, key, { enumerable: false, writable: true, configurable: true, value }); | ||
| } | ||
|
|
||
| // `extra` adds a key the type does not declare, so the record keeps a view | ||
| // for two reasons at once. | ||
| function make(extra: boolean): Schema { | ||
| const o: any = JSON.parse('{"type":"t"}'); | ||
| if (extra) o.description = "d"; | ||
| hide(o, "kind", "K"); | ||
| return o as Schema; | ||
| } | ||
|
|
||
| const s = make(false); | ||
| const x = make(true); | ||
|
|
||
| console.log("read", s.kind, s.type, x.kind, x.type); | ||
| console.log("keys", Object.keys(s).join(","), "|", Object.keys(x).join(",")); | ||
| console.log("values", Object.values(s).join(","), "|", Object.values(x).join(",")); | ||
| console.log("entries", JSON.stringify(Object.entries(s)), JSON.stringify(Object.entries(x))); | ||
| for (const k in s) console.log("for-in s", k); | ||
| for (const k in x) console.log("for-in x", k); | ||
| console.log("json", JSON.stringify(s), JSON.stringify(x)); | ||
| console.log("json nested", JSON.stringify({ wrap: s, list: [x] })); | ||
| console.log(JSON.stringify(s, null, 2)); | ||
| console.log("has", "kind" in s, Object.hasOwn(s, "kind"), "kind" in x, Object.hasOwn(x, "type")); | ||
| console.log("names", Object.getOwnPropertyNames(s).join(","), "|", Object.getOwnPropertyNames(x).join(",")); | ||
| console.log(s); | ||
| console.log(x); | ||
| console.log([s]); | ||
|
|
||
| // A write through the type keeps the attribute. | ||
| s.kind = "K2"; | ||
| x.kind = "K3"; | ||
| console.log("written", s.kind, Object.keys(s).join(","), JSON.stringify(s)); | ||
| console.log("written", x.kind, Object.keys(x).join(","), JSON.stringify(x)); | ||
|
|
||
| // Untyped views of the same records. | ||
| const a: any = s; | ||
| const b: any = x; | ||
| console.log("view", Object.keys(a).join(","), JSON.stringify(a), a.kind); | ||
| console.log("view", JSON.stringify(Object.getOwnPropertyDescriptor(a, "kind"))); | ||
| console.log("view", Object.keys(b).join(","), JSON.stringify(b), b.kind); | ||
| console.log("view", JSON.stringify(Object.getOwnPropertyDescriptor(b, "kind"))); | ||
| console.log("view", JSON.stringify(Object.getOwnPropertyDescriptor(b, "type"))); | ||
| a.kind = "W"; | ||
| console.log("view write", s.kind, Object.keys(s).join(","), JSON.stringify(s)); | ||
| console.log("view write", JSON.stringify(Object.getOwnPropertyDescriptor(a, "kind"))); | ||
|
|
||
| // Back through a checked cast: the same record, the key still hidden. | ||
| const back = a as Schema; | ||
| console.log("back", back === s, Object.keys(back).join(","), JSON.stringify(back)); | ||
|
|
||
| // A conversion to another record type keeps it hidden too. | ||
| const narrow: { type: string } = x; | ||
| console.log("narrow", Object.keys(narrow).join(","), JSON.stringify(narrow)); | ||
| const other = b as { type: string; description: string }; | ||
| console.log("other", Object.keys(other).join(","), JSON.stringify(other)); | ||
|
|
||
| // Records inside records and arrays. | ||
| interface Outer { | ||
| inner: Schema; | ||
| list: Schema[]; | ||
| } | ||
| const outer: Outer = { inner: s, list: [x, make(false)] }; | ||
| console.log("outer", JSON.stringify(outer), Object.keys(outer.inner).join(",")); | ||
| const untypedOuter: any = outer; | ||
| console.log("outer view", JSON.stringify(untypedOuter), Object.keys(untypedOuter.list[1]).join(",")); | ||
| console.log("each", outer.list.map((v) => Object.keys(v).join("+")).join(" ")); | ||
| console.log("from entries", JSON.stringify(Object.fromEntries(Object.entries(x)))); | ||
|
|
||
| // Making the key enumerable again through the view is seen through the type. | ||
| Object.defineProperty(b, "kind", { enumerable: true }); | ||
| console.log("shown", Object.keys(x).join(","), JSON.stringify(x)); | ||
|
|
||
| // A hidden key the type does not declare stays hidden as well. | ||
| function makeUndeclared(): { type: string } { | ||
| const o: any = JSON.parse('{"type":"t"}'); | ||
| hide(o, "secret", 1); | ||
| return o as { type: string }; | ||
| } | ||
| const u = makeUndeclared(); | ||
| const uv: any = u; | ||
| console.log("undeclared", Object.keys(u).join(","), JSON.stringify(u), uv.secret, "secret" in u); |
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.