Skip to content

Commit

Permalink
refactor: use references for name deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 8, 2024
1 parent 5e7d5a0 commit 345f5a8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 217 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@alloy-js/typescript": "^0.3.0",
"keyalesce": "^2.2.0",
"lfi": "^3.8.0",
"pascalcase": "^2.0.0",
"toposort": "^2.0.2"
},
"peerDependencies": {
Expand Down
23 changes: 0 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 19 additions & 20 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type ArbitraryNamespace = {
name: string
namespaces: ArbitraryNamespace[]
nameToArbitrary: Map<string, Arbitrary>
arbitraryToName: Map<Arbitrary, string>
nameToArbitrary: Map<string, ReferenceArbitrary>
arbitraryToName: Map<ReferenceArbitrary, string>
}

export type Arbitrary =
Expand All @@ -16,81 +16,80 @@ export type Arbitrary =
| MergedArbitrary
| ReferenceArbitrary

export type BaseArbitrary = { name: string }

export type IntrinsicArbitrary =
| NullArbitrary
| UndefinedArbitrary
| NeverArbitrary
| UnknownArbitrary
export type NullArbitrary = BaseArbitrary & { type: `null` }
export type UndefinedArbitrary = BaseArbitrary & { type: `undefined` }
export type NeverArbitrary = BaseArbitrary & { type: `never` }
export type UnknownArbitrary = BaseArbitrary & { type: `unknown` }
export type NullArbitrary = { type: `null` }
export type UndefinedArbitrary = { type: `undefined` }
export type NeverArbitrary = { type: `never` }
export type UnknownArbitrary = { type: `unknown` }

export type ScalarArbitrary =
| BooleanArbitrary
| NumberArbitrary
| BigIntArbitrary
| StringArbitrary
| BytesArbitrary
export type BooleanArbitrary = BaseArbitrary & {
export type BooleanArbitrary = {
type: `boolean`
}
export type NumberArbitrary = BaseArbitrary & {
export type NumberArbitrary = {
type: `number`
min: number
max: number
isInteger: boolean
}
export type BigIntArbitrary = BaseArbitrary & {
export type BigIntArbitrary = {
type: `bigint`
min?: bigint
max?: bigint
}
export type StringArbitrary = BaseArbitrary & {
export type StringArbitrary = {
type: `string`
minLength?: number
maxLength?: number
}
export type BytesArbitrary = BaseArbitrary & {
export type BytesArbitrary = {
type: `bytes`
}

export type EnumArbitrary = BaseArbitrary & {
export type EnumArbitrary = {
type: `enum`
values: string[]
}

export type ArrayArbitrary = BaseArbitrary & {
export type ArrayArbitrary = {
type: `array`
value: Arbitrary
minItems?: number
maxItems?: number
}

export type DictionaryArbitrary = BaseArbitrary & {
export type DictionaryArbitrary = {
type: `dictionary`
key: Arbitrary
value: Arbitrary
}

export type UnionArbitrary = BaseArbitrary & {
export type UnionArbitrary = {
type: `union`
variants: Arbitrary[]
}

export type RecordArbitrary = BaseArbitrary & {
export type RecordArbitrary = {
type: `record`
properties: Map<string, Arbitrary>
}

export type MergedArbitrary = BaseArbitrary & {
export type MergedArbitrary = {
type: `merged`
arbitraries: Arbitrary[]
}

export type ReferenceArbitrary = BaseArbitrary & {
export type ReferenceArbitrary = {
type: `reference`
name: string
arbitrary: Arbitrary
}
23 changes: 12 additions & 11 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
MergedArbitrary,
NumberArbitrary,
RecordArbitrary,
ReferenceArbitrary,
StringArbitrary,
UnionArbitrary,
} from './arbitrary.ts'
Expand All @@ -35,7 +36,7 @@ const ArbitraryFile = ({
sharedArbitraries,
}: {
namespace: ArbitraryNamespace
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child =>
ay.Output().children(ts.SourceFile({ path: `arbitraries.js` }).code`
import * as fc from 'fast-check';
Expand All @@ -48,7 +49,7 @@ const GlobalArbitraryNamespace = ({
sharedArbitraries,
}: {
namespace: ArbitraryNamespace
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child =>
ayJoin(
[
Expand Down Expand Up @@ -94,7 +95,7 @@ const NestedArbitraryNamespace = ({
sharedArbitraries,
}: {
namespace: ArbitraryNamespace
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child =>
ts.ObjectExpression().children(
ayJoin(
Expand Down Expand Up @@ -125,9 +126,9 @@ const Arbitrary = ({
sharedArbitraries,
}: {
arbitrary: Arbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child =>
sharedArbitraries.has(arbitrary)
arbitrary.type === `reference` && sharedArbitraries.has(arbitrary)
? refkey(arbitrary)
: ArbitraryDefinition({ arbitrary, sharedArbitraries })

Expand All @@ -136,7 +137,7 @@ const ArbitraryDefinition = ({
sharedArbitraries,
}: {
arbitrary: Arbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child => {
switch (arbitrary.type) {
case `null`:
Expand Down Expand Up @@ -279,7 +280,7 @@ const ArrayArbitrary = ({
sharedArbitraries,
}: {
arbitrary: ArrayArbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child => {
const options = Options({
properties: new Map([
Expand All @@ -299,7 +300,7 @@ const DictionaryArbitrary = ({
sharedArbitraries,
}: {
arbitrary: DictionaryArbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child => {
const Key = Arbitrary({ arbitrary: arbitrary.key, sharedArbitraries })
const Value = Arbitrary({ arbitrary: arbitrary.value, sharedArbitraries })
Expand All @@ -311,7 +312,7 @@ const UnionArbitrary = ({
sharedArbitraries,
}: {
arbitrary: UnionArbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child =>
code`fc.oneof(\n${ay
.Indent()
Expand All @@ -329,7 +330,7 @@ const RecordArbitrary = ({
sharedArbitraries,
}: {
arbitrary: RecordArbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child =>
code`fc.record(${Options({
properties: pipe(
Expand All @@ -348,7 +349,7 @@ const MergedArbitrary = ({
sharedArbitraries,
}: {
arbitrary: MergedArbitrary
sharedArbitraries: ReadonlySet<Arbitrary>
sharedArbitraries: ReadonlySet<ReferenceArbitrary>
}): Child => code`
fc
.tuple(
Expand Down
Loading

0 comments on commit 345f5a8

Please sign in to comment.