Skip to content

Commit

Permalink
fix #3453: use Symbol.for for missing symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Nov 19, 2023
1 parent 50cead7 commit 07e527d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 07e527d

Please sign in to comment.