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

Automate Linting with Husky and lint-staged to Prevent CI Failures #893

Merged
merged 13 commits into from
Sep 5, 2024

Conversation

gwbaik9717
Copy link
Contributor

@gwbaik9717 gwbaik9717 commented Sep 5, 2024

What this PR does / why we need it?

This PR addresses the issue where linting was not being automatically executed before commits, despite Husky being installed. The previous setup had Husky installed at v8, but proper pre-commit hooks were not in place to ensure that linting occurred consistently. Since Husky v5, the configuration and usage have changed. For more details, refer to the the documentation of Husky.

Key changes include:

  • Adding the missing pre-commit configuration in the .husky folder, and ensure Husky works as expected to automate linting at the pre-commit stage.
  • Integrating lint-staged to only lint the files that are staged for commit, improving efficiency.

With these updates, developers will no longer need to manually run lint checks, as the process will be automated and enforced during the commit phase.

Any background context you want to provide?

This PR brings several improvements to the linting setup, along with updates to the Husky and lint-staged configurations.

1. Relocate lint Command to Root

As part of the monorepo migration (issue #648), the lint command was previously located in the sdk package, requiring developers to run pnpm sdk lint. However, this command is intended to lint the entire project, not just the sdk.

To address this, I’ve moved the lint command to the root package.json. Now, you can simply run pnpm lint to lint the whole project.

2. Ignore files in .eslintignore in lint-staged

To prevent unnecessary files from being linted, we use an .eslintignore file to specify which paths should be excluded. Since lint-staged doesn’t automatically respect .eslintignore by default, I’ve added the lint-staged.config.js file to ensure that the ignore rules are applied. This allows us to avoid linting files specified in .eslintignore during the pre-commit stage.

Alternatively, if we want to eliminate this step, we would need to upgrade ESLint to version 8.5.0 or later (we are currently using 8.19.0). For more information, refer to the lint-staged documentation.

Additionally, I’ve moved the .eslintignore file from the sdk package to the root, so it now applies to the entire monorepo. If you need to exclude specific files in other sub-projects, you can add them to this file.

# sdk
packages/sdk/src/api/yorkie/v1/yorkie_grpc_web_pb.d.ts
packages/sdk/src/api/yorkie/v1/yorkie_pb.d.ts
packages/sdk/src/api/yorkie/v1/resources_grpc_web_pb.d.ts
packages/sdk/src/api/yorkie/v1/resources_pb.d.ts
packages/sdk/test/vitest.d.ts
packages/sdk/dist

# devtools
... add files to prevent from being linted

3. Centralize ESLint Configuration in the Root

Since we are now working in a monorepo, it makes sense to centralize ESLint rules in a single configuration file at the root. To achieve this, I’ve created a root .eslintrc.js file, and sub-projects like sdk can extend from it. This ensures consistency across the entire codebase while allowing for project-specific overrides.

Here’s an example of how the sdk package extends the root ESLint configuration:

module.exports = {
  extends: ['../../.eslintrc.js'],
  // skipped
};

What are the relevant tickets?

Fixes #888

Checklist

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced new ESLint configuration and ignore files to enhance code quality checks.
    • Added a pre-commit hook to enforce linting on staged files.
    • Simplified linting commands in the CI workflow.
  • Bug Fixes

    • Removed unnecessary TypeScript dependencies across multiple projects, streamlining the development environment.
  • Documentation

    • Enhanced code readability through improved formatting in various files.
  • Chores

    • Updated package.json files to reflect changes in dependencies and scripts.

Copy link

coderabbitai bot commented Sep 5, 2024

Walkthrough

This update introduces several changes to the project's linting and configuration setup. A new ESLint configuration file and ignore file have been added, along with a pre-commit hook using Husky to automate linting checks before commits. Additionally, various TypeScript dependencies have been removed across multiple example projects, indicating a shift away from TypeScript usage. The CI workflow has also been simplified to streamline the linting process.

Changes

Files Change Summary
.eslintignore, .eslintrc.js, .husky/pre-commit New files added for ESLint configuration and pre-commit hook setup.
.github/workflows/ci.yml Linting command simplified from pnpm sdk lint to pnpm lint.
examples/*/package.json Removed TypeScript dependency from multiple example projects.
packages/sdk/.eslintrc.js, packages/sdk/package.json Simplified ESLint configuration and removed various ESLint and Husky dependencies.
lint-staged.config.js New configuration file for linting staged TypeScript files.
packages/sdk/src/* Minor formatting changes made to improve code readability without altering functionality.

Assessment against linked issues

Objective Addressed Explanation
Automate Linting with Husky to Prevent CI Failures (#888)

🐰 "Hopping through the code so bright,
With linting checks to set things right.
Husky's here, no more despair,
Clean commits with utmost care!
TypeScript's gone, we dance and play,
In a simpler world, we leap all day!" 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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: 0

Outside diff range, codebase verification and nitpick comments (3)
examples/nextjs-scheduler/.eslintrc.js (2)

10-10: Ensure proper handling of links and consider the impact on the user experience and performance.

Disabling the @next/next/no-html-link-for-pages rule allows developers to use HTML links without triggering linting errors. While this change may provide more flexibility in how links are implemented, it's important to consider the following:

  • Ensure that the links are properly handled and navigate to the correct pages.
  • Assess the impact on the user experience, such as page load times and navigation behavior.
  • Evaluate the performance implications of using HTML links instead of Next.js's Link component.

Consider conducting thorough testing and monitoring to ensure that the change does not introduce any unexpected issues or degrade the overall user experience and performance.


2-2: Missing ESLint configuration file at specified path

The ESLint configuration file does not exist at the specified relative path '../../.eslintrc.js'. Please verify the path or ensure that the configuration file is added at the specified location to maintain consistent linting rules across the project.

  • Verify the path '../../.eslintrc.js' or add the missing ESLint configuration file.
Analysis chain

Verify the existence of the ESLint configuration file.

Ensure that the ESLint configuration file exists at the specified relative path '../../.eslintrc.js'.

Run the following script to verify the existence of the file:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the ESLint configuration file.

# Test: Check if the file exists. Expect: The file should exist.
if [ -f "../../.eslintrc.js" ]; then
  echo "The ESLint configuration file exists at the specified relative path."
else
  echo "The ESLint configuration file does not exist at the specified relative path."
fi

Length of output: 194

packages/sdk/.eslintrc.js (1)

4-4: Consider the implications of removing TypeScript-related plugins.

The removal of TypeScript-related plugins may affect the code quality and consistency. Please consider the following:

  • Discuss with the team if the removal aligns with the project's goals and coding standards.
  • Evaluate if the remaining rules in the external configuration file (../../.eslintrc.js) are sufficient to maintain code quality.
  • Consider gradually introducing TypeScript-related plugins and rules to improve type safety and catch potential issues.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2b2ee0b and a41af8a.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (21)
  • .eslintignore (1 hunks)
  • .eslintrc.js (1 hunks)
  • .github/workflows/ci.yml (1 hunks)
  • .husky/pre-commit (1 hunks)
  • examples/nextjs-scheduler/.eslintrc.js (1 hunks)
  • examples/nextjs-scheduler/package.json (1 hunks)
  • examples/react-tldraw/package.json (1 hunks)
  • examples/react-todomvc/package.json (1 hunks)
  • examples/vanilla-codemirror6/package.json (1 hunks)
  • examples/vanilla-quill/package.json (1 hunks)
  • lint-staged.config.js (1 hunks)
  • package.json (2 hunks)
  • packages/devtools/package.json (1 hunks)
  • packages/sdk/.eslintrc.js (1 hunks)
  • packages/sdk/package.json (3 hunks)
  • packages/sdk/src/document/crdt/tree.ts (1 hunks)
  • packages/sdk/src/document/document.ts (3 hunks)
  • packages/sdk/src/document/json/array.ts (4 hunks)
  • packages/sdk/src/util/error.ts (1 hunks)
  • packages/sdk/test/helper/helper.ts (1 hunks)
  • packages/sdk/test/unit/document/crdt/counter_test.ts (1 hunks)
Files skipped from review due to trivial changes (14)
  • .eslintignore
  • .github/workflows/ci.yml
  • .husky/pre-commit
  • examples/react-tldraw/package.json
  • examples/react-todomvc/package.json
  • examples/vanilla-codemirror6/package.json
  • examples/vanilla-quill/package.json
  • packages/sdk/package.json
  • packages/sdk/src/document/crdt/tree.ts
  • packages/sdk/src/document/document.ts
  • packages/sdk/src/document/json/array.ts
  • packages/sdk/src/util/error.ts
  • packages/sdk/test/helper/helper.ts
  • packages/sdk/test/unit/document/crdt/counter_test.ts
Additional comments not posted (19)
packages/sdk/.eslintrc.js (1)

3-3: Verify the existence and content of the external ESLint configuration file.

Ensure that the file ../../.eslintrc.js exists and contains the necessary rules for this project.

Run the following script to verify the existence and content of the external configuration file:

examples/nextjs-scheduler/package.json (2)

24-24: LGTM!

The code changes are approved.


Line range hint 1-27: Verify the removal of eslint-plugin-prettier and typescript dependencies.

The AI-generated summary and the list of alterations indicate the removal of eslint-plugin-prettier and typescript dependencies. Since the provided code is the final state after the changes, it does not display the removed dependencies.

To verify the removal of the dependencies, run the following script:

Verification successful

Dependencies eslint-plugin-prettier and typescript have been removed.

The verification confirms that the dependencies eslint-plugin-prettier and typescript are not present in the examples/nextjs-scheduler/package.json file, indicating their successful removal.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the removal of `eslint-plugin-prettier` and `typescript` dependencies.

# Test: Search for the removed dependencies. Expect: No occurrences.
rg --type json $'eslint-plugin-prettier|typescript' examples/nextjs-scheduler/package.json

Length of output: 91

packages/devtools/package.json (1)

30-30: Verify the impact of removing the typescript dependency.

The removal of the typescript dependency could impact the development workflow if TypeScript is essential for type-checking or compiling the code.

Please ensure that the removal of the typescript dependency does not break the build or development process. If TypeScript is still required, consider the following alternative solutions:

  1. Move the typescript dependency to the root package.json file to manage it centrally for the entire monorepo.
  2. If TypeScript is only required for development purposes, move the typescript dependency to the devDependencies section of the root package.json file.

Run the following script to verify the impact of removing the typescript dependency:

lint-staged.config.js (4)

1-3: LGTM!

The code changes are approved.


5-18: LGTM!

The code changes are approved.


20-39: LGTM!

The code changes are approved.


24-37: LGTM!

The code changes are approved.

package.json (10)

19-20: LGTM!

The code changes are approved.


20-20: LGTM!

The code changes are approved.


21-21: LGTM!

The code changes are approved.


30-30: LGTM!

The code changes are approved.


31-31: LGTM!

The code changes are approved.


32-32: LGTM!

The code changes are approved.


34-35: LGTM!

The code changes are approved.


36-37: LGTM!

The code changes are approved.


38-38: LGTM!

The code changes are approved.


33-33: LGTM!

The code changes are approved.

.eslintrc.js (1)

1-61: LGTM!

The ESLint configuration looks good:

  • It enables several plugins and extends recommended configurations, providing a solid foundation for linting.
  • The custom rules enforce consistent coding style and best practices.
  • The TypeScript override ensures that TypeScript-specific rules are applied.

The configuration is well-structured and follows best practices.

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.

.husky/pre-commit Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a41af8a and 1a6838a.

Files selected for processing (1)
  • .husky/pre-commit (1 hunks)
Files skipped from review due to trivial changes (1)
  • .husky/pre-commit

@hackerwins hackerwins merged commit 4bd2a90 into yorkie-team:main Sep 5, 2024
2 checks passed
@hackerwins
Copy link
Member

Unfortunately, running build:docs failed. I think we need to reorganize the dependencies.
https://github.com/yorkie-team/yorkie-js-sdk/actions/runs/10711549054/job/29700444976

@gwbaik9717
Copy link
Contributor Author

I've relocated TypeScript to the root directory to better manage the version in monorepo. However, it seems tsdoc is referencing the wrong TypeScript version not the root, which is causing typedoc compatibility issues. I'll revisit the problem and work on a solution.

Screenshot 2024-09-05 at 11 13 47 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

Automate Linting with Husky to Prevent CI Failures
2 participants