aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-03-06 17:35:53 +0100
committerGitHub <[email protected]>2024-03-06 16:35:53 +0000
commit2716865a0441b8502c8d0ee3efbdbe1bcebe0101 (patch)
tree2b98c5b34f4dfbd4eab56e8463b9b9f5c72f428a
parentade91fd30d6b5fbcc2ab2534502467b1e0f3b086 (diff)
downloadfaker-2716865a0441b8502c8d0ee3efbdbe1bcebe0101.tar.xz
faker-2716865a0441b8502c8d0ee3efbdbe1bcebe0101.zip
infra(unicorn): switch-case-braces (#2721)
-rw-r--r--.eslintrc.cjs1
-rw-r--r--scripts/apidoc/signature.ts42
-rw-r--r--src/modules/color/index.ts54
-rw-r--r--src/modules/helpers/eval.ts10
-rw-r--r--src/modules/helpers/index.ts3
-rw-r--r--src/modules/location/index.ts11
-rw-r--r--src/modules/person/index.ts9
-rw-r--r--src/modules/string/index.ts22
-rw-r--r--src/modules/system/index.ts15
-rw-r--r--test/scripts/apidoc/verify-jsdoc-tags.spec.ts12
10 files changed, 126 insertions, 53 deletions
diff --git a/.eslintrc.cjs b/.eslintrc.cjs
index 198153de..09b3f68a 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 82d4047e..64910534 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<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;
@@ -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);
@@ -251,8 +260,9 @@ async function typeToText(type_?: Type, short = false): Promise<string> {
return `${type.operator} ${text}`;
}
- default:
+ default: {
return type.toString();
+ }
}
}
@@ -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.
@@ -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 779bd20b..2b355062 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 033c5d35..4a861ec0 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 b21f13e7..b86b2cd7 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 97120766..92284560 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 dce8a45f..6bb4f776 100644
--- a/src/modules/person/index.ts
+++ b/src/modules/person/index.ts
@@ -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) {
diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts
index cf469a2c..cfee0dc1 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 ee0d2600..3468bbb0 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 42882474..da34316e 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;
+ }
}
}