Skip to content

Commit

Permalink
refactor(attest): split up type assertion data by kind
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed May 1, 2024
1 parent 2c99448 commit 2398217
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 44 deletions.
13 changes: 4 additions & 9 deletions ark/attest/assert/attest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import { getBenchCtx } from "../bench/bench.js"
import type { Measure } from "../bench/measure.js"
import { instantiationDataHandler } from "../bench/type.js"
import {
getTypeRelationshipAssertionsAtPosition,
getTypeAssertionsAtPosition,
type VersionedTypeAssertion
} from "../cache/getCachedAssertions.js"
import type {
TypeBenchmarkingAssertionData,
TypeRelationshipAssertionData
} from "../cache/writeAssertionCache.js"
import { getConfig, type AttestConfig } from "../config.js"
import { assertEquals, typeEqualityMapping } from "./assertions.js"
import {
Expand Down Expand Up @@ -41,8 +37,8 @@ export type AssertionContext = {
position: SourcePosition
defaultExpected?: unknown
assertionStack: string
typeRelationshipAssertionEntries?: VersionedTypeAssertion<TypeRelationshipAssertionData>[]
typeBenchmarkingAssertionEntries?: VersionedTypeAssertion<TypeBenchmarkingAssertionData>[]
typeRelationshipAssertionEntries?: VersionedTypeAssertion<"type">[]
typeBenchmarkingAssertionEntries?: VersionedTypeAssertion<"bench">[]
lastSnapName?: string
}

Expand All @@ -67,8 +63,7 @@ export const attestInternal = (
...ctxHooks
}
if (!cfg.skipTypes) {
ctx.typeRelationshipAssertionEntries =
getTypeRelationshipAssertionsAtPosition(position)
ctx.typeRelationshipAssertionEntries = getTypeAssertionsAtPosition(position)
if (ctx.typeRelationshipAssertionEntries[0][1].typeArgs[0]) {
// if there is an expected type arg, check it immediately
assertEquals(undefined, typeEqualityMapping, ctx)
Expand Down
4 changes: 2 additions & 2 deletions ark/attest/bench/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { caller } from "@arktype/fs"
import ts from "typescript"
import { getTypeBenchAssertionsAtPosition } from "../cache/getCachedAssertions.js"
import { getBenchAssertionsAtPosition } from "../cache/getCachedAssertions.js"
import {
TsServer,
getAbsolutePosition,
Expand Down Expand Up @@ -82,7 +82,7 @@ export const instantiationDataHandler = (
const instantiationsContributed =
isBenchFunction ?
getContributedInstantiations(ctx)
: getTypeBenchAssertionsAtPosition(ctx.benchCallPosition)[0][1].count
: getBenchAssertionsAtPosition(ctx.benchCallPosition)[0][1].count

const comparison: MeasureComparison<TypeUnit> = createTypeComparison(
instantiationsContributed,
Expand Down
41 changes: 14 additions & 27 deletions ark/attest/cache/getCachedAssertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type {
AssertionsByFile,
LinePositionRange,
TypeAssertionData,
TypeBenchmarkingAssertionData,
TypeRelationshipAssertionData
TypeAssertionKind
} from "./writeAssertionCache.js"

export type VersionedAssertionsByFile = [
Expand Down Expand Up @@ -72,22 +71,18 @@ const isPositionWithinRange = (
}

export type VersionedTypeAssertion<
data extends TypeAssertionData = TypeAssertionData
> = [tsVersion: string, assertionData: data]
kind extends TypeAssertionKind = TypeAssertionKind
> = [tsVersion: string, assertionData: TypeAssertionData<kind>]

type AssertionKind = "bench" | "type"

const getTypeAssertionsAtPosition = <T extends TypeAssertionData>(
const getAssertionsOfKindAtPosition = <kind extends TypeAssertionKind>(
position: SourcePosition,
assertionType: AssertionKind
): VersionedTypeAssertion<T>[] => {
kind: kind
): VersionedTypeAssertion<kind>[] => {
const fileKey = getFileKey(position.file)
return getCachedAssertionEntries().map(
([version, typeRelationshipAssertions, BenchAssertionAssertions]) => {
const assertions =
assertionType === "type" ?
typeRelationshipAssertions
: BenchAssertionAssertions
kind === "type" ? typeRelationshipAssertions : BenchAssertionAssertions
if (!assertions[fileKey]) {
throw new Error(
`Found no assertion data for '${fileKey}' for TypeScript version ${version}.`
Expand All @@ -109,25 +104,17 @@ const getTypeAssertionsAtPosition = <T extends TypeAssertionData>(
Are sourcemaps enabled and working properly?`
)
}
return [version, matchingAssertion] as VersionedTypeAssertion<T>
return [version, matchingAssertion] as VersionedTypeAssertion<kind>
}
)
}

export const getTypeRelationshipAssertionsAtPosition = (
export const getTypeAssertionsAtPosition = (
position: SourcePosition
): VersionedTypeAssertion<TypeRelationshipAssertionData>[] => {
return getTypeAssertionsAtPosition<TypeRelationshipAssertionData>(
position,
"type"
)
}
): VersionedTypeAssertion<"type">[] =>
getAssertionsOfKindAtPosition(position, "type")

export const getTypeBenchAssertionsAtPosition = (
export const getBenchAssertionsAtPosition = (
position: SourcePosition
): VersionedTypeAssertion<TypeBenchmarkingAssertionData>[] => {
return getTypeAssertionsAtPosition<TypeBenchmarkingAssertionData>(
position,
"bench"
)
}
): VersionedTypeAssertion<"bench">[] =>
getAssertionsOfKindAtPosition(position, "bench")
2 changes: 1 addition & 1 deletion ark/attest/cache/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ const getBaselineSourceFile = (originalFile: ts.SourceFile): string => {
)
})
return baselineSourceFileText
}
}
10 changes: 7 additions & 3 deletions ark/attest/cache/writeAssertionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ export type TypeBenchmarkingAssertionData = {
count: number
}

export type TypeAssertionData =
| TypeRelationshipAssertionData
| TypeBenchmarkingAssertionData
export type TypeAssertionKind = "bench" | "type"

export type TypeAssertionData<
kind extends TypeAssertionKind = TypeAssertionKind
> =
kind extends "bench" ? TypeBenchmarkingAssertionData
: TypeRelationshipAssertionData

export type LinePositionRange = {
start: LinePosition
Expand Down
4 changes: 2 additions & 2 deletions ark/attest/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export { caller, type CallerOfOptions } from "@arktype/fs"
export { attest } from "./assert/attest.js"
export { bench } from "./bench/bench.js"
export {
getTypeBenchAssertionsAtPosition,
getTypeRelationshipAssertionsAtPosition
getBenchAssertionsAtPosition as getTypeBenchAssertionsAtPosition,
getTypeAssertionsAtPosition as getTypeRelationshipAssertionsAtPosition
} from "./cache/getCachedAssertions.js"
export type {
ArgAssertionData,
Expand Down

0 comments on commit 2398217

Please sign in to comment.