From dc118cd4cbc02eb0769e2d0d59ffe551b17c2272 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Tue, 5 Mar 2024 18:56:23 +0100 Subject: [PATCH] infra(unicorn): switch-case-braces --- .eslintrc.cjs | 1 - scripts/apidoc/signature.ts | 42 ++++++++++----- src/modules/color/index.ts | 54 ++++++++++++++----- src/modules/helpers/eval.ts | 10 ++-- src/modules/helpers/index.ts | 3 +- src/modules/location/index.ts | 11 ++-- src/modules/person/index.ts | 9 ++-- src/modules/string/index.ts | 22 +++++--- src/modules/system/index.ts | 15 ++++-- test/scripts/apidoc/verify-jsdoc-tags.spec.ts | 12 +++-- 10 files changed, 126 insertions(+), 53 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 198153defc3..09b3f68a0c9 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -57,7 +57,6 @@ module.exports = defineConfig({ 'unicorn/prefer-string-slice': 'off', 'unicorn/prevent-abbreviations': 'off', 'unicorn/require-array-join-separator': 'off', - 'unicorn/switch-case-braces': 'off', '@typescript-eslint/array-type': [ 'error', diff --git a/scripts/apidoc/signature.ts b/scripts/apidoc/signature.ts index 82d4047ef7d..64910534647 100644 --- a/scripts/apidoc/signature.ts +++ b/scripts/apidoc/signature.ts @@ -145,13 +145,15 @@ async function analyzeParameterOptions( } switch (parameterType.type) { - case 'array': + case 'array': { return analyzeParameterOptions(`${name}[]`, parameterType.elementType); + } - case 'union': + case 'union': { return Promise.all( parameterType.types.map((type) => analyzeParameterOptions(name, type)) ).then((options) => options.flat()); + } case 'reflection': { const properties = parameterType.declaration.children ?? []; @@ -175,11 +177,13 @@ async function analyzeParameterOptions( ); } - case 'typeOperator': + case 'typeOperator': { return analyzeParameterOptions(name, parameterType.target); + } - default: + default: { return []; + } } } @@ -200,13 +204,14 @@ async function typeToText(type_?: Type, short = false): Promise { return isComplexType ? `Array<${text}>` : `${text}[]`; } - case 'union': + case 'union': { return (await Promise.all(type.types.map((t) => typeToText(t, short)))) .map((t) => (t.includes('=>') ? `(${t})` : t)) .sort() .join(' | '); + } - case 'reference': + case 'reference': { if (!type.typeArguments || type.typeArguments.length === 0) { const reflection = type.reflection as DeclarationReflection | undefined; const reflectionType = reflection?.type; @@ -229,18 +234,22 @@ async function typeToText(type_?: Type, short = false): Promise { return `${type.name}<${( await Promise.all(type.typeArguments.map((t) => typeToText(t, short))) ).join(', ')}>`; + } - case 'reflection': + case 'reflection': { return declarationTypeToText(type.declaration, short); + } - case 'indexedAccess': + case 'indexedAccess': { return `${await typeToText(type.objectType, short)}[${await typeToText( type.indexType, short )}]`; + } - case 'literal': + case 'literal': { return (await formatTypescript(type.toString())).replace(/;\n$/, ''); + } case 'typeOperator': { const text = await typeToText(type.target, short); @@ -251,8 +260,9 @@ async function typeToText(type_?: Type, short = false): Promise { return `${type.operator} ${text}`; } - default: + default: { return type.toString(); + } } } @@ -261,13 +271,15 @@ async function declarationTypeToText( short = false ): Promise { switch (declaration.kind) { - case ReflectionKind.Method: + case ReflectionKind.Method: { return signatureTypeToText(declaration.signatures?.[0]); + } - case ReflectionKind.Property: + case ReflectionKind.Property: { return typeToText(declaration.type); + } - case ReflectionKind.TypeLiteral: + case ReflectionKind.TypeLiteral: { if (declaration.children?.length) { if (short) { // This is too long for the parameter table, thus we abbreviate this. @@ -288,9 +300,11 @@ async function declarationTypeToText( } return declaration.toString(); + } - default: + default: { return declaration.toString(); + } } } diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 779bd20b6ce..2b355062168 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -60,12 +60,16 @@ function formatHexColor( const { prefix, casing } = options; switch (casing) { - case 'upper': + case 'upper': { hexColor = hexColor.toUpperCase(); break; - case 'lower': + } + + case 'lower': { hexColor = hexColor.toLowerCase(); break; + } + case 'mixed': // Do nothing } @@ -111,32 +115,49 @@ function toCSS( ): string { const percentage = (value: number) => Math.round(value * 100); switch (cssFunction) { - case 'rgba': + case 'rgba': { return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; - case 'color': + } + + case 'color': { return `color(${space} ${values[0]} ${values[1]} ${values[2]})`; - case 'cmyk': + } + + case 'cmyk': { return `cmyk(${percentage(values[0])}%, ${percentage( values[1] )}%, ${percentage(values[2])}%, ${percentage(values[3])}%)`; - case 'hsl': + } + + case 'hsl': { return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage( values[2] )}%)`; - case 'hsla': + } + + case 'hsla': { return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage( values[2] )}% / ${percentage(values[3])})`; - case 'hwb': + } + + case 'hwb': { return `hwb(${values[0]} ${percentage(values[1])}% ${percentage( values[2] )}%)`; - case 'lab': + } + + case 'lab': { return `lab(${percentage(values[0])}% ${values[1]} ${values[2]})`; - case 'lch': + } + + case 'lch': { return `lch(${percentage(values[0])}% ${values[1]} ${values[2]})`; - case 'rgb': + } + + case 'rgb': { return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; + } } } @@ -155,12 +176,17 @@ function toColorFormat( space: CssSpaceType = 'sRGB' ): string | number[] { switch (format) { - case 'css': + case 'css': { return toCSS(values, cssFunction, space); - case 'binary': + } + + case 'binary': { return toBinary(values); - case 'decimal': + } + + case 'decimal': { return values; + } } } diff --git a/src/modules/helpers/eval.ts b/src/modules/helpers/eval.ts index 033c5d35d80..4a861ec0700 100644 --- a/src/modules/helpers/eval.ts +++ b/src/modules/helpers/eval.ts @@ -119,12 +119,15 @@ function evalProcessFunction( switch (nextChar) { case '.': case '(': - case undefined: + case undefined: { break; // valid - default: + } + + default: { throw new FakerError( `Expected dot ('.'), open parenthesis ('('), or nothing after function call but got '${nextChar}'` ); + } } return [ @@ -223,7 +226,8 @@ function resolveProperty(entrypoint: unknown, key: string): unknown { return entrypoint?.[key as keyof typeof entrypoint]; } - default: + default: { return undefined; + } } } diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index b21f13e7188..b86b2cd7d32 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -56,8 +56,9 @@ function getRepetitionsBasedOnQuantifierParameters( break; } - default: + default: { throw new FakerError('Unknown quantifier symbol provided.'); + } } } else if (quantifierMin != null && quantifierMax != null) { repetitions = faker.number.int({ diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index 9712076629e..92284560600 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -355,12 +355,17 @@ export class LocationModule extends ModuleBase { const { variant = 'alpha-2' } = options; const key = (() => { switch (variant) { - case 'numeric': + case 'numeric': { return 'numeric'; - case 'alpha-3': + } + + case 'alpha-3': { return 'alpha3'; - case 'alpha-2': + } + + case 'alpha-2': { return 'alpha2'; + } } })(); diff --git a/src/modules/person/index.ts b/src/modules/person/index.ts index dce8a45f533..6bb4f7766c0 100644 --- a/src/modules/person/index.ts +++ b/src/modules/person/index.ts @@ -37,17 +37,20 @@ function selectDefinition( let values: T[] | undefined | null; switch (sex) { - case Sex.Female: + case Sex.Female: { values = female; break; + } - case Sex.Male: + case Sex.Male: { values = male; break; + } - default: + default: { values = generic; break; + } } if (values == null) { diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index cf469a2cd89..cfee0dc13fe 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -223,15 +223,20 @@ export class StringModule extends SimpleModuleBase { let charsArray: string[]; switch (casing) { - case 'upper': + case 'upper': { charsArray = [...UPPER_CHARS]; break; - case 'lower': + } + + case 'lower': { charsArray = [...LOWER_CHARS]; break; - case 'mixed': + } + + case 'mixed': { charsArray = [...LOWER_CHARS, ...UPPER_CHARS]; break; + } } charsArray = charsArray.filter((elem) => !exclude.includes(elem)); @@ -313,15 +318,20 @@ export class StringModule extends SimpleModuleBase { let charsArray = [...DIGIT_CHARS]; switch (casing) { - case 'upper': + case 'upper': { charsArray.push(...UPPER_CHARS); break; - case 'lower': + } + + case 'lower': { charsArray.push(...LOWER_CHARS); break; - case 'mixed': + } + + case 'mixed': { charsArray.push(...LOWER_CHARS, ...UPPER_CHARS); break; + } } charsArray = charsArray.filter((elem) => !exclude.includes(elem)); diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index ee0d2600fb7..3468bbb0a79 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -266,23 +266,30 @@ export class SystemModule extends ModuleBase { let prefix = ''; const digit = () => this.faker.string.numeric({ allowLeadingZeros: true }); switch (interfaceSchema) { - case 'index': + case 'index': { suffix = digit(); break; - case 'slot': + } + + case 'slot': { suffix = `${digit()}${ this.faker.helpers.maybe(() => `f${digit()}`) ?? '' }${this.faker.helpers.maybe(() => `d${digit()}`) ?? ''}`; break; - case 'mac': + } + + case 'mac': { suffix = this.faker.internet.mac(''); break; - case 'pci': + } + + case 'pci': { prefix = this.faker.helpers.maybe(() => `P${digit()}`) ?? ''; suffix = `${digit()}s${digit()}${ this.faker.helpers.maybe(() => `f${digit()}`) ?? '' }${this.faker.helpers.maybe(() => `d${digit()}`) ?? ''}`; break; + } } return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`; diff --git a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts index 42882474ccf..da34316e96c 100644 --- a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts +++ b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts @@ -98,18 +98,20 @@ function assertNestedParameterDefault( } switch (parameterType.type) { - case 'array': + case 'array': { return assertNestedParameterDefault( `${name}[]`, parameterType.elementType ); + } - case 'union': + case 'union': { for (const type of parameterType.types) { assertNestedParameterDefault(name, type); } return; + } case 'reflection': { for (const property of parameterType.declaration.children ?? []) { @@ -131,11 +133,13 @@ function assertNestedParameterDefault( return; } - case 'typeOperator': + case 'typeOperator': { return assertNestedParameterDefault(name, parameterType.target); + } - default: + default: { return; + } } }