@ark/attest 0.43.4 → 0.44.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
@@ -112,6 +112,18 @@ describe("attest features", () => {
112
112
  attest({ "f": "🐐" } as Legends).completions({ "f": ["faker"] })
113
113
  })
114
114
 
115
+ it("jsdoc snapshotting", () => {
116
+ // match or snapshot expected jsdoc associated with the value passed to attest
117
+ const t = type({
118
+ /** FOO */
119
+ foo: "string"
120
+ })
121
+
122
+ const out = t.assert({ foo: "foo" })
123
+
124
+ attest(out.foo).jsdoc.snap("FOO")
125
+ })
126
+
115
127
  it("integrate runtime logic with type assertions", () => {
116
128
  const arrayOf = type("<t>", "t[]")
117
129
  const numericArray = arrayOf("number | bigint")
@@ -29,6 +29,7 @@ export declare class ChainableAssertions implements AssertionRecord {
29
29
  get throws(): unknown;
30
30
  throwsAndHasTypeError(matchValue: string | RegExp): void;
31
31
  get completions(): any;
32
+ get jsdoc(): any;
32
33
  get type(): any;
33
34
  }
34
35
  export type AssertionKind = "value" | "type";
@@ -64,6 +65,7 @@ export type comparableValueAssertion<expected, kind extends AssertionKind> = {
64
65
  instanceOf: (constructor: Constructor) => nextAssertions<kind>;
65
66
  is: (value: expected) => nextAssertions<kind>;
66
67
  completions: CompletionsSnap;
68
+ jsdoc: comparableValueAssertion<string, kind>;
67
69
  satisfies: <const def>(def: type.validate<def> & validateExpectedOverlaps<expected, type.infer.In<def>>) => nextAssertions<kind>;
68
70
  unknown: Omit<comparableValueAssertion<unknown, kind>, "unknown">;
69
71
  unwrap: Unwrapper<expected>;
@@ -160,6 +160,15 @@ export class ChainableAssertions {
160
160
  this.ctx.lastSnapName = "completions";
161
161
  return this.snap;
162
162
  }
163
+ get jsdoc() {
164
+ if (this.ctx.cfg.skipTypes)
165
+ return chainableNoOpProxy;
166
+ this.ctx.versionableActual = new TypeAssertionMapping(data => ({
167
+ actual: formatTypeString(data.jsdoc ?? "")
168
+ }));
169
+ this.ctx.allowRegex = true;
170
+ return this.immediateOrChained();
171
+ }
163
172
  get type() {
164
173
  if (this.ctx.cfg.skipTypes)
165
174
  return chainableNoOpProxy;
@@ -1,8 +1,12 @@
1
1
  import * as tsvfs from "@typescript/vfs";
2
2
  import ts from "typescript";
3
- import type { AssertionsByFile, LinePositionRange } from "./writeAssertionCache.ts";
3
+ import type { LinePositionRange } from "./writeAssertionCache.ts";
4
4
  export declare const getCallLocationFromCallExpression: (callExpression: ts.CallExpression) => LinePositionRange;
5
- export declare const gatherInlineInstantiationData: (file: ts.SourceFile, fileAssertions: AssertionsByFile, attestAliasInstantiationMethodCalls: string[]) => void;
5
+ /**
6
+ * Processes inline instantiations from an attest call
7
+ * Preserves any JSDoc comments that are associated with the original expression
8
+ */
9
+ export declare const gatherInlineInstantiationData: (file: ts.SourceFile, assertionsByFile: Record<string, any[]>, instantiationMethodCalls: string[]) => void;
6
10
  export declare const getCallExpressionsByName: (startNode: ts.Node, names: string[], isSnapCall?: boolean) => ts.CallExpression[];
7
11
  export declare const getInstantiationsContributedByNode: (file: ts.SourceFile, benchBlock: ts.FunctionExpression | ts.ArrowFunction) => number;
8
12
  export declare const createOrUpdateFile: (env: tsvfs.VirtualTypeScriptEnvironment, fileName: string, fileText: string) => ts.SourceFile | undefined;
@@ -21,8 +21,12 @@ export const getCallLocationFromCallExpression = (callExpression) => {
21
21
  };
22
22
  return location;
23
23
  };
24
- export const gatherInlineInstantiationData = (file, fileAssertions, attestAliasInstantiationMethodCalls) => {
25
- const expressions = getCallExpressionsByName(file, attestAliasInstantiationMethodCalls);
24
+ /**
25
+ * Processes inline instantiations from an attest call
26
+ * Preserves any JSDoc comments that are associated with the original expression
27
+ */
28
+ export const gatherInlineInstantiationData = (file, assertionsByFile, instantiationMethodCalls) => {
29
+ const expressions = getCallExpressionsByName(file, instantiationMethodCalls);
26
30
  if (!expressions.length)
27
31
  return;
28
32
  const enclosingFunctions = expressions.map(expression => {
@@ -43,8 +47,8 @@ ${enclosingFunction.ancestor.getText()}`);
43
47
  count: getInstantiationsContributedByNode(file, body)
44
48
  };
45
49
  });
46
- const assertions = fileAssertions[getFileKey(file.fileName)] ?? [];
47
- fileAssertions[getFileKey(file.fileName)] = [
50
+ const assertions = assertionsByFile[getFileKey(file.fileName)] ?? [];
51
+ assertionsByFile[getFileKey(file.fileName)] = [
48
52
  ...assertions,
49
53
  ...instantiationInfo
50
54
  ];
@@ -26,6 +26,8 @@ export type TypeRelationshipAssertionData = {
26
26
  typeArgs: ArgAssertionData[];
27
27
  errors: string[];
28
28
  completions: Completions;
29
+ /** JSDoc comment for the first argument, if any */
30
+ jsdoc?: string;
29
31
  };
30
32
  export type TypeBenchmarkingAssertionData = {
31
33
  location: LinePositionRange;
@@ -33,13 +33,85 @@ export const analyzeAssertCall = (assertCall, diagnosticsByFile) => {
33
33
  const typeArgs = types.typeArgs.map(typeArg => serializeArg(typeArg, types));
34
34
  const errors = checkDiagnosticMessages(assertCall, diagnosticsByFile);
35
35
  const completions = getCompletions(assertCall);
36
- return {
36
+ // Extract JSDoc comment for first argument if available
37
+ const jsdoc = extractJSDocFromArgument(assertCall);
38
+ const result = {
37
39
  location,
38
40
  args,
39
41
  typeArgs,
40
42
  errors,
41
43
  completions
42
44
  };
45
+ if (jsdoc)
46
+ result.jsdoc = jsdoc;
47
+ return result;
48
+ };
49
+ /**
50
+ * Extract JSDoc comments associated with the first argument of a call expression
51
+ */
52
+ const extractJSDocFromArgument = (callExpr) => {
53
+ // We're only interested in the first argument
54
+ const firstArg = callExpr.arguments[0];
55
+ if (!firstArg)
56
+ return undefined;
57
+ const checker = getInternalTypeChecker();
58
+ // If the argument is a property access expression (e.g., out.foo)
59
+ if (ts.isPropertyAccessExpression(firstArg)) {
60
+ // Try to find the symbol for the property
61
+ const propSymbol = checker.getSymbolAtLocation(firstArg);
62
+ if (propSymbol) {
63
+ // Get JSDoc from property declarations
64
+ return getJSDocFromSymbol(propSymbol);
65
+ }
66
+ }
67
+ // If argument is an identifier, try to find its declaration's JSDoc
68
+ else if (ts.isIdentifier(firstArg)) {
69
+ const symbol = checker.getSymbolAtLocation(firstArg);
70
+ if (symbol)
71
+ return getJSDocFromSymbol(symbol);
72
+ }
73
+ return undefined;
74
+ };
75
+ /**
76
+ * Extract JSDoc comments from a symbol's declarations
77
+ */
78
+ const getJSDocFromSymbol = (symbol) => {
79
+ // Get JSDoc directly from the symbol if possible
80
+ const symbolDocumentation = ts.displayPartsToString(symbol.getDocumentationComment(getInternalTypeChecker()));
81
+ if (symbolDocumentation)
82
+ return symbolDocumentation.trim();
83
+ // If no symbol documentation, try to get JSDoc from declarations
84
+ const declarations = symbol.getDeclarations() || [];
85
+ for (const declaration of declarations) {
86
+ // For property declarations in object literals, get the JSDoc comment
87
+ if (ts.isPropertyAssignment(declaration) ||
88
+ ts.isShorthandPropertyAssignment(declaration) ||
89
+ ts.isPropertyDeclaration(declaration)) {
90
+ const jsDocTags = ts.getJSDocTags(declaration);
91
+ if (jsDocTags.length > 0) {
92
+ return jsDocTags
93
+ .map(tag => {
94
+ const comment = tag.comment?.toString() || "";
95
+ return tag.tagName.text + (comment ? ` ${comment}` : "");
96
+ })
97
+ .join("\n");
98
+ }
99
+ // Try to get JSDoc comment before the property
100
+ const jsDocComments = ts.getJSDocCommentsAndTags(declaration);
101
+ if (jsDocComments && jsDocComments.length > 0) {
102
+ return jsDocComments
103
+ .map(doc => {
104
+ if (ts.isJSDoc(doc))
105
+ return doc.comment || "";
106
+ return "";
107
+ })
108
+ .filter(Boolean)
109
+ .join("\n")
110
+ .trim();
111
+ }
112
+ }
113
+ }
114
+ return undefined;
43
115
  };
44
116
  const serializeArg = (arg, context) => ({
45
117
  type: arg.toString(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ark/attest",
3
- "version": "0.43.4",
3
+ "version": "0.44.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "David Blass",
@@ -36,9 +36,9 @@
36
36
  "@typescript/analyze-trace": "0.10.1",
37
37
  "@typescript/vfs": "1.6.0",
38
38
  "prettier": "3.4.2",
39
- "arktype": "2.1.4",
40
- "@ark/fs": "0.43.4",
41
- "@ark/util": "0.43.4"
39
+ "arktype": "2.1.5",
40
+ "@ark/util": "0.44.0",
41
+ "@ark/fs": "0.44.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "5.8.2"