-
-
Notifications
You must be signed in to change notification settings - Fork 31
fix #231 filter no named imports #237
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
Conversation
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... 📒 Files selected for processing (1)
Tip CodeRabbit can approve the review once all CodeRabbit's comments are resolved.Enable the WalkthroughFilters out side‑effect‑only imports from macro execution in tryPageMetaFromMacro by updating findImports to collect only ImportDeclaration nodes with specifiers. Public APIs remain unchanged; implementation shifts to an explicit loop for gathering imports. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant P as Page Loader
participant PM as tryPageMetaFromMacro
participant FI as findImports
participant EX as Macro Executor
P->>PM: parse page source
PM->>FI: collect imports (AST)
FI-->>PM: ImportDeclarations with specifiers only
PM->>EX: execute macro with { imports, code, filename }
EX-->>PM: page meta (or error)
PM-->>P: resolved page meta
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Poem
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/core/src/page.ts (1)
170-177: Use Babel type guard for clarity and safer narrowing.Minor readability/typing improvement; behavior unchanged.
- for (const stmt of stmts) { - if (stmt.type !== 'ImportDeclaration') { - continue - } - imports.push(stmt) - } + for (const stmt of stmts) { + if (!t.isImportDeclaration(stmt)) + continue + imports.push(stmt) + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/core/src/page.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/core/src/page.ts (1)
packages/core/src/utils.ts (1)
babelGenerate(144-144)
🔇 Additional comments (1)
packages/core/src/page.ts (1)
106-106: Restrict imports to runnable named value specifiers (exclude default/namespace/type-only)Current filter only removes side‑effect imports and therefore still includes default/namespace imports. To ensure execScript receives only runnable value imports, change the filter to keep import declarations that are not type-only and that contain at least one ImportSpecifier (non-type); strip type-only specifiers from mixed imports.
Apply:
- const imports = findImports(ast.body).filter(imp => !!imp.specifiers.length).map(imp => babelGenerate(imp).code) + const imports = findImports(ast.body) + // keep only value-bearing named imports; drop default/namespace-only and TS type-only + .filter(imp => + imp.importKind !== 'type' && + imp.specifiers.some(s => t.isImportSpecifier(s) && s.importKind !== 'type'), + ) + .map((imp) => { + // strip type-only specifiers from mixed imports to ensure runnable JS + const valueSpecs = imp.specifiers.filter(s => !(t.isImportSpecifier(s) && s.importKind === 'type')) + if (valueSpecs.length !== imp.specifiers.length) { + const clone = t.cloneNode(imp) as t.ImportDeclaration + clone.specifiers = valueSpecs + return babelGenerate(clone).code + } + return babelGenerate(imp).code + })Confirm intended behavior:
- Only exclude side‑effect imports (current code).
- Exclude default/namespace‑only imports as well (use diff above).
Add regression tests for:
- import 'x'
- import foo from 'x'
- import * as ns from 'x'
- import { a, type T } from 'x'
- import type { T } from 'x'
[file: packages/core/src/page.ts — around line 106]
Description 描述
related #231
解决当有非命名引用时,ts 转为 cjs 无法摇树,导致引入了 typescript 文件而出错的bug
Summary by CodeRabbit
Bug Fixes
Refactor