Skip to content

Commit

Permalink
feat: null intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Dec 7, 2024
1 parent 31ebbf4 commit a1d391b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ export type StringArbitrary = {
export type BooleanArbitrary = {
type: `boolean`
}

export type NullArbitrary = {
type: `null`
}
4 changes: 4 additions & 0 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ const ArbitraryDefinition = ({
return StringArbitrary({ arbitrary })
case `boolean`:
return BooleanArbitrary()
case `null`:

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 161 in src/components.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.
return NullArbitrary()
}
}

Expand Down Expand Up @@ -297,6 +299,8 @@ const StringArbitrary = ({

const BooleanArbitrary = (): Child => code`fc.boolean()`

const NullArbitrary = (): Child => code`fc.constant(null)`

const Options = ({
properties,
emitEmpty = false,
Expand Down
21 changes: 21 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@typespec/compiler'
import type {
Enum,
IntrinsicType,
Model,
Namespace,
Numeric,
Expand Down Expand Up @@ -101,6 +102,8 @@ const convertType = (
return convertEnum(type, options)
case `Scalar`:
return convertScalar(program, type, options)
case `Intrinsic`:
return convertIntrinsic(type)
}

throw new Error(`Unhandled type: ${type.kind}`)
Expand Down Expand Up @@ -250,6 +253,21 @@ const convertString = (
const convertBoolean = (boolean: Scalar): Arbitrary =>
memoize({ type: `boolean`, name: boolean.name })

const convertIntrinsic = (intrinsic: IntrinsicType): Arbitrary => {
switch (intrinsic.name) {
case `null`:
return convertNull(intrinsic)
case `ErrorType`:
case `void`:
case `never`:
case `unknown`:
throw new Error(`Unhandled Intrinsic: ${intrinsic.name}`)
}
}

const convertNull = ($null: IntrinsicType): Arbitrary =>
memoize({ type: `null`, name: $null.name })

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 269 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

const getConstraints = (program: Program, type: Type): Constraints =>
pipe(
entries({
Expand Down Expand Up @@ -324,6 +342,8 @@ const getArbitraryKey = (arbitrary: Arbitrary): ArbitraryKey => {
])
case `boolean`:
return keyalesce([arbitrary.type, arbitrary.name])
case `null`:

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 345 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.
return keyalesce([arbitrary.type, arbitrary.name])

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Property 'name' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Property 'type' does not exist on type 'never'.

Check failure on line 346 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Property 'name' does not exist on type 'never'.
}
}

Expand Down Expand Up @@ -410,6 +430,7 @@ const getDirectArbitraryDependencies = (
case `bytes`:
case `string`:
case `boolean`:
case `null`:

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v20 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (macOS-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.

Check failure on line 433 in src/convert.ts

View workflow job for this annotation

GitHub Actions / Node.js v18 (ubuntu-latest)

Type '"null"' is not comparable to type '"string" | "number" | "bigint" | "boolean" | "record" | "union" | "enum" | "bytes" | "dictionary" | "array"'.
return new Set()
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ test.each([
scalar Boolean extends boolean;
`,
},
{
name: `null`,
code: `
model M {
property: null
}
`,
},

// Models
{
Expand Down
5 changes: 5 additions & 0 deletions test/snapshots/null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as fc from 'fast-check'

export const M = fc.record({
property: fc.constant(null),
});

0 comments on commit a1d391b

Please sign in to comment.