fix: rename declarations shadowing globals used by the emit - #505
Closed
dsherret wants to merge 1 commit into
Closed
fix: rename declarations shadowing globals used by the emit#505dsherret wants to merge 1 commit into
dsherret wants to merge 1 commit into
Conversation
A module declaring something like `const Object = "hello";` in the module
scope caused the emit to reference that declaration instead of the global
and throw at runtime (ex. `Object.defineProperty(exports, "__esModule",
{ value: true });` in the CommonJS output or `Object.defineProperty(this,
...)` in the ES module output when downleveling class fields).
Closes denoland#444
Claude-Session: https://claude.ai/code/session_01SqfDuWeEek5XnHSbXxCUE9
Collaborator
Author
|
Bad and way too complicated. I think this is a limitation in TypeScript and we might not want to fix it. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #444
A module declaring a module scoped binding with the same name as an identifier the emit relies on caused the output to resolve that identifier to the declaration instead of the global:
...emitted CommonJS that throws with "Cannot access 'Object' before initialization":
The ES module output has the same problem when downleveling (ex. class fields emit
Object.defineProperty(this, ...)when targeting below ES2022).This adds a compiler transformer that renames these module scoped declarations (
const Object_1 = "hello";) along with their references, keeping the original export names so the public API and declaration files are unchanged:Notes:
exports), so they're left alone there.export const <name> = <renamed>;for CommonJS/UMD (the CommonJS transform can't resolve a synthesized export specifier) and asexport { <renamed> as <name> };for ES modules.