8000 fix(admin-ui): enable no-unnecessary-condition ESLint rule by pierluigilenoci · Pull Request #48218 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

fix(admin-ui): enable no-unnecessary-condition ESLint rule - #48218

Merged
edewit merged 1 commit into
keycloak:mainfrom
pierluigilenoci:fix/admin-ui-no-unnecessary-condition-17770
May 12, 2026
Merged

edewit merged 1 commit into
keycloak:mainfrom
pierluigilenoci:fix/admin-ui-no-unnecessary-condition-17770

Conversation

@pierluigilenoci
@pierluigilenoci pierluigilenoci commented Apr 17, 2026 •
Copy link
Copy Markdown
Contributor

Summary

Enable the @typescript-eslint/no-unnecessary-condition ESLint rule (previously "off") and fix all violations across js/.

Core approach: make RealmContext.realmRepresentation non-optional (guarded by the existing <KeycloakSpinner /> in the provider), allowing ~30 downstream consumers to drop redundant ?. chains.

Changes include:

  • Remove unnecessary optional chains (?.) on non-nullish values
  • Remove unnecessary nullish coalescing (??) on non-nullable expressions
  • Remove always-truthy if conditions and dead code from always-falsy branches
  • Move breadcrumb from route object into handle (required by the type after removing optional access)
  • Initialize state with undefined (not []) where needed to distinguish "not loaded" from "loaded but empty" (UserCredentials, KeysListTab, Organizations)
  • Replace .some() abuse with .forEach() in agent.ts
  • Narrow createOrUpdatePolicy catch to only handle 404 NetworkError (previously swallowed all errors)

Runtime guards preserved with eslint-disable comments where TypeScript types say non-nullable but runtime disagrees (17 sites total — API responses, react-hook-form dynamic values, route params missing on certain routes, array index access, DOM queries).

Additional fixes surfaced during this work:

  • PermissionConfigurationDetails: fix spinner blocking the "create permission" form by distinguishing "loading existing" (null) from "creating new" ({})
  • ResourceType.tsx: restore resourceIds?.some() guard — form.getValues("resources") returns undefined when the form field has no default, causing a crash when selecting a resource type in the search dropdown
  • clients.ts: narrow overly broad catch in createOrUpdatePolicy to 404 only

Closes #17770

AI disclosure: Developed with the assistance of an AI coding agent. All changes were manually reviewed and validated.

Test plan

  • pnpm eslint — 0 violations for no-unnecessary-condition
  • Admin UI lint, Account UI lint, UI Shared lint — all pass
  • Admin UI E2E (chromium) — 424 passed, 0 failed
  • Account UI E2E (chromium) — all pass
  • Keycloak Admin Client lint — pass

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

@ahus1 @edewit This is a redo of #47408, which was closed due to CI failures. All issues from the previous attempt have been addressed:

  • All E2E tests pass (424/424 on chromium)
  • Runtime guards are preserved where TypeScript types disagree with actual runtime behavior (using explicit as T | undefined casts that survive eslint --fix)
  • Overly broad catch in createOrUpdatePolicy narrowed to 404 only
  • Single squashed commit, rebased on main

Ready for review. Thanks for the feedback on the previous PR.

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

Hi @ahus1 @edewit — friendly ping. This is a redo of #47408 with all feedback addressed. All CI checks are green. Would appreciate a review when you have a moment. Thanks!

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.ResetPasswordTest#resetPasswordWithLengthPasswordPolicy

Keycloak CI - Forms IT (chrome)

java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "org.keycloak.testsuite.pages.PageUtils.getPageTitle(org.openqa.selenium.WebDriver)" is null
	at org.keycloak.testsuite.pages.LoginPasswordUpdatePage.isCurrent(LoginPasswordUpdatePage.java:61)
	at org.keycloak.testsuite.pages.AbstractPage.assertCurrent(AbstractPage.java:39)
	at org.keycloak.testsuite.pages.LogoutSessionsPage.assertCurrent(LogoutSessionsPage.java:37)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
...

Report flaky test

Enable the rule (previously "off") and fix all violations across the JS
workspace. The core change makes RealmContext.realmRepresentation
non-optional — guarded by the existing KeycloakSpinner in the provider —
allowing ~30 downstream consumers to drop redundant optional chains.

Where TypeScript types declare a value as non-nullable but runtime
behaviour can still produce undefined/null (API responses, react-hook-form
dynamic values, route params present on one route but not another, array
index access, DOM queries), the existing guards are preserved with
eslint-disable-next-line comments explaining the rationale.

Additional fixes surfaced during this work:
- PermissionConfigurationDetails: fix spinner blocking the "create
  permission" form by distinguishing "loading" (null) from "new" ({})
- ResourceType: restore resourceIds?.some() guard — form.getValues
  returns undefined when the field has no default value
- clients.ts createOrUpdatePolicy: narrow catch to 404 NetworkError
  only instead of swallowing all errors

Closes #17770

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

Hi — friendly follow-up. CI is green (all 84 checks passing) and the branch is up to date. Would you be able to review when you get a chance? Thank you!

@edewit
edewit merged commit d896684 into keycloak:main May 12, 2026
84 checks passed
@stianst stianst mentioned this pull request Jun 1, 2026
sashyo pushed a commit to tide-foundation/keycloak-IGA that referenced this pull request Jul 20, 2026
…oak#48218)

Enable the rule (previously "off") and fix all violations across the JS
workspace. The core change makes RealmContext.realmRepresentation
non-optional — guarded by the existing KeycloakSpinner in the provider —
allowing ~30 downstream consumers to drop redundant optional chains.

Where TypeScript types declare a value as non-nullable but runtime
behaviour can still produce undefined/null (API responses, react-hook-form
dynamic values, route params present on one route but not another, array
index access, DOM queries), the existing guards are preserved with
eslint-disable-next-line comments explaining the rationale.

Additional fixes surfaced during this work:
- PermissionConfigurationDetails: fix spinner blocking the "create
  permission" form by distinguishing "loading" (null) from "new" ({})
- ResourceType: restore resourceIds?.some() guard — form.getValues
  returns undefined when the field has no default value
- clients.ts createOrUpdatePolicy: narrow catch to 404 NetworkError
  only instead of swallowing all errors

Closes keycloak#17770

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove unnecessary conditions from code

2 participants

0