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: add eslint rule for enforcing WorkspaceService naming convention #6309

Closed
magrinj opened this issue Jul 17, 2024 · 1 comment · Fixed by #6308
Closed

feat: add eslint rule for enforcing WorkspaceService naming convention #6309

magrinj opened this issue Jul 17, 2024 · 1 comment · Fixed by #6308
Assignees
Labels
scope: backend Issues that are affecting the backend side only

Comments

@magrinj
Copy link
Member

magrinj commented Jul 17, 2024

Introduces a custom ESLint rule to enforce naming conventions for files and classes that use the @InjectWorkspaceRepository decorator or include services ending with WorkspaceService in their constructors.

@magrinj magrinj converted this from a draft issue Jul 17, 2024
@magrinj magrinj self-assigned this Jul 17, 2024
Copy link
Contributor

greptile-apps bot commented Jul 17, 2024

To add an ESLint rule for enforcing the WorkspaceService naming convention:

  1. Create a custom ESLint rule: Add a new file for the custom rule, e.g., workspace-service-naming.js in your ESLint rules directory.
module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Enforce WorkspaceService naming convention',
      category: 'Best Practices',
      recommended: true
    },
    schema: []
  },
  create(context) {
    return {
      ClassDeclaration(node) {
        if (node.id.name === 'WorkspaceService') {
          context.report({
            node,
            message: 'Class name should follow WorkspaceService naming convention.'
          });
        }
      }
    };
  }
};
  1. Update ESLint configuration: Add the new rule to your .eslintrc.js file.
module.exports = {
  // ... other configurations
  rules: {
    'workspace-service-naming': 'error'
  }
};
  1. Reference WorkspaceService: Ensure the rule checks the WorkspaceService class in /packages/twenty-server/src/engine/modules/workspace/services/workspace.service.ts.

  2. Validate DTOs: Optionally, extend the rule to check DTOs in /packages/twenty-server/src/engine/modules/workspace/dtos if needed.

References

/packages/twenty-server/src/engine/modules/workspace/services
/packages/twenty-server/src/engine/modules/workspace/dtos
/packages/twenty-server/src/engine/modules/workspace

About Greptile

This response provides a starting point for your research, not a precise solution.

Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

Ask Greptile · Edit Issue Bot Settings

@magrinj magrinj added the scope: backend Issues that are affecting the backend side only label Jul 17, 2024
@magrinj magrinj moved this from 🆕 New to 👀 In review in Product development ✅ Jul 17, 2024
charlesBochet added a commit that referenced this issue Jul 19, 2024
#6308)

### Description

This PR introduces a custom ESLint rule named
`inject-workspace-repository`. The purpose of this rule is to enforce
naming conventions for files and classes that use the
`@InjectWorkspaceRepository` decorator or include services ending with
`WorkspaceService` in their constructors.

### Rule Overview

The new ESLint rule checks for the following conditions:

1. **File Naming**:
- Only file ending with `.service.ts` or `.workspace-service.ts` are
checked.
- If a file contains a class using the `@InjectWorkspaceRepository`
decorator or a service ending with `WorkspaceService` in the
constructor, the file name must end with `.workspace-service.ts`.

2. **Class Naming**:
- Classes that use the `@InjectWorkspaceRepository` decorator or include
services ending with `WorkspaceService` in their constructors must have
names that end with `WorkspaceService`.

### How It Works

The rule inspects each TypeScript file to ensure that the naming
conventions are adhered to. It specifically looks for:

- Constructor parameters with the `@InjectWorkspaceRepository`
decorator.
- Constructor parameters with a type annotation ending with
`WorkspaceService`.

When such parameters are found, it checks the class name and the file
name to ensure they conform to the expected patterns.

### Example Code

#### Valid Cases

1. **Correct File and Class Name with Decorator**:
    ```typescript
    // Filename: my.workspace-service.ts
    class MyWorkspaceService {
      constructor(@InjectWorkspaceRepository() private repository) {}
    }
    ```

2. **Service Dependency**:
    ```typescript
    // Filename: another.workspace-service.ts
    class AnotherWorkspaceService {
      constructor(private myWorkspaceService: MyWorkspaceService) {}
    }
    ```

#### Invalid Cases

1. **Incorrect Class Name**:
    ```typescript
    // Filename: my.workspace-service.ts
    class MyService {
      constructor(@InjectWorkspaceRepository() private repository) {}
    }
    // Error: Class name should end with 'WorkspaceService'.
    ```

2. **Incorrect File Name**:
    ```typescript
    // Filename: my.service.ts
    class MyWorkspaceService {
      constructor(@InjectWorkspaceRepository() private repository) {}
    }
    // Error: File name should end with '.workspace-service.ts'.
    ```

3. **Incorrect File and Class Name**:
    ```typescript
    // Filename: my.service.ts
    class MyService {
      constructor(@InjectWorkspaceRepository() private repository) {}
    }
    // Error: Class name should end with 'WorkspaceService'.
    // Error: File name should end with '.workspace-service.ts'.
    ```

4. **Incorrect File Type**:
    ```typescript
    // Filename: another.service.ts
    class AnotherService {
      constructor(private myWorkspaceService: MyWorkspaceService) {}
    }
    // Error: Class name should end with 'WorkspaceService'.
    // Error: File name should end with '.workspace-service.ts'.

    ```

5. **Incorrect Class Name with Dependency**:
    ```typescript
    // Filename: another.workspace-service.ts
    class AnotherService {
      constructor(private myWorkspaceService: MyWorkspaceService) {}
    }
    // Error: Class name should end with 'WorkspaceService'.
    ```

### First step

This rule is only a warning for now, and then we'll migrate all the code
that need to be migrated and move from `warn` to `error`.

Fix #6309

Co-authored-by: Charles Bochet <[email protected]>
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Product development ✅ Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: backend Issues that are affecting the backend side only
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant