@ark/attest 0.9.4 → 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.
Files changed (2) hide show
  1. package/out/cache/ts.js +34 -3
  2. package/package.json +5 -3
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";
@@ -96,12 +97,42 @@ export const getTsLibFiles = (tsconfigOptions) => {
96
97
  export const getProgram = (env) => env?.languageService.getProgram() ??
97
98
  TsServer.instance.virtualEnv.languageService.getProgram();
98
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
+ };
99
119
  export const getStringifiableType = (node) => {
100
120
  const typeChecker = getInternalTypeChecker();
101
- // in a call like attest<object>({a: true}),
102
- // passing arg.expression avoids inferring {a: true} as object
103
121
  const nodeType = typeChecker.getTypeAtLocation(node);
104
- 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
+ }
105
136
  return Object.assign(nodeType, {
106
137
  toString: () => stringified,
107
138
  isUnresolvable: nodeType.intrinsicName === "error"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ark/attest",
3
- "version": "0.9.4",
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
- "arktype": "2.0.0-beta.1",
30
+ "prettier": "3.3.3",
31
+ "@prettier/sync": "0.5.2",
32
+ "@ark/util": "0.1.1",
31
33
  "@ark/fs": "0.1.1",
32
- "@ark/util": "0.1.1"
34
+ "arktype": "2.0.0-beta.1"
33
35
  },
34
36
  "devDependencies": {
35
37
  "typescript": "5.5.3"