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

infra(unicorn): switch-case-braces #2721

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
42 changes: 28 additions & 14 deletions scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [];
Expand All @@ -175,11 +177,13 @@ async function analyzeParameterOptions(
);
}

case 'typeOperator':
case 'typeOperator': {
return analyzeParameterOptions(name, parameterType.target);
}

default:
default: {
return [];
}
}
}

Expand All @@ -200,13 +204,14 @@ async function typeToText(type_?: Type, short = false): Promise<string> {
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;
Expand All @@ -229,18 +234,22 @@ async function typeToText(type_?: Type, short = false): Promise<string> {
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);
Expand All @@ -251,8 +260,9 @@ async function typeToText(type_?: Type, short = false): Promise<string> {
return `${type.operator} ${text}`;
}

default:
default: {
return type.toString();
}
}
}

Expand All @@ -261,13 +271,15 @@ async function declarationTypeToText(
short = false
): Promise<string> {
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.
Expand All @@ -288,9 +300,11 @@ async function declarationTypeToText(
}

return declaration.toString();
}

default:
default: {
return declaration.toString();
}
}
}

Expand Down
54 changes: 40 additions & 14 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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]})`;
}
}
}

Expand All @@ -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;
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/modules/helpers/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -223,7 +226,8 @@ function resolveProperty(entrypoint: unknown, key: string): unknown {
return entrypoint?.[key as keyof typeof entrypoint];
}

default:
default: {
return undefined;
}
}
}
3 changes: 2 additions & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
11 changes: 8 additions & 3 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
})();

Expand Down
9 changes: 6 additions & 3 deletions src/modules/person/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ function selectDefinition<T>(
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) {
Expand Down
22 changes: 16 additions & 6 deletions src/modules/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 11 additions & 4 deletions src/modules/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
Loading