Skip to content

Commit

Permalink
Clean Up Assertions + Workspace Tweaks (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Dec 18, 2024
1 parent 989b634 commit dc5d125
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 1,143 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
- uses: actions/setup-node@v4
with:
cache: npm
cache-dependency-path: target/npm/package-lock.json
cache-dependency-path: .liminal/npm/package-lock.json
node-version: 21.x
registry-url: "https://registry.npmjs.org"
- run: cd target/npm && npm version ${{ github.event.release.tag_name }} --no-git-tag-version --allow-same-version && npm publish
- run: cd .liminal/npm && npm version ${{ github.event.release.tag_name }} --no-git-tag-version --allow-same-version && npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
jsr:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.env
.liminal
target
18 changes: 10 additions & 8 deletions core/DescriptionContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { recombine } from "../util/mod.ts"
import { DescriptionParamKey } from "./annotations/mod.ts"
import type { PartialType } from "./mod.ts"
import type { Type } from "./Type.ts"

Expand All @@ -9,7 +10,7 @@ export function description(type: Type<unknown>): string | undefined {
export class DescriptionContext {
constructor(
readonly pins: Map<PartialType, string> = new Map(),
readonly args: Record<symbol, string> = {},
readonly args: Record<symbol, unknown> = {},
) {}

pin = (type: PartialType): string => {
Expand Down Expand Up @@ -54,19 +55,20 @@ export class DescriptionContext {
break
}
case "Param": {
const arg = this.args[annotation.key]!
segments.push(arg)
const arg = this.args[annotation.key]
if (typeof arg === "string") {
segments.push(arg)
} else if (typeof arg === "object" && arg !== null && DescriptionParamKey in arg) {
segments.push(arg[DescriptionParamKey] as string)
}
break
}
case "Arg": {
this.args[annotation.key] = annotation.value as string
this.args[annotation.key] = annotation.value
break
}
case "Assertion": {
const { description, args } = annotation
assertionDescriptions.push(
typeof description === "string" ? description : description(...args ?? []),
)
assertionDescriptions.push(annotation.description)
break
}
}
Expand Down
6 changes: 4 additions & 2 deletions core/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import type { Param, TemplatePart } from "./annotations/mod.ts"
import type { ReduceDependencies } from "./ReduceDependencies.ts"

export interface Type<T, D extends symbol = never> {
<A extends Array<Annotation<T>>>(
...annotations: A | Array<Annotation<T>> & { length: never }
): Type<T, ReduceDependencies<D, A>>

<A extends Array<TemplatePart>>(
template: TemplateStringsArray,
...descriptionParts: A
): Type<T, ReduceDependencies<D, A>>

<A extends Array<Annotation<T>>>(...annotations: A): Type<T, ReduceDependencies<D, A>>

T: T
D: D

Expand Down
43 changes: 6 additions & 37 deletions core/annotations/Assertion.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
import type { PromiseOr } from "../../util/mod.ts"

export interface Assertion<T = any, A extends Array<unknown> = Array<any>> {
export interface Assertion<T = any> {
type: "Assertion"
description: string | ((...args: A) => string)
f?: (value: T, ...args: A) => PromiseOr<void>
args?: A
description: string
f?: (value: T) => PromiseOr<void>
}

export function Assertion<T>(
description: string,
f?: (value: T) => PromiseOr<void>,
): Assertion<T, []>
export function Assertion<T, A extends unknown[]>(
description: (...args: A) => string,
f?: (value: T, ...args: A) => PromiseOr<void>,
...args: A
): Assertion<T, A>
export function Assertion<T, A extends unknown[]>(
description: (...args: A) => string,
f?: (value: T, ...args: A) => PromiseOr<void>,
): (...args: A) => Assertion<T, A>
export function Assertion<T>(
description: string | ((...args: unknown[]) => string),
f?: (value: T, ...args: unknown[]) => PromiseOr<void>,
...args: unknown[]
): Assertion<T> | ((...args: unknown[]) => Assertion<T>) {
if (typeof description === "string") {
return {
type: "Assertion",
description,
f,
}
}
if (args.length) {
return {
type: "Assertion",
description,
f,
args,
}
}
return (...args) => ({
): Assertion<T> {
return {
type: "Assertion",
description,
f,
args,
})
}
}
4 changes: 2 additions & 2 deletions core/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ const visit = TypeVisitor<AssertContext, void>({
if (annotationDiagnostics) {
type.annotations
.filter((annotation) => typeof annotation === "object" && annotation?.type === "Assertion")
.forEach(({ f, args }) => {
.forEach(({ f }) => {
if (f) {
annotationDiagnostics.push((async () => {
try {
await f(ctx.value, ...args ?? [])
await f(ctx.value)
} catch (exception: unknown) {
return {
type,
Expand Down
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
}],
"enableGlobDot": true,
"dictionaries": ["project-words"],
"ignorePaths": [".git", ".liminal", "LICENSE", "target"]
"ignorePaths": [".git", ".liminal", "LICENSE"]
}
6 changes: 3 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"tasks": {
"mod": {
"description": "Regenerate barrels and run formatting.",
"command": "deno run -A https://deno.land/x/moderate@0.0.5/mod.ts --exclude '*.test.ts' --exclude target --exclude '*.node.ts' && dprint fmt"
"command": "deno run -A https://deno.land/x/moderate@0.0.5/mod.ts --exclude .liminal --exclude '*.test.ts' --exclude '*.node.ts' && dprint fmt"
},
"check": {
"description": "Ensure all TypeScript files pass typechecking and linting.",
Expand All @@ -21,15 +21,15 @@
"command": "deno test -A -- --update"
},
"npm": {
"description": "Build (`target/npm`) for use within Node.js and Bun projects.",
"description": "Build (`.liminal/npm`) for use within Node.js and Bun projects.",
"command": "deno run -A tasks/npm.ts"
},
"docs": {
"description": "Run vitepress subcommands within the documentation directory.",
"command": "cd docs && npm i && ./node_modules/.bin/vitepress"
}
},
"exclude": ["docs", "target"],
"exclude": [".liminal", "docs"],
"compilerOptions": {
"lib": ["deno.window", "dom", "dom.iterable"],
"noFallthroughCasesInSwitch": true,
Expand Down
Loading

0 comments on commit dc5d125

Please sign in to comment.