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 named-use-effect custom eslint rule #36725

Merged
merged 2 commits into from
Oct 9, 2024

Conversation

hetunandu
Copy link
Member

@hetunandu hetunandu commented Oct 7, 2024

Description

Adds a custom es lint rule that would add a warning when a useEffect is used without a named function inside it

Automation

/ok-to-test tags="@tag.Sanity"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/11252621528
Commit: 334c680
Cypress dashboard.
Tags: @tag.Sanity
Spec:


Wed, 09 Oct 2024 10:55:00 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Introduced a new ESLint rule to enforce the use of named functions within the useEffect hook, promoting better code readability.
    • Updated recommended ESLint configuration to include the new rule with a warning level.
  • Tests

    • Added a comprehensive test suite for the new namedUseEffectRule, covering both valid and invalid usage scenarios.

Copy link
Contributor

coderabbitai bot commented Oct 7, 2024

Walkthrough

The changes in this pull request introduce a new ESLint rule named namedUseEffectRule, which enforces the use of named functions within the useEffect hook in React components. This rule is registered in the ESLint plugin's configuration, and a corresponding test suite is added to ensure its functionality. The tests validate both correct and incorrect usages of functions within useEffect, enhancing the overall code quality by promoting clearer naming conventions.

Changes

File Path Change Summary
app/client/packages/eslint-plugin/src/index.ts Added new rule "named-use-effect" and updated recommended config to include "@appsmith/named-use-effect": "warn".
app/client/packages/eslint-plugin/src/named-use-effect/rule.ts Introduced namedUseEffectRule to enforce named functions in useEffect.
app/client/packages/eslint-plugin/src/named-use-effect/rule.test.ts Added test suite for namedUseEffectRule, covering valid and invalid cases for function usage.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant ESLint
    participant Rule
    Developer->>ESLint: Run ESLint on code
    ESLint->>Rule: Check for `useEffect` usage
    alt Valid function
        Rule->>Developer: No warning
    else Invalid function
        Rule->>Developer: Warning: Use named function
    end
Loading

In the land of code where functions reside,
A new rule emerged, with a guide by its side.
"Name your functions," it cheerfully said,
"For clarity's sake, let your code be well-read!"
With tests all in place, the rule takes its stand,
Helping developers write code that's quite grand!
🌟✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 001ee58 and 334c680.

📒 Files selected for processing (2)
  • app/client/packages/eslint-plugin/src/named-use-effect/rule.test.ts (1 hunks)
  • app/client/packages/eslint-plugin/src/named-use-effect/rule.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • app/client/packages/eslint-plugin/src/named-use-effect/rule.test.ts
  • app/client/packages/eslint-plugin/src/named-use-effect/rule.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.

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 the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Oct 7, 2024
Copy link
Contributor

@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 (3)
app/client/packages/eslint-plugin/src/named-use-effect/rule.test.ts (2)

8-11: Good start on valid test cases, but let's expand our horizons!

Your valid test case correctly demonstrates the proper use of a named function in useEffect. However, to ensure our rule is robust, we should consider adding more valid test cases.

Consider adding these additional valid test cases:

{
  code: "useEffect(function myEffect() { /* do something */ }, [dependency])",
},
{
  code: "useEffect(function cleanupEffect() { return () => { /* cleanup */ } }, [])",
},

These will help us cover more scenarios and ensure our rule works correctly in various situations.


12-21: Well done on the invalid test cases, class!

You've correctly identified two key scenarios where our rule should flag an error: anonymous functions and arrow functions in useEffect. The consistent use of the "useNamedUseEffect" messageId is commendable.

To make our tests even more robust, consider adding a test case for a named arrow function, which should be valid:

{
  code: "useEffect(const myEffect = () => { /* do something */ }, [])",
},

This will ensure our rule correctly handles all function syntaxes in useEffect.

app/client/packages/eslint-plugin/src/named-use-effect/rule.ts (1)

13-14: Enhance the error message for clarity

Remember, a clear and actionable error message helps developers understand how to fix issues promptly. Consider rephrasing the message to provide guidance, such as: "Avoid using anonymous functions in useEffect; use a named function instead for better readability and debugging."

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d15eea3 and 001ee58.

📒 Files selected for processing (3)
  • app/client/packages/eslint-plugin/src/index.ts (1 hunks)
  • app/client/packages/eslint-plugin/src/named-use-effect/rule.test.ts (1 hunks)
  • app/client/packages/eslint-plugin/src/named-use-effect/rule.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (5)
app/client/packages/eslint-plugin/src/index.ts (3)

7-7: Now, let's look at how we've added this rule to our rulebook.

Excellent work adding the new rule to our collection! You've followed the existing pattern perfectly, which helps keep our code organized and easy to understand.


2-13: Class, let's recap what we've learned from these changes.

Overall, these changes demonstrate a well-structured approach to expanding our ESLint plugin. By adding the new namedUseEffectRule, we're encouraging better coding practices in our React components. The consistency in naming, file structure, and configuration is commendable.

Remember, clear and consistent code like this makes our project easier to maintain and understand. Keep up the good work!


13-13: Let's examine how we've configured our new rule.

Well done on adding the new rule to our recommended configuration! You've maintained consistency with our existing setup, which is crucial for a well-organized codebase. The "warn" level is appropriate for introducing a new rule.

To ensure our new rule is working as intended, let's verify its implementation:

✅ Verification successful

Let's ensure our new ESLint rule is correctly implemented by searching the codebase for any references to namedUseEffect.


Great observation! It seems ripgrep doesn't recognize the typescript file type in our current environment. Let's adjust our search to target .ts and .tsx files explicitly to locate any implementations related to namedUseEffect.


Excellent work! The new named-use-effect rule has been properly implemented and integrated into our ESLint configuration. Everything looks good and follows our established standards.

  • namedUseEffectRule is correctly defined in rule.ts.
  • The rule is imported and registered in index.ts.
  • Comprehensive tests are present in rule.test.ts.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of the new ESLint rule

# Test: Search for the rule implementation
ast-grep --lang typescript --pattern $'export const namedUseEffectRule = {
  $$$
  create(context) {
    $$$
  }
  $$$
}'

Length of output: 122


Script:

#!/bin/bash
# Description: Search for implementations related to 'namedUseEffect'

rg 'namedUseEffect' --type typescript

Length of output: 72


Script:

#!/bin/bash
# Description: Search for implementations related to 'namedUseEffect' in TypeScript files

rg 'namedUseEffect' --glob '*.ts' --glob '*.tsx'

Length of output: 627

app/client/packages/eslint-plugin/src/named-use-effect/rule.test.ts (2)

1-4: Good job setting up the test environment, class!

You've correctly imported the necessary modules and set up the RuleTester. This is an excellent foundation for our ESLint rule testing.


6-7: Excellent test suite configuration, students!

You've correctly set up the test suite for our "named-use-effect" rule. Remember, proper configuration is key to effective testing.

@hetunandu hetunandu added the ok-to-test Required label for CI label Oct 9, 2024
@hetunandu hetunandu enabled auto-merge (squash) October 9, 2024 09:57
@hetunandu hetunandu merged commit 71963b5 into release Oct 9, 2024
51 checks passed
@hetunandu hetunandu deleted the chore/eslint-named-use-effect branch October 9, 2024 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants