taro icon indicating copy to clipboard operation
taro copied to clipboard

chore: shared no enum

Open ianzone opened this issue 6 months ago • 4 comments

这个 PR 做了什么? (简要描述所做更改) 由于 enum 并不是 js 的原语,不同构建工具转译 enum 时可能产生不一致的结果,故应该避免使用

参考:

  1. https://www.youtube.com/watch?v=jjMbPt_H3RQ
  2. https://github.com/rolldown/tsdown/issues/156
  3. https://github.com/NervJS/taro/pull/17746#issuecomment-2908076979

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

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

这个 PR 涉及以下平台:

  • [ ] 所有小程序
  • [ ] 微信小程序
  • [ ] 支付宝小程序
  • [ ] 百度小程序
  • [ ] 字节跳动小程序
  • [ ] QQ 轻应用
  • [ ] 京东小程序
  • [ ] 快应用平台(QuickApp)
  • [ ] Web 平台(H5)
  • [ ] 移动端(React-Native)
  • [ ] 鸿蒙(harmony)

Summary by CodeRabbit

  • 样式
    • 优化多处错误提示信息的中英文标点和空格,使输出更符合中文习惯。
    • 统一日志、注释及错误提示的空格和标点格式,提升可读性。
  • 测试
    • 更新部分测试用例的断言内容和描述,提升一致性和可读性。
  • 新特性
    • Jest 测试环境新增多项全局标志位,增强测试配置能力。
  • 重构
    • 平台类型与钩子类型由枚举重构为常量对象及类型别名,类型定义更为精确。
    • 快捷方式相关类型由枚举改为常量对象及类型别名,导出方式调整。
    • 新增通用类型工具 ValueOf<T>,提升类型表达能力。
    • 统一导出方式,扩展共享模块导出内容。
    • 节点类型由枚举改为常量对象及类型别名,相关接口类型更新,提升类型安全和代码清晰度。
    • 生命周期类型由枚举改为常量对象及类型别名,提升类型表达准确性。
    • 适配器类型由枚举改为常量对象及类型别名,提升类型表达准确性。
    • 枚举类型改为常量对象及类型别名的还有样式预处理语言相关类型,提升类型表达准确性。
    • 生命周期、上下文动作、DOM 节点类型、MutationRecord 类型等多处枚举改为常量对象,修正拼写错误,提升类型安全和代码一致性。
    • 修正函数名拼写错误,提升代码规范性。
    • 统一参数命名风格,将多个函数参数名由 reactdom 修改为 reactDOM,提升代码一致性。

ianzone avatar Jun 08 '25 12:06 ianzone

"""

Walkthrough

本次更改主要包括将部分 TypeScript 枚举(enum)重构为常量对象及联合类型,调整相关类型声明;同时对多处中文错误提示信息进行了标点和格式微调。此外,部分测试代码进行了命名、断言和错误处理的规范化,Jest 配置新增了若干全局标志。还对共享模块的导出方式进行了调整,新增了实用类型 ValueOf

Changes

文件/分组 变更摘要
packages/shared/src/constants.ts PLATFORM_TYPE 从枚举改为常量对象及联合类型,调整 quickappquick
packages/shared/src/runtime-hooks.ts HOOK_TYPE 从枚举改为常量对象及联合类型,修正注释格式和标点。
packages/shared/src/shortcuts.ts 修正注释拼写错误,Shortcuts 枚举注释中“Attrtibutes”改为“Attributes”。
packages/shared/src/types.ts 新增实用类型 ValueOf<T>,用于提取对象值的联合类型。
packages/shared/src/index.ts 修改导出方式,shortcuts 改为全部导出,新增 types 全部导出。
packages/taro-webpack5-prebundle/src/index.ts 注释格式微调,无逻辑变更。
packages/taro-h5/jest.config.ts Jest 配置新增多个全局布尔标志。
packages/taro-cli/src/tests/info.spec.ts
packages/taro-cli/src/tests/update.spec.ts
测试用例预期错误信息的标点从英文感叹号改为全角感叹号,并调整空格。
packages/taro-cli/src/tests/inspect.spec.ts 测试代码重构:引入常量、断言一致性、用词中文化、错误输出规范。
packages/taro-cli/src/presets/commands/build.ts
packages/taro-cli/src/presets/commands/info.ts
packages/taro-cli/src/presets/commands/inspect.ts
packages/taro-cli/src/presets/commands/update.ts
错误提示信息调整:增加空格,英文感叹号改为全角感叹号。
packages/taroize/src/wxml.ts NodeType 枚举改为常量对象及联合类型,更新相关接口类型声明,函数重命名,调整格式和注释。
packages/taroize/src/template.ts 函数重命名修正(createWxmlVistorcreateWxmlVisitor),注释及日志格式调整。
packages/taroize/tests/template.test.ts 测试断言中错误信息的冒号由半角改为全角。
packages/taro-with-weapp/src/lifecycle.ts TaroLifeCycles 枚举改为常量对象及联合类型,新增类型别名。
packages/taro-transformer-wx/src/adapter.ts Adaptersconst enum 改为常量对象及联合类型。
packages/taro-transformer-wx/src/lifecycle.ts Lifecycle 枚举改为常量对象及联合类型,新增类型别名。
packages/taro-transformer-wx/src/types.ts 新增实用类型 ValueOf<T>,用于提取对象值的联合类型。
packages/taro-vite-runner/src/harmony/postcss/constants.ts 将三个 const enum 替换为常量对象及联合类型,新增对应类型别名。
packages/taro-vite-runner/src/harmony/postcss/resolvers.ts 修改 loadPreprocessor 函数重载参数类型为常量对象成员类型。
packages/taro-runtime/src/bom/history.ts 修正事件名拼写错误,将 DESTORY 改为 DESTROY
packages/taro-runtime/src/bom/location.ts 修正事件名拼写错误,将 DESTORY 改为 DESTROY
packages/taro-runtime/src/bom/window.ts 修正事件名拼写错误,将 DESTORY 改为 DESTROY
packages/taro-runtime/src/constants/index.ts CONTEXT_ACTIONS 枚举改为常量对象,修正 DESTORYDESTROY,新增类型别名。
packages/taro-runtime/src/dom-external/mutation-observer/record.ts MutationRecordTypeconst enum 改为常量对象及联合类型。
packages/taro-runtime/src/dom/anchor-element.ts 将锚点元素属性名从 const enum 改为常量对象,新增类型别名。
packages/taro-runtime/src/dom/node_types.ts NodeTypeconst enum 改为常量对象及联合类型。
packages/taro-runtime/src/dsl/common.ts 修正生命周期卸载时触发的上下文动作名称拼写错误。
packages/taro-service/src/utils/constants.ts PluginType 从枚举改为常量对象及联合类型。
packages/taro-framework-react/src/runtime/connect-native.ts 修正注释格式,参数命名由 reactdom 改为 reactDOM
packages/taro-platform-harmony-cpp/src/runtime/framework/native-page.ts 修正注释,参数命名由 reactdom 改为 reactDOM,修正拼写错误。
packages/taro-platform-harmony-cpp/src/runtime/framework/page.ts 修正卸载生命周期事件名拼写错误。
packages/taro-platform-harmony-cpp/types/runtime.d.ts 移除 NodeType 枚举中的 ENTITY_REFERENCE_NODE 成员。
packages/taro-platform-harmony/src/runtime-ets/dom/node.ts NodeType 枚举改为常量对象及联合类型,更新构造函数参数类型。
packages/taro-platform-harmony/src/runtime-framework/react/native-page.ts 修正注释,参数命名由 reactdom 改为 reactDOM,调整字符串转换。
packages/taro-platform-harmony/src/runtime-framework/react/page.ts 修正卸载生命周期事件名拼写错误。
packages/taro-platform-harmony/src/runtime-framework/solid/page.ts 修正卸载生命周期事件名拼写错误。
packages/taro-plugin-mini-ci/src/index.ts EnumActionconst enum 改为常量对象及联合类型,修正标点。
packages/taro-rn-style-transformer/src/transforms/index.ts ProcessTypesconst enum 改为常量对象及联合类型,修正注释格式。
packages/taro-rn-style-transformer/src/types/index.ts ResolveLogLevelEnumconst enum 改为常量对象及联合类型。
packages/taro-runtime/src/tests/location.spec.js 修正注释拼写错误,将 DESTORY 改为 DESTROY
packages/taro-cli-convertor/src/util/constants.ts Constantsconst enum 改为常量对象及联合类型。
packages/taro-cli/src/create/page.ts ConfigModificationState 枚举改为常量对象及联合类型,调整日志标点。
packages/taro-cli/src/presets/commands/create.ts createTemplateTypeEnumconst enum 改为常量对象,调整注释和命令描述。
packages/taro-components-react/src/components/pull-down-refresh/index.tsx PullDownStateINDICATOR 从枚举改为常量对象及联合类型,更新类型声明。
packages/taro-platform-harmony/src/runtime-ets/dom/element/element.ts 注释格式调整,访问 eventSource 时将数字键转换为字符串。
packages/taro-cli/src/util/createPage.ts state 变量添加显式类型注解,无逻辑变更。

Sequence Diagram(s)

sequenceDiagram
    participant 用户
    participant CLI
    participant 文件系统

    用户->>CLI: 执行命令(如 info、inspect、build、update)
    CLI->>文件系统: 检查配置文件是否存在
    alt 配置文件不存在
        CLI->>用户: 输出“项目配置文件 xxx 不存在!”
        CLI->>CLI: 退出进程
    else 配置文件存在
        CLI->>CLI: 正常执行后续逻辑
    end

Possibly related PRs

  • NervJS/taro#17748: 该 PR 同样修改了 PLATFORM_TYPE 的声明,新增了 ASCF 平台类型,与本次将 PLATFORM_TYPE 从枚举改为常量对象的改动存在代码层面关联。

Suggested reviewers

  • tutuxxx
  • yoyo837

Poem

🐰
新旧枚举换新颜,
常量对象显身段。
中文标点更地道,
测试用例也完善。
兔子挥爪庆更新,
代码整洁心欢畅!

"""


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e0c43222904cac51bd5b71bdc52523df6f8ae784 and 6c7af01feaa84719bf8efe7b637bfa825eb55365.

⛔ Files ignored due to path filters (1)
  • packages/taroize/__tests__/__snapshots__/wxml.test.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (1)
  • packages/taroize/__tests__/template.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/taroize/tests/template.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings
🧪 Generate Unit Tests
  • [ ] Create PR with Unit Tests
  • [ ] Post Copyable Unit Tests in 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

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

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @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.

coderabbitai[bot] avatar Jun 08 '25 12:06 coderabbitai[bot]

Codecov Report

Attention: Patch coverage is 80.55556% with 7 lines in your changes missing coverage. Please review.

Project coverage is 55.84%. Comparing base (1f097d5) to head (e0c4322).

Files with missing lines Patch % Lines
packages/taroize/src/wxml.ts 81.25% 3 Missing :warning:
packages/taro-runtime/src/bom/window.ts 33.33% 2 Missing :warning:
packages/taro-cli/src/presets/commands/build.ts 0.00% 1 Missing :warning:
packages/taroize/src/template.ts 75.00% 1 Missing :warning:

:x: Your patch check has failed because the patch coverage (66.66%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage. :x: Your project check has failed because the head coverage (55.84%) 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   #17839      +/-   ##
==========================================
- Coverage   55.90%   55.84%   -0.07%     
==========================================
  Files         417      419       +2     
  Lines       21693    21663      -30     
  Branches     5385     5363      -22     
==========================================
- Hits        12127    12097      -30     
  Misses       7930     7930              
  Partials     1636     1636              
Flag Coverage Δ
taro-cli 72.42% <75.00%> (ø)
taro-runtime 59.83% <66.66%> (-0.04%) :arrow_down:
taro-web 53.19% <ø> (ø)

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

Files with missing lines Coverage Δ
packages/taro-cli-convertor/src/util/constants.ts 100.00% <100.00%> (ø)
packages/taro-cli/src/presets/commands/info.ts 85.71% <100.00%> (ø)
packages/taro-cli/src/presets/commands/inspect.ts 100.00% <100.00%> (ø)
packages/taro-cli/src/presets/commands/update.ts 87.80% <100.00%> (ø)
packages/taro-helper/src/constants.ts 100.00% <100.00%> (ø)
.../taro-rn-style-transformer/src/transforms/index.ts 100.00% <100.00%> (ø)
...kages/taro-rn-style-transformer/src/types/index.ts 100.00% <100.00%> (ø)
packages/taro-runtime/src/bom/history.ts 40.00% <100.00%> (ø)
packages/taro-runtime/src/bom/location.ts 16.56% <ø> (ø)
packages/taro-runtime/src/constants/index.ts 100.00% <100.00%> (ø)
... and 5 more

... and 5 files with indirect coverage changes

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

codecov[bot] avatar Jun 08 '25 12:06 codecov[bot]

@tutuxxx 项目中还有其他包使用了 enum,可否确认一下前进方向是否正确?

ianzone avatar Jun 09 '25 08:06 ianzone

@yoyo837 Hi,有空的话可否审一下。66.66% of diff hit 主要是由于改了函数名更新快照导致的,原来的函数名有拼写错误

ianzone avatar Jun 15 '25 17:06 ianzone

依赖:https://github.com/NervJS/taro/pull/17928

ianzone avatar Jul 01 '25 12:07 ianzone