-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(compile): fall back to compiled JS emit for TS namespace/export= declaration merges #10673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
proggeramlug
wants to merge
2
commits into
main
Choose a base branch
from
fix/10662-namespace-export-equals-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| Fixed `axios` (and any `perry.compilePackages` target with a similar shape) | ||
| throwing `TypeError: Class extends value is not a constructor` at | ||
| module-init time. The blocker was in the `https-proxy-agent` -> `agent-base` | ||
| dependency chain: `agent-base`'s TypeScript source merges `namespace | ||
| createAgent { export class Agent extends EventEmitter { ... } }` onto a | ||
| same-named `function createAgent()` and exports it with `export =` — | ||
| Perry's HIR doesn't correctly attach the namespace's exported members to | ||
| the same runtime value `export =` ends up exporting, so | ||
| `require("agent-base").Agent` read back as `undefined` and the downstream | ||
| `class HttpsProxyAgent extends agent_base_1.Agent` threw. Perry's | ||
| `compilePackages` module resolution now detects this TS | ||
| namespace/function-merge + `export =` shape and falls back to the | ||
| package's compiled JS emit instead of its raw `.ts` source — the same file | ||
| Node itself runs, since `--experimental-strip-types` can't execute raw | ||
| `namespace`/`export =` syntax either. Extends the existing #6586 | ||
| ESM+CJS-epilogue fallback in `is_hybrid_cjs_emit_input` with a second, | ||
| narrowly-scoped trigger; not keyed on the `agent-base` package name. |
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
101 changes: 101 additions & 0 deletions
101
crates/perry/src/commands/compile/cjs_wrap/issue_10662_tests.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //! Regression tests for #10662: `agent-base`'s TypeScript `namespace | ||
| //! createAgent { export class Agent extends EventEmitter { … } } export = | ||
| //! createAgent;` — a `function createAgent()` merged with a namespace of the | ||
| //! same name, exported via TS's `export =` form. Perry's HIR lowers the | ||
| //! namespace's exported `Agent` class as a static-field-set against a | ||
| //! synthetic class entity distinct from the runtime function value | ||
| //! `export =` actually exports, so a downstream `https-proxy-agent extends | ||
| //! agent_base_1.Agent` (an `axios` transitive dependency) sees `.Agent` as | ||
| //! `undefined` and throws "Class extends value is not a constructor". | ||
| //! | ||
| //! `has_top_level_namespace_or_module_block` / | ||
| //! `has_top_level_export_equals` detect this shape so | ||
| //! `is_hybrid_cjs_emit_input` (`resolve.rs`) can fall back to the package's | ||
| //! compiled JS emit — the same emit Node itself runs, since | ||
| //! `--experimental-strip-types` can't execute raw `namespace`/`export =` | ||
| //! syntax either. | ||
|
|
||
| use super::detect::{ | ||
| has_top_level_export_equals, has_top_level_namespace_or_module_block, | ||
| strip_comments_and_strings, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn namespace_block_detects_the_agent_base_shape() { | ||
| let src = strip_comments_and_strings( | ||
| "function createAgent(opts) {\n return new createAgent.Agent(opts);\n}\n\nnamespace createAgent {\n export class Agent extends EventEmitter {}\n}\n\nexport = createAgent;\n", | ||
| ); | ||
| assert!(has_top_level_namespace_or_module_block(&src)); | ||
| assert!(has_top_level_export_equals(&src)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn namespace_block_accepts_legacy_module_keyword_and_dotted_names() { | ||
| let src = strip_comments_and_strings("module Foo.Bar {\n export const x = 1;\n}\n"); | ||
| assert!(has_top_level_namespace_or_module_block(&src)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ambient_declare_namespace_is_not_flagged() { | ||
| // `declare namespace X { … }` is type-only — it never emits runtime | ||
| // code, so it cannot be the cause of a namespace/function merge going | ||
| // missing at runtime, and must not trigger the JS-emit fallback. | ||
| let src = strip_comments_and_strings( | ||
| "declare namespace createAgent {\n export class Agent {}\n}\nexport = createAgent;\n", | ||
| ); | ||
| assert!(!has_top_level_namespace_or_module_block(&src)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ordinary_cjs_module_exports_object_literal_is_not_flagged() { | ||
| // `module.exports = { … }` is the single most common CommonJS shape — | ||
| // `module` is followed by `.`, never by whitespace then an identifier, | ||
| // so it must never be mistaken for a `namespace`/`module X {` block. | ||
| let src = strip_comments_and_strings( | ||
| "function build() { return 1; }\nmodule.exports = { build: build, value: 42 };\n", | ||
| ); | ||
| assert!(!has_top_level_namespace_or_module_block(&src)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn export_equals_matches_the_export_equals_form_only() { | ||
| assert!(has_top_level_export_equals(&strip_comments_and_strings( | ||
| "export = createAgent;\n" | ||
| ))); | ||
| assert!(has_top_level_export_equals(&strip_comments_and_strings( | ||
| "export=createAgent;\n" | ||
| ))); | ||
|
|
||
| // Ordinary ESM export forms must not match — none of these are the | ||
| // CJS-interop `export =` shape. | ||
| assert!(!has_top_level_export_equals(&strip_comments_and_strings( | ||
| "export const x = 1;\n" | ||
| ))); | ||
| assert!(!has_top_level_export_equals(&strip_comments_and_strings( | ||
| "export class Foo {}\n" | ||
| ))); | ||
| assert!(!has_top_level_export_equals(&strip_comments_and_strings( | ||
| "export default Foo;\n" | ||
| ))); | ||
| assert!(!has_top_level_export_equals(&strip_comments_and_strings( | ||
| "export { Foo };\n" | ||
| ))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn plain_esm_or_cjs_source_without_the_merge_shape_is_unaffected() { | ||
| // A normal ESM file with a class extending an imported native builtin — | ||
| // the overwhelmingly common case — must not be flagged: no namespace | ||
| // block, no `export =`. | ||
| let esm = strip_comments_and_strings( | ||
| "import { EventEmitter } from 'events';\nexport class Agent extends EventEmitter {}\n", | ||
| ); | ||
| assert!(!has_top_level_namespace_or_module_block(&esm)); | ||
| assert!(!has_top_level_export_equals(&esm)); | ||
|
|
||
| // A normal CJS file. | ||
| let cjs = | ||
| strip_comments_and_strings("'use strict';\nclass Agent {}\nmodule.exports = { Agent };\n"); | ||
| assert!(!has_top_level_namespace_or_module_block(&cjs)); | ||
| assert!(!has_top_level_export_equals(&cjs)); | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 25088
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 31478
Detect declarations at statement boundaries.
Both regexes require
namespaceandexport =to begin a physical line. Afterstrip_comments_and_strings, the compact valid TypeScript source remains on one line, so both detectors returnfalse.is_hybrid_cjs_emit_inputthen misses the fallback and can select raw TypeScript, retaining the namespace/function merge failure.TypeScript and the resolver impose no line-boundary requirement. Recognize declarations after
;and line breaks with a top-level statement scan or AST check. Add the compact-source case as a regression test.🤖 Prompt for AI Agents