@ark/attest 0.56.7 → 0.57.1
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 +9 -385
- package/out/assert/assertions.d.ts +1 -1
- package/out/assert/assertions.js +12 -18
- package/out/assert/attest.d.ts +2 -3
- package/out/assert/attest.js +3 -3
- package/out/assert/chainableAssertions.d.ts +1 -1
- package/out/assert/chainableAssertions.js +2 -1
- package/out/bench/type.js +6 -3
- package/out/cache/getCachedAssertions.d.ts +33 -11
- package/out/cache/getCachedAssertions.js +23 -46
- package/out/cache/snapshots.d.ts +6 -2
- package/out/cache/snapshots.js +32 -26
- package/out/cache/ts.js +6 -3
- package/out/cache/utils.d.ts +1 -1
- package/out/cache/writeAssertionCache.d.ts +1 -30
- package/out/cache/writeAssertionCache.js +2 -1
- package/out/cli/precache.js +3 -4
- package/out/cli/shared.d.ts +1 -1
- package/out/cli/shared.js +4 -1
- package/out/cli/trace.js +60 -25
- package/out/config.d.ts +6 -22
- package/out/config.js +5 -30
- package/out/fixtures.js +5 -18
- package/out/index.d.ts +1 -3
- package/out/index.js +1 -2
- package/out/ts7/analyze.d.ts +10 -0
- package/out/ts7/analyze.js +188 -0
- package/out/ts7/countInstantiations.d.ts +1 -0
- package/out/ts7/countInstantiations.js +44 -0
- package/out/ts7/instantiations.d.ts +13 -0
- package/out/ts7/instantiations.js +113 -0
- package/out/ts7/server.d.ts +48 -0
- package/out/ts7/server.js +224 -0
- package/out/ts7/snapshots.d.ts +5 -0
- package/out/ts7/snapshots.js +27 -0
- package/out/utils.d.ts +17 -0
- package/out/utils.js +33 -1
- package/package.json +8 -6
- package/out/tsVersioning.d.ts +0 -31
- package/out/tsVersioning.js +0 -172
package/out/cache/snapshots.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type SourcePosition } from "@ark/fs";
|
|
2
|
-
import type ts from "typescript";
|
|
3
2
|
export type SnapshotArgs = {
|
|
4
3
|
position: SourcePosition;
|
|
5
4
|
serializedValue: unknown;
|
|
@@ -15,7 +14,7 @@ export declare const getSnapshotByName: (file: string, name: string, customPath:
|
|
|
15
14
|
export declare const queueSnapshotUpdate: (args: SnapshotArgs) => void;
|
|
16
15
|
export type QueuedUpdate = {
|
|
17
16
|
position: SourcePosition;
|
|
18
|
-
|
|
17
|
+
snapCallArgs: SnapCallArgs;
|
|
19
18
|
snapFunctionName: string;
|
|
20
19
|
newArgText: string;
|
|
21
20
|
baselinePath: string[] | undefined;
|
|
@@ -24,6 +23,11 @@ export type ExternalSnapshotArgs = SnapshotArgs & {
|
|
|
24
23
|
name: string;
|
|
25
24
|
customPath: string | undefined;
|
|
26
25
|
};
|
|
26
|
+
export type SnapCallArgs = {
|
|
27
|
+
pos: number;
|
|
28
|
+
end: number;
|
|
29
|
+
firstArgText: string | undefined;
|
|
30
|
+
};
|
|
27
31
|
export declare const updateExternalSnapshot: ({ serializedValue: value, position, name, customPath }: ExternalSnapshotArgs) => void;
|
|
28
32
|
export declare const writeSnapshotUpdatesOnExit: () => void;
|
|
29
33
|
export declare const writeUpdates: (queuedUpdates: QueuedUpdate[]) => void;
|
package/out/cache/snapshots.js
CHANGED
|
@@ -3,7 +3,8 @@ import { throwInternalError } from "@ark/util";
|
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
4
|
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
5
5
|
import { getConfig } from "../config.js";
|
|
6
|
-
import {
|
|
6
|
+
import { getSnapCallArgs as getSnapCallArgsTs7 } from "../ts7/snapshots.js";
|
|
7
|
+
import { getFileKey, isTs7 } from "../utils.js";
|
|
7
8
|
import { TsServer, getAbsolutePosition, nearestCallExpressionChild } from "./ts.js";
|
|
8
9
|
import { getCallExpressionsByName } from "./utils.js";
|
|
9
10
|
export const resolveSnapshotPath = (testFile, customPath) => {
|
|
@@ -21,7 +22,7 @@ export const getSnapshotByName = (file, name, customPath) => {
|
|
|
21
22
|
*/
|
|
22
23
|
export const queueSnapshotUpdate = (args) => {
|
|
23
24
|
const config = getConfig();
|
|
24
|
-
const path = config.
|
|
25
|
+
const path = config.assertionCachePath;
|
|
25
26
|
if (existsSync(path)) {
|
|
26
27
|
const existing = readJson(path);
|
|
27
28
|
writeJson(path, {
|
|
@@ -32,15 +33,19 @@ export const queueSnapshotUpdate = (args) => {
|
|
|
32
33
|
else
|
|
33
34
|
writeJson(path, { updates: [args] });
|
|
34
35
|
};
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
const startNode = nearestCallExpressionChild(file,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
const getSnapCallArgs = (position, functionName) => {
|
|
37
|
+
if (isTs7)
|
|
38
|
+
return getSnapCallArgsTs7(position, functionName);
|
|
39
|
+
const file = TsServer.instance.getSourceFileOrThrow(position.file);
|
|
40
|
+
const startNode = nearestCallExpressionChild(file, getAbsolutePosition(file, position));
|
|
41
|
+
if (!getCallExpressionsByName(startNode, [functionName], true).length) {
|
|
42
|
+
throwInternalError(`Unable to locate expected inline ${functionName} call from assertion at ${positionToString(position)}.`);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
pos: startNode.arguments.pos,
|
|
46
|
+
end: startNode.arguments.end,
|
|
47
|
+
firstArgText: startNode.arguments[0]?.getText()
|
|
48
|
+
};
|
|
44
49
|
};
|
|
45
50
|
export const updateExternalSnapshot = ({ serializedValue: value, position, name, customPath }) => {
|
|
46
51
|
const snapshotPath = resolveSnapshotPath(position.file, customPath);
|
|
@@ -56,20 +61,21 @@ let snapshotsWillBeWritten = false;
|
|
|
56
61
|
export const writeSnapshotUpdatesOnExit = () => {
|
|
57
62
|
if (snapshotsWillBeWritten)
|
|
58
63
|
return;
|
|
59
|
-
|
|
64
|
+
// before TS 7's API stops its server on exit
|
|
65
|
+
process.prependListener("exit", writeCachedInlineSnapshotUpdates);
|
|
60
66
|
snapshotsWillBeWritten = true;
|
|
61
67
|
};
|
|
62
68
|
const writeCachedInlineSnapshotUpdates = () => {
|
|
63
69
|
const config = getConfig();
|
|
64
70
|
let snapshotData;
|
|
65
|
-
if (!existsSync(config.
|
|
71
|
+
if (!existsSync(config.assertionCachePath))
|
|
66
72
|
return;
|
|
67
73
|
try {
|
|
68
|
-
snapshotData = readJson(config.
|
|
74
|
+
snapshotData = readJson(config.assertionCachePath).updates;
|
|
69
75
|
}
|
|
70
76
|
catch {
|
|
71
77
|
// If we can't read the snapshot, log an error and move onto the next update
|
|
72
|
-
console.error(`Unable to read snapshot data from expected location ${config.
|
|
78
|
+
console.error(`Unable to read snapshot data from expected location ${config.assertionCachePath}.`);
|
|
73
79
|
}
|
|
74
80
|
if (snapshotData) {
|
|
75
81
|
try {
|
|
@@ -82,7 +88,7 @@ const writeCachedInlineSnapshotUpdates = () => {
|
|
|
82
88
|
}
|
|
83
89
|
};
|
|
84
90
|
const snapshotArgsToQueuedUpdate = ({ position, serializedValue, snapFunctionName = "snap", baselinePath }) => {
|
|
85
|
-
const
|
|
91
|
+
const snapCallArgs = getSnapCallArgs(position, snapFunctionName);
|
|
86
92
|
let newArgText = typeof serializedValue === "string" && serializedValue.includes("\n") ?
|
|
87
93
|
"`" + serializedValue.replace(/`/g, "\\`").replace(/\${/g, "\\${") + "`"
|
|
88
94
|
: JSON.stringify(serializedValue);
|
|
@@ -91,7 +97,7 @@ const snapshotArgsToQueuedUpdate = ({ position, serializedValue, snapFunctionNam
|
|
|
91
97
|
.replace(/"\$ark.undefined"/g, "undefined");
|
|
92
98
|
return {
|
|
93
99
|
position,
|
|
94
|
-
|
|
100
|
+
snapCallArgs,
|
|
95
101
|
snapFunctionName,
|
|
96
102
|
newArgText,
|
|
97
103
|
baselinePath
|
|
@@ -119,7 +125,7 @@ const runFormatterIfAvailable = (queuedUpdates) => {
|
|
|
119
125
|
return;
|
|
120
126
|
try {
|
|
121
127
|
const updatedPaths = [
|
|
122
|
-
...new Set(queuedUpdates.map(update => filePath(update.
|
|
128
|
+
...new Set(queuedUpdates.map(update => filePath(update.position.file)))
|
|
123
129
|
];
|
|
124
130
|
shell(`${formatter} ${updatedPaths.join(" ")}`);
|
|
125
131
|
}
|
|
@@ -131,23 +137,23 @@ const writeFileUpdates = (path, updates) => {
|
|
|
131
137
|
let fileText = readFile(path);
|
|
132
138
|
let offSet = 0;
|
|
133
139
|
for (const update of updates) {
|
|
134
|
-
const
|
|
140
|
+
const { pos, end } = update.snapCallArgs;
|
|
135
141
|
fileText =
|
|
136
|
-
fileText.slice(0,
|
|
142
|
+
fileText.slice(0, pos + offSet) +
|
|
137
143
|
update.newArgText +
|
|
138
|
-
fileText.slice(
|
|
139
|
-
offSet += update.newArgText.length -
|
|
140
|
-
summarizeSnapUpdate(update
|
|
144
|
+
fileText.slice(end + offSet);
|
|
145
|
+
offSet += update.newArgText.length - (end - pos);
|
|
146
|
+
summarizeSnapUpdate(update);
|
|
141
147
|
}
|
|
142
148
|
writeFile(path, fileText);
|
|
143
149
|
};
|
|
144
|
-
const summarizeSnapUpdate = (
|
|
145
|
-
|
|
150
|
+
const summarizeSnapUpdate = (update) => {
|
|
151
|
+
const previousValue = update.snapCallArgs.firstArgText;
|
|
152
|
+
let updateSummary = `${previousValue ? "🆙 Updated" : "📸 Established"} `;
|
|
146
153
|
updateSummary +=
|
|
147
154
|
update.baselinePath ?
|
|
148
155
|
`baseline '${update.baselinePath.join("/")}' `
|
|
149
156
|
: `snap at ${getFileKey(update.position.file)}:${update.position.line} `;
|
|
150
|
-
const previousValue = update.snapCall.arguments[0]?.getText();
|
|
151
157
|
updateSummary +=
|
|
152
158
|
previousValue ?
|
|
153
159
|
`from ${previousValue} to `
|
package/out/cache/ts.js
CHANGED
|
@@ -5,6 +5,7 @@ import { readFileSync } from "node:fs";
|
|
|
5
5
|
import { resolve, dirname, join } from "node:path";
|
|
6
6
|
import ts from "typescript";
|
|
7
7
|
import { getConfig } from "../config.js";
|
|
8
|
+
import { narrowToNamedTestFiles } from "../utils.js";
|
|
8
9
|
export class TsServer {
|
|
9
10
|
rootFiles;
|
|
10
11
|
virtualEnv;
|
|
@@ -21,14 +22,14 @@ export class TsServer {
|
|
|
21
22
|
const tsLibPaths = getTsLibFiles(this.tsConfigInfo.parsed.options);
|
|
22
23
|
// TS represents windows paths as `C:/Users/ssalb/...`
|
|
23
24
|
const normalizedCwd = fromCwd().replace(/\\/g, "/");
|
|
24
|
-
this.rootFiles = this.tsConfigInfo.parsed.fileNames.filter(path => {
|
|
25
|
+
this.rootFiles = narrowToNamedTestFiles(this.tsConfigInfo.parsed.fileNames.filter(path => {
|
|
25
26
|
if (!path.startsWith(normalizedCwd))
|
|
26
27
|
return;
|
|
27
28
|
// exclude empty files as they lead to a crash
|
|
28
29
|
// when createVirtualTypeScriptEnvironment is called
|
|
29
30
|
const contents = readFile(path).trim();
|
|
30
31
|
return contents !== "";
|
|
31
|
-
});
|
|
32
|
+
}));
|
|
32
33
|
const system = tsvfs.createFSBackedSystem(tsLibPaths.defaultMapFromNodeModules, this.tsConfigInfo.path ? dirname(this.tsConfigInfo.path) : fromCwd(), ts);
|
|
33
34
|
this.virtualEnv = tsvfs.createVirtualTypeScriptEnvironment(system, this.rootFiles, ts, this.tsConfigInfo.parsed.options);
|
|
34
35
|
this.program = this.virtualEnv.languageService.getProgram();
|
|
@@ -124,8 +125,10 @@ const defaultDiagnosticHost = {
|
|
|
124
125
|
const throwConfigParseError = (error) => throwError(ts.formatDiagnostics([error], defaultDiagnosticHost));
|
|
125
126
|
const throwConfigInstantiationError = (instantiatedConfig) => throwError(ts.formatDiagnostics(instantiatedConfig.errors, defaultDiagnosticHost));
|
|
126
127
|
export const getTsLibFiles = (tsconfigOptions) => {
|
|
127
|
-
const defaultMapFromNodeModules = tsvfs.createDefaultMapFromNodeModules(tsconfigOptions);
|
|
128
128
|
const libPath = dirname(ts.getDefaultLibFilePath(tsconfigOptions));
|
|
129
|
+
// read libs from the typescript attest imported rather than letting
|
|
130
|
+
// tsvfs resolve its own
|
|
131
|
+
const defaultMapFromNodeModules = tsvfs.createDefaultMapFromNodeModules(tsconfigOptions, ts, libPath);
|
|
129
132
|
return {
|
|
130
133
|
defaultMapFromNodeModules,
|
|
131
134
|
resolvedPaths: [...defaultMapFromNodeModules.keys()].map(path => join(libPath, path))
|
package/out/cache/utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as tsvfs from "@typescript/vfs";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
-
import type { LinePositionRange } from "./
|
|
3
|
+
import type { LinePositionRange } from "./getCachedAssertions.ts";
|
|
4
4
|
export declare const getCallLocationFromCallExpression: (callExpression: ts.CallExpression) => LinePositionRange;
|
|
5
5
|
/**
|
|
6
6
|
* Processes inline instantiations from an attest call
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import type { LinePosition } from "@ark/fs";
|
|
2
1
|
import ts from "typescript";
|
|
2
|
+
import type { AssertionsByFile, TypeAssertionData, TypeRelationship } from "./getCachedAssertions.ts";
|
|
3
3
|
import { type StringifiableType } from "./ts.ts";
|
|
4
|
-
export type AssertionsByFile = Record<string, TypeAssertionData[]>;
|
|
5
4
|
export declare const analyzeProjectAssertions: () => AssertionsByFile;
|
|
6
5
|
export declare const getAssertionsInFile: (file: ts.SourceFile, diagnosticsByFile: DiagnosticsByFile, attestAliases: string[]) => TypeAssertionData[];
|
|
7
6
|
export declare const analyzeAssertCall: (assertCall: ts.CallExpression, diagnosticsByFile: DiagnosticsByFile) => TypeAssertionData;
|
|
8
|
-
export type Completions = Record<string, string[]> | string;
|
|
9
7
|
export type DiagnosticData = {
|
|
10
8
|
start: number;
|
|
11
9
|
end: number;
|
|
@@ -13,32 +11,5 @@ export type DiagnosticData = {
|
|
|
13
11
|
};
|
|
14
12
|
export type DiagnosticsByFile = Record<string, DiagnosticData[]>;
|
|
15
13
|
export declare const getDiagnosticsByFile: () => DiagnosticsByFile;
|
|
16
|
-
export type ArgAssertionData = {
|
|
17
|
-
type: string;
|
|
18
|
-
relationships: {
|
|
19
|
-
args: TypeRelationship[];
|
|
20
|
-
typeArgs: TypeRelationship[];
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
export type TypeRelationshipAssertionData = {
|
|
24
|
-
location: LinePositionRange;
|
|
25
|
-
args: ArgAssertionData[];
|
|
26
|
-
typeArgs: ArgAssertionData[];
|
|
27
|
-
errors: string[];
|
|
28
|
-
completions: Completions;
|
|
29
|
-
/** JSDoc comment for the first argument, if any */
|
|
30
|
-
jsdoc?: string;
|
|
31
|
-
};
|
|
32
|
-
export type TypeBenchmarkingAssertionData = {
|
|
33
|
-
location: LinePositionRange;
|
|
34
|
-
count: number;
|
|
35
|
-
};
|
|
36
|
-
export type TypeAssertionKind = "bench" | "type";
|
|
37
|
-
export type TypeAssertionData<kind extends TypeAssertionKind = TypeAssertionKind> = kind extends "bench" ? TypeBenchmarkingAssertionData : TypeRelationshipAssertionData;
|
|
38
|
-
export type LinePositionRange = {
|
|
39
|
-
start: LinePosition;
|
|
40
|
-
end: LinePosition;
|
|
41
|
-
};
|
|
42
|
-
export type TypeRelationship = "subtype" | "supertype" | "equality" | "none";
|
|
43
14
|
export declare const compareTsTypes: (l: StringifiableType, r: StringifiableType) => TypeRelationship;
|
|
44
15
|
export declare const checkDiagnosticMessages: (attestCall: ts.CallExpression, diagnosticsByFile: DiagnosticsByFile) => string[];
|
|
@@ -127,7 +127,8 @@ const getCompletions = (attestCall) => {
|
|
|
127
127
|
const descendants = getDescendants(arg);
|
|
128
128
|
const file = attestCall.getSourceFile();
|
|
129
129
|
const text = file.getFullText();
|
|
130
|
-
|
|
130
|
+
// null prototype so inherited names like "constructor" aren't seen as duplicates
|
|
131
|
+
const completions = Object.create(null);
|
|
131
132
|
for (const descendant of descendants) {
|
|
132
133
|
if (ts.isStringLiteral(descendant) || ts.isTemplateLiteral(descendant)) {
|
|
133
134
|
// descendant.pos tends to be an open quote while d.end tends to be right after the closing quote.
|
package/out/cli/precache.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { join } from "node:path";
|
|
1
|
+
import { ensureCacheDirs, getConfig } from "../config.js";
|
|
3
2
|
import { writeAssertionData } from "../fixtures.js";
|
|
4
3
|
export const precache = (args) => {
|
|
5
|
-
|
|
6
|
-
writeAssertionData(
|
|
4
|
+
ensureCacheDirs();
|
|
5
|
+
writeAssertionData(args[0] ?? getConfig().assertionCachePath);
|
|
7
6
|
};
|
package/out/cli/shared.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const baseDiagnosticTscCmd
|
|
1
|
+
export declare const baseDiagnosticTscCmd: string;
|
package/out/cli/shared.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { isTs7 } from "../utils.js";
|
|
2
|
+
export const baseDiagnosticTscCmd = "npm exec -- tsc --noEmit --extendedDiagnostics --incremental false --tsBuildInfoFile null" +
|
|
3
|
+
// parallel checkers each instantiate their own copies of types
|
|
4
|
+
(isTs7 ? " --singleThreaded" : "");
|
package/out/cli/trace.js
CHANGED
|
@@ -2,9 +2,47 @@ import { ensureDir, getShellOutput, readJson, writeFile } from "@ark/fs";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { basename, join, relative, resolve } from "node:path";
|
|
4
4
|
import ts from "typescript";
|
|
5
|
-
import { TsServer, getDescendants, getStringifiableType, nearestBoundingCallExpression } from "../cache/ts.js";
|
|
5
|
+
import { TsServer, getDescendants as getDescendantsTs5, getStringifiableType, nearestBoundingCallExpression as nearestBoundingCallExpressionTs5 } from "../cache/ts.js";
|
|
6
6
|
import { getConfig } from "../config.js";
|
|
7
|
+
import { stringifyTypeAtLocation } from "../ts7/analyze.js";
|
|
8
|
+
import { TsgoServer, ast, getDescendants as getDescendantsTs7 } from "../ts7/server.js";
|
|
9
|
+
import { nearestBoundingCallExpression as nearestBoundingCallExpressionTs7 } from "../ts7/snapshots.js";
|
|
10
|
+
import { isTs7 } from "../utils.js";
|
|
7
11
|
import { baseDiagnosticTscCmd } from "./shared.js";
|
|
12
|
+
// TS 7's AST matches TS 5's shape for everything used here
|
|
13
|
+
const syntax = isTs7 ? ast : ts;
|
|
14
|
+
const getSourceFile = (path) => isTs7 ?
|
|
15
|
+
TsgoServer.instance.getSourceFileOrThrow(path)
|
|
16
|
+
: TsServer.instance.getSourceFileOrThrow(path);
|
|
17
|
+
const utf8ByPath = new Map();
|
|
18
|
+
// tsgo's trace positions are UTF-8 byte offsets rather than UTF-16 indices
|
|
19
|
+
const getTextPosition = (path, position) => {
|
|
20
|
+
if (!isTs7)
|
|
21
|
+
return position;
|
|
22
|
+
const text = getSourceFile(path).getFullText();
|
|
23
|
+
let utf8 = utf8ByPath.get(path);
|
|
24
|
+
if (!utf8)
|
|
25
|
+
utf8ByPath.set(path, (utf8 = Buffer.from(text)));
|
|
26
|
+
return utf8.length === text.length ?
|
|
27
|
+
position
|
|
28
|
+
: utf8.subarray(0, position).toString().length;
|
|
29
|
+
};
|
|
30
|
+
const getDescendants = (node) => isTs7 ? getDescendantsTs7(node) : getDescendantsTs5(node);
|
|
31
|
+
const nearestBoundingCallExpression = (node, position) => isTs7 ?
|
|
32
|
+
nearestBoundingCallExpressionTs7(node, position)
|
|
33
|
+
: nearestBoundingCallExpressionTs5(node, position);
|
|
34
|
+
const describeType = (node) => {
|
|
35
|
+
if (isTs7) {
|
|
36
|
+
const { type, string } = stringifyTypeAtLocation(node);
|
|
37
|
+
return { name: string, id: `${type.id}` };
|
|
38
|
+
}
|
|
39
|
+
const type = getStringifiableType(node);
|
|
40
|
+
const name = type.toString();
|
|
41
|
+
return {
|
|
42
|
+
name,
|
|
43
|
+
id: `${type.id ?? name.substring(0, 20)}`
|
|
44
|
+
};
|
|
45
|
+
};
|
|
8
46
|
/**
|
|
9
47
|
* Helper to write output to both console and collect for file output
|
|
10
48
|
*/
|
|
@@ -166,7 +204,6 @@ const generateTraceData = (traceDir, tsconfigPath, packageDir) => {
|
|
|
166
204
|
}
|
|
167
205
|
};
|
|
168
206
|
const initializeAnalysisContext = (traceDir) => {
|
|
169
|
-
const tsServer = TsServer.instance;
|
|
170
207
|
const tracePath = join(traceDir, "trace.json");
|
|
171
208
|
if (!existsSync(tracePath)) {
|
|
172
209
|
throw new Error(`Critical: Expected a trace file at ${tracePath}, but it was not found during context initialization.`);
|
|
@@ -174,7 +211,6 @@ const initializeAnalysisContext = (traceDir) => {
|
|
|
174
211
|
const traceEntries = readJson(tracePath);
|
|
175
212
|
return {
|
|
176
213
|
traceDir,
|
|
177
|
-
tsServer,
|
|
178
214
|
traceEntries,
|
|
179
215
|
durationEntries: [],
|
|
180
216
|
callRanges: [],
|
|
@@ -198,10 +234,10 @@ const filterDurationEntries = (ctx) => {
|
|
|
198
234
|
};
|
|
199
235
|
const processDurationEntry = (entry, ctx) => {
|
|
200
236
|
const entryPath = entry.args.path;
|
|
201
|
-
const entryPos = entry.args.pos;
|
|
202
|
-
const entryEnd = entry.args.end;
|
|
237
|
+
const entryPos = getTextPosition(entryPath, entry.args.pos);
|
|
238
|
+
const entryEnd = getTextPosition(entryPath, entry.args.end);
|
|
203
239
|
const entryDur = entry.dur;
|
|
204
|
-
const sourceFile =
|
|
240
|
+
const sourceFile = getSourceFile(entryPath);
|
|
205
241
|
let callRangeData;
|
|
206
242
|
const callExpr = findCallExpressionInRange(sourceFile, entryPos, entryEnd);
|
|
207
243
|
if (callExpr) {
|
|
@@ -214,12 +250,11 @@ const processDurationEntry = (entry, ctx) => {
|
|
|
214
250
|
else {
|
|
215
251
|
const relevantNode = findMostSpecificNodeInRange(sourceFile, entryPos, entryEnd);
|
|
216
252
|
if (relevantNode) {
|
|
217
|
-
const nodeType =
|
|
218
|
-
const nodeKind =
|
|
219
|
-
const typeName = `${nodeKind}: ${nodeType.
|
|
220
|
-
const typeNodeIdPart = nodeType.id ?? nodeType.toString().substring(0, 20);
|
|
253
|
+
const nodeType = describeType(relevantNode);
|
|
254
|
+
const nodeKind = syntax.SyntaxKind[relevantNode.kind];
|
|
255
|
+
const typeName = `${nodeKind}: ${nodeType.name.substring(0, 25)}`;
|
|
221
256
|
callRangeData = {
|
|
222
|
-
typeId: `node-${relevantNode.kind}-${
|
|
257
|
+
typeId: `node-${relevantNode.kind}-${nodeType.id}`,
|
|
223
258
|
functionName: typeName
|
|
224
259
|
};
|
|
225
260
|
}
|
|
@@ -247,16 +282,16 @@ const findCallExpressionInRange = (file, start, end) => {
|
|
|
247
282
|
return boundingCall;
|
|
248
283
|
const allNodes = getDescendants(file);
|
|
249
284
|
const nodesInRange = allNodes.filter(node => node.pos >= start && node.end <= end);
|
|
250
|
-
const callExpressions = nodesInRange.filter(node =>
|
|
285
|
+
const callExpressions = nodesInRange.filter(node => syntax.isCallExpression(node));
|
|
251
286
|
if (callExpressions.length === 0)
|
|
252
287
|
return undefined;
|
|
253
|
-
const methodCalls = callExpressions.filter(call =>
|
|
288
|
+
const methodCalls = callExpressions.filter(call => syntax.isPropertyAccessExpression(call.expression));
|
|
254
289
|
return methodCalls.length > 0 ? methodCalls[0] : callExpressions[0];
|
|
255
290
|
};
|
|
256
291
|
const extractFunctionName = (callExpr) => {
|
|
257
|
-
if (
|
|
292
|
+
if (syntax.isPropertyAccessExpression(callExpr.expression))
|
|
258
293
|
return callExpr.expression.name.getText();
|
|
259
|
-
if (
|
|
294
|
+
if (syntax.isIdentifier(callExpr.expression))
|
|
260
295
|
return callExpr.expression.getText();
|
|
261
296
|
return "anonymousFunction";
|
|
262
297
|
};
|
|
@@ -508,7 +543,7 @@ const formatLocation = (location) => {
|
|
|
508
543
|
const pos = parseInt(posStr, 10);
|
|
509
544
|
if (isNaN(pos))
|
|
510
545
|
return `${relativePath}:${positionRange}`;
|
|
511
|
-
const sourceFile =
|
|
546
|
+
const sourceFile = getSourceFile(filePath);
|
|
512
547
|
const lineAndChar = sourceFile.getLineAndCharacterOfPosition(pos);
|
|
513
548
|
return `${relativePath}:${lineAndChar.line + 1}:${lineAndChar.character + 1}`;
|
|
514
549
|
};
|
|
@@ -523,23 +558,23 @@ const findMostSpecificNodeInRange = (file, start, end) => {
|
|
|
523
558
|
const findNodeByPreference = (nodes) => {
|
|
524
559
|
if (nodes.length === 0)
|
|
525
560
|
return undefined;
|
|
526
|
-
const typeReference = nodes.find(node =>
|
|
561
|
+
const typeReference = nodes.find(node => syntax.isTypeReferenceNode(node) || syntax.isTypeQueryNode(node));
|
|
527
562
|
if (typeReference)
|
|
528
563
|
return typeReference;
|
|
529
|
-
const declaration = nodes.find(node =>
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
564
|
+
const declaration = nodes.find(node => syntax.isVariableDeclaration(node) ||
|
|
565
|
+
syntax.isFunctionDeclaration(node) ||
|
|
566
|
+
syntax.isClassDeclaration(node) ||
|
|
567
|
+
syntax.isInterfaceDeclaration(node));
|
|
533
568
|
if (declaration)
|
|
534
569
|
return declaration;
|
|
535
|
-
const propertyAccess = nodes.find(node =>
|
|
570
|
+
const propertyAccess = nodes.find(node => syntax.isPropertyAccessExpression(node));
|
|
536
571
|
if (propertyAccess)
|
|
537
572
|
return propertyAccess;
|
|
538
|
-
const assignment = nodes.find(node =>
|
|
539
|
-
node.operatorToken.kind ===
|
|
573
|
+
const assignment = nodes.find(node => syntax.isBinaryExpression(node) &&
|
|
574
|
+
node.operatorToken.kind === syntax.SyntaxKind.EqualsToken);
|
|
540
575
|
if (assignment)
|
|
541
576
|
return assignment;
|
|
542
|
-
const expression = nodes.find(node =>
|
|
577
|
+
const expression = nodes.find(node => syntax.isExpressionStatement(node) || syntax.isExpression(node));
|
|
543
578
|
if (expression)
|
|
544
579
|
return expression;
|
|
545
580
|
return nodes.sort((a, b) => a.end - a.pos - (b.end - b.pos))[0];
|
package/out/config.d.ts
CHANGED
|
@@ -1,30 +1,16 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type JsonObject } from "@ark/util";
|
|
2
2
|
import type * as prettier from "prettier";
|
|
3
|
-
import type ts from "typescript";
|
|
4
|
-
import { type TsVersionData } from "./tsVersioning.ts";
|
|
5
|
-
export type TsVersionAliases = autocomplete<"*"> | string[];
|
|
6
3
|
export type BenchErrorConfig = "runtime" | "types" | boolean;
|
|
7
4
|
type BaseAttestConfig = {
|
|
8
5
|
tsconfig: string | null | undefined;
|
|
9
|
-
compilerOptions
|
|
6
|
+
/** compilerOptions as they would be written in tsconfig.json */
|
|
7
|
+
compilerOptions: JsonObject;
|
|
10
8
|
updateSnapshots: boolean;
|
|
11
9
|
failOnMissingSnapshots: boolean;
|
|
12
|
-
/** A string or list of strings representing the TypeScript version aliases to run.
|
|
13
|
-
*
|
|
14
|
-
* Aliases must be specified as a package.json dependency or devDependency beginning with "typescript".
|
|
15
|
-
* Alternate aliases can be specified using the "npm:" prefix:
|
|
16
|
-
* ```json
|
|
17
|
-
* "typescript": "latest",
|
|
18
|
-
* "typescript-next: "npm:typescript@next",
|
|
19
|
-
* "typescript-1": "npm:typescript@5.2"
|
|
20
|
-
* "typescript-2": "npm:typescript@5.1"
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* "*" can be pased to run all discovered versions beginning with "typescript".
|
|
24
|
-
*/
|
|
25
|
-
tsVersions: TsVersionAliases | TsVersionData[];
|
|
26
10
|
skipTypes: boolean;
|
|
27
11
|
skipInlineInstantiations: boolean;
|
|
12
|
+
/** analyze only the test files named on the command line */
|
|
13
|
+
onlyNamedTestFiles: boolean;
|
|
28
14
|
attestAliases: string[];
|
|
29
15
|
benchPercentThreshold: number;
|
|
30
16
|
benchErrorOnThresholdExceeded: BenchErrorConfig;
|
|
@@ -49,9 +35,7 @@ export declare const getDefaultAttestConfig: () => BaseAttestConfig;
|
|
|
49
35
|
export declare const attestEnvPrefix = "ATTEST_";
|
|
50
36
|
export interface ParsedAttestConfig extends Readonly<BaseAttestConfig> {
|
|
51
37
|
cacheDir: string;
|
|
52
|
-
|
|
53
|
-
defaultAssertionCachePath: string;
|
|
54
|
-
tsVersions: TsVersionData[];
|
|
38
|
+
assertionCachePath: string;
|
|
55
39
|
}
|
|
56
40
|
export declare const getConfig: () => ParsedAttestConfig;
|
|
57
41
|
export declare const ensureCacheDirs: () => void;
|
package/out/config.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { ensureDir, fromCwd } from "@ark/fs";
|
|
2
|
-
import {
|
|
2
|
+
import { tryParseNumber } from "@ark/util";
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
4
|
import { join, resolve } from "node:path";
|
|
5
|
-
import { findAttestTypeScriptVersions } from "./tsVersioning.js";
|
|
6
5
|
export const getDefaultAttestConfig = () => ({
|
|
7
6
|
tsconfig: existsSync(fromCwd("tsconfig.json")) ? fromCwd("tsconfig.json") : undefined,
|
|
8
7
|
compilerOptions: {},
|
|
@@ -11,7 +10,7 @@ export const getDefaultAttestConfig = () => ({
|
|
|
11
10
|
updateSnapshots: false,
|
|
12
11
|
skipTypes: false,
|
|
13
12
|
skipInlineInstantiations: false,
|
|
14
|
-
|
|
13
|
+
onlyNamedTestFiles: false,
|
|
15
14
|
benchPercentThreshold: 20,
|
|
16
15
|
benchErrorOnThresholdExceeded: true,
|
|
17
16
|
filter: undefined,
|
|
@@ -45,7 +44,7 @@ const getParamValue = (param) => {
|
|
|
45
44
|
return null;
|
|
46
45
|
if (param === "benchPercentThreshold")
|
|
47
46
|
return tryParseNumber(raw, { errorOnFail: true });
|
|
48
|
-
if (param === "
|
|
47
|
+
if (param === "attestAliases")
|
|
49
48
|
return raw.split(",");
|
|
50
49
|
if (param === "typeToStringFormat" || param === "compilerOptions")
|
|
51
50
|
return JSON.parse(raw);
|
|
@@ -75,33 +74,10 @@ const addEnvConfig = (config) => {
|
|
|
75
74
|
return config;
|
|
76
75
|
};
|
|
77
76
|
const parseConfig = () => {
|
|
78
|
-
const baseConfig = addEnvConfig(getDefaultAttestConfig());
|
|
79
77
|
const cacheDir = resolve(".attest");
|
|
80
|
-
|
|
81
|
-
const defaultAssertionCachePath = join(assertionCacheDir, "typescript.json");
|
|
82
|
-
return Object.assign(baseConfig, {
|
|
78
|
+
return Object.assign(addEnvConfig(getDefaultAttestConfig()), {
|
|
83
79
|
cacheDir,
|
|
84
|
-
|
|
85
|
-
defaultAssertionCachePath,
|
|
86
|
-
tsVersions: baseConfig.skipTypes ? []
|
|
87
|
-
: isTsVersionAliases(baseConfig.tsVersions) ?
|
|
88
|
-
parseTsVersions(baseConfig.tsVersions)
|
|
89
|
-
: baseConfig.tsVersions
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
const isTsVersionAliases = (v) => typeof v === "string" || (isArray(v) && typeof v[0] === "string");
|
|
93
|
-
const parseTsVersions = (aliases) => {
|
|
94
|
-
const versions = findAttestTypeScriptVersions();
|
|
95
|
-
if (aliases === "*" || (isArray(aliases) && aliases[0] === "*"))
|
|
96
|
-
return versions;
|
|
97
|
-
return liftArray(aliases).map(alias => {
|
|
98
|
-
const matching = versions.find(v => v.alias === alias);
|
|
99
|
-
if (!matching) {
|
|
100
|
-
throw new Error(`Specified TypeScript version ${alias} does not exist.` +
|
|
101
|
-
` It should probably be specified in package.json like:
|
|
102
|
-
"@ark/attest-ts-${alias}": "npm:typescript@latest"`);
|
|
103
|
-
}
|
|
104
|
-
return matching;
|
|
80
|
+
assertionCachePath: join(cacheDir, "assertions.json")
|
|
105
81
|
});
|
|
106
82
|
};
|
|
107
83
|
let cachedConfig;
|
|
@@ -116,5 +92,4 @@ if (!globalThis.localStorage?.getItem)
|
|
|
116
92
|
export const ensureCacheDirs = () => {
|
|
117
93
|
cachedConfig ??= getConfig();
|
|
118
94
|
ensureDir(cachedConfig.cacheDir);
|
|
119
|
-
ensureDir(cachedConfig.assertionCacheDir);
|
|
120
95
|
};
|
package/out/fixtures.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { writeJson } from "@ark/fs";
|
|
2
2
|
import { rmSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
3
|
import { writeSnapshotUpdatesOnExit } from "./cache/snapshots.js";
|
|
5
4
|
import { analyzeProjectAssertions } from "./cache/writeAssertionCache.js";
|
|
6
5
|
import { ensureCacheDirs, getConfig } from "./config.js";
|
|
7
|
-
import {
|
|
6
|
+
import { analyzeProjectAssertions as analyzeProjectAssertionsTs7 } from "./ts7/analyze.js";
|
|
7
|
+
import { isTs7 } from "./utils.js";
|
|
8
8
|
export const setup = (options) => {
|
|
9
9
|
const { ...config } = getConfig();
|
|
10
10
|
if (options)
|
|
@@ -14,25 +14,12 @@ export const setup = (options) => {
|
|
|
14
14
|
ensureCacheDirs();
|
|
15
15
|
if (config.skipTypes)
|
|
16
16
|
return teardown;
|
|
17
|
-
|
|
18
|
-
config.tsVersions[0].alias === "default")
|
|
19
|
-
writeAssertionData(config.defaultAssertionCachePath);
|
|
20
|
-
else {
|
|
21
|
-
forTypeScriptVersions(config.tsVersions, version => {
|
|
22
|
-
const precachePath = join(config.assertionCacheDir, version.alias + ".json");
|
|
23
|
-
// if we're in our own repo, we need to pnpm to use the root script to execute ts directly
|
|
24
|
-
if (fileName().endsWith("ts"))
|
|
25
|
-
shell(`pnpm attest precache ${precachePath}`);
|
|
26
|
-
// otherwise, just use npm to run the CLI command from build output
|
|
27
|
-
else
|
|
28
|
-
shell(`npm exec -c "attest precache ${precachePath}"`);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
17
|
+
writeAssertionData(config.assertionCachePath);
|
|
31
18
|
return teardown;
|
|
32
19
|
};
|
|
33
20
|
export const writeAssertionData = (toPath) => {
|
|
34
21
|
console.log("⏳ Waiting for TypeScript to check your project (this may take a while)...");
|
|
35
|
-
writeJson(toPath, analyzeProjectAssertions());
|
|
22
|
+
writeJson(toPath, isTs7 ? analyzeProjectAssertionsTs7() : analyzeProjectAssertions());
|
|
36
23
|
};
|
|
37
24
|
export const cleanup = () => writeSnapshotUpdatesOnExit();
|
|
38
25
|
/** alias for cleanup to align with vitest and others */
|
package/out/index.d.ts
CHANGED
|
@@ -2,8 +2,6 @@ export { cleanup, setup, teardown, writeAssertionData } from "./fixtures.ts";
|
|
|
2
2
|
export { caller, type CallerOfOptions } from "@ark/fs";
|
|
3
3
|
export { attest } from "./assert/attest.ts";
|
|
4
4
|
export { bench } from "./bench/bench.ts";
|
|
5
|
-
export {
|
|
6
|
-
export type { ArgAssertionData, LinePositionRange, TypeAssertionData, TypeRelationship } from "./cache/writeAssertionCache.ts";
|
|
5
|
+
export { getBenchAssertionAtPosition, getTypeAssertionAtPosition, type ArgAssertionData, type LinePositionRange, type TypeAssertionData, type TypeRelationship } from "./cache/getCachedAssertions.ts";
|
|
7
6
|
export { getDefaultAttestConfig, type AttestConfig } from "./config.ts";
|
|
8
|
-
export { findAttestTypeScriptVersions, getPrimaryTsVersionUnderTest } from "./tsVersioning.ts";
|
|
9
7
|
export { contextualize } from "./utils.ts";
|
package/out/index.js
CHANGED
|
@@ -3,7 +3,6 @@ export { cleanup, setup, teardown, writeAssertionData } from "./fixtures.js";
|
|
|
3
3
|
export { caller } from "@ark/fs";
|
|
4
4
|
export { attest } from "./assert/attest.js";
|
|
5
5
|
export { bench } from "./bench/bench.js";
|
|
6
|
-
export {
|
|
6
|
+
export { getBenchAssertionAtPosition, getTypeAssertionAtPosition } from "./cache/getCachedAssertions.js";
|
|
7
7
|
export { getDefaultAttestConfig } from "./config.js";
|
|
8
|
-
export { findAttestTypeScriptVersions, getPrimaryTsVersionUnderTest } from "./tsVersioning.js";
|
|
9
8
|
export { contextualize } from "./utils.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AssertionsByFile } from "../cache/getCachedAssertions.ts";
|
|
2
|
+
import { type Node, type Type } from "./server.ts";
|
|
3
|
+
export declare const analyzeProjectAssertions: () => AssertionsByFile;
|
|
4
|
+
type StringifiedType = {
|
|
5
|
+
type: Type;
|
|
6
|
+
string: string;
|
|
7
|
+
isUnresolvable: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare const stringifyTypeAtLocation: (node: Node) => StringifiedType;
|
|
10
|
+
export {};
|