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

Enforce stricter formatting rules for TypeScript files in ESLint #1945

Closed
vvidday opened this issue Mar 13, 2023 · 0 comments · Fixed by #1957
Closed

Enforce stricter formatting rules for TypeScript files in ESLint #1945

vvidday opened this issue Mar 13, 2023 · 0 comments · Fixed by #1957

Comments

@vvidday
Copy link
Contributor

vvidday commented Mar 13, 2023

What feature(s) would you like to see in RepoSense

As pointed out by @HCY123902, our current ESLint ruleset for TypeScript files does not enforce certain formatting rules, such as semicolons as delimiters in TypeScript interfaces.

We could add these formatting rules to ESLint.

Is the feature request related to a problem?

We are already following the Airbnb coding standard, which contains style-specific rules (e.g. comma dangle, single quote for strings, semicolons at end of statement) that helps maintain consistency across the codebase, which improves readability.

As TypeScript introduces new code that isn't covered by the above JavaScript coding standards, it would be good to enforce consistency in terms of style.

If possible, describe the solution

#1913 adds support for the typescript-eslint plugin, that allows us to enforce rules for TypeScript code. Although we add the Airbnb Typescript and Typescript-eslint recommended rulesets in that PR, there are still certain style rules that aren't included in those rulesets.

Hence, the solution would be to simply add these missing rules to our eslintrc.json file.

Additionally, it would be good to update the DG to specify these additional rules that we're adding, since they are not included in the Airbnb ruleset that is currently linked on the DG.

If applicable, describe alternatives you've considered

Additional context

It is not particularly easy/straightforward to get a list of rules that might be missing from the Airbnb ruleset.

All the rules included by the Airbnb typescript ruleset can be found in this file. The syntax might be confusing, so consider this example in line 152 of the above file:

    'no-magic-numbers': 'off',
    '@typescript-eslint/no-magic-numbers': baseBestPracticesRules['no-magic-numbers'],

The first line is just turning the default ESLint option for that rule off, to not interfere with the TypeScript version. The second line is the actual value of that rule that is being set. However, the problem here is that since Airbnb imports the value from their base ruleset, we actually have to reference the JavaScript repo to find the actual value. In this case, we can see it's in baseBestPracticesRules, so we go to the best-practices.js file in the JavaScript repo above to find the value that is being used:

    'no-magic-numbers': ['off', {
      ignore: [],
      ignoreArrayIndexes: true,
      enforceConst: true,
      detectObjects: false,
    }],

As you can tell, the process is quite tedious/cumbersome, hence why I mentioned that it is not really easy to get a list of rules that we might be missing.

Rules to add

I've done a preliminary comb through the TypeScript rules and so far there are two rules that I think we should consider adding:

I've labelled this 'to discuss' since I might have missed some rules. If anyone has any suggestions/knows of any rules that should be enforced, please feel free to add them here! Otherwise, as mentioned above, since it's quite difficult to tell what's missing, this might be something that we add on to occasionally when we run into such issues (i.e. we realize the linter doesn't pick up on a style issue).

@vvidday vvidday self-assigned this Mar 13, 2023
dcshzj pushed a commit that referenced this issue Mar 25, 2023
Currently, our ESLint ruleset does not enforce certain formatting rules
for TypeScript code, such as delimiters in TypeScript interfaces and
spacing in type annotations.

Let's update the ESLint ruleset to check for proper delimiters and
spacing in type annotations. This will enforce consistency throughout
the codebase and improve code readability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Closed/Completed
Development

Successfully merging a pull request may close this issue.

1 participant