Prefer optional chaining over validating references (#992)

This commit is contained in:
Jon Koops 2021-08-12 13:15:47 +02:00 committed by GitHub
parent 6f29839b06
commit 317acd8239
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 110 additions and 155 deletions

View file

@ -17,6 +17,8 @@ module.exports = {
}, },
}, },
rules: { rules: {
// Always prefer using an optional chain expression, as it's more concise and easier to read.
"@typescript-eslint/prefer-optional-chain": "error",
"no-unused-vars": "off", "no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error", "@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-empty-function": "error", "@typescript-eslint/no-empty-function": "error",

View file

@ -175,8 +175,7 @@ export const ClientSettings = ({ save, reset }: ClientSettingsProps) => {
{t("common:choose")} {t("common:choose")}
</SelectOption> </SelectOption>
<> <>
{loginThemes && {loginThemes?.map((theme) => (
loginThemes.map((theme) => (
<SelectOption <SelectOption
selected={theme.name === value} selected={theme.name === value}
key={theme.name} key={theme.name}

View file

@ -20,7 +20,7 @@ export const X509 = () => {
} }
helperTextInvalid={t("common:required")} helperTextInvalid={t("common:required")}
validated={ validated={
errors.attributes && errors.attributes["x509-subjectdn"] errors.attributes?.["x509-subjectdn"]
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
@ -32,7 +32,7 @@ export const X509 = () => {
id="kc-subject" id="kc-subject"
name="attributes.x509-subjectdn" name="attributes.x509-subjectdn"
validated={ validated={
errors.attributes && errors.attributes["x509-subjectdn"] errors.attributes?.["x509-subjectdn"]
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }

View file

@ -107,9 +107,7 @@ export const AttributesForm = ({
aria-label="key-input" aria-label="key-input"
defaultValue={attribute.key} defaultValue={attribute.key}
validated={ validated={
errors.attributes && errors.attributes[rowIndex] errors.attributes?.[rowIndex] ? "error" : "default"
? "error"
: "default"
} }
/> />
</Td> </Td>

View file

@ -76,10 +76,7 @@ export const MultiLineInput = ({
onClick={() => append({})} onClick={() => append({})}
tabIndex={-1} tabIndex={-1}
aria-label={t("common:add")} aria-label={t("common:add")}
isDisabled={ isDisabled={rest.isDisabled || !currentValues?.[index]?.value}
rest.isDisabled ||
!(currentValues && currentValues[index]?.value)
}
> >
<PlusCircleIcon /> {t(addButtonLabel || "common:add")} <PlusCircleIcon /> {t(addButtonLabel || "common:add")}
</Button> </Button>

View file

@ -224,12 +224,7 @@ export function KeycloakDataTable<T>({
}), }),
}, },
]; ];
if ( if (detailColumns?.[0]?.enabled?.(value)) {
detailColumns &&
detailColumns[0] &&
detailColumns[0].enabled &&
detailColumns[0].enabled(value)
) {
row.push({ row.push({
parent: index * 2, parent: index * 2,
cells: detailColumns!.map((col) => { cells: detailColumns!.map((col) => {

View file

@ -49,10 +49,10 @@ export const TableToolbar: FunctionComponent<TableToolbarProps> = ({
const onSearch = () => { const onSearch = () => {
if (searchValue !== "") { if (searchValue !== "") {
setSearchValue(searchValue); setSearchValue(searchValue);
inputGroupOnEnter && inputGroupOnEnter(searchValue); inputGroupOnEnter?.(searchValue);
} else { } else {
setSearchValue(""); setSearchValue("");
inputGroupOnEnter && inputGroupOnEnter(""); inputGroupOnEnter?.("");
} }
}; };
@ -66,7 +66,7 @@ export const TableToolbar: FunctionComponent<TableToolbarProps> = ({
value: string, value: string,
event: FormEvent<HTMLInputElement> event: FormEvent<HTMLInputElement>
) => { ) => {
inputGroupOnChange && inputGroupOnChange(value, event); inputGroupOnChange?.(value, event);
setSearchValue(value); setSearchValue(value);
}; };

View file

@ -8,7 +8,7 @@ import { useAdminClient, useFetch } from "../auth/AdminClient";
export class WhoAmI { export class WhoAmI {
constructor(private me?: WhoAmIRepresentation) { constructor(private me?: WhoAmIRepresentation) {
if (this.me !== undefined && this.me.locale) { if (this.me?.locale) {
i18n.changeLanguage(this.me.locale, (error) => { i18n.changeLanguage(this.me.locale, (error) => {
if (error) console.error("Unable to set locale to", this.me?.locale); if (error) console.error("Unable to set locale to", this.me?.locale);
}); });
@ -28,7 +28,7 @@ export class WhoAmI {
} }
public canCreateRealm(): boolean { public canCreateRealm(): boolean {
return this.me !== undefined && this.me.createRealm; return !!this.me?.createRealm;
} }
public getRealmAccess(): Readonly<{ public getRealmAccess(): Readonly<{

View file

@ -72,8 +72,7 @@ const LoginFlow = ({
)} )}
</> </>
<> <>
{flows && {flows?.map((option) => (
flows.map((option) => (
<SelectOption <SelectOption
selected={option.alias === value} selected={option.alias === value}
key={option.id} key={option.id}

View file

@ -37,7 +37,7 @@ const Fields = ({ readOnly }: DiscoverySettingsProps) => {
fieldId="kc-authorization-url" fieldId="kc-authorization-url"
isRequired isRequired
validated={ validated={
errors.config && errors.config.authorizationUrl errors.config?.authorizationUrl
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
@ -50,7 +50,7 @@ const Fields = ({ readOnly }: DiscoverySettingsProps) => {
name="config.authorizationUrl" name="config.authorizationUrl"
ref={register({ required: true })} ref={register({ required: true })}
validated={ validated={
errors.config && errors.config.authorizationUrl errors.config?.authorizationUrl
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
@ -63,7 +63,7 @@ const Fields = ({ readOnly }: DiscoverySettingsProps) => {
fieldId="tokenUrl" fieldId="tokenUrl"
isRequired isRequired
validated={ validated={
errors.config && errors.config.tokenUrl errors.config?.tokenUrl
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
@ -75,7 +75,7 @@ const Fields = ({ readOnly }: DiscoverySettingsProps) => {
name="config.tokenUrl" name="config.tokenUrl"
ref={register({ required: true })} ref={register({ required: true })}
validated={ validated={
errors.config && errors.config.tokenUrl errors.config?.tokenUrl
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }

View file

@ -117,8 +117,7 @@ export const OpenIdConnectSettings = () => {
/> />
} }
validated={ validated={
(discoveryResult && discoveryResult.error) || discoveryResult?.error || errors.discoveryEndpoint
errors.discoveryEndpoint
? "error" ? "error"
: !discoveryResult : !discoveryResult
? "default" ? "default"
@ -141,8 +140,7 @@ export const OpenIdConnectSettings = () => {
onChange={setDiscoveryUrl} onChange={setDiscoveryUrl}
onBlur={() => setDiscovering(!discovering)} onBlur={() => setDiscovering(!discovering)}
validated={ validated={
(discoveryResult && discoveryResult.error) || discoveryResult?.error || errors.discoveryEndpoint
errors.discoveryEndpoint
? "error" ? "error"
: !discoveryResult : !discoveryResult
? "default" ? "default"
@ -168,9 +166,7 @@ export const OpenIdConnectSettings = () => {
forID="kc-import-config" forID="kc-import-config"
/> />
} }
validated={ validated={discoveryResult?.error ? "error" : "default"}
discoveryResult && discoveryResult.error ? "error" : "default"
}
helperTextInvalid={discoveryResult?.error?.toString()} helperTextInvalid={discoveryResult?.error?.toString()}
> >
<JsonFileUpload <JsonFileUpload
@ -178,9 +174,7 @@ export const OpenIdConnectSettings = () => {
helpText="identity=providers-help:jsonFileUpload" helpText="identity=providers-help:jsonFileUpload"
hideDefaultPreview hideDefaultPreview
unWrap unWrap
validated={ validated={discoveryResult?.error ? "error" : "default"}
discoveryResult && discoveryResult.error ? "error" : "default"
}
onChange={(value) => fileUpload(value)} onChange={(value) => fileUpload(value)}
/> />
</FormGroup> </FormGroup>

View file

@ -32,7 +32,7 @@ export const ClientIdSecret = ({
fieldId="kc-client-id" fieldId="kc-client-id"
isRequired isRequired
validated={ validated={
errors.config && errors.config.clientId errors.config?.clientId
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
@ -59,7 +59,7 @@ export const ClientIdSecret = ({
fieldId="kc-client-secret" fieldId="kc-client-secret"
isRequired={secretRequired} isRequired={secretRequired}
validated={ validated={
errors.config && errors.config.clientSecret errors.config?.clientSecret
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }

View file

@ -77,9 +77,7 @@ export const RoleAttributes = ({
aria-label="key-input" aria-label="key-input"
defaultValue={attribute.key} defaultValue={attribute.key}
validated={ validated={
errors.attributes && errors.attributes[rowIndex] errors.attributes?.[rowIndex] ? "error" : "default"
? "error"
: "default"
} }
/> />
</Td> </Td>

View file

@ -139,9 +139,7 @@ export const KerberosSettingsRequired = ({
})} })}
data-testid="kerberos-realm" data-testid="kerberos-realm"
/> />
{form.errors.config && {form.errors.config?.kerberosRealm?.[0] && (
form.errors.config.kerberosRealm &&
form.errors.config.kerberosRealm[0] && (
<div className="error"> <div className="error">
{form.errors.config.kerberosRealm[0].message} {form.errors.config.kerberosRealm[0].message}
</div> </div>
@ -173,9 +171,7 @@ export const KerberosSettingsRequired = ({
})} })}
data-testid="kerberos-principal" data-testid="kerberos-principal"
/> />
{form.errors.config && {form.errors.config?.serverPrincipal?.[0] && (
form.errors.config.serverPrincipal &&
form.errors.config.serverPrincipal[0] && (
<div className="error"> <div className="error">
{form.errors.config.serverPrincipal[0].message} {form.errors.config.serverPrincipal[0].message}
</div> </div>
@ -207,12 +203,8 @@ export const KerberosSettingsRequired = ({
})} })}
data-testid="kerberos-keytab" data-testid="kerberos-keytab"
/> />
{form.errors.config && {form.errors.config?.keyTab?.[0] && (
form.errors.config.keyTab && <div className="error">{form.errors.config.keyTab[0].message}</div>
form.errors.config.keyTab[0] && (
<div className="error">
{form.errors.config.keyTab[0].message}
</div>
)} )}
</FormGroup> </FormGroup>

View file

@ -116,9 +116,7 @@ export const LdapSettingsConnection = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.connectionUrl?.[0] && (
form.errors.config.connectionUrl &&
form.errors.config.connectionUrl[0] && (
<div className="error"> <div className="error">
{form.errors.config.connectionUrl[0].message} {form.errors.config.connectionUrl[0].message}
</div> </div>

View file

@ -93,9 +93,7 @@ export const LdapSettingsKerberosIntegration = ({
})} })}
data-testid="kerberos-realm" data-testid="kerberos-realm"
/> />
{form.errors.config && {form.errors.config?.kerberosRealm?.[0] && (
form.errors.config.kerberosRealm &&
form.errors.config.kerberosRealm[0] && (
<div className="error"> <div className="error">
{form.errors.config.kerberosRealm[0].message} {form.errors.config.kerberosRealm[0].message}
</div> </div>
@ -127,9 +125,7 @@ export const LdapSettingsKerberosIntegration = ({
})} })}
data-testid="kerberos-principal" data-testid="kerberos-principal"
/> />
{form.errors.config && {form.errors.config?.serverPrincipal?.[0] && (
form.errors.config.serverPrincipal &&
form.errors.config.serverPrincipal[0] && (
<div className="error"> <div className="error">
{form.errors.config.serverPrincipal[0].message} {form.errors.config.serverPrincipal[0].message}
</div> </div>
@ -161,9 +157,7 @@ export const LdapSettingsKerberosIntegration = ({
})} })}
data-testid="kerberos-keytab" data-testid="kerberos-keytab"
/> />
{form.errors.config && {form.errors.config?.keyTab?.[0] && (
form.errors.config.keyTab &&
form.errors.config.keyTab[0] && (
<div className="error"> <div className="error">
{form.errors.config.keyTab[0].message} {form.errors.config.keyTab[0].message}
</div> </div>

View file

@ -106,12 +106,8 @@ export const LdapSettingsSearching = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.usersDn?.[0] && (
form.errors.config.usersDn && <div className="error">{form.errors.config.usersDn[0].message}</div>
form.errors.config.usersDn[0] && (
<div className="error">
{form.errors.config.usersDn[0].message}
</div>
)} )}
</FormGroup> </FormGroup>
<FormGroup <FormGroup
@ -140,9 +136,7 @@ export const LdapSettingsSearching = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.usernameLDAPAttribute?.[0] && (
form.errors.config.usernameLDAPAttribute &&
form.errors.config.usernameLDAPAttribute[0] && (
<div className="error"> <div className="error">
{form.errors.config.usernameLDAPAttribute[0].message} {form.errors.config.usernameLDAPAttribute[0].message}
</div> </div>
@ -174,9 +168,7 @@ export const LdapSettingsSearching = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.rdnLDAPAttribute?.[0] && (
form.errors.config.rdnLDAPAttribute &&
form.errors.config.rdnLDAPAttribute[0] && (
<div className="error"> <div className="error">
{form.errors.config.rdnLDAPAttribute[0].message} {form.errors.config.rdnLDAPAttribute[0].message}
</div> </div>
@ -208,9 +200,7 @@ export const LdapSettingsSearching = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.uuidLDAPAttribute?.[0] && (
form.errors.config.uuidLDAPAttribute &&
form.errors.config.uuidLDAPAttribute[0] && (
<div className="error"> <div className="error">
{form.errors.config.uuidLDAPAttribute[0].message} {form.errors.config.uuidLDAPAttribute[0].message}
</div> </div>
@ -242,9 +232,7 @@ export const LdapSettingsSearching = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.userObjectClasses?.[0] && (
form.errors.config.userObjectClasses &&
form.errors.config.userObjectClasses[0] && (
<div className="error"> <div className="error">
{form.errors.config.userObjectClasses[0].message} {form.errors.config.userObjectClasses[0].message}
</div> </div>
@ -272,9 +260,7 @@ export const LdapSettingsSearching = ({
}, },
})} })}
/> />
{form.errors.config && {form.errors.config?.customUserSearchFilter?.[0] && (
form.errors.config.customUserSearchFilter &&
form.errors.config.customUserSearchFilter[0] && (
<div className="error"> <div className="error">
{form.errors.config.customUserSearchFilter[0].message} {form.errors.config.customUserSearchFilter[0].message}
</div> </div>

View file

@ -36,7 +36,7 @@ export const LdapMapperHardcodedLdapGroup = ({
name="config.group[0]" name="config.group[0]"
ref={form.register({ required: true })} ref={form.register({ required: true })}
validated={ validated={
form.errors.config && form.errors.config.group form.errors.config?.group
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }

View file

@ -78,7 +78,7 @@ export const LdapMapperHardcodedLdapRole = ({
name="config.role[0]" name="config.role[0]"
ref={form.register({ required: true })} ref={form.register({ required: true })}
validated={ validated={
form.errors.config && form.errors.config.role form.errors.config?.role
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }

View file

@ -79,10 +79,10 @@ export const LdapMapperRoleGroup = ({
ref={form.register({ required: true })} ref={form.register({ required: true })}
validated={ validated={
isRole isRole
? form.errors.config && form.errors.config["roles-dn"] ? form.errors.config?.["roles-dn"]
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
: form.errors.config && form.errors.config["groups-dn"] : form.errors.config?.["groups-dn"]
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
@ -583,7 +583,7 @@ export const LdapMapperRoleGroup = ({
name="config.groups-path[0]" name="config.groups-path[0]"
ref={form.register({ required: true })} ref={form.register({ required: true })}
validated={ validated={
form.errors.config && form.errors.config["groups-path"] form.errors.config?.["groups-path"]
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }

View file

@ -111,8 +111,11 @@ export const UserGroups = () => {
const arr = getAllSubgroupPaths( const arr = getAllSubgroupPaths(
rootLevelGroups, rootLevelGroups,
(x: GroupRepresentation, context: GroupRepresentation[][]) => { (
if (x !== undefined && x.subGroups) context.push(x.subGroups); x: GroupRepresentation | undefined,
context: GroupRepresentation[][]
) => {
if (x?.subGroups) context.push(x.subGroups);
}, },
[] []
); );