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

feat: support replacing the css paths imported from a specified package #2675

Merged
merged 3 commits into from
Nov 1, 2024

Conversation

oasis-cloud
Copy link
Collaborator

@oasis-cloud oasis-cloud commented Oct 29, 2024

image
替换为

import '@nutui/jdesign-icons-react-taro/dist/style_iconfont.css'

Summary by CodeRabbit

  • 新功能

    • 更新了 replace 函数以支持更复杂的库路径替换。
    • 新增 replace-icon-css.test.ts 测试文件,验证图标替换功能。
  • 测试

    • 修改了测试配置,新增 sourceLibrary 属性以支持多个库的图标替换。
    • 引入新的 Vitest 配置文件以优化测试设置。
  • 文档

    • 在代码中添加了注释,以提高可读性和理解性。

Copy link

coderabbitai bot commented Oct 29, 2024

Walkthrough

此次更改主要集中在replace-icons.ts文件中的replace函数,新增了条件块以处理sourceLibrarypath.node.source.value不匹配的情况。通过循环检查path.node.source.value是否包含sourceLibrary中的库名,并相应地替换导入语句中的库名。此外,replace-case.test.ts文件中添加了sourceLibrary属性以支持多个库的图标替换。还新增了测试文件replace-icon-css.test.ts以验证图标替换的功能,并引入了Vitest配置文件以支持测试环境的设置。

Changes

文件路径 更改摘要
packages/nutui-replace-icons/src/replace-icons.ts 修改replace函数,新增条件块处理sourceLibrary与导入路径匹配的情况,添加注释以澄清代码目的。
packages/nutui-replace-icons/test/replace-case.test.ts replaceIcons插件配置中添加sourceLibrary属性,支持多个库的图标替换。更新replaceIcons方法签名。
packages/nutui-replace-icons/test/replace-icon-css.test.ts 新增测试文件,验证replaceIcons函数的图标替换功能,使用Babel进行AST转换并进行快照比较。
packages/nutui-replace-icons/vitest.config.ts 新增Vitest配置文件,定义测试配置对象,准备进一步自定义测试参数。

Possibly related PRs

Poem

🐇 在草地上跳跃,欢声笑语,
图标替换,真是乐趣无穷。
新增条件,逻辑更清晰,
测试齐全,确保无误。
兔子们欢呼,代码如歌,
让我们一起,享受这变化! 🌼


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e6d3db2 and a94c9be.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (1)
  • packages/nutui-replace-icons/src/replace-icons.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/nutui-replace-icons/src/replace-icons.ts

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

@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: 3

🧹 Outside diff range and nitpick comments (6)
packages/nutui-replace-icons/test/replace-case.test.ts (2)

6-6: 配置结构清晰,建议添加注释说明

sourceLibrary 数组的引入支持了多库图标替换,这是个很好的改进。建议为配置对象添加 JSDoc 注释,说明每个配置项的用途和预期值。

建议添加如下注释:

const plugin = replaceIcons({
+ /**
+  * @property {string[]} sourceLibrary - 源图标库列表
+  * @property {string} targetLibrary - 目标替换库
+  * @property {Record<string, string>} iconMappings - 图标映射关系
+  */
  sourceLibrary: ['@nutui/icons-react', '@nutui/icons-react-taro'],
  targetLibrary: '@test/aa',
  iconMappings: {
    Loading: 'Star',
  },
})

Line range hint 19-26: 建议增加更多测试场景

当前测试用例仅覆盖了基本的图标替换场景。考虑到 PR 的目标是支持 CSS 路径替换,建议添加以下测试场景:

  1. CSS 路径替换的测试用例
  2. 多源库并存时的替换逻辑
  3. 错误处理场景(无效的库名、找不到映射等)

建议添加如下测试用例:

// CSS 路径替换测试
const cssImportCase = `
import '@nutui/icons-react/dist/style.css'
import { Loading } from '@nutui/icons-react'
`

// 多源库测试
const multiSourceCase = `
import { Loading } from '@nutui/icons-react'
import { Star } from '@nutui/icons-react-taro'
`

// 错误处理测试
const errorCase = `
import { NonExistentIcon } from '@nutui/icons-react'
`
packages/nutui-replace-icons/test/replace-icon-css.test.ts (2)

5-8: 建议添加类型定义

插件配置对象缺少类型定义,建议添加接口定义以提高代码的可维护性。

建议添加如下类型定义:

interface PluginOptions {
  sourceLibrary: string[];
  targetLibrary: string;
  iconMappings?: Record<string, string>;
}

const plugin = replaceIcons({
  sourceLibrary: ['@nutui/icons-react', '@nutui/icons-react-taro'],
  targetLibrary: '@test/aa',
} as PluginOptions)

14-18: 建议增加更多测试用例

当前测试用例只覆盖了基本场景,建议添加以下场景的测试:

  1. 无效的库名称
  2. 空字符串输入
  3. 多行导入语句
  4. 非 CSS 文件导入

需要我帮您生成这些测试用例的代码吗?

packages/nutui-replace-icons/src/replace-icons.ts (2)

Line range hint 9-20: 建议完善代码注释

当前注释过于简单,建议补充以下信息:

  • 函数的具体作用和处理逻辑
  • 参数的数据结构说明
  • 替换规则的示例

建议添加如下注释:

-// import {Loading} from '@nutui/icons-react'
+/**
+ * 处理直接从源库导入的情况
+ * 例如: import {Loading} from '@nutui/icons-react'
+ * @param path - ImportDeclaration 节点
+ * @example
+ * 输入: import {Loading} from '@nutui/icons-react'
+ * 输出: import {Loading} from '@custom/icons'
+ */

建议增加参数验证和错误处理机制

建议在 replaceIcons 函数中添加以下增强:

  1. 在函数开始处添加参数验证:
if (!options.sourceLibrary?.length) {
  throw new Error('sourceLibrary 不能为空数组');
}
if (!options.targetLibrary) {
  throw new Error('targetLibrary 不能为空');
}
if (options.iconMappings && typeof options.iconMappings !== 'object') {
  throw new Error('iconMappings 必须是对象类型');
}
  1. 在 visitor 中添加错误处理:
ImportDeclaration(path) {
  try {
    // 现有的替换逻辑
  } catch (error) {
    console.error('处理导入声明时发生错误:', error);
    throw error;
  }
}

根据测试用例的使用情况,这些验证和错误处理对于提高代码的健壮性很有必要。

🔗 Analysis chain

Line range hint 1-43: 建议增加错误处理和类型验证

当前代码缺少必要的参数验证和错误处理机制。

建议添加以下增强:

  1. 参数验证
function validateOptions(options: IOptions): void {
  if (!Array.isArray(options.sourceLibrary)) {
    throw new Error('sourceLibrary 必须是数组类型');
  }
  if (typeof options.targetLibrary !== 'string') {
    throw new Error('targetLibrary 必须是字符串类型');
  }
}
  1. 错误处理
try {
  // 现有的替换逻辑
} catch (error) {
  console.error('图标替换过程中发生错误:', error);
  // 可以选择继续处理或抛出错误
}

运行以下脚本检查是否有其他使用此功能的地方:


Let me check the test files to understand how this function is being used and if there's error handling in place.


Let me check the type definition to understand the complete interface structure.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索项目中使用 replaceIcons 的位置
rg "replaceIcons" --type ts

Length of output: 658


Script:

#!/bin/bash
# Check test files content
cat packages/nutui-replace-icons/test/replace-case.test.ts
cat packages/nutui-replace-icons/test/replace-icon-css.test.ts

Length of output: 1574


Script:

#!/bin/bash
# Check type definition
cat packages/nutui-replace-icons/src/type.ts

Length of output: 169

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f313eb3 and e6d3db2.

⛔ Files ignored due to path filters (1)
  • packages/nutui-replace-icons/test/__snapshots__/replace-icon-css.test.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • packages/nutui-replace-icons/src/replace-icons.ts (2 hunks)
  • packages/nutui-replace-icons/test/replace-case.test.ts (1 hunks)
  • packages/nutui-replace-icons/test/replace-icon-css.test.ts (1 hunks)
  • packages/nutui-replace-icons/vitest.config.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/nutui-replace-icons/vitest.config.ts
🔇 Additional comments (2)
packages/nutui-replace-icons/test/replace-case.test.ts (1)

Line range hint 1-33: 验证测试覆盖率

建议检查当前测试文件的覆盖率情况,确保对新增功能有足够的测试覆盖。

✅ Verification successful

Let me check the test coverage configuration and test cases in more detail.

The results will help me understand:

  1. If there's proper coverage configuration in place
  2. What other test scenarios are covered in the other test file
  3. Whether the current test coverage is adequate for the functionality

This will allow me to provide a more informed response about the test coverage situation.


Let me check the actual test implementation and coverage configuration more specifically.

This will help me understand:

  1. The complete functionality being tested
  2. The actual test assertions through snapshots
  3. Whether current test cases cover all the main functionality paths

测试覆盖率已经足够完整

当前测试用例已经很好地覆盖了 replaceIcons 的主要功能:

  • 替换图标组件导入的包名和组件名映射
  • 替换样式文件导入路径
  • 包含了正向和边界场景测试

测试配置也已正确设置:

  • 使用 vitest 进行测试
  • 启用了代码覆盖率统计
  • 配置了 HTML 报告输出
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查测试覆盖率相关配置
rg -l "coverage" .

# 检查是否有其他相关测试文件
fd -e test.ts -e spec.ts

Length of output: 208


Script:

#!/bin/bash
# Check coverage configuration in package.json and vite.config.ts
cat package.json | jq '.scripts + .vitest'
cat vite.config.ts

# Check test cases content
cat packages/nutui-replace-icons/test/replace-icon-css.test.ts

Length of output: 7874


Script:

#!/bin/bash
# Check the source file being tested
cat packages/nutui-replace-icons/src/replace-icons.ts

# Check test snapshots
cat packages/nutui-replace-icons/test/__snapshots__/replace-case.test.ts.snap
cat packages/nutui-replace-icons/test/__snapshots__/replace-icon-css.test.ts.snap

Length of output: 2227

packages/nutui-replace-icons/test/replace-icon-css.test.ts (1)

1-4: 导入语句看起来不错!

导入声明清晰明确,包含了所有必要的依赖。

Copy link

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (next@f313eb3). Learn more about missing BASE report.
Report is 2 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2675   +/-   ##
=======================================
  Coverage        ?   84.07%           
=======================================
  Files           ?      217           
  Lines           ?    17830           
  Branches        ?     2608           
=======================================
  Hits            ?    14991           
  Misses          ?     2834           
  Partials        ?        5           

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

@oasis-cloud oasis-cloud merged commit b97e0a5 into jdf2e:next Nov 1, 2024
5 checks passed
@oasis-cloud oasis-cloud deleted the feat/replace-icon-iconfont branch November 1, 2024 07:16
@coderabbitai coderabbitai bot mentioned this pull request Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant