Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(attest): add Bun support #1130

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions ark/attest/assert/chainableAssertions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { caller } from "@ark/fs"
import { printable, snapshot, type Constructor } from "@ark/util"
import prettier from "@prettier/sync"
import { type } from "arktype"
import * as assert from "node:assert/strict"
import { isDeepStrictEqual } from "node:util"
Expand All @@ -11,7 +10,6 @@ import {
type SnapshotArgs
} from "../cache/snapshots.ts"
import type { Completions } from "../cache/writeAssertionCache.ts"
import { getConfig } from "../config.ts"
import { chainableNoOpProxy } from "../utils.ts"
import {
TypeAssertionMapping,
Expand All @@ -23,6 +21,7 @@ import {
throwAssertionError
} from "./assertions.ts"
import type { AssertionContext } from "./attest.ts"
import { formatTypeString } from "./format.ts"

export type ChainableAssertionOptions = {
allowRegex?: boolean
Expand Down Expand Up @@ -219,7 +218,7 @@ export class ChainableAssertions implements AssertionRecord {
return {
get toString() {
self.ctx.actual = new TypeAssertionMapping(data => ({
actual: formatTypeString(data.args[0].type)
actual: formatTypeString(data.args[0].type, self.ctx.position.file)
}))
self.ctx.allowRegex = true
return self.immediateOrChained()
Expand All @@ -238,20 +237,6 @@ export class ChainableAssertions implements AssertionRecord {
}
}

const declarationPrefix = "type T = "

const formatTypeString = (typeString: string) =>
prettier
.format(`${declarationPrefix}${typeString}`, {
semi: false,
printWidth: 60,
trailingComma: "none",
parser: "typescript",
...getConfig().typeToStringFormat
})
.slice(declarationPrefix.length)
.trimEnd()

export type AssertionKind = "value" | "type"

export type rootAssertions<t, kind extends AssertionKind> = valueAssertions<
Expand Down
25 changes: 25 additions & 0 deletions ark/attest/assert/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { format } from "prettier"
import { createSyncFn } from "synckit"
import { fileURLToPath } from "url"
import { getConfig } from "../config.ts"

const formatSync = createSyncFn<typeof format>(
fileURLToPath(import.meta.resolve("./formatWorker.js"))
)

const declarationPrefix = "type T = "

export const formatTypeString = (
typeString: string,
filename: string
): string =>
formatSync(`${declarationPrefix}${typeString}`, {
semi: false,
printWidth: 60,
trailingComma: "none",
// use filepath to allow global formatting options
filepath: filename,
...getConfig().typeToStringFormat
})
.slice(declarationPrefix.length)
.trimEnd()
4 changes: 4 additions & 0 deletions ark/attest/assert/formatWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { format } from "prettier"
import { runAsWorker } from "synckit"

runAsWorker(format)
8 changes: 4 additions & 4 deletions ark/attest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
"attest": "out/cli/cli.js"
},
"scripts": {
"build": "ts ../repo/build.ts",
"build": "ts ../repo/build.ts && cp ./assert/formatWorker.js ./out/assert/formatWorker.js",
"test": "ts ../repo/testPackage.ts"
},
"dependencies": {
"arktype": "workspace:*",
"@ark/fs": "workspace:*",
"@ark/util": "workspace:*",
"@typescript/vfs": "1.6.0",
"@typescript/analyze-trace": "0.10.1",
"@typescript/vfs": "1.6.0",
"arktype": "workspace:*",
"prettier": "3.3.3",
"@prettier/sync": "0.5.2"
"synckit": "^0.9.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use "0.9.1" instead

},
"devDependencies": {
"typescript": "catalog:"
Expand Down
4 changes: 3 additions & 1 deletion ark/attest/tsVersioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const forTypeScriptVersions = (
const nodeModules = join(assertPackageRoot(process.cwd()), "node_modules")
const tsPrimaryPath = join(nodeModules, "typescript")
const tsTemporaryPath = join(nodeModules, "typescript-temp")
if (existsSync(tsTemporaryPath)) unlinkSync(tsTemporaryPath)
if (existsSync(tsPrimaryPath)) renameSync(tsPrimaryPath, tsTemporaryPath)

try {
Expand All @@ -41,7 +42,7 @@ export const forTypeScriptVersions = (
try {
if (existsSync(tsPrimaryPath)) unlinkSync(tsPrimaryPath)

symlinkSync(targetPath, tsPrimaryPath)
symlinkSync(targetPath, tsPrimaryPath, "junction")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also from the other PR correct? It's possible though that this does break on Windows as the cross-os CI doesn't run across multiple TS versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version that doesn't touch default TS was not on main yet so I did need this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, I'd rather pnpm testTsVersions work on Windows as well, so you can leave this in. Just not sure about the build stuff from the other PR.

Copy link
Contributor Author

@Dimava Dimava Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll may drop this commit from this PR later, I did need it to test if it does work

fn(version)
passedVersions.push(version)
} catch (e) {
Expand All @@ -65,6 +66,7 @@ export const forTypeScriptVersions = (
} finally {
if (existsSync(tsTemporaryPath)) {
console.log(`⏮️ Restoring your original TypeScript version...`)
unlinkSync(tsPrimaryPath)
renameSync(tsTemporaryPath, tsPrimaryPath)
}
}
Expand Down
6 changes: 3 additions & 3 deletions ark/repo/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { symlinkSync } from "fs"
import { copyFileSync } from "fs"
import { join } from "path"
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import {
Expand All @@ -23,7 +23,7 @@ const buildCurrentProject = () =>
try {
rmRf(outDir)
rmRf("tsconfig.build.json")
symlinkSync(`../repo/tsconfig.${buildKind}.json`, "tsconfig.build.json")
copyFileSync(`../repo/tsconfig.${buildKind}.json`, "tsconfig.build.json")
buildCurrentProject()
walkPaths(outDir, { excludeDirs: true }).forEach(jsPath =>
rewriteFile(jsPath, src =>
Expand All @@ -34,7 +34,7 @@ try {
)
)
rmRf("tsconfig.build.json")
symlinkSync(`../repo/tsconfig.dts.json`, "tsconfig.build.json")
copyFileSync(`../repo/tsconfig.dts.json`, "tsconfig.build.json")
buildCurrentProject()
if (buildKind === "cjs")
writeJson(join(outDir, "package.json"), { type: "commonjs" })
Expand Down
19 changes: 19 additions & 0 deletions ark/repo/bun.globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="@types/bun" />
import { cleanup, setup } from "@ark/attest"
import { afterAll, beforeAll, describe, it } from "bun:test"

process.env.TZ = "America/New_York"

// $ bun test --preload ./repo/bun.globalSetup.ts

Object.assign(globalThis, { describe, it })

beforeAll(() => {
setup({
typeToStringFormat: {
useTabs: true
}
})
})

afterAll(cleanup)
3 changes: 3 additions & 0 deletions ark/repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
},
"bin": {
"ts": "./ts.js"
},
"devDependencies": {
"@types/bun": "^1.1.9"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"testTyped": "mocha --exclude 'ark/attest/**/*.test.*'",
"testRepo": "pnpm test && pnpm testV8 && cd ./ark/attest && pnpm test",
"testV8": "node --allow-natives-syntax ./ark/repo/testV8.js",
"testBun": "bun test --preload ./ark/repo/bun.globalSetup.ts",
"testTsVersions": "pnpm testTyped --tsconfig null --tsVersions '*' --compilerOptions '{ \"strictNullChecks\": true }'",
"bench": "pnpm benchOperand && pnpm benchOperator && pnpm benchObject && pnpm benchCyclic",
"benchUp": "ATTEST_updateSnapshots=1 pnpm bench",
Expand Down