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

Stricter ESLint configuration for code quality, consistency & cleaner merges #2343

Merged
merged 40 commits into from
Mar 8, 2024

Conversation

ybnd
Copy link
Member

@ybnd ybnd commented Jun 27, 2023

References

Description

This PR makes our lint configuration a bit stricter, most importantly:

Instructions for Reviewers

Review whether the changes to the ESLint configuration are sensible

  • If some of the rules are too strict/limiting, they can be changed or dropped
  • Since this PR already forces many formatting/code style changes, it's a good opportunity to make everything more strict if we want to. Bundling everything in one PR would make it easier to incorporate.
    • e.g.: Prettier would be a more consistent option for formatting, but would involve even more conflicting changes. IMO it's worth it.
    • So far I've addressed the issues I've noticed, but there are bound to be more

Confirm whether the stricter rules are worth the increased linting time (from ~2 minutes to ~3 minutes)

The app (and the tests) should build and run in exactly the same way as before.

Checklist

This checklist provides a reminder of what we are going to look for when reviewing your PR. You need not complete this checklist prior to creating your PR (draft PRs are always welcome). If you are unsure about an item in the checklist, don't hesitate to ask. We're here to help!

  • My PR is small in size (e.g. less than 1,000 lines of code, not including comments & specs/tests), or I have provided reasons as to why that's not possible.
  • My PR passes ESLint validation using yarn lint
  • My PR doesn't introduce circular dependencies (verified via yarn check-circ-deps)
  • My PR includes TypeDoc comments for all new (or modified) public methods and classes. It also includes TypeDoc for large or complex private methods.
  • My PR passes all specs/tests and includes new/updated specs or tests based on the Code Testing Guide.
  • If my PR includes new libraries/dependencies (in package.json), I've made sure their licenses align with the DSpace BSD License based on the Licensing of Contributions documentation.
  • If my PR includes new features or configurations, I've provided basic technical documentation in the PR itself.
  • If my PR fixes an issue ticket, I've linked them together.

ybnd added 9 commits June 27, 2023 16:47
https://eslint.org/docs/latest/rules/prefer-const

In our tests we use this pattern a lot:
```
let something;

beforeEach(() => {
  something = ...;
});
```

This is not valid ~ prefer-const, but can be "let through" with some configuration.
However, this would make the rule too loose for the non-test codebase.

Therefore, we enable it overall but turn it off for tests only -- the `*.spec.ts` overrides apply to test files only and take precedence over `*.ts`
https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/no-negated-async.md

Prevents us from writing conditions that may pass/fail before an observable has emitted (this is likely to cause unexpected behaviour)
@ybnd ybnd changed the title Stricter ESLint configration for code quality, consistency & cleaner merges Stricter ESLint configuration for code quality, consistency & cleaner merges Jun 27, 2023
ybnd added 13 commits June 27, 2023 17:19
https://github.com/cartant/eslint-plugin-rxjs#rules

Mainly to steer us away from unsafe patterns

Skipping two recommended rules for now:
rxjs/no-implicit-any-catch → not immediately clear how this should be addressed
rxjs/no-nested-subscribe   → there are quite a few cases, would take some effort to fix all of them
Mainly to reduce merge conflict noise

- Order by origin & alphabetically
- Imports should be at the top of the file
- Newline between imports & the rest of the file
- Disallow duplicate imports
- Enforce single-line when only importing one object
- Enforce multi-line when importing two or more
PaginationComponent: expected arguments of updateRoute were out of sync with (fixed) types
GroupFormComponent:  groupBeingEdited is never set to null
@ybnd ybnd self-assigned this Jun 28, 2023
ybnd added 2 commits June 28, 2023 12:51
In most cases we can deal with the untyped errors by introducing an explicit instanceof check

DspaceRestService includes an unsafe catch/rethrow → made it explicitly typed as `any`, but it should be revisited at some point
@github-actions
Copy link

github-actions bot commented Jul 6, 2023

Hi @ybnd,
Conflicts have been detected against the base branch.
Please resolve these conflicts as soon as you can. Thanks!

@ybnd
Copy link
Member Author

ybnd commented Mar 7, 2024

@tdonohue @atarix83 @artlowel
I've addressed the comments above

  • Replaced (... | async) === false cases with (... | async) !== true
    • Haven't found any obvious situations where this would cause problems, so I think it's easier if it's consistent
    • There are a few situations where the source observable is a BehaviorSubject<boolean> that never emits null or undefined, so the change is not even necessary, but again: consistency
  • Added two new pipes in case we want to check null/undefined for arbitrary types: dsHasValue and dsHasNoValue
    • Less of a hassle to write out, and we use the equivalent hasValue/hasNoValue methods all over the place already, so this feels like the way to go

@ybnd ybnd requested a review from atarix83 March 7, 2024 08:33
- Introduced pipes for combined null/undefined checks: dsHasValue & dsHasNoValue
- Safer to async !== true than async === false
- Went over other instances of async === to confirm that they should be fine
Copy link

github-actions bot commented Mar 7, 2024

Hi @ybnd,
Conflicts have been detected against the base branch.
Please resolve these conflicts as soon as you can. Thanks!

@ybnd
Copy link
Member Author

ybnd commented Mar 7, 2024

(force pushed last two commits because I'd committed a temporary Karma config change by accident)

Copy link
Member

@tdonohue tdonohue left a comment

Choose a reason for hiding this comment

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

@ybnd: I'm reviewing/testing this today. Overall, it looks good, and I'm not seeing any changes in behaviour when running the UI in production mode (yarn build:prod; yarn serve:ssr). However, I am seeing that all HTTP errors (404s, etc) are now logged in great detail in the SSR logs (in console where you ran yarn serve:ssr) and in the browser DevTools Console. Is there maybe a stray console.log in this PR (I'm having difficulty finding it myself)? Somehow this PR makes those logs a lot more noisy.

Beyond that, I haven't found any other users, but I'm still testing various areas of the application.

@tdonohue
Copy link
Member

tdonohue commented Mar 7, 2024

@ybnd : Regarding my prior comment, I think the stray console.log is in dspace-rest.service.ts on line 113 (in the "catchError" block). It should likely be removed as it's very noisy. It results in every HTTP 404/400 to be logged, which I don't think we should do.

Once that log is removed, I'm +1 this PR. I haven't found any obvious bugs or changes in behavior in my testing today.

@ybnd
Copy link
Member Author

ybnd commented Mar 8, 2024

@tdonohue thanks for pointing that out, looks like I left it there after testing
I'll take a quick look around to ensure there are no other new console.logs that are less noisy maybe → there's a few that can be removed too, but not related to this PR

Ok apparently the code used to only log errors for the get(), and not for request(). I'd moved it to request() and refactored get() to call it.

This made everything more noisy because the get() method itself is not used that often

  • Only 4 usages outside of tests
  • Do we even need it, or the enforced console.log call?

Out of scope for a linting PR, so I restored the original behaviour.

@ybnd ybnd force-pushed the more-eslint branch 2 times, most recently from fdb1914 to 0f2e776 Compare March 8, 2024 09:56
When refactoring to meet the rxjs/no-implicit-any-catch ESLint rule, I'd made it so errors were logged for DspaceRestService.request calls as well, making it a lot more noisy
The console.log in .get is explicitly required by tests though
Copy link
Member

@tdonohue tdonohue left a comment

Choose a reason for hiding this comment

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

👍 Thanks @ybnd! This looks good to me now. (I also agree that restoring the behavior of that console.log seems good enough for now & we can set that aside for a different PR/ticket)

@tdonohue tdonohue dismissed atarix83’s stale review March 8, 2024 15:17

Verified that all feedback was addressed. If further cleanup is needed it can be done in a follow-up PR

@tdonohue
Copy link
Member

tdonohue commented Mar 8, 2024

Merging this immediately in order to free up work on #2750 (as discussed in yesterday's DevMtg).

@ybnd : Now that this is merged, can you please help to enhance/update the Code Style Guide (Angular section) with details about the new style rules from this PR? At a minimum, I think we should document the following:

  • Don't use !([variable] | async) or ([variable] | async) === false. Instead use ([variable] | async) !== true
  • Similar instructions on when to appropriately use dsHasValue and dsHasNoValue pipes
  • Other major code rules (e.g. ordering of imports) you feel should be documented for developers. (We don't need to document every single thing that lint enforces, but it would be nice to add some basic notes.)

If you don't feel comfortable updating this wiki page, you can add notes in a comment here & I'll copy them over. In the meantime, I'll flag this as needs documentation.

@tdonohue tdonohue added the needs documentation PR is missing documentation. All new features and config changes require documentation. label Mar 8, 2024
@tdonohue tdonohue added this to the 8.0 milestone Mar 8, 2024
@tdonohue tdonohue merged commit a2531e5 into DSpace:main Mar 8, 2024
13 checks passed
@tdonohue tdonohue mentioned this pull request Mar 8, 2024
8 tasks
4science-it pushed a commit to 4Science/dspace-angular that referenced this pull request Oct 10, 2024
Task/dspace cris 2023 02 x/DSC-1823

Approved-by: Andrea Barbasso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code task high priority improvement needs documentation PR is missing documentation. All new features and config changes require documentation.
Projects
No open projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Force alphabetical ordering of TypeScript imports via tslint Organize imports
4 participants