@openpkg-ts/extract 0.11.2 → 0.11.3

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/dist/bin/tspec.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  extract
4
- } from "../shared/chunk-570ne82m.js";
4
+ } from "../shared/chunk-9x67bae3.js";
5
5
 
6
6
  // src/cli/spec.ts
7
7
  import * as fs from "node:fs";
@@ -29,6 +29,27 @@ class TypeRegistry {
29
29
 
30
30
  // src/ast/utils.ts
31
31
  import ts from "typescript";
32
+ function parseExamplesFromTags(tags) {
33
+ const examples = [];
34
+ for (const tag of tags) {
35
+ if (tag.name !== "example")
36
+ continue;
37
+ const text = tag.text.trim();
38
+ const fenceMatch = text.match(/^```(\w*)\n([\s\S]*?)\n?```$/);
39
+ if (fenceMatch) {
40
+ const lang = fenceMatch[1] || undefined;
41
+ const code = fenceMatch[2].trim();
42
+ const example = { code };
43
+ if (lang && ["ts", "js", "tsx", "jsx", "shell", "json"].includes(lang)) {
44
+ example.language = lang;
45
+ }
46
+ examples.push(example);
47
+ } else if (text) {
48
+ examples.push({ code: text });
49
+ }
50
+ }
51
+ return examples;
52
+ }
32
53
  function getJSDocComment(node) {
33
54
  const jsDocTags = ts.getJSDocTags(node);
34
55
  const tags = jsDocTags.map((tag) => ({
@@ -43,7 +64,8 @@ function getJSDocComment(node) {
43
64
  description = typeof firstDoc.comment === "string" ? firstDoc.comment : ts.getTextOfJSDocComment(firstDoc.comment);
44
65
  }
45
66
  }
46
- return { description, tags };
67
+ const examples = parseExamplesFromTags(tags);
68
+ return { description, tags, examples };
47
69
  }
48
70
  function getSourceLocation(node, sourceFile) {
49
71
  const { line } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
@@ -109,7 +131,7 @@ function serializeClass(node, ctx) {
109
131
  if (!name)
110
132
  return null;
111
133
  const declSourceFile = node.getSourceFile();
112
- const { description, tags } = getJSDocComment(node);
134
+ const { description, tags, examples } = getJSDocComment(node);
113
135
  const source = getSourceLocation(node, declSourceFile);
114
136
  return {
115
137
  id: name,
@@ -118,7 +140,8 @@ function serializeClass(node, ctx) {
118
140
  description,
119
141
  tags,
120
142
  source,
121
- members: []
143
+ members: [],
144
+ ...examples.length > 0 ? { examples } : {}
122
145
  };
123
146
  }
124
147
 
@@ -129,7 +152,7 @@ function serializeEnum(node, ctx) {
129
152
  if (!name)
130
153
  return null;
131
154
  const declSourceFile = node.getSourceFile();
132
- const { description, tags } = getJSDocComment(node);
155
+ const { description, tags, examples } = getJSDocComment(node);
133
156
  const source = getSourceLocation(node, declSourceFile);
134
157
  const members = node.members.map((member) => {
135
158
  const memberSymbol = ctx.typeChecker.getSymbolAtLocation(member.name);
@@ -147,7 +170,8 @@ function serializeEnum(node, ctx) {
147
170
  description,
148
171
  tags,
149
172
  source,
150
- members
173
+ members,
174
+ ...examples.length > 0 ? { examples } : {}
151
175
  };
152
176
  }
153
177
 
@@ -170,7 +194,7 @@ function serializeFunctionExport(node, ctx) {
170
194
  if (!name)
171
195
  return null;
172
196
  const declSourceFile = node.getSourceFile();
173
- const { description, tags } = getJSDocComment(node);
197
+ const { description, tags, examples } = getJSDocComment(node);
174
198
  const source = getSourceLocation(node, declSourceFile);
175
199
  const type = ctx.typeChecker.getTypeAtLocation(node);
176
200
  const callSignatures = type.getCallSignatures();
@@ -191,7 +215,8 @@ function serializeFunctionExport(node, ctx) {
191
215
  description,
192
216
  tags,
193
217
  source,
194
- signatures
218
+ signatures,
219
+ ...examples.length > 0 ? { examples } : {}
195
220
  };
196
221
  }
197
222
 
@@ -202,7 +227,7 @@ function serializeInterface(node, ctx) {
202
227
  if (!name)
203
228
  return null;
204
229
  const declSourceFile = node.getSourceFile();
205
- const { description, tags } = getJSDocComment(node);
230
+ const { description, tags, examples } = getJSDocComment(node);
206
231
  const source = getSourceLocation(node, declSourceFile);
207
232
  return {
208
233
  id: name,
@@ -211,7 +236,8 @@ function serializeInterface(node, ctx) {
211
236
  description,
212
237
  tags,
213
238
  source,
214
- members: []
239
+ members: [],
240
+ ...examples.length > 0 ? { examples } : {}
215
241
  };
216
242
  }
217
243
 
@@ -222,7 +248,7 @@ function serializeTypeAlias(node, ctx) {
222
248
  if (!name)
223
249
  return null;
224
250
  const declSourceFile = node.getSourceFile();
225
- const { description, tags } = getJSDocComment(node);
251
+ const { description, tags, examples } = getJSDocComment(node);
226
252
  const source = getSourceLocation(node, declSourceFile);
227
253
  const type = ctx.typeChecker.getTypeAtLocation(node);
228
254
  const typeString = ctx.typeChecker.typeToString(type);
@@ -233,7 +259,8 @@ function serializeTypeAlias(node, ctx) {
233
259
  description,
234
260
  tags,
235
261
  source,
236
- ...typeString !== name ? { type: typeString } : {}
262
+ ...typeString !== name ? { type: typeString } : {},
263
+ ...examples.length > 0 ? { examples } : {}
237
264
  };
238
265
  }
239
266
 
@@ -244,7 +271,7 @@ function serializeVariable(node, statement, ctx) {
244
271
  if (!name)
245
272
  return null;
246
273
  const declSourceFile = node.getSourceFile();
247
- const { description, tags } = getJSDocComment(statement);
274
+ const { description, tags, examples } = getJSDocComment(statement);
248
275
  const source = getSourceLocation(node, declSourceFile);
249
276
  const type = ctx.typeChecker.getTypeAtLocation(node);
250
277
  const typeString = ctx.typeChecker.typeToString(type);
@@ -255,7 +282,8 @@ function serializeVariable(node, statement, ctx) {
255
282
  description,
256
283
  tags,
257
284
  source,
258
- ...typeString && typeString !== name ? { type: typeString } : {}
285
+ ...typeString && typeString !== name ? { type: typeString } : {},
286
+ ...examples.length > 0 ? { examples } : {}
259
287
  };
260
288
  }
261
289
 
@@ -8,14 +8,18 @@ declare class TypeRegistry {
8
8
  getAll(): SpecType[];
9
9
  registerFromSymbol(symbol: ts.Symbol, checker: ts.TypeChecker): SpecType | undefined;
10
10
  }
11
- import { SpecSource, SpecTag } from "@openpkg-ts/spec";
11
+ import { SpecExample, SpecSource, SpecTag } from "@openpkg-ts/spec";
12
12
  import ts2 from "typescript";
13
13
  declare function getJSDocComment(node: ts2.Node): {
14
14
  description?: string;
15
15
  tags: SpecTag[];
16
+ examples: SpecExample[];
16
17
  };
17
18
  declare function getSourceLocation(node: ts2.Node, sourceFile: ts2.SourceFile): SpecSource;
18
19
  import { OpenPkg } from "@openpkg-ts/spec";
20
+ import { TypeChecker as TypeChecker_vexmldwuwp } from "typescript";
21
+ import { Program as Program_lhgjafqgga } from "typescript";
22
+ import { SourceFile as SourceFile_sameteuoys } from "typescript";
19
23
  interface ExtractOptions {
20
24
  entryFile: string;
21
25
  baseDir?: string;
@@ -38,9 +42,9 @@ interface Diagnostic {
38
42
  };
39
43
  }
40
44
  interface SerializerContext {
41
- typeChecker: import("typescript").TypeChecker;
42
- program: import("typescript").Program;
43
- sourceFile: import("typescript").SourceFile;
45
+ typeChecker: TypeChecker_vexmldwuwp;
46
+ program: Program_lhgjafqgga;
47
+ sourceFile: SourceFile_sameteuoys;
44
48
  maxTypeDepth: number;
45
49
  resolveExternalTypes: boolean;
46
50
  }
package/dist/src/index.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  serializeInterface,
12
12
  serializeTypeAlias,
13
13
  serializeVariable
14
- } from "../shared/chunk-570ne82m.js";
14
+ } from "../shared/chunk-9x67bae3.js";
15
15
  // src/schema/adapters/arktype.ts
16
16
  var arktypeAdapter = {
17
17
  name: "arktype",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpkg-ts/extract",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "description": "TypeScript export extraction to OpenPkg spec",
5
5
  "keywords": [
6
6
  "openpkg",
@@ -40,7 +40,7 @@
40
40
  "format": "biome format --write src/"
41
41
  },
42
42
  "dependencies": {
43
- "@openpkg-ts/spec": "workspace:*",
43
+ "@openpkg-ts/spec": "^0.11.1",
44
44
  "commander": "^12.0.0",
45
45
  "typescript": "^5.0.0"
46
46
  },