Skip to content

Commit

Permalink
feat: some formatting improvements (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Dec 25, 2024
1 parent 97543ed commit ef11ab1
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 160 deletions.
138 changes: 77 additions & 61 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,6 @@ const NestedArbitraryNamespace = ({
)
}

const Commented = stc(
({ comment, children }: { comment?: string; children?: Child }) =>
ayJoin([comment && Comment({ comment }), children].filter(Boolean), {
joiner: `\n`,
}),
)

const Comment = ({ comment }: { comment: string }): Child => {
const lines = comment.split(`\n`)
if (lines.length <= 1) {
return code`/** ${comment} */`
}

return [`/**`, ...lines.map(line => ` * ${line}`), ` */`].join(`\n`)
}

const Arbitrary = ({
arbitrary,
sharedArbitraries,
Expand Down Expand Up @@ -415,6 +399,7 @@ const NumberArbitrary = ({
args: [
ObjectExpression({
properties: { min: arbitraryMin, max: arbitraryMax },
singlePropertyOneLine: true,
}),
],
})
Expand All @@ -433,6 +418,7 @@ const BigIntArbitrary = ({
min: arbitrary.min == null ? null : `${arbitrary.min}n`,
max: arbitrary.max == null ? null : `${arbitrary.max}n`,
},
singlePropertyOneLine: true,
}),
],
})
Expand All @@ -450,6 +436,7 @@ const StringArbitrary = ({
minLength: arbitrary.minLength,
maxLength: arbitrary.maxLength,
},
singlePropertyOneLine: true,
}),
],
})
Expand Down Expand Up @@ -491,8 +478,9 @@ const ArrayArbitrary = ({
minLength: arbitrary.minItems,
maxLength: arbitrary.maxItems,
},
singlePropertyOneLine: true,
}),
],
].filter(Boolean),
oneLine: true,
})

Expand Down Expand Up @@ -598,21 +586,23 @@ const RecordArbitrary = ({
}),
requiredProperties.size === arbitrary.properties.size
? null
: ObjectExpression({
properties:
requiredProperties.size === 0
? { withDeletedKeys: `true` }
: {
requiredKeys: ArrayExpression({
values: pipe(
requiredProperties,
map(property => StringLiteral({ string: property })),
reduce(toArray()),
),
}),
},
}),
],
: requiredProperties.size === 0
? ObjectExpression({
properties: { withDeletedKeys: `true` },
singlePropertyOneLine: true,
})
: ObjectExpression({
properties: {
requiredKeys: ArrayExpression({
values: pipe(
requiredProperties,
map(property => StringLiteral({ string: property })),
reduce(toArray()),
),
}),
},
}),
].filter(Boolean),
})
}

Expand All @@ -639,15 +629,39 @@ const IntersectionArbitrary = ({
.map(values => Object.assign(...values))
`

const RecursiveReferenceArbitrary = ({
arbitrary,
}: {
arbitrary: ReferenceArbitrary
}): Child => code`tie(${StringLiteral({ string: arbitrary.name })})`

const ArrayExpression = ({ values }: { values: Child[] }): Child =>
code`[${ayJoin(values, { joiner: `, ` })}]`

const Commented = stc(
({ comment, children }: { comment?: string; children?: Child }) =>
ayJoin([comment && Comment({ comment }), children].filter(Boolean), {
joiner: `\n`,
}),
)

const Comment = ({ comment }: { comment: string }): Child => {
const lines = comment.split(`\n`)
if (lines.length <= 1) {
return code`/** ${comment} */`
}

return [`/**`, ...lines.map(line => ` * ${line}`), ` */`].join(`\n`)
}

const ObjectExpression = ({
properties,
emitEmpty = false,
singlePropertyOneLine = false,
}: {
properties: Record<string, Children>
emitEmpty?: boolean
singlePropertyOneLine?: boolean
}): Child => {
const filteredProperties = pipe(
entries(properties),
Expand All @@ -658,30 +672,26 @@ const ObjectExpression = ({
return emitEmpty ? `{}` : ``
}

return ts.ObjectExpression().children(
ayJoin(
pipe(
filteredProperties,
map(
([name, value]) =>
code`${ts.ObjectProperty({
name,
// https://github.com/alloy-framework/alloy/issues/42
value: typeof value === `number` ? String(value) : value,
})},\n`,
),
reduce(toArray()),
),
const objectProperties = pipe(
filteredProperties,
map(([name, value]) =>
ts.ObjectProperty({
name,
// https://github.com/alloy-framework/alloy/issues/42
value: typeof value === `number` ? String(value) : value,
}),
),
reduce(toArray()),
)
return singlePropertyOneLine && filteredProperties.size === 1
? code`{ ${Commas({ values: objectProperties, oneLine: true })} }`
: code`
{
${Commas({ values: objectProperties })}
}
`
}

const RecursiveReferenceArbitrary = ({
arbitrary,
}: {
arbitrary: ReferenceArbitrary
}): Child => code`tie(${StringLiteral({ string: arbitrary.name })})`

const CallExpression = ({
name,
args,
Expand All @@ -690,21 +700,27 @@ const CallExpression = ({
name: string
args: Child[]
oneLine?: boolean
}): Child => {
args = filterChildren(args)
return !oneLine && args.length >= 2
}): Child =>
!oneLine && args.length >= 2
? code`
${name}(
${ayMapJoin(args, arg => code`${arg},`, { joiner: `\n` })}
${Commas({ values: args })}
)
`
: code`${name}(${ayJoin(args, { joiner: `, ` })})`
}
: code`${name}(${Commas({ values: args, oneLine: true })})`

const Commas = ({
values,
oneLine = false,
}: {
values: Child[]
oneLine?: boolean
}): Child =>
oneLine
? ayJoin(values, { joiner: `, ` })
: ayMapJoin(values, value => code`${value},\n`)

const StringLiteral = ({ string }: { string: string }): Child =>
JSON.stringify(string)

const filterChildren = (children: Child[]): Child[] =>
children.filter(child => child != null && child !== `` && child !== false)

export default ArbitraryFile
8 changes: 2 additions & 6 deletions test/snapshots/array/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import * as fc from "fast-check";

export const $Array = fc.array(fc.string());

export const MinItemsArray = fc.array(fc.string(), {
minLength: 3,
});
export const MinItemsArray = fc.array(fc.string(), { minLength: 3 });

export const Min0ItemsArray = fc.array(fc.string());

export const MaxItemsArray = fc.array(fc.string(), {
maxLength: 12,
});
export const MaxItemsArray = fc.array(fc.string(), { maxLength: 12 });

export const MinMaxItemsArray = fc.array(fc.string(), {
minLength: 3,
Expand Down
8 changes: 2 additions & 6 deletions test/snapshots/comments/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const group = fc.letrec(tie => ({
{
property: tie("MultiLineRecursiveModel"),
},
{
withDeletedKeys: true,
},
{ withDeletedKeys: true },
),
}));
/**
Expand All @@ -21,9 +19,7 @@ const group_2 = fc.letrec(tie => ({
{
property: tie("SingleLineRecursiveModel"),
},
{
withDeletedKeys: true,
},
{ withDeletedKeys: true },
),
}));
/** Recursive model comment. */
Expand Down
8 changes: 2 additions & 6 deletions test/snapshots/decimal/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import * as fc from "fast-check";

export const Decimal = fc.double();

export const MinValueDecimal = fc.double({
min: -3.4e+39,
});
export const MinValueDecimal = fc.double({ min: -3.4e+39 });

export const MaxValueDecimal = fc.double({
max: 3.4e+39,
});
export const MaxValueDecimal = fc.double({ max: 3.4e+39 });

export const MinMaxValueDecimal = fc.double({
min: -3.4e+39,
Expand Down
8 changes: 2 additions & 6 deletions test/snapshots/decimal128/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import * as fc from "fast-check";

export const Decimal128 = fc.double();

export const MinValueDecimal128 = fc.double({
min: -3.4e+39,
});
export const MinValueDecimal128 = fc.double({ min: -3.4e+39 });

export const MaxValueDecimal128 = fc.double({
max: 3.4e+39,
});
export const MaxValueDecimal128 = fc.double({ max: 3.4e+39 });

export const MinMaxValueDecimal128 = fc.double({
min: -3.4e+39,
Expand Down
12 changes: 3 additions & 9 deletions test/snapshots/float32/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ import * as fc from "fast-check";

export const Float32 = fc.float();

export const MinValueFloat32 = fc.float({
min: -3.140000104904175,
});
export const MinValueFloat32 = fc.float({ min: -3.140000104904175 });

export const MinValue0Float32 = fc.float({
min: 0,
});
export const MinValue0Float32 = fc.float({ min: 0 });

export const MaxValueFloat32 = fc.float({
max: 3.140000104904175,
});
export const MaxValueFloat32 = fc.float({ max: 3.140000104904175 });

export const MinMaxValueFloat32 = fc.float({
min: -3.140000104904175,
Expand Down
8 changes: 2 additions & 6 deletions test/snapshots/float64/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import * as fc from "fast-check";

export const Float64 = fc.double();

export const MinValueFloat64 = fc.double({
min: -3.4e+39,
});
export const MinValueFloat64 = fc.double({ min: -3.4e+39 });

export const MaxValueFloat64 = fc.double({
max: 3.4e+39,
});
export const MaxValueFloat64 = fc.double({ max: 3.4e+39 });

export const MinMaxValueFloat64 = fc.double({
min: -3.4e+39,
Expand Down
4 changes: 1 addition & 3 deletions test/snapshots/int16/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export const MinValueInt16 = fc.integer({
max: 32767,
});

export const Min0ValueInt16 = fc.nat({
max: 32767,
});
export const Min0ValueInt16 = fc.nat({ max: 32767 });

export const MaxValueInt16 = fc.integer({
min: -32768,
Expand Down
8 changes: 2 additions & 6 deletions test/snapshots/int32/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import * as fc from "fast-check";

export const Int32 = fc.integer();

export const MinValueInt32 = fc.integer({
min: -40000,
});
export const MinValueInt32 = fc.integer({ min: -40000 });

export const Min0ValueInt32 = fc.nat();

export const MaxValueInt32 = fc.integer({
max: 40000,
});
export const MaxValueInt32 = fc.integer({ max: 40000 });

export const MinMaxValueInt32 = fc.integer({
min: -40000,
Expand Down
4 changes: 1 addition & 3 deletions test/snapshots/int8/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export const MinValueInt8 = fc.integer({
max: 127,
});

export const Min0ValueInt8 = fc.nat({
max: 127,
});
export const Min0ValueInt8 = fc.nat({ max: 127 });

export const MaxValueInt8 = fc.integer({
min: -128,
Expand Down
12 changes: 3 additions & 9 deletions test/snapshots/integer/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ import * as fc from "fast-check";

export const Integer = fc.bigInt();

export const MinValueInteger = fc.bigInt({
min: 92233720368547758000n,
});
export const MinValueInteger = fc.bigInt({ min: 92233720368547758000n });

export const MinValue0Integer = fc.bigInt({
min: 0n,
});
export const MinValue0Integer = fc.bigInt({ min: 0n });

export const MaxValueInteger = fc.bigInt({
max: 922337203685477580000n,
});
export const MaxValueInteger = fc.bigInt({ max: 922337203685477580000n });

export const MinMaxValueInteger = fc.bigInt({
min: 92233720368547758000n,
Expand Down
4 changes: 1 addition & 3 deletions test/snapshots/model-default-property/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,5 @@ export const DefaultPropertiesModel = fc.record(
}),
),
},
{
withDeletedKeys: true,
},
{ withDeletedKeys: true },
);
Loading

0 comments on commit ef11ab1

Please sign in to comment.