@ark/attest 0.57.1 → 0.58.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/config.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { type JsonObject } from "@ark/util";
2
2
  import type * as prettier from "prettier";
3
+ /** tsjs is TypeScript's JS API, tsgo its native port and tsrs a Rust port */
4
+ export type Checker = "tsjs" | "tsgo" | "tsrs";
3
5
  export type BenchErrorConfig = "runtime" | "types" | boolean;
4
6
  type BaseAttestConfig = {
5
7
  tsconfig: string | null | undefined;
@@ -9,6 +11,12 @@ type BaseAttestConfig = {
9
11
  failOnMissingSnapshots: boolean;
10
12
  skipTypes: boolean;
11
13
  skipInlineInstantiations: boolean;
14
+ /**
15
+ * the type checker to analyze assertions with, defaulting to tsrs if its
16
+ * binary is installed alongside TypeScript 7, else tsgo for TypeScript 7,
17
+ * else tsjs
18
+ */
19
+ checker: Checker | undefined;
12
20
  /** analyze only the test files named on the command line */
13
21
  onlyNamedTestFiles: boolean;
14
22
  attestAliases: string[];
package/out/config.js CHANGED
@@ -10,6 +10,7 @@ export const getDefaultAttestConfig = () => ({
10
10
  updateSnapshots: false,
11
11
  skipTypes: false,
12
12
  skipInlineInstantiations: false,
13
+ checker: undefined,
13
14
  onlyNamedTestFiles: false,
14
15
  benchPercentThreshold: 20,
15
16
  benchErrorOnThresholdExceeded: true,
package/out/fixtures.js CHANGED
@@ -4,7 +4,7 @@ import { writeSnapshotUpdatesOnExit } from "./cache/snapshots.js";
4
4
  import { analyzeProjectAssertions } from "./cache/writeAssertionCache.js";
5
5
  import { ensureCacheDirs, getConfig } from "./config.js";
6
6
  import { analyzeProjectAssertions as analyzeProjectAssertionsTs7 } from "./ts7/analyze.js";
7
- import { isTs7 } from "./utils.js";
7
+ import { getChecker } from "./ts7/server.js";
8
8
  export const setup = (options) => {
9
9
  const { ...config } = getConfig();
10
10
  if (options)
@@ -19,7 +19,9 @@ export const setup = (options) => {
19
19
  };
20
20
  export const writeAssertionData = (toPath) => {
21
21
  console.log("⏳ Waiting for TypeScript to check your project (this may take a while)...");
22
- writeJson(toPath, isTs7 ? analyzeProjectAssertionsTs7() : analyzeProjectAssertions());
22
+ writeJson(toPath, getChecker() === "tsjs" ?
23
+ analyzeProjectAssertions()
24
+ : analyzeProjectAssertionsTs7());
23
25
  };
24
26
  export const cleanup = () => writeSnapshotUpdatesOnExit();
25
27
  /** alias for cleanup to align with vitest and others */
@@ -2,7 +2,7 @@ import { flatMorph, throwInternalError } from "@ark/util";
2
2
  import { getConfig } from "../config.js";
3
3
  import { getFileKey } from "../utils.js";
4
4
  import { getInstantiationsContributedByNodes } from "./instantiations.js";
5
- import { TsgoServer, ast, getAncestors, getCallExpressionsByName, getCallLocation, getDescendants, getFirstFunctionDescendant, noTruncation } from "./server.js";
5
+ import { TsgoServer, ast, getAncestors, getCallExpressionsByName, getCallLocation, getDescendants, getFirstFunctionDescendant, getNoTruncation } from "./server.js";
6
6
  export const analyzeProjectAssertions = () => {
7
7
  const config = getConfig();
8
8
  const server = TsgoServer.instance;
@@ -46,7 +46,7 @@ export const stringifyTypeAtLocation = (node) => {
46
46
  const type = checker.getTypeAtLocation(node);
47
47
  let string = checker.typeToString(type);
48
48
  if (string.includes("...")) {
49
- const nonTruncated = checker.typeToString(type, undefined, noTruncation);
49
+ const nonTruncated = checker.typeToString(type, undefined, getNoTruncation());
50
50
  string =
51
51
  nonTruncated.includes(" any") && !string.includes(" any") ?
52
52
  nonTruncated.replace(/ any/g, " cyclic")
@@ -4,7 +4,7 @@ import { basename, dirname, extname, join } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { isPositionWithinRange } from "../cache/getCachedAssertions.js";
6
6
  import { getConfig } from "../config.js";
7
- import { TsgoServer, ast, getCallExpressionsByName, getCallLocation, getFirstFunctionDescendant, tempId, tscPath } from "./server.js";
7
+ import { TsgoServer, ast, getCallExpressionsByName, getCallLocation, getFirstFunctionDescendant, tempId, getTscPath } from "./server.js";
8
8
  const countInstantiationsScript = fileURLToPath(new URL(`./countInstantiations${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
9
9
  /**
10
10
  * tsgo's API doesn't expose instantiation counts, so each block is checked by
@@ -57,7 +57,7 @@ export const getInstantiationsContributedByNodes = (file, blocks) => {
57
57
  });
58
58
  return configPath;
59
59
  });
60
- const [baseline, ...counts] = JSON.parse(execFileSync(process.execPath, [countInstantiationsScript, tscPath], {
60
+ const [baseline, ...counts] = JSON.parse(execFileSync(process.execPath, [countInstantiationsScript, getTscPath()], {
61
61
  input: JSON.stringify({ files, configPaths }),
62
62
  encoding: "utf8",
63
63
  maxBuffer: 1e8
@@ -2,9 +2,13 @@ import { type JsonObject } from "@ark/util";
2
2
  import type * as tsgoAst from "typescript/ast";
3
3
  import type * as tsgoApi from "typescript/sync";
4
4
  import type { LinePositionRange } from "../cache/getCachedAssertions.ts";
5
+ import { type Checker } from "../config.ts";
6
+ /** the checker from attest's config, or the best one installed */
7
+ export declare const getChecker: () => Checker;
8
+ /** populated on first use, after setup() has written attest's config */
5
9
  export declare const ast: typeof tsgoAst;
6
- export declare const noTruncation: number;
7
- export declare const tscPath: string;
10
+ export declare const getNoTruncation: () => number;
11
+ export declare const getTscPath: () => string;
8
12
  export declare const tempId: string;
9
13
  export type Node = tsgoAst.Node;
10
14
  export type SourceFile = tsgoAst.SourceFile;
package/out/ts7/server.js CHANGED
@@ -9,12 +9,61 @@ import { getConfig } from "../config.js";
9
9
  import { isTs7, narrowToNamedTestFiles } from "../utils.js";
10
10
  // resolved relative to attest so the API matches the installed typescript
11
11
  const requireTypeScript = createRequire(import.meta.url);
12
- // only loaded for TS 7+ so this module can be imported unconditionally
13
- export const ast = isTs7 ? requireTypeScript("typescript/ast") : undefined;
14
- const { API, TypeFormatFlags } = isTs7 ? requireTypeScript("typescript/sync") : {};
15
- const { createFileSystemLayer } = isTs7 ? requireTypeScript("typescript/fs") : {};
16
- export const noTruncation = isTs7 ? TypeFormatFlags.NoTruncation : 0;
17
- export const tscPath = join(dirname(requireTypeScript.resolve("typescript/package.json")), "bin", "tsc");
12
+ const isResolvable = (require, specifier) => {
13
+ try {
14
+ require.resolve(specifier);
15
+ return true;
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ };
21
+ // tsrs's binary ships in a per-platform optional dependency, so it installs
22
+ // without one on unsupported platforms like Windows
23
+ const hasTsrsBinary = () => {
24
+ if (!isResolvable(requireTypeScript, "tsrs/package.json"))
25
+ return false;
26
+ if (process.env.TSRS_BINARY)
27
+ return true;
28
+ const packagePath = requireTypeScript.resolve("tsrs/package.json");
29
+ const requireTsrs = createRequire(packagePath);
30
+ const { optionalDependencies = {} } = requireTsrs(packagePath);
31
+ return Object.keys(optionalDependencies).some(name => isResolvable(requireTsrs, `${name}/package.json`));
32
+ };
33
+ /** the checker from attest's config, or the best one installed */
34
+ export const getChecker = () => {
35
+ const { checker } = getConfig();
36
+ if (checker === undefined) {
37
+ return (!isTs7 ? "tsjs"
38
+ : hasTsrsBinary() ? "tsrs"
39
+ : "tsgo");
40
+ }
41
+ if (checker !== "tsjs" && checker !== "tsgo" && checker !== "tsrs")
42
+ throwError(`checker must be "tsjs", "tsgo" or "tsrs" (was "${checker}")`);
43
+ if ((checker === "tsjs") === isTs7) {
44
+ throwError(`checker "${checker}" requires TypeScript ${checker === "tsjs" ? "<7" : "7+"} (found ${ts.version})`);
45
+ }
46
+ return checker;
47
+ };
48
+ let compiler;
49
+ /** populated on first use, after setup() has written attest's config */
50
+ export const ast = {};
51
+ // tsrs serves the same API as tsgo, so it only changes where it's loaded from
52
+ const loadCompiler = () => {
53
+ if (compiler)
54
+ return compiler;
55
+ const name = getChecker() === "tsrs" ? "tsrs" : "typescript";
56
+ // tsrs still exports the API from tsgo's previous unstable/ subpaths
57
+ const entry = name === "tsrs" ? "tsrs/unstable" : "typescript";
58
+ Object.assign(ast, requireTypeScript(`${entry}/ast`));
59
+ return (compiler = {
60
+ ...requireTypeScript(`${entry}/sync`),
61
+ ...requireTypeScript(`${entry}/fs`),
62
+ tscPath: join(dirname(requireTypeScript.resolve(`${name}/package.json`)), "bin", name === "tsrs" ? "tsrs" : "tsc")
63
+ });
64
+ };
65
+ export const getNoTruncation = () => loadCompiler().TypeFormatFlags.NoTruncation;
66
+ export const getTscPath = () => loadCompiler().tscPath;
18
67
  // unique to this thread, since worker threads share a pid
19
68
  export const tempId = `${process.pid}-${threadId}`;
20
69
  export class TsgoServer {
@@ -32,9 +81,9 @@ export class TsgoServer {
32
81
  return (TsgoServer._instance ??= new TsgoServer());
33
82
  }
34
83
  constructor() {
35
- if (ts.version.startsWith("7.0."))
84
+ if (getChecker() === "tsgo" && ts.version.startsWith("7.0."))
36
85
  throwError(`@ark/attest requires TypeScript 7.1+ (found ${ts.version})`);
37
- this.api = new API({ cwd: fromCwd() });
86
+ this.api = new (loadCompiler().API)({ cwd: fromCwd() });
38
87
  this.projectConfig = getProjectConfig();
39
88
  // only exists in tsgo's view of the filesystem, so nothing is written
40
89
  this.configPath = join(this.projectConfig.dir, ".attest.tsconfig.json");
@@ -89,7 +138,7 @@ export class TsgoServer {
89
138
  openProject(files = []) {
90
139
  const snapshot = this.api.createSnapshot({
91
140
  openProjects: [this.configPath],
92
- fileSystem: createFileSystemLayer([
141
+ fileSystem: loadCompiler().createFileSystemLayer([
93
142
  [this.configPath, this.config],
94
143
  ...files
95
144
  ])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ark/attest",
3
- "version": "0.57.1",
3
+ "version": "0.58.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "David Blass",
@@ -41,17 +41,24 @@
41
41
  "@typescript/vfs": "1.6.1",
42
42
  "prettier": "3.6.2",
43
43
  "@ark/fs": "0.56.8",
44
- "@ark/util": "0.56.8",
45
44
  "@ark/schema": "0.56.8",
46
- "arktype": "2.2.9"
45
+ "arktype": "2.2.9",
46
+ "@ark/util": "0.56.8"
47
47
  },
48
48
  "devDependencies": {
49
+ "tsrs": "0.18.0-ts7.1.0-dev.20260929",
49
50
  "typescript": "7.1.0-beta",
50
51
  "typescript-6": "npm:typescript@6.0.3",
51
52
  "typescript-7": "npm:typescript@7.1.0-beta"
52
53
  },
53
54
  "peerDependencies": {
54
- "typescript": "*"
55
+ "typescript": "*",
56
+ "tsrs": "*"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "tsrs": {
60
+ "optional": true
61
+ }
55
62
  },
56
63
  "publishConfig": {
57
64
  "access": "public"