Skip to content

Commit

Permalink
perf: optimize object types, split up tests (#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad authored Sep 15, 2024
1 parent cda8376 commit 24df831
Show file tree
Hide file tree
Showing 72 changed files with 2,121 additions and 1,333 deletions.
2 changes: 1 addition & 1 deletion ark/attest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const mochaGlobalTeardown = teardown

You should also add `.attest` to your repository's `.gitignore` file.

Bun support is currently pending a [bug in the way their source maps translate to stack traces](https://github.com/oven-sh/bun/issues/7120). If this is a problem for you, please 👍 that issue so they prioritize it!
Bun support is currently pending [them supporting @prettier/sync for type formatting](https://github.com/oven-sh/bun/issues/10768). If this is a problem for you, please 👍 that issue so they prioritize it!

## Assertions

Expand Down
4 changes: 2 additions & 2 deletions ark/attest/assert/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { printable, throwInternalError } from "@ark/util"
import type { Type } from "arktype"
import type { type } from "arktype"
import { AssertionError } from "node:assert"
import * as assert from "node:assert/strict"
import type { TypeRelationshipAssertionData } from "../cache/writeAssertionCache.ts"
Expand Down Expand Up @@ -99,7 +99,7 @@ export const assertEquals: AssertFn = versionableAssertion(
)

const unversionedAssertSatisfies = (
t: Type.Any,
t: type.Any,
data: unknown,
ctx: AssertionContext
) => {
Expand Down
21 changes: 16 additions & 5 deletions ark/attest/cli/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { getConfig } from "../config.ts"
import { baseDiagnosticTscCmd } from "./shared.ts"

export const trace = async (args: string[]): Promise<void> => {
const packageDir = args[0] ?? process.cwd()
const packageDir = resolve(args[0] ?? process.cwd())
const config = getConfig()

if (!config.tsconfig) {
console.error(
`attest trace must be run from a directory with a tsconfig.json file`
)
process.exit(1)
}

const traceDir = resolve(config.cacheDir, "trace")
ensureDir(traceDir)

Expand All @@ -17,10 +25,13 @@ export const trace = async (args: string[]): Promise<void> => {
// the .attest/trace directory will contain a trace.json file and a types.json file.
// the trace.json file can be viewed via a tool like https://ui.perfetto.dev/
// the types.json file can be used to associate IDs from the trace file with type aliases
execSync(`${baseDiagnosticTscCmd} --generateTrace ${traceDir}`, {
cwd: packageDir,
stdio: "inherit"
})
execSync(
`${baseDiagnosticTscCmd} --project ${config.tsconfig} --generateTrace ${traceDir}`,
{
cwd: packageDir,
stdio: "inherit"
}
)
} catch (e) {
console.error(String(e))
} finally {
Expand Down
2 changes: 1 addition & 1 deletion ark/attest/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const setup = (options?: Partial<AttestConfig>): typeof teardown => {

if (
config.tsVersions.length === 1 &&
config.tsVersions[0].alias === "typescript"
config.tsVersions[0].alias === "default"
)
writeAssertionData(config.defaultAssertionCachePath)
else {
Expand Down
2 changes: 1 addition & 1 deletion ark/attest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/attest",
"version": "0.18.0",
"version": "0.18.1",
"author": {
"name": "David Blass",
"email": "david@arktype.io",
Expand Down
2 changes: 1 addition & 1 deletion ark/dark/injected.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"arkChained": {
"contentName": "meta.embedded.arktype.definition",
"begin": "([^\\)\\(\\s]+)?(\\.)\\b(and|or|when|extends|intersect|exclude|extract|overlaps|subsumes|to)(\\()",
"begin": "([^\\)\\(\\s]+)?(\\.)\\b(and|or|when|equals|ifEquals|extends|ifExtends|intersect|exclude|extract|overlaps|subsumes|to)(\\()",
"beginCaptures": {
"2": {
"name": "punctuation.accessor.ts"
Expand Down
2 changes: 1 addition & 1 deletion ark/dark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "arkdark",
"displayName": "ArkDark",
"description": "Syntax highlighting, inline errors and theme for ArkType⛵",
"version": "5.12.0",
"version": "5.12.1",
"publisher": "arktypeio",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion ark/dark/tsWithArkType.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"arkChained": {
"contentName": "meta.embedded.arktype.definition",
"begin": "([^\\)\\(\\s]+)?(\\.)\\b(and|or|when|extends|intersect|exclude|extract|overlaps|subsumes|to)(\\()",
"begin": "([^\\)\\(\\s]+)?(\\.)\\b(and|or|when|equals|ifEquals|extends|ifExtends|intersect|exclude|extract|overlaps|subsumes|to)(\\()",
"beginCaptures": {
"2": {
"name": "punctuation.accessor.ts"
Expand Down
8 changes: 4 additions & 4 deletions ark/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
"@astrojs/react": "3.6.2",
"@astrojs/starlight": "0.27.1",
"@astrojs/ts-plugin": "1.10.2",
"@shikijs/transformers": "1.16.3",
"@shikijs/twoslash": "1.16.3",
"@shikijs/transformers": "1.17.6",
"@shikijs/twoslash": "1.17.6",
"arkdark": "workspace:*",
"arktype": "workspace:*",
"astro": "4.15.4",
"astro": "4.15.6",
"astro-og-canvas": "0.5.3",
"canvaskit-wasm": "0.39.1",
"framer-motion": "11.5.4",
"react": "18.3.1",
"react-dom": "18.3.1",
"sharp": "0.33.5",
"shiki": "1.16.3",
"shiki": "1.17.6",
"twoslash": "0.2.11"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion ark/fs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/fs",
"version": "0.9.0",
"version": "0.10.0",
"author": {
"name": "David Blass",
"email": "david@arktype.io",
Expand Down
40 changes: 21 additions & 19 deletions ark/repo/scratch.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { type } from "arktype"

const oneTo9999 = type("1 <= number.integer <= 9999")

const T = type({
PORT: ["string.integer.parse", "=>", oneTo9999]
export const cloudinaryResource = type({
"[string]": "unknown",
"alt?": "string",
"caption?": "string"
})
const defaults: typeof T.infer = {
PORT: 123
}

process.env.PORT = "456"

const out = T.assert(process.env)

console.log(out.PORT)
const user = type({
name: "string",
device: {
platform: "'android' | 'ios'",
"version?": "number | string"
}
})

export const _assetOptionsSchema = type({
"assetType?": type("string")
.default("image")
.configure({
text: "Uses Generative Fill to extended padded image with AI",
url: "https://cloudinary.com/documentation/transformation_reference#g_gravity"
} as ArkEnv.meta)
// ---cut---
user.extends("object") // true
user.extends("string") // false
// true (string is narrower than unknown)
user.extends({
name: "unknown"
})
// false (string is wider than "Alan")
user.extends({
name: "'Alan'"
})
2 changes: 1 addition & 1 deletion ark/repo/scratch/typeClass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DynamicBase } from "@ark/util"
import { type } from "arktype"

const Class = <def>(def: type.validate<def>) => {
const Class = <const def>(def: type.validate<def>) => {
const validator = type(def as never)

return class TypeConstructor<t = type.infer<def>> extends DynamicBase<
Expand Down
9 changes: 7 additions & 2 deletions ark/schema/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,13 @@ export abstract class BaseNode<
return this.expression
}

equals(other: BaseNode): boolean {
return this.innerHash === other.innerHash
equals(r: unknown): boolean {
const rNode: BaseNode = isNode(r) ? r : this.$.parseDefinition(r)
return this.innerHash === rNode.innerHash
}

ifEquals(r: unknown): BaseNode | undefined {
return this.equals(r) ? this : undefined
}

hasKind<kind extends NodeKind>(kind: kind): this is nodeOfKind<kind> {
Expand Down
2 changes: 1 addition & 1 deletion ark/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/schema",
"version": "0.9.0",
"version": "0.10.0",
"license": "MIT",
"author": {
"name": "David Blass",
Expand Down
19 changes: 12 additions & 7 deletions ark/schema/roots/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export abstract class BaseRoot<
) as never
}

exclude(r: BaseRoot): BaseRoot {
exclude(r: unknown): BaseRoot {
const rNode = this.$.parseDefinition(r)
return this.$.schema(
this.branches.filter(branch => !branch.extends(rNode))
Expand All @@ -253,20 +253,25 @@ export abstract class BaseRoot<
) as never
}

overlaps(r: BaseRoot): boolean {
const intersection = this.intersect(r as never)
overlaps(r: unknown): boolean {
const intersection = this.intersect(r)
return !(intersection instanceof Disjoint)
}

extends(r: BaseRoot): boolean {
const intersection = this.intersect(r as never)
extends(r: unknown): boolean {
const intersection = this.intersect(r)
return (
!(intersection instanceof Disjoint) && this.equals(intersection as never)
)
}

subsumes(r: BaseRoot): boolean {
return r.extends(this as never)
ifExtends(r: unknown): BaseRoot | undefined {
return this.extends(r) ? this : undefined
}

subsumes(r: unknown): boolean {
const rNode = this.$.parseDefinition(r)
return rNode.extends(this)
}

configure(meta: MetaSchema): this {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ value at [1] must be a number (was boolean)`)
})

it("optional tuple", () => {
const t = type(["string", "?"])
const t = type([["string", "?"]])
attest<[string?]>(t.infer)
attest(t([])).equals([])
attest(t(["foo"])).equals(["foo"])
Expand All @@ -155,9 +155,40 @@ value at [1] must be a number (was boolean)`)
)
})

it("nested optional tuple", () => {
const t = type([["string", "?"], "string", "?"])
attest<[[string?], string?]>(t.infer)
it("optional string-embedded tuple", () => {
const t = type(["string?"])

const expected = type([["string", "?"]])
attest<typeof expected>(t)
attest(t.expression).equals(expected.expression)
})

it("optional object tuple", () => {
const t = type([[{ foo: "string" }, "?"], "string?"])
attest<
[
{
foo: string
}?,
string?
]
>(t.t)
attest(t.expression).snap("[{ foo: string }?, string?]")
})

it("optional nested object tuple", () => {
const t = type([[[{ foo: "string" }, "?"]], ["string", "?"]])
attest<
[
[
{
foo: string
}?
],
string?
]
>(t.t)
attest(t.expression).snap("[[{ foo: string }?], string?]")
})
})

Expand Down Expand Up @@ -313,29 +344,23 @@ value at [1] must be a number (was boolean)`)
it("kitchen sink", () => {
const l = type([
{ a: "0" },
{ b: "1" },
"?",
{ c: "2" },
"?",
[{ b: "1" }, "?"],
[{ c: "2" }, "?"],
"...",
[{ d: "3" }, "[]"]
])
const r = type([
{ e: "4" },
"?",
{ f: "5" },
"?",
[{ e: "4" }, "?"],
[{ f: "5" }, "?"],
"...",
[{ g: "6" }, "[]"]
])
const result = l.and(r)

const expected = type([
{ a: "0", e: "4" },
{ b: "1", f: "5" },
"?",
{ c: "2", g: "6" },
"?",
[{ b: "1", f: "5" }, "?"],
[{ c: "2", g: "6" }, "?"],
"...",
[{ d: "3", g: "6" }, "[]"]
])
Expand All @@ -345,7 +370,7 @@ value at [1] must be a number (was boolean)`)
)

attest<typeof expected>(result)
attest(result.json).equals(expected.json)
attest(result.expression).equals(expected.expression)
})

it("prefix and postfix", () => {
Expand Down
Loading

0 comments on commit 24df831

Please sign in to comment.