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

web: all aboard the anti-if bus, according to tooling #10220

Merged
merged 61 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
3146e5a
web: fix esbuild issue with style sheets
kensternberg-authentik Mar 8, 2024
fe52f44
Merge branch 'main' into dev
kensternberg-authentik Mar 11, 2024
e505f27
Merge branch 'main' into dev
kensternberg-authentik Mar 11, 2024
5090621
Merge branch 'main' into dev
kensternberg-authentik Mar 11, 2024
035bda4
Merge branch 'main' into dev
kensternberg-authentik Mar 12, 2024
2d0117d
Merge branch 'main' into dev
kensternberg-authentik Mar 13, 2024
22cb5b7
Merge branch 'main' into dev
kensternberg-authentik Mar 14, 2024
8b4e036
Merge branch 'main' into dev
kensternberg-authentik Mar 14, 2024
db96e1a
Merge branch 'main' into dev
kensternberg-authentik Mar 18, 2024
8946b81
Merge branch 'main' into dev
kensternberg-authentik Mar 19, 2024
30beca9
Merge branch 'main' into dev
kensternberg-authentik Mar 19, 2024
5d84082
Merge branch 'main' into dev
kensternberg-authentik Mar 20, 2024
a7e3dca
Merge branch 'main' into dev
kensternberg-authentik Mar 21, 2024
2d254d6
Merge branch 'main' into dev
kensternberg-authentik Mar 25, 2024
3f95020
Merge branch 'main' into dev
kensternberg-authentik Mar 26, 2024
a056703
Merge branch 'main' into dev
kensternberg-authentik Mar 27, 2024
fc00bde
Merge branch 'main' into dev
kensternberg-authentik Mar 29, 2024
7123b2c
Merge branch 'main' into dev
kensternberg-authentik Mar 29, 2024
5d4c380
Merge branch 'main' into dev
kensternberg-authentik Apr 1, 2024
66cefcc
Merge branch 'main' into dev
kensternberg-authentik Apr 2, 2024
875fc5c
Merge branch 'main' into dev
kensternberg-authentik Apr 4, 2024
c84be1d
Merge branch 'main' into dev
kensternberg-authentik Apr 8, 2024
b08dcc2
Merge branch 'main' into dev
kensternberg-authentik Apr 9, 2024
272fdc5
Merge branch 'main' into dev
kensternberg-authentik Apr 10, 2024
23665d1
Merge branch 'main' into dev
kensternberg-authentik Apr 10, 2024
cacdf64
Merge branch 'main' into dev
kensternberg-authentik Apr 11, 2024
085debf
Merge branch 'main' into dev
kensternberg-authentik Apr 12, 2024
f19ed14
Merge branch 'main' into dev
kensternberg-authentik Apr 16, 2024
ac4ba5d
Merge branch 'main' into dev
kensternberg-authentik Apr 17, 2024
98503f6
Merge branch 'main' into dev
kensternberg-authentik Apr 18, 2024
2d94b16
Merge branch 'main' into dev
kensternberg-authentik Apr 22, 2024
34de6bf
Merge branch 'main' into dev
kensternberg-authentik Apr 24, 2024
ca42506
Merge branch 'main' into dev
kensternberg-authentik Apr 25, 2024
2a96900
Merge branch 'main' into dev
kensternberg-authentik May 2, 2024
9acebec
Merge branch 'main' into dev
kensternberg-authentik May 3, 2024
8248163
Merge branch 'main' into dev
kensternberg-authentik May 3, 2024
ee37e92
Merge branch 'main' into dev
kensternberg-authentik May 7, 2024
e1d565d
Merge branch 'main' into dev
kensternberg-authentik May 8, 2024
3d532d4
Merge branch 'main' into dev
kensternberg-authentik May 9, 2024
fffc8c7
Merge branch 'main' into dev
kensternberg-authentik May 10, 2024
3fae9e5
Merge branch 'main' into dev
kensternberg-authentik May 13, 2024
09803fe
Merge branch 'main' into dev
kensternberg-authentik May 14, 2024
5752497
Merge branch 'main' into dev
kensternberg-authentik May 17, 2024
61eb9fa
Merge branch 'main' into dev
kensternberg-authentik May 22, 2024
3ff20ca
Merge branch 'main' into dev
kensternberg-authentik May 24, 2024
5b132c8
Merge branch 'main' into dev
kensternberg-authentik May 28, 2024
2488eb9
Merge branch 'main' into dev
kensternberg-authentik Jun 3, 2024
312f364
Merge branch 'main' into dev
kensternberg-authentik Jun 4, 2024
fcab990
Merge branch 'main' into dev
kensternberg-authentik Jun 11, 2024
10bfc4e
erge branch 'main' into dev
kensternberg-authentik Jun 13, 2024
c49185d
Merge branch 'main' into dev
kensternberg-authentik Jun 14, 2024
7b208d9
Merge branch 'main' into dev
kensternberg-authentik Jun 18, 2024
75b605f
Merge branch 'main' into dev
kensternberg-authentik Jun 19, 2024
186e1bf
Merge branch 'main' into dev
kensternberg-authentik Jun 20, 2024
be9b44a
Merge branch 'main' into dev
kensternberg-authentik Jun 24, 2024
f5a561f
web: all-aboard the anti-if bus, according to tooling
kensternberg-authentik Jun 25, 2024
d3e2273
web: better names and logic for comparing the dates of Xliff vs gener…
kensternberg-authentik Jun 25, 2024
f800754
Missed one.
kensternberg-authentik Jun 25, 2024
b0f2575
Merge branch 'main' into web/lint/sonar-rider
kensternberg-authentik Jul 15, 2024
1a644e3
Merge branch 'main' into web/lint/sonar-rider
kensternberg-authentik Jul 15, 2024
dd9349c
Fixed a redirect issue that was creating an empty file in the ./web f…
kensternberg-authentik Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions web/scripts/build-locales.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,38 @@ import process from "process";

const localizeRules = JSON.parse(fs.readFileSync("./lit-localize.json", "utf-8"));

function compareXlfAndSrc(loc) {
const xlf = path.join("./xliff", `${loc}.xlf`);
const src = path.join("./src/locales", `${loc}.ts`);
function generatedFileIsUpToDateWithXliffSource(loc) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It took me awhile to figure out what this function does. That isn't good, considering I wrote it initially. I've revised it with a better name and the clear comparison eslint demanded.

const xliff = path.join("./xliff", `${loc}.xlf`);
const gened = path.join("./src/locales", `${loc}.ts`);

// Returns false if: the expected XLF file doesn't exist, The expected
// generated file doesn't exist, or the XLF file is newer (has a higher date)
// than the generated file. The missing XLF file is important enough it
// generates a unique error message and halts the build.

try {
var xlfStat = fs.statSync(xlf);
var xlfStat = fs.statSync(xliff);
} catch (_error) {
console.error(`lit-localize expected '${loc}.xlf', but XLF file is not present`);
process.exit(1);
}

// If the generated file doesn't exist, of course it's not up to date.
try {
var srcStat = fs.statSync(src);
var genedStat = fs.statSync(gened);
} catch (_error) {
return false;
}

// if the xlf is newer (greater) than src, it's out of date.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// prefer-single-boolean-return

if (xlfStat.mtimeMs > srcStat.mtimeMs) {
return false;
}
return true;
// if the generated file is the same age or older (date is greater) than the xliff file, it's
// presumed to have been generated by that file and is up-to-date.
return genedStat.mtimeMs >= xlfStat.mtimeMs;
}

// For all the expected files, find out if any aren't up-to-date.

const upToDate = localizeRules.targetLocales.reduce(
(acc, loc) => acc && compareXlfAndSrc(loc),
(acc, loc) => acc && generatedFileIsUpToDateWithXliffSource(loc),
true,
);

Expand All @@ -61,7 +60,9 @@ if (!upToDate) {
.map((locale) => `Locale '${locale}' has ${counts.get(locale)} missing translations`)
.join("\n");

// eslint-disable-next-line no-console
console.log(`Translation tables rebuilt.\n${report}\n`);
}

// eslint-disable-next-line no-console
console.log("Locale ./src is up-to-date");
1 change: 1 addition & 0 deletions web/scripts/check-spelling.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const cmd = [
"-S './src/locales/**' ./src -s",
].join(" ");

// eslint-disable-next-line no-console
console.log(execSync(cmd, { encoding: "utf8" }));
55 changes: 55 additions & 0 deletions web/scripts/eslint-nightmare.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { execFileSync } from "child_process";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ESlint in nightmare mode, only this checks everything.

import { ESLint } from "eslint";
import path from "path";
import process from "process";

// Code assumes this script is in the './web/scripts' folder.
const projectRoot = execFileSync("git", ["rev-parse", "--show-toplevel"], {
encoding: "utf8",
}).replace("\n", "");
process.chdir(path.join(projectRoot, "./web"));

const eslintConfig = {
overrideConfig: {
env: {
browser: true,
es2021: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:lit/recommended",
"plugin:custom-elements/recommended",
"plugin:storybook/recommended",
"plugin:sonarjs/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 12,
sourceType: "module",
},
plugins: ["@typescript-eslint", "lit", "custom-elements", "sonarjs"],
rules: {
"indent": "off",
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double", { avoidEscape: true }],
"semi": ["error", "always"],
"@typescript-eslint/ban-ts-comment": "off",
"sonarjs/cognitive-complexity": ["error", 9],
"sonarjs/no-duplicate-string": "off",
"sonarjs/no-nested-template-literals": "off",
},
},
};

const updated = ["./src/", "./build.mjs", "./scripts/*.mjs"];

const eslint = new ESLint(eslintConfig);
const results = await eslint.lintFiles(updated);
const formatter = await eslint.loadFormatter("stylish");
const resultText = formatter.format(results);
const errors = results.reduce((acc, result) => acc + result.errorCount, 0);

// eslint-disable-next-line no-console
console.log(resultText);
process.exit(errors > 1 ? 1 : 0);
2 changes: 1 addition & 1 deletion web/scripts/patch-spotlight.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

TARGET="./node_modules/@spotlightjs/overlay/dist/index-"[0-9a-f]*.js

if [[ $(grep -L "QX2" "$TARGET" > /dev/null 2&>1) ]]; then
if [[ $(grep -L "QX2" "$TARGET" > /dev/null 2> /dev/null) ]]; then
patch --forward -V none --no-backup-if-mismatch -p0 $TARGET <<EOF

TARGET=$(find "./node_modules/@spotlightjs/overlay/dist/" -name "index-[0-9a-f]*.js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ export class ApplicationWizardCommitApplication extends BasePanel {
);
if (!providerModel) {
throw new Error(
`Could not determine provider model from user request: ${JSON.stringify(
this.wizard,
null,
2,
)}`,
`Could not determine provider model from user request: ${JSON.stringify(this.wizard, null, 2)}`,
);
}

Expand All @@ -118,7 +114,6 @@ export class ApplicationWizardCommitApplication extends BasePanel {
};

this.send(request);
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// redundant-return

}
}

Expand Down
15 changes: 6 additions & 9 deletions web/src/admin/property-mappings/PropertyMappingTestForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "@goauthentik/elements/forms/SearchSelect";
import YAML from "yaml";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { TemplateResult, html, nothing } from "lit";
import { customElement, property } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";

Expand Down Expand Up @@ -66,14 +66,11 @@ export class PolicyTestForm extends Form<PropertyMappingTestRequest> {
</ak-form-element-horizontal>`;
}

renderExampleButtons(): TemplateResult {
const header = html`<p>${msg("Example context data")}</p>`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// small-switch-statement

switch (this.mapping?.metaModelName) {
case "authentik_sources_ldap.ldappropertymapping":
return html`${header}${this.renderExampleLDAP()}`;
default:
return html``;
}
renderExampleButtons() {
return this.mapping?.metaModelName === "authentik_sources_ldap.ldappropertymapping"
? html`<p>${msg("Example context data")}</p>
${this.renderExampleLDAP()}`
: nothing;
}

renderExampleLDAP(): TemplateResult {
Expand Down
14 changes: 6 additions & 8 deletions web/src/common/helpers/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ export function transformCredentialCreateOptions(
user.id = u8arr(b64enc(u8arr(stringId)));
const challenge = u8arr(credentialCreateOptions.challenge.toString());

const transformedCredentialCreateOptions = Object.assign({}, credentialCreateOptions, {
return {
...credentialCreateOptions,
challenge,
user,
});

return transformedCredentialCreateOptions;
};
}

export interface Assertion {
Expand Down Expand Up @@ -98,12 +97,11 @@ export function transformCredentialRequestOptions(
},
);

const transformedCredentialRequestOptions = Object.assign({}, credentialRequestOptions, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// prefer-immediate-return

return {
...credentialRequestOptions,
challenge,
allowCredentials,
});

return transformedCredentialRequestOptions;
};
}

export interface AuthAssertion {
Expand Down
12 changes: 7 additions & 5 deletions web/src/elements/Tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ export class Tabs extends AKElement {
const pages = Array.from(this.querySelectorAll(":scope > [slot^='page-']"));
if (window.location.hash.includes(ROUTE_SEPARATOR)) {
const params = getURLParams();
if (this.pageIdentifier in params && !this.currentPage) {
if (this.querySelector(`[slot='${params[this.pageIdentifier]}']`) !== null) {
// To update the URL to match with the current slot
this.onClick(params[this.pageIdentifier] as string);
}
if (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// collapse-nested-if

this.pageIdentifier in params &&
!this.currentPage &&
this.querySelector(`[slot='${params[this.pageIdentifier]}']`) !== null
) {
// To update the URL to match with the current slot
this.onClick(params[this.pageIdentifier] as string);
}
}
if (!this.currentPage) {
Expand Down
11 changes: 7 additions & 4 deletions web/src/elements/forms/SearchSelect/ak-search-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,13 @@ export class SearchSelect<T> extends CustomEmitterElement(AKElement) {
const onFocus = (ev: FocusEvent) => {
this.open = true;
this.renderMenu();
if (this.blankable && this.renderedValue === this.emptyOption) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// collapse-nested-if

if (ev.target && ev.target instanceof HTMLInputElement) {
ev.target.value = "";
}
if (
this.blankable &&
this.renderedValue === this.emptyOption &&
ev.target &&
ev.target instanceof HTMLInputElement
) {
ev.target.value = "";
}
};

Expand Down
18 changes: 5 additions & 13 deletions web/src/elements/sidebar/SidebarItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,11 @@ export class SidebarItem extends AKElement {
if (!this.path) {
return false;
}
if (this.path) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized the two comparisons were complimentary, so that takes out the if. exec isn't needed here, just test. Giving the two comparisons names helps understand what the code is doing.

// prefer-single-boolean-return

const ourPath = this.path.split(";")[0];
if (new RegExp(`^${ourPath}$`).exec(path)) {
return true;
}
}
return this.activeMatchers.some((v) => {
const match = v.exec(path);
if (match !== null) {
return true;
}
return false;
});

const ourPath = this.path.split(";")[0];
const pathIsWholePath = new RegExp(`^${ourPath}$`).test(path);
const pathIsAnActivePath = this.activeMatchers.some((v) => v.test(path));
return pathIsWholePath || pathIsAnActivePath;
}

expandParentRecursive(activePath: string, item: SidebarItem): void {
Expand Down
7 changes: 2 additions & 5 deletions web/src/flow/stages/prompt/PromptStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,11 @@ ${prompt.initialValue}</textarea

shouldRenderInWrapper(prompt: StagePrompt): boolean {
// Special types that aren't rendered in a wrapper
if (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// prefer-single-boolean-return

return !(
prompt.type === PromptTypeEnum.Static ||
prompt.type === PromptTypeEnum.Hidden ||
prompt.type === PromptTypeEnum.Separator
) {
return false;
}
return true;
);
}

renderField(prompt: StagePrompt): TemplateResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ export class UserSettingsFlowExecutor
})
.then((data) => {
this.challenge = data;
if (this.challenge.responseErrors) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// prefer-single-boolean-return

return false;
}
return true;
return !this.challenge.responseErrors;
})
.catch((e: Error | ResponseError) => {
this.errorMessage(e);
Expand Down
24 changes: 10 additions & 14 deletions web/src/user/user-settings/details/stages/prompt/PromptStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ import { PromptTypeEnum, StagePrompt } from "@goauthentik/api";
@customElement("ak-user-stage-prompt")
export class UserSettingsPromptStage extends PromptStage {
renderPromptInner(prompt: StagePrompt): TemplateResult {
switch (prompt.type) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

// small-switch-statement

// Checkbox requires slightly different rendering here due to the use of horizontal form elements
case PromptTypeEnum.Checkbox:
return html`<input
type="checkbox"
class="pf-c-check__input"
name="${prompt.fieldKey}"
?checked=${prompt.initialValue !== ""}
?required=${prompt.required}
style="vertical-align: bottom"
/>`;
default:
return super.renderPromptInner(prompt);
}
return prompt.type === PromptTypeEnum.Checkbox
? html`<input
type="checkbox"
class="pf-c-check__input"
name="${prompt.fieldKey}"
?checked=${prompt.initialValue !== ""}
?required=${prompt.required}
style="vertical-align: bottom"
/>`
: super.renderPromptInner(prompt);
}

renderField(prompt: StagePrompt): TemplateResult {
Expand Down
Loading