Skip to content

Conversation

@wudalei
Copy link

@wudalei wudalei commented Aug 16, 2025

这个 PR 做了什么? (简要描述所做更改)
Snipaste_2025-08-16_15-24-21
注释的宏命令会产生影响,导致页面产生了分享设置无效的bug。定位到definePageConfig正则会匹配注释的代码,解决方案为:执行前过滤掉注释代码内容。
ps:可能还有别的宏函数也有类似问题,此处只修复了definePageConfig。

这个 PR 是什么类型? (至少选择一个)

  • [√ ] 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • [ √ ] 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

  • Improvements
    • 在解析页面配置前移除源代码注释并采用更宽松的匹配规则,提升对含注释、不同空白和异常写法页面配置的识别能力与兼容性。
  • Bug Fixes
    • 修复因注释或格式差异导致页面配置未被识别或误判的问题,减少漏识别情况,提升稳定性。

@coderabbitai
Copy link

coderabbitai bot commented Aug 16, 2025

Walkthrough

packages/taro-helper/src/utils.ts 中新增并导出 removeComments(code: string): stringreadSFCPageConfig 先对 SFC 源码调用该函数得到 codeWithoutComments,再基于清洗后的代码使用更新后的 definePageConfig 正则进行匹配;控制流与错误处理未变更。

Changes

Cohort / File(s) 摘要
Utils 注释清理与配置解析调整
packages/taro-helper/src/utils.ts
新增并导出 removeComments(code: string),支持移除单行(保留 URL 等协议)与多行注释;readSFCPageConfig 在匹配前使用 removeComments 得到 codeWithoutComments;更新 definePageConfig 正则为 /\bdefinePageConfig\s*\(\s*\{[\s\S]*?\}\s*\)/g,并添加注释说明“先移除注释,再应用正则表达式”。

Sequence Diagram(s)

sequenceDiagram
  participant Reader as readSFCPageConfig
  participant Cleaner as removeComments
  participant Matcher as RegExp (definePageConfig)

  Reader->>Cleaner: removeComments(sourceCode)
  Cleaner-->>Reader: codeWithoutComments
  Reader->>Matcher: match(codeWithoutComments)
  Matcher-->>Reader: matchResult / null
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

小兔轻啃旧注释,斑斓语句露真心。
先扫干净再寻影,配置现形不迷津。
代码净,步子轻,喵——不是我,是兔在庆。 🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between dee08e6 and 7a2d5e2.

📒 Files selected for processing (1)
  • packages/taro-helper/src/utils.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/taro-helper/src/utils.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
  • GitHub Check: Build Rust WASM / stable - wasm32-wasi
  • GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

@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)
packages/taro-helper/src/utils.ts (2)

585-592: 正则去注释会误删字符串里的 “http://”“https://” 等内容,存在潜在回归风险

当前实现用 /\/\/.*$/gm/\/\*[\s\S]*?\*\//g 直接删注释,会把字符串/模板字符串/正则字面量中的 ///* ... */ 一并清掉。典型场景:definePageConfig 配置里若包含 http:// URL,则该行从 // 起被整行删除,可能导致括号或引号缺失,进而匹配不到 definePageConfig 或解析失败。这属于隐患型问题,建议加一道保护,至少不要删除协议前缀导致的 //

建议的最小修正(保留协议型 :// 之前的 //),在本段内可直接替换:

 export function removeComments(code: string): string {
-  // 移除单行注释
-  code = code.replace(/\/\/.*$/gm, '')
-  // 移除多行注释
-  code = code.replace(/\/\*[\s\S]*?\*\//g, '')
-  return code
+  // 移除单行注释(避免误删字符串中的协议前缀,如 http://、https://、ws://、ftp://、file://)
+  code = code.replace(/\/\/.*$/gm, (match: string, ...args: any[]) => {
+    const offset = args[args.length - 2] as number
+    const src = args[args.length - 1] as string
+    // 前一个字符为 ':' 大概率是协议场景,跳过删除
+    if (offset > 0 && src[offset - 1] === ':') return match
+    return ''
+  })
+  // 移除多行注释
+  code = code.replace(/\/\*[\s\S]*?\*\//g, '')
+  return code
 }

补充建议与验证点:

  • 覆盖用例:含有 http:///https:///ws:// 等 URL 的 definePageConfig 能正常匹配与解析。
  • 可进一步(后续 PR)用状态机或引入 strip-comments 等库,实现“识别字符串/模板字符串/正则字面量后再去注释”的更稳妥版本。如需我来补充实现与测试,请告知。

599-603: 修复方向正确:先移除注释,再匹配 definePageConfig,能规避注释命中导致的误处理

这一步正好解决了 PR 中描述的 false-positive 问题,思路合理、影响面可控。为提升健壮性,正则可以稍作放宽以容忍空白与换行,减少格式差异导致的漏匹配:

可以在本段微调正则,增强容错性(不改变原有语义):

-  const dpcReg = /definePageConfig\(\{[\w\W]+?\}\)/g
+  // 容忍空白与换行、保证单词边界,避免如“definePageConfig ({ ... })”之类的漏匹配
+  const dpcReg = /\bdefinePageConfig\s*\(\s*\{[\s\S]*?\}\s*\)/g

建议补充两类单测(或样例)验证:

  • 注释中包含 definePageConfig({...}):应不匹配。
  • 实际代码中包含 definePageConfig({...}) 且对象里有包含 http:// 的字符串:应能匹配并被后续 Babel 解析成功。
    如需我来补充测试用例,我可以直接给出用 Jest 的最小可复现场景。
📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between b3127a8 and a98a157.

📒 Files selected for processing (1)
  • packages/taro-helper/src/utils.ts (1 hunks)

@uicheung
Copy link

优秀,优秀,优秀

code = code.replace(/\/\*[\s\S]*?\*\//g, '')
return code
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不清楚历史,代码解析有关的逻辑为啥不是AST处理的呢

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赞同用AST处理。同样不清楚历史业务,所以没有做过大的改动,仅按照原有的逻辑去修正了问题。

This was referenced Aug 23, 2025
@codecov
Copy link

codecov bot commented Aug 27, 2025

Codecov Report

❌ Patch coverage is 9.09091% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.04%. Comparing base (1d2aead) to head (7a2d5e2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
packages/taro-helper/src/utils.ts 9.09% 9 Missing and 1 partial ⚠️

❌ Your project check has failed because the head coverage (55.04%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18195      +/-   ##
==========================================
- Coverage   55.06%   55.04%   -0.02%     
==========================================
  Files         416      416              
  Lines       21563    21572       +9     
  Branches     5286     5274      -12     
==========================================
+ Hits        11873    11874       +1     
- Misses       8161     8173      +12     
+ Partials     1529     1525       -4     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 59.87% <ø> (ø)
taro-web 53.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-helper/src/utils.ts 21.93% <9.09%> (-0.26%) ⬇️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This was referenced Aug 30, 2025
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.

3 participants