From 87f2edc7355ffb5a3ce8a0c82909c8d1cb3d420e Mon Sep 17 00:00:00 2001 From: Brainslug Date: Mon, 1 May 2023 21:07:40 +0200 Subject: [PATCH] Prevent nested ternary expressions (#18376) Co-authored-by: Pascal Jufer --- .changeset/breezy-numbers-brake.md | 7 +++ .eslintrc.js | 1 + app/src/components/v-template-input.vue | 7 ++- .../input-rich-text-html/useMedia.ts | 4 +- .../interfaces/select-color/select-color.vue | 23 +++++++-- .../helpers/generate-bundle-entrypoint.ts | 47 ++++++++++--------- tests/blackbox/common/config.ts | 10 ++-- .../timezone-changed-node-tz-america.test.ts | 13 +++-- .../timezone-changed-node-tz-asia.test.ts | 9 +++- 9 files changed, 82 insertions(+), 39 deletions(-) create mode 100644 .changeset/breezy-numbers-brake.md diff --git a/.changeset/breezy-numbers-brake.md b/.changeset/breezy-numbers-brake.md new file mode 100644 index 0000000000..96cff3ac6a --- /dev/null +++ b/.changeset/breezy-numbers-brake.md @@ -0,0 +1,7 @@ +--- +"@directus/app": patch +"@directus/extensions-sdk": patch +"tests-blackbox": patch +--- + +Added `no-nested-ternary` eslint rule to ensure better readability in the code base diff --git a/.eslintrc.js b/.eslintrc.js index e1e8c89848..ecf0ec24b2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -37,6 +37,7 @@ const defaultRules = { { blankLine: 'any', prev: ['export', 'import'], next: ['export', 'import'] }, ], 'lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }], + 'no-nested-ternary': 'error', }; module.exports = { diff --git a/app/src/components/v-template-input.vue b/app/src/components/v-template-input.vue index 7323a492cf..88bf3cc888 100644 --- a/app/src/components/v-template-input.vue +++ b/app/src/components/v-template-input.vue @@ -228,8 +228,13 @@ function parseHTML(innerText?: string, isDirectInput = false) { } let newHTML = input.value.innerText; + let caretPos = 0; - const caretPos = isDirectInput ? previousCaretPos : window.getSelection()?.rangeCount ? position(input.value).pos : 0; + if (isDirectInput) { + caretPos = previousCaretPos; + } else if (window.getSelection()?.rangeCount) { + caretPos = position(input.value).pos; + } let lastMatchIndex = 0; const matches = newHTML.match(new RegExp(`${props.captureGroup}(?!)`, 'gi')); diff --git a/app/src/interfaces/input-rich-text-html/useMedia.ts b/app/src/interfaces/input-rich-text-html/useMedia.ts index 714c31bd67..b7a42a1399 100644 --- a/app/src/interfaces/input-rich-text-html/useMedia.ts +++ b/app/src/interfaces/input-rich-text-html/useMedia.ts @@ -124,7 +124,7 @@ export default function useMedia(editor: Ref, imageToken: Ref, imageToken: Ref({ get() { - const arr = color.value !== null ? color.value.rgb().array() : props.opacity ? [0, 0, 0, 1] : [0, 0, 0]; - return arr.length === 4 ? [...arr.slice(0, -1).map(Math.round), arr[3]] : arr.map(Math.round); + if (color.value !== null) { + return roundColorValues(color.value.rgb().array()); + } + + return roundColorValues(props.opacity ? [0, 0, 0, 1] : [0, 0, 0]); }, set(newRGB) { setColor(Color.rgb(newRGB).alpha(newRGB.length === 4 ? newRGB[3] : 1)); @@ -314,8 +317,11 @@ function useColor() { const hsl = computed({ get() { - const arr = color.value !== null ? color.value.hsl().array() : props.opacity ? [0, 0, 0, 1] : [0, 0, 0]; - return arr.length === 4 ? [...arr.slice(0, -1).map(Math.round), arr[3]] : arr.map(Math.round); + if (color.value !== null) { + return roundColorValues(color.value.hsl().array()); + } + + return roundColorValues(props.opacity ? [0, 0, 0, 1] : [0, 0, 0]); }, set(newHSL) { setColor(Color.hsl(newHSL).alpha(newHSL.length === 4 ? newHSL[3] : 1)); @@ -361,6 +367,15 @@ function useColor() { emit('input', getHexa()); } } + + function roundColorValues(arr: number[]): number[] { + if (arr.length === 4) { + // Do not round the opacity + return [...arr.slice(0, -1).map((x) => Math.round(x)), arr[3]]; + } + + return arr.map((x) => Math.round(x)); + } } diff --git a/packages/extensions-sdk/src/cli/commands/helpers/generate-bundle-entrypoint.ts b/packages/extensions-sdk/src/cli/commands/helpers/generate-bundle-entrypoint.ts index 4f30d37707..a309f41ceb 100644 --- a/packages/extensions-sdk/src/cli/commands/helpers/generate-bundle-entrypoint.ts +++ b/packages/extensions-sdk/src/cli/commands/helpers/generate-bundle-entrypoint.ts @@ -9,28 +9,33 @@ export default function generateBundleEntrypoint(mode: 'app' | 'api', entries: E const entriesForTypes = entries.filter((entry) => isIn(entry.type, types)); - const imports = entriesForTypes.map( - (entry, i) => - `import e${i} from './${pathToRelativeUrl( - path.resolve( - isTypeIn(entry, HYBRID_EXTENSION_TYPES) - ? mode === 'app' - ? entry.source.app - : entry.source.api - : entry.source - ) - )}';` - ); + const imports = entriesForTypes.map((entry, index) => { + let entryPath: string; - const exports = types.map( - (type) => - `export const ${pluralize(type)} = [${entriesForTypes - .map((entry, i) => - entry.type === type ? (mode === 'app' ? `e${i}` : `{name:'${entry.name}',config:e${i}}`) : null - ) - .filter((e): e is string => e !== null) - .join(',')}];` - ); + if (isTypeIn(entry, HYBRID_EXTENSION_TYPES)) { + entryPath = mode === 'app' ? entry.source.app : entry.source.api; + } else { + entryPath = entry.source; + } + + return `import e${index} from './${pathToRelativeUrl(path.resolve(entryPath))}';`; + }); + + const exports = types.map((type) => { + const entries = entriesForTypes.reduce((result, entry, index) => { + if (entry.type !== type) return result; + + if (mode === 'app') { + result.push(`e${index}`); + } else { + result.push(`{name:'${entry.name}',config:e${index}}`); + } + + return result; + }, []); + + return `export const ${pluralize(type)} = [${entries.join(',')}];`; + }); return `${imports.join('')}${exports.join('')}`; } diff --git a/tests/blackbox/common/config.ts b/tests/blackbox/common/config.ts index 583b2fdfb9..091a89d2bb 100644 --- a/tests/blackbox/common/config.ts +++ b/tests/blackbox/common/config.ts @@ -32,11 +32,11 @@ const knexConfig = { const allowedLogLevels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']; -const logLevel = process.env.TEST_SAVE_LOGS - ? allowedLogLevels.includes(process.env.TEST_SAVE_LOGS) - ? process.env.TEST_SAVE_LOGS - : 'info' - : 'error'; +let logLevel = 'error'; + +if (process.env.TEST_SAVE_LOGS) { + logLevel = allowedLogLevels.includes(process.env.TEST_SAVE_LOGS) ? process.env.TEST_SAVE_LOGS : 'info'; +} const directusAuthConfig = { AUTH_PROVIDERS: 'saml', diff --git a/tests/blackbox/schema/timezone/timezone-changed-node-tz-america.test.ts b/tests/blackbox/schema/timezone/timezone-changed-node-tz-america.test.ts index a957e2e777..0bd3a94332 100644 --- a/tests/blackbox/schema/timezone/timezone-changed-node-tz-america.test.ts +++ b/tests/blackbox/schema/timezone/timezone-changed-node-tz-america.test.ts @@ -30,13 +30,16 @@ describe('schema', () => { const currentTzOffset = new Date().getTimezoneOffset(); const isWindows = ['win32', 'win64'].includes(process.platform); const newTzOffset = currentTzOffset !== 180 ? 180 : 360; + let newTz: string; // Different timezone format for Windows - const newTz = isWindows - ? String(newTzOffset * 60) - : newTzOffset === 180 - ? 'America/Sao_Paulo' - : 'America/Mexico_City'; + if (isWindows) { + newTz = String(newTzOffset * 60); + } else if (newTzOffset === 180) { + newTz = 'America/Sao_Paulo'; + } else { + newTz = 'America/Mexico_City'; + } const sampleDates: SchemaTimezoneTypesObject[] = []; diff --git a/tests/blackbox/schema/timezone/timezone-changed-node-tz-asia.test.ts b/tests/blackbox/schema/timezone/timezone-changed-node-tz-asia.test.ts index 49dcf3357d..f30a3fc7f1 100644 --- a/tests/blackbox/schema/timezone/timezone-changed-node-tz-asia.test.ts +++ b/tests/blackbox/schema/timezone/timezone-changed-node-tz-asia.test.ts @@ -31,9 +31,16 @@ describe('schema', () => { const isWindows = ['win32', 'win64'].includes(process.platform); const newTzOffset = currentTzOffset !== -540 ? -540 : -240; + let newTz: string; // Different timezone format for Windows - const newTz = isWindows ? String(newTzOffset * 60) : newTzOffset === -540 ? 'Asia/Seoul' : 'Asia/Dubai'; + if (isWindows) { + newTz = String(newTzOffset * 60); + } else if (newTzOffset === -540) { + newTz = 'Asia/Seoul'; + } else { + newTz = 'Asia/Dubai'; + } const sampleDates: SchemaTimezoneTypesObject[] = [];