From 07e527dae956fc6caffd3c2ba14b7778be1c7c82 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sat, 18 Nov 2023 19:31:33 -0500 Subject: [PATCH] fix #3453: use `Symbol.for` for missing symbols --- CHANGELOG.md | 4 ++++ internal/runtime/runtime.go | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4894a04d05a..92beba10950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,10 @@ } ``` +* No longer throw an error when a `Symbol` is missing ([#3453](https://github.com/evanw/esbuild/issues/3453)) + + Certain JavaScript syntax features use special properties on the global `Symbol` object. For example, the asynchronous iteration syntax uses `Symbol.asyncIterator`. Previously esbuild's generated code for older browsers required this symbol to be polyfilled. However, starting with this release esbuild will use [`Symbol.for()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for) to construct these symbols if they are missing instead of throwing an error about a missing polyfill. This means your code no longer needs to include a polyfill for missing symbols as long as your code also uses `Symbol.for()` for missing symbols. + * Parse upcoming changes to TypeScript syntax ([#3490](https://github.com/evanw/esbuild/issues/3490), [#3491](https://github.com/evanw/esbuild/pull/3491)) With this release, you can now use `from` as the name of a default type-only import in TypeScript code, as well as `of` as the name of an `await using` loop iteration variable: diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index a031775de10..bd2b26f70d7 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -76,8 +76,7 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source { var __reflectSet = Reflect.set var __knownSymbol = (name, symbol) => { - if (symbol = Symbol[name]) return symbol - throw Error('Symbol.' + name + ' is not defined') + return (symbol = Symbol[name]) ? symbol : Symbol.for('Symbol.' + name) } export var __pow = Math.pow @@ -383,7 +382,7 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source { var method = k => it[k] = x => new Promise((yes, no) => resume(k, x, yes, no)) var it = {} return generator = generator.apply(__this, __arguments), - it[Symbol.asyncIterator] = () => it, + it[__knownSymbol('asyncIterator')] = () => it, method('next'), method('throw'), method('return'),