Skip to content
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

fix: https://github.com/umijs/mako/issues/1007 #1738

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

notcold
Copy link
Contributor

@notcold notcold commented Jan 3, 2025

fix #1007
support BinaryExpression

Summary by CodeRabbit

  • 新功能

    • 增强了在变量声明中处理二进制表达式的能力,特别是在涉及 require 调用的场景。
  • 测试

    • 添加了新的测试用例,验证在二进制表达式中处理 require 的场景。
  • 代码改进

    • 优化了模块导入和表达式处理逻辑,提高了代码的健壮性。

support BinaryExpression
Copy link
Contributor

coderabbitai bot commented Jan 3, 2025

概述

演练

这次更改主要增强了 TryResolve 结构体在处理变量声明中二进制表达式的能力。具体来说,新增了对包含 require 调用的逻辑或(||)和逻辑与(&&)表达式的支持。修改涉及 try_resolve.rs 文件中的表达式处理逻辑,并在端到端测试中添加了相应的测试用例。

变更

文件 变更摘要
crates/mako/src/visitors/try_resolve.rs 更新导入语句,添加 BinExpr;修改 visit_mut_stmt 方法以支持二进制表达式
e2e/fixtures/resolve.failed-in-try-statement/src/index.tsx 添加新的 try-catch 块,使用 `0
e2e/fixtures/resolve.failed-in-try-statement/expect.js 新增断言以验证错误处理

对链接问题的评估

目标 已解决 说明
支持 `a require('not-found')` 场景
修改 transform_try_resolve.rs
添加相关测试用例

诗歌

🐰 代码兔子轻跳跃
二进制表达式展翅飞
逻辑或与与相携
模块解析不再迷
代码世界又添翼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Walkthrough

This pull request addresses issue #1007 by adding support for handling BinaryExpression in the codebase. The changes primarily involve modifying the visitor logic to correctly process binary expressions, particularly in scenarios involving require statements. Additionally, new test cases have been added to ensure the functionality works as expected.

Changes

File Summary
crates/mako/src/visitors/try_resolve.rs Added logic to handle BinaryExpression by recursively processing left and right expressions. Included new tests for this functionality.
e2e/fixtures/resolve.failed-in-try-statement/expect.js Updated test expectations to include scenarios with binary expressions.
e2e/fixtures/resolve.failed-in-try-statement/src/index.tsx Added new test cases to cover binary expressions in try-catch blocks.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 (2)
e2e/fixtures/resolve.failed-in-try-statement/src/index.tsx (1)

10-12: 建议在异常处理中记录错误信息或提示。
当前仅在 try/catch 中使用了 require('./faa'),如果引入失败会静默处理,没有额外的日志或提示。建议在 catch 块中加上相应的日志或错误处理逻辑,以便在调试或排查问题时更直观地发现具体的异常原因。

crates/mako/src/visitors/try_resolve.rs (1)

99-111: 在二元表达式的左右两侧均支持处理 CallExpr,增强了潜在场景覆盖。
此处的实现对于 Expr::Bin 判断全面,能够分别检测左右两侧是否有 require('...'),并进行必要的处理,符合预期需求。若后续有更多逻辑运算符(如 &&、三元操作符等)也需要类似处理,可考虑进一步抽象这个逻辑以保持可维护性。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c37e244 and bad80f8.

📒 Files selected for processing (3)
  • crates/mako/src/visitors/try_resolve.rs (3 hunks)
  • e2e/fixtures/resolve.failed-in-try-statement/expect.js (1 hunks)
  • e2e/fixtures/resolve.failed-in-try-statement/src/index.tsx (1 hunks)
🔇 Additional comments (3)
e2e/fixtures/resolve.failed-in-try-statement/expect.js (1)

24-29: 检查测试正则表达式与实际模块名称是否保持一致。
此处断言检查的错误信息是 Cannot find module './hoo',但在 index.tsx 中引入的实际模块却是 './faa'。请确认是否存在拼写或路径不一致的问题,以免断言无法准确覆盖新逻辑。

crates/mako/src/visitors/try_resolve.rs (2)

5-6: 新导入的 BinExpr 对应代码处理已添加,逻辑清晰。
添加对 BinExpr 的导入有助于在后续处理二元表达式时更快调用相关的 AST API,当前用法与需求保持一致,没有明显问题。


227-242: 新测试用例覆盖了二元表达式中出现 require('foo') 的场景。
此测试能够验证对 0 || require('foo') 形式的代码是否能够正确地进行依赖解析并抛出对应的异常提示,进一步提升了对异常场景的覆盖率。实现思路正确且与整体用例风格一致。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

try resolve 不支持 a || require('not-found')
1 participant