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

chore: add lint-staged and husky to package.json #741

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

coding-ice
Copy link
Contributor

@coding-ice coding-ice commented Oct 24, 2024

Summary by CodeRabbit

  • 新功能

    • 在提交前钩子中集成了代码质量检查,确保提交的代码符合项目的格式规范。
    • 新增了准备脚本,自动运行Husky和Dumi设置。
    • 添加了对lint-staged的支持,以便在提交时自动格式化代码。
  • 依赖更新

    • 添加了huskylint-staged作为开发依赖。

Copy link

coderabbitai bot commented Oct 24, 2024

Walkthrough

在此次更改中,.husky/pre-commit 文件中新增了 lint-staged 的条目,以便在提交前自动执行对暂存文件的 linting 检查。此外,package.json 文件也进行了多项更新,包括新增准备阶段的脚本、添加 huskylint-staged 的依赖项,以及配置 lint-staged 以便使用 Prettier 格式化文件。这些更改旨在提升代码质量和项目的可维护性。

Changes

文件 更改摘要
.husky/pre-commit 新增 lint-staged 条目以集成 linting 检查到预提交钩子中。
package.json - 新增脚本 "prepare": "husky && dumi setup"
- 新增依赖 "husky": "^9.1.6"
- 新增依赖 "lint-staged": "^15.2.10"
- 新增配置 "lint-staged": { "*": "prettier --write --ignore-unknown" }

Poem

在月光下,兔子轻跳,
新增工具,代码更好。
提交前,检查一遍,
格式整齐,心欢畅。
让我们一起,欢庆这变换! 🐇✨


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

codecov bot commented Oct 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.59%. Comparing base (d7bb0e0) to head (981884d).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #741   +/-   ##
=======================================
  Coverage   99.59%   99.59%           
=======================================
  Files          27       27           
  Lines         736      736           
  Branches      200      202    +2     
=======================================
  Hits          733      733           
  Misses          3        3           

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

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

🧹 Outside diff range and nitpick comments (1)
package.json (1)

70-70: 依赖版本锁定建议

新增的开发依赖使用了 ^ 版本范围,这可能在团队成员之间造成版本不一致。

建议:

  1. 考虑使用确切版本号以确保所有开发环境的一致性
  2. 确保在 package-lock.json 或 yarn.lock 中锁定具体版本
-    "husky": "^9.1.6",
-    "lint-staged": "^15.2.10",
+    "husky": "9.1.6",
+    "lint-staged": "15.2.10",

Also applies to: 72-72

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d7bb0e0 and 981884d.

📒 Files selected for processing (2)
  • .husky/pre-commit (1 hunks)
  • package.json (3 hunks)
🔇 Additional comments (2)
package.json (2)

43-44: 确认 prepare 脚本的执行顺序

prepare 脚本将在 npm install 后自动执行。请确认 huskydumi setup 的执行顺序是否符合预期,因为 dumi setup 可能依赖于其他初始化步骤。

建议考虑将 husky 的初始化单独放在 postinstall 脚本中:

 "scripts": {
   "test": "rc-test",
-  "prepare": "husky && dumi setup"
+  "prepare": "dumi setup",
+  "postinstall": "husky"
 }

83-83: 格式一致性更新

添加的尾随逗号有助于保持 JSON 格式的一致性。

@@ -0,0 +1 @@
lint-staged
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

Based on the results, I can now provide a final response as we have confirmed:

  1. The pre-commit hook lacks executable permissions
  2. The husky setup is incomplete (missing .husky/_ directory)
  3. The package.json has husky and lint-staged configurations

需要完善 Git Hook 配置和权限设置

pre-commit 钩子脚本存在以下问题需要修正:

  • 文件内容需要更新:
+#!/usr/bin/env sh
+. "$(dirname -- "$0")/_/husky.sh"
+
-lint-staged
+npx lint-staged
  • 需要执行以下命令来设置正确的文件权限:
chmod +x .husky/pre-commit
  • Husky 设置不完整,需要重新执行初始化:
npm run prepare

这些更改将确保:

  1. 脚本能被正确解释执行
  2. Git Hook 具有必要的执行权限
  3. Husky 的依赖文件被正确安装
🔗 Analysis chain

需要完善 shell 脚本的基本要求

这个 pre-commit 钩子脚本缺少必要的 shell 脚本组件,可能会导致执行失败。建议进行以下修改:

应用以下更改:

+#!/usr/bin/env sh
+. "$(dirname -- "$0")/_/husky.sh"
+
-lint-staged
+npx lint-staged

修改说明:

  1. 添加 shebang 指令以确保正确的解释器
  2. 导入 husky 的 shell 助手函数
  3. 使用 npx 来执行 lint-staged 命令

Let me verify the husky setup further to provide a comprehensive response.

Based on the results, I'll generate a final response in Chinese that addresses both the script content and file permissions issues.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 验证文件是否具有可执行权限
ls -l .husky/pre-commit

Length of output: 90


Script:

#!/bin/bash
# Check package.json for husky and lint-staged configuration
cat package.json | jq '.scripts,.husky,.["lint-staged"]'

# Check if husky.sh exists
ls -l .husky/_

Length of output: 626

Comment on lines +84 to +85
"lint-staged": {
"*": "prettier --write --ignore-unknown"
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

lint-staged 配置优化建议

当前配置对所有文件类型使用 Prettier 进行格式化,这可能不是最优的方案。

建议根据文件类型设置不同的 lint 规则:

 "lint-staged": {
-  "*": "prettier --write --ignore-unknown"
+  "*.{js,jsx,ts,tsx}": [
+    "eslint --fix",
+    "prettier --write"
+  ],
+  "*.{json,md,yml}": "prettier --write"
 }

这样可以:

  1. 对 JavaScript/TypeScript 文件进行 ESLint 检查
  2. 针对不同文件类型使用合适的格式化规则
  3. 提高提交前检查的效率
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"lint-staged": {
"*": "prettier --write --ignore-unknown"
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,yml}": "prettier --write"
}

@afc163 afc163 merged commit 9f942d5 into react-component:master Oct 24, 2024
9 checks passed
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