Skip to content

fix: taro default import error #3057

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

Merged
merged 1 commit into from
Mar 6, 2025

Conversation

oasis-cloud
Copy link
Collaborator

@oasis-cloud oasis-cloud commented Mar 6, 2025

Summary by CodeRabbit

  • 重构
    • 优化了获取设备、窗口及应用基础信息的实现方式,使系统信息获取逻辑更加清晰高效。
    • 简化了平台环境检测逻辑,调整后的实现减少了不必要的依赖,提升了代码可读性与维护性。

Copy link

coderabbitai bot commented Mar 6, 2025

Walkthrough

本次 PR 调整了代码中对 Taro 库的引入方式。在 src/utils/get-system-info.ts 中,将原先通过 Taro 命名空间直接调用的方式替换为通过别名调用,并更新了三个导出函数的实现。在 src/utils/platform-taro.ts 中,从默认导入改为直接导入 getEnv,并更新了环境检测函数的调用方式。这些修改旨在提升代码可读性并减少对 Taro 命名空间的重复调用。

Changes

文件 变更概况
src/utils/get-system-info.ts 修改导入语句,使用 canIUsetaroGetDeviceInfotaroGetWindowInfotaroGetAppBaseInfo 的别名;更新 getDeviceInfogetWindowInfogetAppBaseInfo 函数调用。
src/utils/platform-taro.ts 将默认导入 Taro 替换为直接导入 getEnv;更新 harmonywebminiprogram 函数中对环境检测的调用方式。

Possibly related PRs

Suggested reviewers

  • xiaoyatong
  • irisSong

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 6a23255 and deaf40a.

📒 Files selected for processing (2)
  • src/utils/get-system-info.ts (2 hunks)
  • src/utils/platform-taro.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/utils/platform-taro.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (5)
src/utils/get-system-info.ts (5)

1-7: 优化了 Taro 库的导入方式

从使用默认导入并通过 Taro 命名空间访问方法,改为直接导入所需的具体函数。这种方式更加清晰,并且可能解决了与默认导入相关的错误问题。


17-17: 改进了函数实现,提高代码清晰度

将原来的 Taro.canIUseTaro.getDeviceInfo 调用替换为直接使用导入的函数 canIUsetaroGetDeviceInfo,减少了代码冗余,提高了可读性。


25-25: 改进了函数实现,保持一致性

getDeviceInfo 函数类似,将 Taro.canIUseTaro.getWindowInfo 替换为导入的函数,保持了代码风格的一致性。


33-33: 改进了函数实现,遵循相同模式

与其他两个函数保持一致,优化了 getAppBaseInfo 的实现方式,使整个文件的代码风格更加统一。


1-7:

✅ Verification successful

考虑是否仍需要默认导入 Taro

注意到虽然改为使用命名导入,但仍然保留了 Taro 的默认导入。这似乎主要用于类型定义(如 Taro.getWindowInfo.Result)。如果可能,考虑直接导入这些类型定义,完全消除对默认导入的依赖。


🏁 Script executed:

#!/bin/bash
# 检查 Taro 默认导入是否在其他地方被使用
grep -r "Taro\." --include="*.ts" --include="*.tsx" src/utils/get-system-info.ts | grep -v "extends\|interface\|type\|\/\/"

Length of output: 348


关注 Taro 默认导入在文件中的用途

经过验证,文件 src/utils/get-system-info.ts 中 Taro 默认导入主要出现在 JSDoc 注释中,用于引用类型(例如 Taro.getWindowInfo.ResultTaro.getAppBaseInfo.Result)。建议评估是否能直接通过类型导入代替默认导入,以消除不必要的默认依赖,如果确认仅为类型引用,则可使用类似 import type 的方式来引入所需类型。

  • 在 JSDoc 注释中使用 Taro 的类型引用(如 Taro.getWindowInfo.ResultTaro.getAppBaseInfo.Result)是目前唯一的用途
  • 考虑直接导入相关类型定义(例如通过 import type { ... }),从而移除默认导入 Taro
  • 若做出修改,请确保更新所有相关注释,使项目类型定义保持一致
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • 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.
  • @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.

@github-actions github-actions bot added action:review This PR needs more reviews (less than 2 approvals) 3.x Target branch 3.x labels Mar 6, 2025
Copy link

codecov bot commented Mar 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.06%. Comparing base (6a23255) to head (deaf40a).
Report is 3 commits behind head on feat_v3.x.

Additional details and impacted files
@@            Coverage Diff             @@
##           feat_v3.x    #3057   +/-   ##
==========================================
  Coverage      86.06%   86.06%           
==========================================
  Files            275      275           
  Lines          18234    18234           
  Branches        2751     2751           
==========================================
  Hits           15694    15694           
  Misses          2535     2535           
  Partials           5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

return Taro.canIUse('getDeviceInfo')
? Taro.getDeviceInfo()
: Taro.getSystemInfoSync()
return canIUse('getDeviceInfo') ? taroGetDeviceInfo() : getSystemInfoSync()
}

/**
* 获取窗口信息,兼容新旧 API
* @returns {Taro.getWindowInfo.Result} 窗口信息
*/
export const getWindowInfo = (): Taro.getWindowInfo.Result => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个文件里是不是都用taroGetxxx就行了,不需要引入Taro了

@oasis-cloud oasis-cloud merged commit f548c99 into jdf2e:feat_v3.x Mar 6, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants