Skip to content

Commit

Permalink
Polyfill Symbol.metadata, DecoratorContext.metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 7, 2024
1 parent ece4051 commit cc4ee6f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
release:
runs-on: ubuntu-latest
needs: ci
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -30,6 +32,6 @@ jobs:
run: npm run build

- name: Publish
run: npm publish --access=public
run: npm publish --access=public --provenance
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
23 changes: 23 additions & 0 deletions src/internal/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,34 @@ export function isValidMetadata<T extends Metadata = Metadata>(
return arg != null && typeof arg == 'object' && metadata in arg;
}

/**
* Polyfill Symbol.metadata
* @see https://github.com/microsoft/TypeScript/issues/53461
*/
(Symbol as { metadata: symbol }).metadata ??= Symbol.for('Symbol.metadata');

/**
* Polyfill context.metadata
* @see https://github.com/microsoft/TypeScript/issues/53461
*/
export function _polyfill_contextMetadata(target: object): void {
if (!Symbol?.metadata) {
return;
}
Object.defineProperty(target, Symbol.metadata, {
enumerable: true,
configurable: true,
writable: true,
value: Object.create(null),

Check warning on line 83 in src/internal/struct.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Unsafe assignment of an `any` value

Check warning on line 83 in src/internal/struct.ts

View workflow job for this annotation

GitHub Actions / macos-latest

Unsafe assignment of an `any` value

Check warning on line 83 in src/internal/struct.ts

View workflow job for this annotation

GitHub Actions / windows-latest

Unsafe assignment of an `any` value
});
}

/**
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node)
*/
export function symbol_metadata(arg: ClassLike): typeof Symbol.metadata {
const symbol_metadata = Symbol.metadata || Object.getOwnPropertySymbols(arg).find(s => s.description == 'Symbol.metadata');
_polyfill_contextMetadata(arg);
if (!symbol_metadata) {
throw new ReferenceError('Could not get a reference to Symbol.metadata');
}
Expand Down
File renamed without changes.

0 comments on commit cc4ee6f

Please sign in to comment.