Title: SES compatibility for '--emitDecoratorMetadata' · Issue #43463 · microsoft/TypeScript · GitHub
Open Graph Title: SES compatibility for '--emitDecoratorMetadata' · Issue #43463 · microsoft/TypeScript
X Title: SES compatibility for '--emitDecoratorMetadata' · Issue #43463 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms ses reflect metadata decorator emitDecoratorMetadata experimentalDecorators ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavi...
Open Graph Description: Suggestion 🔍 Search Terms ses reflect metadata decorator emitDecoratorMetadata experimentalDecorators ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code...
X Description: Suggestion 🔍 Search Terms ses reflect metadata decorator emitDecoratorMetadata experimentalDecorators ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/43463
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SES compatibility for '--emitDecoratorMetadata'","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\nses reflect metadata decorator emitDecoratorMetadata experimentalDecorators\r\n\r\n## ✅ Viability Checklist\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [ ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n## ❗ Problem\r\n\r\nSES (Secure ECMAScript) is an initiative to create a locked-down and sandboxed JavaScript execution environment. One of the tenets of SES is that \"primordials\" (built-in/native ECMAScript global objects, prototypes, functions, etc.) should be immutable and be locked down to a specific set of APIs. This set of APIs includes those specified in the ECMAScript standard and other \"blessed\" APIs depending on the runtime environment. \r\n\r\nA longstanding package used by many in the community is [`reflect-metadata`](https://github.com/rbuckton/reflect-metadata), which is used with TypeScript's `--emitDecoratorMetadata` compiler flag and the `__metadata` helper:\r\n\r\n**Example:**\r\n```bash\r\ntsc example.ts \\\r\n --target esnext \\\r\n --experimentalDecorators \\\r\n --emitDecoratorMetadata\r\n```\r\n\r\n*example.ts*\r\n```ts\r\ndeclare const dec: any;\r\n@dec\r\nclass C {\r\n constructor(x: int) {\r\n }\r\n}\r\n```\r\n\r\n*example.js*\r\n```js\r\nvar __decorate = ...\r\nvar __metadata = (this && this.__metadata) || function (k, v) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\r\n};\r\nlet C = class C {\r\n constructor(x) {\r\n }\r\n};\r\nC = __decorate([\r\n dec,\r\n __metadata(\"design:paramtypes\", [Number])\r\n], C);\r\n```\r\n\r\nThe issue is that the `__metadata` helper function explicitly relies on an early draft of a [proposal](https://rbuckton.github.io/reflect-metadata/) that has not yet been brought to committee (as it depends on the decorators proposal, which has not yet reached Stage 3). Unfortunately, `reflect-metadata` mutates the global `Reflect` object to shim its API, which violates SES. This was recently brought up in https://github.com/endojs/endo/issues/612 and brought to my attention by @erights. \r\n\r\nAlternatives to `reflect-metadata` exist that do not rely on global mutation, such as https://esfx.js.org/esfx/api/metadata.html. However, they cannot be easily used with TypeScript's `--emitDecoratorMetadata` since our helper is hardcoded to check for the specific global. While it is possible to overwrite the helper locally by providing your own `__metadata` helper, this is behavior is intentionally undocumented and is also burdensome on developers.\r\n\r\n## ⭐ Suggestion\r\n\r\nI am proposing that we add two new compiler options to control how we emit decorator metadata:\r\n\r\n- `--metadataDecorator \u003cstring>` — Accepts a valid `EntityName` that should be used as the decorator in place of `__metadata`\r\n - This option can only be specified if both `--experimentalDecorators` and `--emitDecoratorMetadata` are set.\r\n- `--metadataDecoratorImportSource \u003cstring>` — A module specifier used to import the provided metadata decorator.\r\n - This option can only be specified if `--experimentalDecorators`, `--emitDecoratorMetadata`, and `--metadataDecorator` are set.\r\n\r\n***NOTE:** These two options are somewhat analogous to our existing `--jsxFactory` and `--jsxImportSource` options.*\r\n\r\n### `--metadataDecorator` option\r\n\r\nIf the `--metadataDecorator` option is set without specifying `--metadataDecoratorImportSource`, then the following occurs:\r\n- During command line validation we verify that the provided option is a valid `EntityName`.\r\n- During check we verify that the provided name can be resolved to a value that can be called as a decorator appropriate to the element on which it would appear as a result of `--emitDecoratorMetadata`.\r\n- During emit, we would emit the provided `EntityName` in place of `__metadata`.\r\n\r\n***NOTE:** This scenario assumes that the provided metadata decorator exists as a global, or is manually imported into any module.*\r\n\r\n**Example**:\r\n\r\n```bash\r\ntsc example.ts \\\r\n --target esnext \\\r\n --experimentalDecorators \\\r\n --emitDecoratorMetadata \\\r\n --metadataDecorator myCustomMetadata\r\n```\r\n\r\n*example.ts*\r\n```ts\r\ndeclare const dec: any;\r\n\r\n@dec\r\nclass C {\r\n constructor(x: number) {}\r\n}\r\n```\r\n\r\n*example.js*\r\n```\r\nvar __decorate = ...\r\nlet C = class C {\r\n constructor(x) {\r\n }\r\n}\r\nC = __decorate([\r\n dec,\r\n myCustomMetadata(\"design:paramtypes\", [Number])\r\n], C);\r\n```\r\n\r\n### `--metadataDecoratorImportSource` option\r\n\r\nIf the `--metadataDecoratorImportSource` option is set along with `--metadataDecorator`, then the following occurs:\r\n- During command line validation we verify that the provided option is a valid `EntityName`.\r\n- During check we verify that the provided import source can be resolved as a module.\r\n- During check we verify that the first identifier in the provided `EntityName` is an export of the import source.\r\n- During check we verify that the remainder of the provided `EntityName` can be resolved to a value relative to the imported binding above, that can be called as a decorator appropriate to the element on which it would appear as a result of `--emitDecoratorMetadata`.\r\n- During emit, we would emit a synthetic `import` statement for the import source.\r\n- During emit, we would emit the provided `EntityName` in place of `__metadata`.\r\n\r\n**Example**:\r\n\r\n```bash\r\ntsc example.ts \\\r\n --target esnext \\\r\n --experimentalDecorators \\\r\n --emitDecoratorMetadata \\\r\n --metadataDecorator myCustomMetadata \\\r\n --metadataDecoratorImportSource my-custom-metadata\r\n```\r\n\r\n*example.ts*\r\n```ts\r\ndeclare const dec: any;\r\n\r\n@dec\r\nexport export class C {\r\n constructor(x: number) {}\r\n}\r\n```\r\n\r\n*example.js*\r\n```js\r\nvar __decorate = ...\r\nimport { myCustomMetadata } from \"my-custom-metadata\"\r\nlet C = class C {\r\n constructor(x) {\r\n }\r\n}\r\nC = __decorate([\r\n dec,\r\n myCustomMetadata(\"design:paramtypes\", [Number])\r\n], C);\r\nexport { C }\r\n```\r\n\r\n## Caveats\r\n\r\nSpecifying `--metadataDecoratorImportSource` implies that the custom metadata decorator can only be reached from within a module, therefore any source file that expects metadata must have at least one `import` or `export` declaration, or the `--isolatedModules` option must be set. If a file is not determined to be a module, we will fall back to the existing `__metadata` helper (which is essentially a no-op if the `Reflect.metadata` function does not exist at runtime).\r\n\r\n## Out-of-scope\r\n\r\n- Further changes to type metadata emit related to `--emitDecoratorMetadata` are out of scope. This is only intended to address an ecosystem concern brought to us by those involved with SES.\r\n- Changes to decorator emit related to the ongoing work in the Decorators proposal are out of scope. We do not intend to make any substantial changes to our decorator support until that proposal has reached Stage 3.\r\n\r\n## Related Issues\r\n\r\n- #43450 — ES5 class rewriting incompatible with frozen intrinsics\r\n","author":{"url":"https://github.com/rbuckton","@type":"Person","name":"rbuckton"},"datePublished":"2021-03-31T18:53:20Z","url":"https://github.com/microsoft/TypeScript/issues/43463"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a6800564-1096-9470-ba4f-08614a0d381b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E836:E03FE:1B025A:239724:6B016511 |
| html-safe-nonce | 436999f80a885fd3dcef73a7db89b6f9acf5b599376c159ceb4e722ed16db102 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODM2OkUwM0ZFOjFCMDI1QToyMzk3MjQ6NkIwMTY1MTEiLCJ2aXNpdG9yX2lkIjoiMjc4NzE3NDQ4NTQyODYyNjcwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cb12805bdcd33b67526621dacfef0a55c231eba56239a26c1ec3bb871e3d82d1 |
| hovercard-subject-tag | issue:847137963 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/43463/issue_layout |
| twitter:image | https://opengraph.githubassets.com/81cd9ec1c18d8420128deef88144699ddc4288555b9625d367d949b6240c00c8/microsoft/TypeScript/issues/43463 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/81cd9ec1c18d8420128deef88144699ddc4288555b9625d367d949b6240c00c8/microsoft/TypeScript/issues/43463 |
| og:image:alt | Suggestion 🔍 Search Terms ses reflect metadata decorator emitDecoratorMetadata experimentalDecorators ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rbuckton |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width