@ark/attest 0.9.3 → 0.10.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/README.md CHANGED
@@ -168,6 +168,31 @@ bench(
168
168
  .types([337, "instantiations"])
169
169
  ```
170
170
 
171
+ If you're benchmarking an API, you'll need to include a "baseline expression" so that instantiations created when your API is initially invoked don't add noise to the individual tests.
172
+
173
+ Here's an example of what that looks like:
174
+
175
+ ```ts
176
+ import { bench } from "@ark/attest"
177
+ import { type } from "arktype"
178
+
179
+ // baseline expression
180
+ type("boolean")
181
+
182
+ bench("single-quoted", () => {
183
+ const _ = type("'nineteen characters'")
184
+ // would be 2697 without baseline
185
+ }).types([610, "instantiations"])
186
+
187
+ bench("keyword", () => {
188
+ const _ = type("string")
189
+ // would be 2507 without baseline
190
+ }).types([356, "instantiations"])
191
+ ```
192
+
193
+ > [!WARNING]
194
+ > Be sure your baseline expression is not identical to an expression you are using in any of your benchmarks. If it is, the individual benchmarks will reuse its cached types, leading to reduced (or 0) instantiations.
195
+
171
196
  If you'd like to fail in CI above a threshold, you can add flags like the following (default value is 20%, but it will not throw unless `--benchErrorOnThresholdExceeded` is set):
172
197
 
173
198
  ```
package/out/cache/ts.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare class TsServer {
5
5
  private tsConfigInfo;
6
6
  rootFiles: string[];
7
7
  virtualEnv: tsvfs.VirtualTypeScriptEnvironment;
8
+ program: ts.Program;
8
9
  private static _instance;
9
10
  static get instance(): TsServer;
10
11
  private constructor();
package/out/cache/ts.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { fromCwd } from "@ark/fs";
2
2
  import { throwInternalError } from "@ark/util";
3
+ import prettier from "@prettier/sync";
3
4
  import * as tsvfs from "@typescript/vfs";
4
5
  import { readFileSync } from "node:fs";
5
6
  import { dirname, join } from "node:path";
@@ -9,6 +10,7 @@ export class TsServer {
9
10
  tsConfigInfo;
10
11
  rootFiles;
11
12
  virtualEnv;
13
+ program;
12
14
  static _instance = null;
13
15
  static get instance() {
14
16
  return new TsServer();
@@ -23,6 +25,7 @@ export class TsServer {
23
25
  this.rootFiles = tsConfigInfo.parsed.fileNames.filter(path => path.startsWith(normalizedCwd));
24
26
  const system = tsvfs.createFSBackedSystem(tsLibPaths.defaultMapFromNodeModules, dirname(this.tsConfigInfo.path), ts);
25
27
  this.virtualEnv = tsvfs.createVirtualTypeScriptEnvironment(system, this.rootFiles, ts, this.tsConfigInfo.parsed.options);
28
+ this.program = this.virtualEnv.languageService.getProgram();
26
29
  TsServer._instance = this;
27
30
  }
28
31
  getSourceFileOrThrow(path) {
@@ -94,12 +97,42 @@ export const getTsLibFiles = (tsconfigOptions) => {
94
97
  export const getProgram = (env) => env?.languageService.getProgram() ??
95
98
  TsServer.instance.virtualEnv.languageService.getProgram();
96
99
  export const getInternalTypeChecker = (env) => getProgram(env).getTypeChecker();
100
+ const declarationPrefix = "type T = ";
101
+ const typeFormatOptions = {
102
+ parser: "typescript",
103
+ semi: false,
104
+ useTabs: true,
105
+ printWidth: 60,
106
+ trailingComma: "none"
107
+ };
108
+ const formatTypeString = (typeString) => prettier
109
+ .format(`${declarationPrefix}${typeString}`, typeFormatOptions)
110
+ .slice(declarationPrefix.length)
111
+ .trimEnd();
112
+ const replaceKnownInvalidSyntax = (typeString) => {
113
+ // Match '...' and '... x more ...' (known truncated type syntax)
114
+ const regex = /\.\.\.\s*(\d+\s*more\s*\.\.\.)?/g;
115
+ // replace these with a string literal "..." so that they can still
116
+ // form valid expressions, e.g. "..."[]
117
+ return typeString.replaceAll(regex, '"..."');
118
+ };
97
119
  export const getStringifiableType = (node) => {
98
120
  const typeChecker = getInternalTypeChecker();
99
- // in a call like attest<object>({a: true}),
100
- // passing arg.expression avoids inferring {a: true} as object
101
121
  const nodeType = typeChecker.getTypeAtLocation(node);
102
- const stringified = typeChecker.typeToString(nodeType);
122
+ let stringified = typeChecker.typeToString(nodeType);
123
+ try {
124
+ stringified = formatTypeString(stringified);
125
+ }
126
+ catch {
127
+ // if formatting fails (e.g. due to custom typeToString syntax like ... X more),
128
+ // try to comment out problematic sections
129
+ try {
130
+ stringified = formatTypeString(replaceKnownInvalidSyntax(stringified));
131
+ }
132
+ catch {
133
+ // if still fails somehow, just swallow the error and use the unformatted type
134
+ }
135
+ }
103
136
  return Object.assign(nodeType, {
104
137
  toString: () => stringified,
105
138
  isUnresolvable: nodeType.intrinsicName === "error"
@@ -6,4 +6,12 @@ export declare const gatherInlineInstantiationData: (file: ts.SourceFile, fileAs
6
6
  export declare const getCallExpressionsByName: (startNode: ts.Node, names: string[], isSnapCall?: boolean) => ts.CallExpression[];
7
7
  export declare const getInstantiationsContributedByNode: (file: ts.SourceFile, benchBlock: ts.FunctionExpression | ts.ArrowFunction) => number;
8
8
  export declare const createOrUpdateFile: (env: tsvfs.VirtualTypeScriptEnvironment, fileName: string, fileText: string) => ts.SourceFile | undefined;
9
+ declare module "typescript" {
10
+ interface SourceFile {
11
+ imports: ts.StringLiteral[];
12
+ }
13
+ interface Program {
14
+ getResolvedModuleFromModuleSpecifier(moduleSpecifier: ts.StringLiteralLike, sourceFile?: ts.SourceFile): ts.ResolvedModuleWithFailedLookupLocations;
15
+ }
16
+ }
9
17
  export declare const getIsolatedEnv: () => tsvfs.VirtualTypeScriptEnvironment;
@@ -1,5 +1,4 @@
1
1
  import { filePath } from "@ark/fs";
2
- import { throwInternalError } from "@ark/util";
3
2
  import * as tsvfs from "@typescript/vfs";
4
3
  import ts from "typescript";
5
4
  import { getConfig } from "../config.js";
@@ -88,8 +87,6 @@ const getInstantiationsWithFile = (fileText, fileName) => {
88
87
  const program = getProgram(env);
89
88
  program.emit(file);
90
89
  const count = program.getInstantiationCount();
91
- if (count === undefined)
92
- throwInternalError(`Unable to gather instantiation count for ${fileText}`);
93
90
  return count;
94
91
  };
95
92
  let virtualEnv = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ark/attest",
3
- "version": "0.9.3",
3
+ "version": "0.10.0",
4
4
  "author": {
5
5
  "name": "David Blass",
6
6
  "email": "david@arktype.io",
@@ -27,9 +27,11 @@
27
27
  "dependencies": {
28
28
  "@typescript/vfs": "1.5.3",
29
29
  "@typescript/analyze-trace": "0.10.1",
30
+ "prettier": "3.3.3",
31
+ "@prettier/sync": "0.5.2",
30
32
  "@ark/util": "0.1.1",
31
- "arktype": "2.0.0-beta.1",
32
- "@ark/fs": "0.1.1"
33
+ "@ark/fs": "0.1.1",
34
+ "arktype": "2.0.0-beta.1"
33
35
  },
34
36
  "devDependencies": {
35
37
  "typescript": "5.5.3"