@ark/attest 0.11.0 → 0.12.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.
@@ -1,9 +1,11 @@
1
1
  import { caller } from "@ark/fs";
2
2
  import { printable, snapshot } from "@ark/util";
3
+ import prettier from "@prettier/sync";
3
4
  import { type } from "arktype";
4
5
  import * as assert from "node:assert/strict";
5
6
  import { isDeepStrictEqual } from "node:util";
6
7
  import { getSnapshotByName, queueSnapshotUpdate, updateExternalSnapshot } from "../cache/snapshots.js";
8
+ import { getConfig } from "../config.js";
7
9
  import { chainableNoOpProxy } from "../utils.js";
8
10
  import { TypeAssertionMapping, assertEqualOrMatching, assertEquals, assertSatisfies, callAssertedFunction, getThrownMessage, throwAssertionError } from "./assertions.js";
9
11
  export class ChainableAssertions {
@@ -153,7 +155,7 @@ export class ChainableAssertions {
153
155
  return {
154
156
  get toString() {
155
157
  self.ctx.actual = new TypeAssertionMapping(data => ({
156
- actual: data.args[0].type
158
+ actual: formatTypeString(data.args[0].type)
157
159
  }));
158
160
  self.ctx.allowRegex = true;
159
161
  return self.immediateOrChained();
@@ -175,3 +177,14 @@ const checkCompletionsForErrors = (completions) => {
175
177
  if (typeof completions === "string")
176
178
  throw new Error(completions);
177
179
  };
180
+ const declarationPrefix = "type T = ";
181
+ const formatTypeString = (typeString) => prettier
182
+ .format(`${declarationPrefix}${typeString}`, {
183
+ semi: false,
184
+ printWidth: 60,
185
+ trailingComma: "none",
186
+ parser: "typescript",
187
+ ...getConfig().typeToStringFormat
188
+ })
189
+ .slice(declarationPrefix.length)
190
+ .trimEnd();
package/out/cache/ts.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { fromCwd } from "@ark/fs";
2
2
  import { throwInternalError } from "@ark/util";
3
- import prettier from "@prettier/sync";
4
3
  import * as tsvfs from "@typescript/vfs";
5
4
  import { readFileSync } from "node:fs";
6
5
  import { dirname, join } from "node:path";
@@ -58,10 +57,11 @@ export const getAbsolutePosition = (file, position) => {
58
57
  return pos;
59
58
  };
60
59
  export const getTsConfigInfoOrThrow = () => {
61
- const config = getConfig().tsconfig;
62
- const configFilePath = config ?? ts.findConfigFile(fromCwd(), ts.sys.fileExists, "tsconfig.json");
60
+ const config = getConfig();
61
+ const tsconfig = config.tsconfig;
62
+ const configFilePath = tsconfig ?? ts.findConfigFile(fromCwd(), ts.sys.fileExists, "tsconfig.json");
63
63
  if (!configFilePath) {
64
- throw new Error(`File ${config ?? join(fromCwd(), "tsconfig.json")} must exist.`);
64
+ throw new Error(`File ${tsconfig ?? join(fromCwd(), "tsconfig.json")} must exist.`);
65
65
  }
66
66
  const configFileText = readFileSync(configFilePath).toString();
67
67
  const result = ts.parseConfigFileTextToJson(configFilePath, configFileText);
@@ -72,8 +72,9 @@ export const getTsConfigInfoOrThrow = () => {
72
72
  getNewLine: () => ts.sys.newLine
73
73
  }));
74
74
  }
75
- const configObject = result.config;
76
- const configParseResult = ts.parseJsonConfigFileContent(configObject, ts.sys, dirname(configFilePath), {}, configFilePath);
75
+ const configJson = result.config;
76
+ configJson.compilerOptions = Object.assign(configJson.compilerOptions ?? {}, config.compilerOptions);
77
+ const configParseResult = ts.parseJsonConfigFileContent(configJson, ts.sys, dirname(configFilePath), {}, configFilePath);
77
78
  if (configParseResult.errors.length > 0) {
78
79
  throw new Error(ts.formatDiagnostics(configParseResult.errors, {
79
80
  getCanonicalFileName: fileName => fileName,
@@ -97,43 +98,16 @@ export const getTsLibFiles = (tsconfigOptions) => {
97
98
  export const getProgram = (env) => env?.languageService.getProgram() ??
98
99
  TsServer.instance.virtualEnv.languageService.getProgram();
99
100
  export const getInternalTypeChecker = (env) => getProgram(env).getTypeChecker();
100
- const declarationPrefix = "type T = ";
101
- const baseTypeFormatOptions = {
102
- semi: false,
103
- printWidth: 60,
104
- trailingComma: "none",
105
- parser: "typescript"
106
- };
107
- const formatTypeString = (typeString) => prettier
108
- .format(`${declarationPrefix}${typeString}`, {
109
- ...baseTypeFormatOptions,
110
- ...getConfig().typeToStringFormat
111
- })
112
- .slice(declarationPrefix.length)
113
- .trimEnd();
114
- const replaceKnownInvalidSyntax = (typeString) => {
115
- // Match '...' and '... x more ...' (known truncated type syntax)
116
- const regex = /\.\.\.\s*(\d+\s*more\s*\.\.\.)?/g;
117
- // replace these with a string literal "..." so that they can still
118
- // form valid expressions, e.g. "..."[]
119
- return typeString.replaceAll(regex, '"..."');
120
- };
121
101
  export const getStringifiableType = (node) => {
122
102
  const typeChecker = getInternalTypeChecker();
123
103
  const nodeType = typeChecker.getTypeAtLocation(node);
124
104
  let stringified = typeChecker.typeToString(nodeType);
125
- try {
126
- stringified = formatTypeString(stringified);
127
- }
128
- catch {
129
- // if formatting fails (e.g. due to custom typeToString syntax like ... X more),
130
- // try to comment out problematic sections
131
- try {
132
- stringified = formatTypeString(replaceKnownInvalidSyntax(stringified));
133
- }
134
- catch {
135
- // if still fails somehow, just swallow the error and use the unformatted type
136
- }
105
+ if (stringified.includes("...")) {
106
+ const nonTruncated = typeChecker.typeToString(nodeType, undefined, ts.TypeFormatFlags.NoTruncation);
107
+ if (nonTruncated.includes(" any") && !stringified.includes(" any"))
108
+ stringified = nonTruncated.replaceAll(" any", " cyclic");
109
+ else
110
+ stringified = nonTruncated;
137
111
  }
138
112
  return Object.assign(nodeType, {
139
113
  toString: () => stringified,
package/out/config.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { type autocomplete } from "@ark/util";
2
2
  import type * as prettier from "prettier";
3
+ import type ts from "typescript";
3
4
  import { type TsVersionData } from "./tsVersioning.js";
4
5
  export type TsVersionAliases = autocomplete<"*"> | string[];
5
6
  type BaseAttestConfig = {
6
7
  tsconfig: string | undefined;
8
+ compilerOptions: ts.CompilerOptions;
7
9
  updateSnapshots: boolean;
8
10
  /** A string or list of strings representing the TypeScript version aliases to run.
9
11
  *
package/out/config.js CHANGED
@@ -5,6 +5,7 @@ import { join, resolve } from "node:path";
5
5
  import { findAttestTypeScriptVersions } from "./tsVersioning.js";
6
6
  export const getDefaultAttestConfig = () => ({
7
7
  tsconfig: existsSync(fromCwd("tsconfig.json")) ? fromCwd("tsconfig.json") : undefined,
8
+ compilerOptions: {},
8
9
  attestAliases: ["attest", "attestInternal"],
9
10
  updateSnapshots: false,
10
11
  skipTypes: false,
@@ -18,11 +19,22 @@ export const getDefaultAttestConfig = () => ({
18
19
  shouldFormat: true,
19
20
  typeToStringFormat: {}
20
21
  });
21
- const hasFlag = (flag) => process.argv.some(arg => arg.includes(flag));
22
+ const flagAliases = {
23
+ updateSnapshots: ["u", "update"]
24
+ };
25
+ const findParamIndex = (flagOrAlias) => process.argv.findIndex(arg => arg === `-${flagOrAlias}` || arg === `--${flagOrAlias}`);
26
+ const hasFlag = (flag) => findParamIndex(flag) !== -1 ||
27
+ flagAliases[flag]?.some(alias => findParamIndex(alias) !== -1);
22
28
  const getParamValue = (param) => {
23
- const paramIndex = process.argv.findIndex(arg => arg.includes(param));
24
- if (paramIndex === -1)
25
- return undefined;
29
+ let paramIndex = findParamIndex(param);
30
+ if (paramIndex === -1) {
31
+ if (!flagAliases[param])
32
+ return;
33
+ for (let i = 0; i < flagAliases[param].length && paramIndex === -1; i++)
34
+ paramIndex = findParamIndex(flagAliases[param][i]);
35
+ if (paramIndex === -1)
36
+ return;
37
+ }
26
38
  const raw = process.argv[paramIndex + 1];
27
39
  if (raw === "true")
28
40
  return true;
@@ -32,7 +44,7 @@ const getParamValue = (param) => {
32
44
  return tryParseNumber(raw, { errorOnFail: true });
33
45
  if (param === "tsVersions" || param === "attestAliases")
34
46
  return raw.split(",");
35
- if (param === "typeToStringFormat")
47
+ if (param === "typeToStringFormat" || param === "compilerOptions")
36
48
  return JSON.parse(raw);
37
49
  return raw;
38
50
  };
@@ -77,7 +89,7 @@ const parseConfig = () => {
77
89
  const isTsVersionAliases = (v) => typeof v === "string" || (isArray(v) && typeof v[0] === "string");
78
90
  const parseTsVersions = (aliases) => {
79
91
  const versions = findAttestTypeScriptVersions();
80
- if (aliases === "*")
92
+ if (aliases === "*" || (isArray(aliases) && aliases[0] === "*"))
81
93
  return versions;
82
94
  return liftArray(aliases).map(alias => {
83
95
  const matching = versions.find(v => v.alias === alias);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ark/attest",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "author": {
5
5
  "name": "David Blass",
6
6
  "email": "david@arktype.io",
@@ -29,9 +29,9 @@
29
29
  "@typescript/analyze-trace": "0.10.1",
30
30
  "prettier": "3.3.3",
31
31
  "@prettier/sync": "0.5.2",
32
- "@ark/util": "0.2.0",
33
- "@ark/fs": "0.2.0",
34
- "arktype": "2.0.0-beta.3"
32
+ "arktype": "2.0.0-beta.4",
33
+ "@ark/fs": "0.2.1",
34
+ "@ark/util": "0.2.1"
35
35
  },
36
36
  "devDependencies": {
37
37
  "typescript": "5.5.4"