@openpkg-ts/sdk 0.48.0 → 0.49.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/dist/index.d.ts +30 -1
- package/dist/index.js +421 -346
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1300,7 +1300,7 @@ function filterSpec(spec, criteria) {
|
|
|
1300
1300
|
};
|
|
1301
1301
|
}
|
|
1302
1302
|
// src/primitives/get.ts
|
|
1303
|
-
import
|
|
1303
|
+
import ts12 from "typescript";
|
|
1304
1304
|
|
|
1305
1305
|
// src/ast/utils.ts
|
|
1306
1306
|
import * as path2 from "node:path";
|
|
@@ -1846,13 +1846,84 @@ function createProgram(options) {
|
|
|
1846
1846
|
}
|
|
1847
1847
|
|
|
1848
1848
|
// src/serializers/classes.ts
|
|
1849
|
-
import
|
|
1849
|
+
import ts8 from "typescript";
|
|
1850
1850
|
|
|
1851
1851
|
// src/types/parameters.ts
|
|
1852
|
-
import
|
|
1852
|
+
import ts5 from "typescript";
|
|
1853
1853
|
|
|
1854
1854
|
// src/types/schema-builder.ts
|
|
1855
|
+
import ts4 from "typescript";
|
|
1856
|
+
|
|
1857
|
+
// src/ast/type-identity.ts
|
|
1858
|
+
import * as path4 from "node:path";
|
|
1855
1859
|
import ts3 from "typescript";
|
|
1860
|
+
var NODE_MODULES_PKG = /node_modules\/(@[^/]+\/[^/]+|[^/]+)/;
|
|
1861
|
+
function packageLabel(fileName, workspacePackages) {
|
|
1862
|
+
const match = fileName.match(NODE_MODULES_PKG);
|
|
1863
|
+
let pkg = match?.[1];
|
|
1864
|
+
if (!pkg) {
|
|
1865
|
+
for (const [name, dir] of workspacePackages) {
|
|
1866
|
+
if (fileName.startsWith(`${path4.resolve(dir)}${path4.sep}`)) {
|
|
1867
|
+
pkg = name;
|
|
1868
|
+
break;
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
return (pkg ?? "local").replace(/^@/, "").replace(/\//g, "-");
|
|
1873
|
+
}
|
|
1874
|
+
function declKey(symbol, checker) {
|
|
1875
|
+
let resolved = symbol;
|
|
1876
|
+
if (symbol.flags & ts3.SymbolFlags.Alias) {
|
|
1877
|
+
try {
|
|
1878
|
+
resolved = checker.getAliasedSymbol(symbol);
|
|
1879
|
+
} catch {}
|
|
1880
|
+
}
|
|
1881
|
+
const decl = resolved.declarations?.[0];
|
|
1882
|
+
if (!decl)
|
|
1883
|
+
return;
|
|
1884
|
+
return `${decl.getSourceFile().fileName}#${decl.getStart()}`;
|
|
1885
|
+
}
|
|
1886
|
+
function resolveTypeId(symbol, ctx) {
|
|
1887
|
+
const cached = ctx.typeIds.get(symbol);
|
|
1888
|
+
if (cached)
|
|
1889
|
+
return cached;
|
|
1890
|
+
const key = declKey(symbol, ctx.typeChecker);
|
|
1891
|
+
if (key) {
|
|
1892
|
+
const existing = ctx.declIds.get(key);
|
|
1893
|
+
if (existing) {
|
|
1894
|
+
ctx.typeIds.set(symbol, existing);
|
|
1895
|
+
return existing;
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
const claim = (id) => {
|
|
1899
|
+
ctx.typeIds.set(symbol, id);
|
|
1900
|
+
if (key)
|
|
1901
|
+
ctx.declIds.set(key, id);
|
|
1902
|
+
ctx.idOwner.set(id, key ?? id);
|
|
1903
|
+
return id;
|
|
1904
|
+
};
|
|
1905
|
+
const name = symbol.getName();
|
|
1906
|
+
const owner = ctx.idOwner.get(name);
|
|
1907
|
+
if (!owner || owner === key)
|
|
1908
|
+
return claim(name);
|
|
1909
|
+
const file = symbol.declarations?.[0]?.getSourceFile().fileName ?? "";
|
|
1910
|
+
const scoped = `${packageLabel(file, ctx.workspacePackages)}.${name}`;
|
|
1911
|
+
const scopedOwner = ctx.idOwner.get(scoped);
|
|
1912
|
+
if (!scopedOwner || scopedOwner === key)
|
|
1913
|
+
return claim(scoped);
|
|
1914
|
+
let n = 2;
|
|
1915
|
+
while (ctx.idOwner.has(`${name}_${n}`))
|
|
1916
|
+
n++;
|
|
1917
|
+
return claim(`${name}_${n}`);
|
|
1918
|
+
}
|
|
1919
|
+
function typeRefId(type, ctx) {
|
|
1920
|
+
const symbol = type.aliasSymbol ?? type.getSymbol();
|
|
1921
|
+
if (!symbol)
|
|
1922
|
+
return "";
|
|
1923
|
+
if (!ctx)
|
|
1924
|
+
return symbol.getName();
|
|
1925
|
+
return resolveTypeId(symbol, ctx);
|
|
1926
|
+
}
|
|
1856
1927
|
|
|
1857
1928
|
// src/schema/builtins.ts
|
|
1858
1929
|
var BUILTIN_TYPE_SCHEMAS = {
|
|
@@ -1889,11 +1960,11 @@ function escapeRegex(text) {
|
|
|
1889
1960
|
}
|
|
1890
1961
|
function buildTemplatePattern(type) {
|
|
1891
1962
|
const slotPattern = (slot) => {
|
|
1892
|
-
if (slot.flags &
|
|
1963
|
+
if (slot.flags & ts4.TypeFlags.NumberLike)
|
|
1893
1964
|
return "-?\\d+(?:\\.\\d+)?";
|
|
1894
|
-
if (slot.flags &
|
|
1965
|
+
if (slot.flags & ts4.TypeFlags.BigIntLike)
|
|
1895
1966
|
return "-?\\d+";
|
|
1896
|
-
if (slot.flags &
|
|
1967
|
+
if (slot.flags & ts4.TypeFlags.BooleanLike)
|
|
1897
1968
|
return "(?:true|false)";
|
|
1898
1969
|
return ".*";
|
|
1899
1970
|
};
|
|
@@ -1903,6 +1974,11 @@ function buildTemplatePattern(type) {
|
|
|
1903
1974
|
});
|
|
1904
1975
|
return `${pattern}$`;
|
|
1905
1976
|
}
|
|
1977
|
+
function namedRefId(type, name, ctx) {
|
|
1978
|
+
if (!ctx)
|
|
1979
|
+
return name;
|
|
1980
|
+
return typeRefId(type, ctx) || name;
|
|
1981
|
+
}
|
|
1906
1982
|
function builtinSchema(name) {
|
|
1907
1983
|
const schema = { ...BUILTIN_TYPE_SCHEMAS[name] ?? { type: "object" } };
|
|
1908
1984
|
setSchemaExtension(schema, "x-ts-type", name);
|
|
@@ -1915,12 +1991,28 @@ function scrubImportQualifiers(text) {
|
|
|
1915
1991
|
return text.replace(/import\((?:"[^"]*"|'[^']*')\)\./g, "");
|
|
1916
1992
|
}
|
|
1917
1993
|
function renderTypeText(type, checker, enclosing, extraFlags = 0) {
|
|
1918
|
-
return scrubImportQualifiers(checker.typeToString(type, enclosing,
|
|
1994
|
+
return scrubImportQualifiers(checker.typeToString(type, enclosing, ts4.TypeFormatFlags.NoTruncation | extraFlags));
|
|
1995
|
+
}
|
|
1996
|
+
function writtenTypeText(typeNode) {
|
|
1997
|
+
if (!typeNode)
|
|
1998
|
+
return;
|
|
1999
|
+
if (!ts4.isTypeReferenceNode(typeNode) && !ts4.isUnionTypeNode(typeNode))
|
|
2000
|
+
return;
|
|
2001
|
+
try {
|
|
2002
|
+
const text = scrubImportQualifiers(typeNode.getText().replace(/\s+/g, " ").replace(/^\|\s*/, "").trim());
|
|
2003
|
+
return text.length > 0 ? text : undefined;
|
|
2004
|
+
} catch {
|
|
2005
|
+
return;
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
function declaredTypeNode(decl) {
|
|
2009
|
+
const withType = decl;
|
|
2010
|
+
return withType?.type;
|
|
1919
2011
|
}
|
|
1920
2012
|
function stripUndefinedFromType(type, checker) {
|
|
1921
2013
|
if (!type.isUnion())
|
|
1922
2014
|
return type;
|
|
1923
|
-
const nonUndefinedTypes = type.types.filter((t) => !(t.flags &
|
|
2015
|
+
const nonUndefinedTypes = type.types.filter((t) => !(t.flags & ts4.TypeFlags.Undefined));
|
|
1924
2016
|
if (nonUndefinedTypes.length === 0)
|
|
1925
2017
|
return type;
|
|
1926
2018
|
if (nonUndefinedTypes.length === 1)
|
|
@@ -1929,13 +2021,13 @@ function stripUndefinedFromType(type, checker) {
|
|
|
1929
2021
|
}
|
|
1930
2022
|
function isReadonlyPropertySymbol(prop) {
|
|
1931
2023
|
const decls = prop.getDeclarations() ?? [];
|
|
1932
|
-
return decls.some((d) => (
|
|
2024
|
+
return decls.some((d) => (ts4.getCombinedModifierFlags(d) & ts4.ModifierFlags.Readonly) !== 0);
|
|
1933
2025
|
}
|
|
1934
2026
|
function decoratePropertySchema(schema, prop, propType, checker) {
|
|
1935
2027
|
if (typeof schema !== "object" || schema === null || Array.isArray(schema))
|
|
1936
2028
|
return schema;
|
|
1937
2029
|
const decl = prop.valueDeclaration ?? prop.getDeclarations()?.[0];
|
|
1938
|
-
const optional = !!(prop.flags &
|
|
2030
|
+
const optional = !!(prop.flags & ts4.SymbolFlags.Optional);
|
|
1939
2031
|
const textType = optional ? stripUndefinedFromType(propType, checker) : propType;
|
|
1940
2032
|
const text = renderTypeText(textType, checker, decl);
|
|
1941
2033
|
const obj = schema;
|
|
@@ -1944,17 +2036,26 @@ function decoratePropertySchema(schema, prop, propType, checker) {
|
|
|
1944
2036
|
let result = obj;
|
|
1945
2037
|
if (!derivable && !("x-ts-type" in obj)) {
|
|
1946
2038
|
result = { ...result, "x-ts-type": text };
|
|
2039
|
+
const declared = writtenTypeText(declaredTypeNode(decl));
|
|
2040
|
+
if (declared && declared !== text && !PRIMITIVES.has(declared) && !("x-ts-declared" in obj)) {
|
|
2041
|
+
result = { ...result, "x-ts-declared": declared };
|
|
2042
|
+
}
|
|
1947
2043
|
}
|
|
1948
2044
|
if (isReadonlyPropertySymbol(prop) && !("readOnly" in obj)) {
|
|
1949
2045
|
result = { ...result, readOnly: true };
|
|
1950
2046
|
}
|
|
1951
|
-
if (prop.flags &
|
|
2047
|
+
if (prop.flags & ts4.SymbolFlags.Method && !("x-ts-method" in obj)) {
|
|
1952
2048
|
result = { ...result, "x-ts-method": true };
|
|
1953
2049
|
}
|
|
1954
2050
|
return result;
|
|
1955
2051
|
}
|
|
1956
2052
|
function shouldEmitAliasTypeText(typeNode) {
|
|
1957
|
-
|
|
2053
|
+
if (ts4.isMappedTypeNode(typeNode))
|
|
2054
|
+
return false;
|
|
2055
|
+
if (ts4.isTypeLiteralNode(typeNode)) {
|
|
2056
|
+
return typeNode.members.length > 0 && typeNode.members.every((m) => ts4.isIndexSignatureDeclaration(m));
|
|
2057
|
+
}
|
|
2058
|
+
return true;
|
|
1958
2059
|
}
|
|
1959
2060
|
var PRIMITIVES = new Set([
|
|
1960
2061
|
"string",
|
|
@@ -2205,7 +2306,7 @@ function buildSchema(type, checker, ctx) {
|
|
|
2205
2306
|
return ensureNonEmptySchema(schema, type, checker);
|
|
2206
2307
|
}
|
|
2207
2308
|
function buildMaxDepthSchema(type, checker) {
|
|
2208
|
-
if (type.flags &
|
|
2309
|
+
if (type.flags & ts4.TypeFlags.TypeParameter && type.isThisType !== true) {
|
|
2209
2310
|
return { "x-ts-type": checker.typeToString(type) };
|
|
2210
2311
|
}
|
|
2211
2312
|
const symbol = type.getSymbol() || type.aliasSymbol;
|
|
@@ -2218,19 +2319,19 @@ function buildMaxDepthSchema(type, checker) {
|
|
|
2218
2319
|
return { $ref: `#/types/${name}` };
|
|
2219
2320
|
}
|
|
2220
2321
|
}
|
|
2221
|
-
if (type.flags &
|
|
2322
|
+
if (type.flags & ts4.TypeFlags.String)
|
|
2222
2323
|
return { type: "string" };
|
|
2223
|
-
if (type.flags &
|
|
2324
|
+
if (type.flags & ts4.TypeFlags.Number)
|
|
2224
2325
|
return { type: "number" };
|
|
2225
|
-
if (type.flags &
|
|
2326
|
+
if (type.flags & ts4.TypeFlags.Boolean)
|
|
2226
2327
|
return { type: "boolean" };
|
|
2227
|
-
if (type.flags &
|
|
2328
|
+
if (type.flags & ts4.TypeFlags.Undefined)
|
|
2228
2329
|
return { type: "undefined" };
|
|
2229
|
-
if (type.flags &
|
|
2330
|
+
if (type.flags & ts4.TypeFlags.Null)
|
|
2230
2331
|
return { type: "null" };
|
|
2231
|
-
if (type.flags &
|
|
2332
|
+
if (type.flags & ts4.TypeFlags.Void)
|
|
2232
2333
|
return { type: "void" };
|
|
2233
|
-
if (type.flags &
|
|
2334
|
+
if (type.flags & ts4.TypeFlags.TemplateLiteral) {
|
|
2234
2335
|
return {
|
|
2235
2336
|
type: "string",
|
|
2236
2337
|
pattern: buildTemplatePattern(type),
|
|
@@ -2262,59 +2363,59 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2262
2363
|
if (BUILTIN_TYPES.has(name) || isBuiltinGeneric(name)) {
|
|
2263
2364
|
return builtinSchema(name);
|
|
2264
2365
|
}
|
|
2265
|
-
return { $ref: `#/types/${name}` };
|
|
2366
|
+
return { $ref: `#/types/${namedRefId(type, name, ctx)}` };
|
|
2266
2367
|
}
|
|
2267
2368
|
return { type: checker.typeToString(type) };
|
|
2268
2369
|
}
|
|
2269
|
-
const addedToVisited = !!(ctx && type.flags &
|
|
2370
|
+
const addedToVisited = !!(ctx && type.flags & ts4.TypeFlags.Object);
|
|
2270
2371
|
if (addedToVisited) {
|
|
2271
2372
|
ctx.visitedTypes.add(type);
|
|
2272
2373
|
}
|
|
2273
2374
|
try {
|
|
2274
|
-
if (type.flags &
|
|
2375
|
+
if (type.flags & ts4.TypeFlags.String)
|
|
2275
2376
|
return { type: "string" };
|
|
2276
|
-
if (type.flags &
|
|
2377
|
+
if (type.flags & ts4.TypeFlags.Number)
|
|
2277
2378
|
return { type: "number" };
|
|
2278
|
-
if (type.flags &
|
|
2379
|
+
if (type.flags & ts4.TypeFlags.Boolean)
|
|
2279
2380
|
return { type: "boolean" };
|
|
2280
|
-
if (type.flags &
|
|
2381
|
+
if (type.flags & ts4.TypeFlags.Undefined)
|
|
2281
2382
|
return { type: "undefined" };
|
|
2282
|
-
if (type.flags &
|
|
2383
|
+
if (type.flags & ts4.TypeFlags.Null)
|
|
2283
2384
|
return { type: "null" };
|
|
2284
|
-
if (type.flags &
|
|
2385
|
+
if (type.flags & ts4.TypeFlags.Void)
|
|
2285
2386
|
return { type: "void" };
|
|
2286
|
-
if (type.flags &
|
|
2387
|
+
if (type.flags & ts4.TypeFlags.Any)
|
|
2287
2388
|
return { type: "any" };
|
|
2288
|
-
if (type.flags &
|
|
2389
|
+
if (type.flags & ts4.TypeFlags.Unknown)
|
|
2289
2390
|
return { type: "unknown" };
|
|
2290
|
-
if (type.flags &
|
|
2391
|
+
if (type.flags & ts4.TypeFlags.Never)
|
|
2291
2392
|
return { type: "never" };
|
|
2292
|
-
if (type.flags &
|
|
2393
|
+
if (type.flags & ts4.TypeFlags.BigInt)
|
|
2293
2394
|
return { type: "bigint" };
|
|
2294
|
-
if (type.flags &
|
|
2395
|
+
if (type.flags & ts4.TypeFlags.ESSymbol)
|
|
2295
2396
|
return { type: "symbol" };
|
|
2296
2397
|
if (type.isThisType === true) {
|
|
2297
2398
|
const constraint = type.getConstraint?.();
|
|
2298
2399
|
const symbol2 = constraint?.getSymbol() ?? type.getSymbol();
|
|
2299
2400
|
if (symbol2 && !isAnonymous(type)) {
|
|
2300
2401
|
return {
|
|
2301
|
-
$ref: `#/types/${symbol2.getName()}`,
|
|
2402
|
+
$ref: `#/types/${ctx ? resolveTypeId(symbol2, ctx) : symbol2.getName()}`,
|
|
2302
2403
|
"x-ts-type": "this"
|
|
2303
2404
|
};
|
|
2304
2405
|
}
|
|
2305
2406
|
}
|
|
2306
|
-
if (type.flags &
|
|
2407
|
+
if (type.flags & ts4.TypeFlags.TypeParameter) {
|
|
2307
2408
|
return { "x-ts-type": checker.typeToString(type) };
|
|
2308
2409
|
}
|
|
2309
|
-
if (type.flags &
|
|
2410
|
+
if (type.flags & ts4.TypeFlags.StringLiteral) {
|
|
2310
2411
|
const literal = type.value;
|
|
2311
2412
|
return { type: "string", enum: [literal] };
|
|
2312
2413
|
}
|
|
2313
|
-
if (type.flags &
|
|
2414
|
+
if (type.flags & ts4.TypeFlags.NumberLiteral) {
|
|
2314
2415
|
const literal = type.value;
|
|
2315
2416
|
return { type: "number", enum: [literal] };
|
|
2316
2417
|
}
|
|
2317
|
-
if (type.flags &
|
|
2418
|
+
if (type.flags & ts4.TypeFlags.BooleanLiteral) {
|
|
2318
2419
|
const typeString2 = checker.typeToString(type);
|
|
2319
2420
|
return { type: "boolean", enum: [typeString2 === "true"] };
|
|
2320
2421
|
}
|
|
@@ -2322,40 +2423,40 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2322
2423
|
const aliasName = type.aliasSymbol.getName();
|
|
2323
2424
|
if (!aliasName.startsWith("__") && !isPrimitiveName(aliasName)) {
|
|
2324
2425
|
const packageOrigin = getTypeOrigin(type, checker);
|
|
2325
|
-
const schema = { $ref: `#/types/${aliasName}` };
|
|
2426
|
+
const schema = { $ref: `#/types/${namedRefId(type, aliasName, ctx)}` };
|
|
2326
2427
|
if (packageOrigin) {
|
|
2327
2428
|
setSchemaExtension(schema, "x-ts-package", packageOrigin);
|
|
2328
2429
|
}
|
|
2329
2430
|
return schema;
|
|
2330
2431
|
}
|
|
2331
2432
|
}
|
|
2332
|
-
if (type.flags &
|
|
2433
|
+
if (type.flags & ts4.TypeFlags.TemplateLiteral) {
|
|
2333
2434
|
return {
|
|
2334
2435
|
type: "string",
|
|
2335
2436
|
pattern: buildTemplatePattern(type),
|
|
2336
2437
|
"x-ts-type": renderTypeText(type, checker)
|
|
2337
2438
|
};
|
|
2338
2439
|
}
|
|
2339
|
-
if (type.flags &
|
|
2440
|
+
if (type.flags & ts4.TypeFlags.StringMapping) {
|
|
2340
2441
|
return { type: "string", "x-ts-type": renderTypeText(type, checker) };
|
|
2341
2442
|
}
|
|
2342
2443
|
if (type.isUnion()) {
|
|
2343
2444
|
const types = type.types;
|
|
2344
|
-
const allStringLiterals = types.every((t) => t.flags &
|
|
2445
|
+
const allStringLiterals = types.every((t) => t.flags & ts4.TypeFlags.StringLiteral);
|
|
2345
2446
|
if (allStringLiterals) {
|
|
2346
2447
|
const enumValues = types.map((t) => t.value);
|
|
2347
2448
|
return { type: "string", enum: enumValues };
|
|
2348
2449
|
}
|
|
2349
|
-
const allNumberLiterals = types.every((t) => t.flags &
|
|
2450
|
+
const allNumberLiterals = types.every((t) => t.flags & ts4.TypeFlags.NumberLiteral);
|
|
2350
2451
|
if (allNumberLiterals) {
|
|
2351
2452
|
const enumValues = types.map((t) => t.value);
|
|
2352
2453
|
return { type: "number", enum: enumValues };
|
|
2353
2454
|
}
|
|
2354
|
-
const allBooleanLiterals = types.every((t) => t.flags &
|
|
2455
|
+
const allBooleanLiterals = types.every((t) => t.flags & ts4.TypeFlags.BooleanLiteral);
|
|
2355
2456
|
if (allBooleanLiterals) {
|
|
2356
2457
|
return { type: "boolean" };
|
|
2357
2458
|
}
|
|
2358
|
-
const isBoolLiteral = (t) => !!(t.flags &
|
|
2459
|
+
const isBoolLiteral = (t) => !!(t.flags & ts4.TypeFlags.BooleanLiteral);
|
|
2359
2460
|
let members = types;
|
|
2360
2461
|
if (types.filter(isBoolLiteral).length === 2) {
|
|
2361
2462
|
const firstBool = types.findIndex(isBoolLiteral);
|
|
@@ -2369,10 +2470,10 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2369
2470
|
}
|
|
2370
2471
|
return { anyOf: members.map(buildBranch) };
|
|
2371
2472
|
}
|
|
2372
|
-
const isIntersectionType = type.isIntersection() || !!(type.flags &
|
|
2473
|
+
const isIntersectionType = type.isIntersection() || !!(type.flags & ts4.TypeFlags.Intersection);
|
|
2373
2474
|
if (isIntersectionType && "types" in type) {
|
|
2374
2475
|
const intersectionType = type;
|
|
2375
|
-
const filteredTypes = intersectionType.types.filter((t) => !(t.flags &
|
|
2476
|
+
const filteredTypes = intersectionType.types.filter((t) => !(t.flags & ts4.TypeFlags.Never));
|
|
2376
2477
|
if (filteredTypes.length === 0) {
|
|
2377
2478
|
return { type: "never" };
|
|
2378
2479
|
}
|
|
@@ -2461,7 +2562,7 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2461
2562
|
if (ctx) {
|
|
2462
2563
|
return withDepth(ctx, () => {
|
|
2463
2564
|
const schema2 = {
|
|
2464
|
-
$ref: `#/types/${name}`,
|
|
2565
|
+
$ref: `#/types/${namedRefId(typeRef.target, name, ctx)}`,
|
|
2465
2566
|
typeArguments: typeArgs.map((t) => buildSchema(t, checker, ctx))
|
|
2466
2567
|
};
|
|
2467
2568
|
if (packageOrigin) {
|
|
@@ -2487,7 +2588,7 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2487
2588
|
if (BUILTIN_TYPES.has(name)) {
|
|
2488
2589
|
return builtinSchema(name);
|
|
2489
2590
|
}
|
|
2490
|
-
if (RESOLVED_UTILITY_TYPES.has(name) && type.flags &
|
|
2591
|
+
if (RESOLVED_UTILITY_TYPES.has(name) && type.flags & ts4.TypeFlags.Object) {
|
|
2491
2592
|
const props = type.getProperties();
|
|
2492
2593
|
const hasIndex = checker.getIndexInfosOfType(type).length > 0;
|
|
2493
2594
|
if (props.length > 0 || hasIndex) {
|
|
@@ -2506,7 +2607,7 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2506
2607
|
if (ctx) {
|
|
2507
2608
|
return withDepth(ctx, () => {
|
|
2508
2609
|
const schema2 = {
|
|
2509
|
-
$ref: `#/types/${name}`,
|
|
2610
|
+
$ref: `#/types/${namedRefId(type, name, ctx)}`,
|
|
2510
2611
|
typeArguments: aliasTypeArgs.map((t) => buildSchema(t, checker, ctx))
|
|
2511
2612
|
};
|
|
2512
2613
|
if (packageOrigin) {
|
|
@@ -2525,7 +2626,7 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2525
2626
|
return schema;
|
|
2526
2627
|
}
|
|
2527
2628
|
}
|
|
2528
|
-
if (type.flags &
|
|
2629
|
+
if (type.flags & ts4.TypeFlags.Object) {
|
|
2529
2630
|
const callSignatures = type.getCallSignatures();
|
|
2530
2631
|
if (callSignatures.length > 0) {
|
|
2531
2632
|
return buildFunctionSchema(callSignatures, checker, ctx);
|
|
@@ -2544,17 +2645,17 @@ function buildSchemaInternal(type, checker, ctx) {
|
|
|
2544
2645
|
}
|
|
2545
2646
|
if (!name.startsWith("__")) {
|
|
2546
2647
|
const packageOrigin = getTypeOrigin(type, checker);
|
|
2547
|
-
const schema = { $ref: `#/types/${name}` };
|
|
2648
|
+
const schema = { $ref: `#/types/${namedRefId(type, name, ctx)}` };
|
|
2548
2649
|
if (packageOrigin) {
|
|
2549
2650
|
setSchemaExtension(schema, "x-ts-package", packageOrigin);
|
|
2550
2651
|
}
|
|
2551
2652
|
return schema;
|
|
2552
2653
|
}
|
|
2553
2654
|
}
|
|
2554
|
-
if (type.flags &
|
|
2655
|
+
if (type.flags & ts4.TypeFlags.Object) {
|
|
2555
2656
|
const objectType = type;
|
|
2556
2657
|
const properties = type.getProperties();
|
|
2557
|
-
if (properties.length > 0 || objectType.objectFlags &
|
|
2658
|
+
if (properties.length > 0 || objectType.objectFlags & ts4.ObjectFlags.Anonymous || checker.getIndexInfosOfType(type).length > 0) {
|
|
2558
2659
|
return buildObjectSchema(properties, checker, ctx, type);
|
|
2559
2660
|
}
|
|
2560
2661
|
}
|
|
@@ -2598,8 +2699,8 @@ function buildFunctionSchema(callSignatures, checker, ctx) {
|
|
|
2598
2699
|
}
|
|
2599
2700
|
function buildObjectSchema(properties, checker, ctx, originalType) {
|
|
2600
2701
|
const isArrayLikeType = originalType ? checker.isArrayType(originalType) || checker.isTupleType(originalType) || originalType.symbol?.getName() === "Array" && isBuiltinSymbol(originalType.symbol) : false;
|
|
2601
|
-
const isStringLikeType = !!(originalType && originalType.flags &
|
|
2602
|
-
const isNumberLikeType = !!(originalType && originalType.flags &
|
|
2702
|
+
const isStringLikeType = !!(originalType && originalType.flags & ts4.TypeFlags.StringLike);
|
|
2703
|
+
const isNumberLikeType = !!(originalType && originalType.flags & ts4.TypeFlags.NumberLike);
|
|
2603
2704
|
const buildProps = () => {
|
|
2604
2705
|
const props = {};
|
|
2605
2706
|
const required = [];
|
|
@@ -2616,7 +2717,7 @@ function buildObjectSchema(properties, checker, ctx, originalType) {
|
|
|
2616
2717
|
if (isNumberLikeType && NUMBER_PROTOTYPE_METHODS.has(propName)) {
|
|
2617
2718
|
continue;
|
|
2618
2719
|
}
|
|
2619
|
-
const isOptionalProp = !!(prop.flags &
|
|
2720
|
+
const isOptionalProp = !!(prop.flags & ts4.SymbolFlags.Optional);
|
|
2620
2721
|
const rawPropType = checker.getTypeOfSymbol(prop);
|
|
2621
2722
|
const propType = isOptionalProp ? stripUndefinedFromType(rawPropType, checker) : rawPropType;
|
|
2622
2723
|
let propSchema = buildSchema(propType, checker, ctx);
|
|
@@ -2634,7 +2735,7 @@ function buildObjectSchema(properties, checker, ctx, originalType) {
|
|
|
2634
2735
|
}
|
|
2635
2736
|
propSchema = decoratePropertySchema(propSchema, prop, propType, checker);
|
|
2636
2737
|
props[propName] = propSchema;
|
|
2637
|
-
if (!(prop.flags &
|
|
2738
|
+
if (!(prop.flags & ts4.SymbolFlags.Optional)) {
|
|
2638
2739
|
required.push(propName);
|
|
2639
2740
|
}
|
|
2640
2741
|
}
|
|
@@ -2644,11 +2745,11 @@ function buildObjectSchema(properties, checker, ctx, originalType) {
|
|
|
2644
2745
|
...required.length > 0 ? { required } : {}
|
|
2645
2746
|
};
|
|
2646
2747
|
const indexInfos = originalType ? checker.getIndexInfosOfType(originalType) : [];
|
|
2647
|
-
const stringIndex = indexInfos.find((i) => i.keyType.flags &
|
|
2748
|
+
const stringIndex = indexInfos.find((i) => i.keyType.flags & ts4.TypeFlags.String);
|
|
2648
2749
|
if (stringIndex) {
|
|
2649
2750
|
schema.additionalProperties = buildSchema(stringIndex.type, checker, ctx);
|
|
2650
2751
|
}
|
|
2651
|
-
const numberIndex = indexInfos.find((i) => i.keyType.flags &
|
|
2752
|
+
const numberIndex = indexInfos.find((i) => i.keyType.flags & ts4.TypeFlags.Number);
|
|
2652
2753
|
if (numberIndex) {
|
|
2653
2754
|
schema.patternProperties = {
|
|
2654
2755
|
"^\\d+$": buildSchema(numberIndex.type, checker, ctx)
|
|
@@ -2737,7 +2838,7 @@ function deduplicateSchemas(schemas) {
|
|
|
2737
2838
|
function findDiscriminatorProperty(unionTypes, checker) {
|
|
2738
2839
|
const memberProps = [];
|
|
2739
2840
|
for (const t of unionTypes) {
|
|
2740
|
-
if (t.flags & (
|
|
2841
|
+
if (t.flags & (ts4.TypeFlags.Null | ts4.TypeFlags.Undefined)) {
|
|
2741
2842
|
continue;
|
|
2742
2843
|
}
|
|
2743
2844
|
const props = t.getProperties();
|
|
@@ -2791,13 +2892,13 @@ function extractParameters(signature, ctx) {
|
|
|
2791
2892
|
const { typeChecker: checker } = ctx;
|
|
2792
2893
|
const result = [];
|
|
2793
2894
|
const signatureDecl = signature.getDeclaration();
|
|
2794
|
-
const jsdocTags = signatureDecl ?
|
|
2895
|
+
const jsdocTags = signatureDecl ? ts5.getJSDocTags(signatureDecl) : [];
|
|
2795
2896
|
for (const param of signature.getParameters()) {
|
|
2796
2897
|
const decl = param.valueDeclaration;
|
|
2797
2898
|
if (!decl)
|
|
2798
2899
|
continue;
|
|
2799
2900
|
const type = checker.getTypeOfSymbolAtLocation(param, decl);
|
|
2800
|
-
if (decl &&
|
|
2901
|
+
if (decl && ts5.isObjectBindingPattern(decl.name)) {
|
|
2801
2902
|
const expandedParams = expandBindingPattern(decl, type, jsdocTags, ctx);
|
|
2802
2903
|
result.push(...expandedParams);
|
|
2803
2904
|
} else {
|
|
@@ -2829,13 +2930,13 @@ function expandBindingPattern(paramDecl, paramType, jsdocTags, ctx) {
|
|
|
2829
2930
|
const allProperties = getEffectiveProperties(paramType, checker);
|
|
2830
2931
|
const inferredAlias = inferParamAlias(jsdocTags);
|
|
2831
2932
|
for (const element of bindingPattern.elements) {
|
|
2832
|
-
if (!
|
|
2933
|
+
if (!ts5.isBindingElement(element))
|
|
2833
2934
|
continue;
|
|
2834
|
-
const propertyName = element.propertyName ?
|
|
2935
|
+
const propertyName = element.propertyName ? ts5.isIdentifier(element.propertyName) ? element.propertyName.text : element.propertyName.getText() : ts5.isIdentifier(element.name) ? element.name.text : element.name.getText();
|
|
2835
2936
|
const propSymbol = allProperties.get(propertyName);
|
|
2836
2937
|
if (!propSymbol)
|
|
2837
2938
|
continue;
|
|
2838
|
-
const isOptional = !!(propSymbol.flags &
|
|
2939
|
+
const isOptional = !!(propSymbol.flags & ts5.SymbolFlags.Optional) || element.initializer !== undefined;
|
|
2839
2940
|
const propType = checker.getTypeOfSymbol(propSymbol);
|
|
2840
2941
|
const effectiveType = isOptional ? stripUndefinedFromType(propType, checker) : propType;
|
|
2841
2942
|
registerReferencedTypes(effectiveType, ctx);
|
|
@@ -2875,7 +2976,7 @@ function inferParamAlias(jsdocTags) {
|
|
|
2875
2976
|
for (const tag of jsdocTags) {
|
|
2876
2977
|
if (tag.tagName.text !== "param")
|
|
2877
2978
|
continue;
|
|
2878
|
-
const tagText = typeof tag.comment === "string" ? tag.comment :
|
|
2979
|
+
const tagText = typeof tag.comment === "string" ? tag.comment : ts5.getTextOfJSDocComment(tag.comment) ?? "";
|
|
2879
2980
|
const paramTag = tag;
|
|
2880
2981
|
const paramName = paramTag.name?.getText() ?? "";
|
|
2881
2982
|
if (paramName.includes(".")) {
|
|
@@ -2898,22 +2999,22 @@ function inferParamAlias(jsdocTags) {
|
|
|
2898
2999
|
return Array.from(counts.entries()).sort((a, b) => b[1] - a[1])[0]?.[0];
|
|
2899
3000
|
}
|
|
2900
3001
|
function extractLiteralDefault(initializer) {
|
|
2901
|
-
if (
|
|
3002
|
+
if (ts5.isStringLiteral(initializer)) {
|
|
2902
3003
|
return { literal: true, value: initializer.text };
|
|
2903
3004
|
}
|
|
2904
|
-
if (
|
|
3005
|
+
if (ts5.isNumericLiteral(initializer)) {
|
|
2905
3006
|
return { literal: true, value: Number(initializer.text) };
|
|
2906
3007
|
}
|
|
2907
|
-
if (
|
|
3008
|
+
if (ts5.isPrefixUnaryExpression(initializer) && initializer.operator === ts5.SyntaxKind.MinusToken && ts5.isNumericLiteral(initializer.operand)) {
|
|
2908
3009
|
return { literal: true, value: -Number(initializer.operand.text) };
|
|
2909
3010
|
}
|
|
2910
|
-
if (initializer.kind ===
|
|
3011
|
+
if (initializer.kind === ts5.SyntaxKind.TrueKeyword) {
|
|
2911
3012
|
return { literal: true, value: true };
|
|
2912
3013
|
}
|
|
2913
|
-
if (initializer.kind ===
|
|
3014
|
+
if (initializer.kind === ts5.SyntaxKind.FalseKeyword) {
|
|
2914
3015
|
return { literal: true, value: false };
|
|
2915
3016
|
}
|
|
2916
|
-
if (initializer.kind ===
|
|
3017
|
+
if (initializer.kind === ts5.SyntaxKind.NullKeyword) {
|
|
2917
3018
|
return { literal: true, value: null };
|
|
2918
3019
|
}
|
|
2919
3020
|
return { literal: false, text: initializer.getText() };
|
|
@@ -2934,7 +3035,7 @@ function registerReferencedTypes(type, ctx, depth = 0) {
|
|
|
2934
3035
|
return;
|
|
2935
3036
|
if (ctx.registeredTypes.has(type))
|
|
2936
3037
|
return;
|
|
2937
|
-
const isPrimitive = type.flags & (
|
|
3038
|
+
const isPrimitive = type.flags & (ts5.TypeFlags.String | ts5.TypeFlags.Number | ts5.TypeFlags.Boolean | ts5.TypeFlags.Void | ts5.TypeFlags.Undefined | ts5.TypeFlags.Null | ts5.TypeFlags.Any | ts5.TypeFlags.Unknown | ts5.TypeFlags.Never | ts5.TypeFlags.StringLiteral | ts5.TypeFlags.NumberLiteral | ts5.TypeFlags.BooleanLiteral);
|
|
2938
3039
|
if (!isPrimitive) {
|
|
2939
3040
|
ctx.registeredTypes.add(type);
|
|
2940
3041
|
}
|
|
@@ -2960,7 +3061,7 @@ function registerReferencedTypes(type, ctx, depth = 0) {
|
|
|
2960
3061
|
if (typeSymbol && ctx.shouldExpandExternal && !typeSymbol.getName().startsWith("__") && !ctx.shouldExpandExternal(typeSymbol)) {
|
|
2961
3062
|
return;
|
|
2962
3063
|
}
|
|
2963
|
-
if (type.flags &
|
|
3064
|
+
if (type.flags & ts5.TypeFlags.Object) {
|
|
2964
3065
|
const props = type.getProperties();
|
|
2965
3066
|
const limit = ctx.maxProperties;
|
|
2966
3067
|
if (props.length > limit && ctx.onTruncation) {
|
|
@@ -2975,10 +3076,10 @@ function registerReferencedTypes(type, ctx, depth = 0) {
|
|
|
2975
3076
|
}
|
|
2976
3077
|
|
|
2977
3078
|
// src/serializers/context.ts
|
|
2978
|
-
import
|
|
3079
|
+
import ts7 from "typescript";
|
|
2979
3080
|
|
|
2980
3081
|
// src/ast/registry.ts
|
|
2981
|
-
import
|
|
3082
|
+
import ts6 from "typescript";
|
|
2982
3083
|
var BUILTINS = new Set([
|
|
2983
3084
|
"Array",
|
|
2984
3085
|
"ArrayBuffer",
|
|
@@ -3082,18 +3183,19 @@ class TypeRegistry {
|
|
|
3082
3183
|
return;
|
|
3083
3184
|
if (name.startsWith("__"))
|
|
3084
3185
|
return;
|
|
3085
|
-
if (symbol.flags &
|
|
3186
|
+
if (symbol.flags & ts6.SymbolFlags.EnumMember)
|
|
3086
3187
|
return;
|
|
3087
|
-
if (symbol.flags &
|
|
3188
|
+
if (symbol.flags & ts6.SymbolFlags.TypeParameter)
|
|
3088
3189
|
return;
|
|
3089
|
-
if (symbol.flags &
|
|
3190
|
+
if (symbol.flags & ts6.SymbolFlags.Method)
|
|
3090
3191
|
return;
|
|
3091
|
-
if (symbol.flags &
|
|
3192
|
+
if (symbol.flags & ts6.SymbolFlags.Function)
|
|
3092
3193
|
return;
|
|
3093
3194
|
if (isGenericTypeParameter(name))
|
|
3094
3195
|
return;
|
|
3095
|
-
|
|
3096
|
-
|
|
3196
|
+
const id = resolveTypeId(symbol, ctx);
|
|
3197
|
+
if (this.has(id))
|
|
3198
|
+
return id;
|
|
3097
3199
|
if (ctx.shouldExpandExternal && !ctx.shouldExpandExternal(symbol)) {
|
|
3098
3200
|
const origin = getTypeOrigin(type, ctx.typeChecker);
|
|
3099
3201
|
const schema = {
|
|
@@ -3102,47 +3204,47 @@ class TypeRegistry {
|
|
|
3102
3204
|
if (origin)
|
|
3103
3205
|
schema["x-ts-package"] = origin;
|
|
3104
3206
|
this.add({
|
|
3105
|
-
id
|
|
3207
|
+
id,
|
|
3106
3208
|
name,
|
|
3107
3209
|
kind: "external",
|
|
3108
3210
|
external: true,
|
|
3109
3211
|
schema
|
|
3110
3212
|
});
|
|
3111
|
-
return
|
|
3213
|
+
return id;
|
|
3112
3214
|
}
|
|
3113
|
-
if (this.processing.has(
|
|
3114
|
-
return
|
|
3115
|
-
this.processing.add(
|
|
3215
|
+
if (this.processing.has(id))
|
|
3216
|
+
return id;
|
|
3217
|
+
this.processing.add(id);
|
|
3116
3218
|
try {
|
|
3117
|
-
const specType = this.buildSpecType(type, symbol, ctx);
|
|
3219
|
+
const specType = this.buildSpecType(type, symbol, id, ctx);
|
|
3118
3220
|
if (specType) {
|
|
3119
3221
|
this.add(specType);
|
|
3120
3222
|
return specType.id;
|
|
3121
3223
|
}
|
|
3122
3224
|
} finally {
|
|
3123
|
-
this.processing.delete(
|
|
3225
|
+
this.processing.delete(id);
|
|
3124
3226
|
}
|
|
3125
3227
|
return;
|
|
3126
3228
|
}
|
|
3127
|
-
buildSpecType(type, symbol, ctx) {
|
|
3229
|
+
buildSpecType(type, symbol, id, ctx) {
|
|
3128
3230
|
const name = symbol.getName();
|
|
3129
3231
|
const decl = symbol.declarations?.[0];
|
|
3130
3232
|
const checker = ctx.typeChecker;
|
|
3131
3233
|
let kind = "type";
|
|
3132
3234
|
const external = decl ? isExternalType(decl) : false;
|
|
3133
3235
|
if (decl) {
|
|
3134
|
-
if (
|
|
3236
|
+
if (ts6.isClassDeclaration(decl))
|
|
3135
3237
|
kind = "class";
|
|
3136
|
-
else if (
|
|
3238
|
+
else if (ts6.isInterfaceDeclaration(decl))
|
|
3137
3239
|
kind = "interface";
|
|
3138
|
-
else if (
|
|
3240
|
+
else if (ts6.isEnumDeclaration(decl))
|
|
3139
3241
|
kind = "enum";
|
|
3140
3242
|
}
|
|
3141
3243
|
if (external) {
|
|
3142
3244
|
kind = "external";
|
|
3143
3245
|
}
|
|
3144
3246
|
let schema = buildSchema(type, checker, ctx);
|
|
3145
|
-
if (this.isSelfRef(schema,
|
|
3247
|
+
if (this.isSelfRef(schema, id)) {
|
|
3146
3248
|
schema = this.resolveSelRefSchema(type, checker, ctx);
|
|
3147
3249
|
}
|
|
3148
3250
|
if (kind === "enum") {
|
|
@@ -3151,18 +3253,22 @@ class TypeRegistry {
|
|
|
3151
3253
|
schema = enumSchema;
|
|
3152
3254
|
}
|
|
3153
3255
|
}
|
|
3154
|
-
if (kind === "type" && decl &&
|
|
3155
|
-
const text = renderTypeText(type, checker, decl,
|
|
3256
|
+
if (kind === "type" && decl && ts6.isTypeAliasDeclaration(decl) && shouldEmitAliasTypeText(decl.type) && typeof schema === "object" && schema !== null && !("x-ts-type" in schema)) {
|
|
3257
|
+
const text = renderTypeText(type, checker, decl, ts6.TypeFormatFlags.InTypeAlias);
|
|
3156
3258
|
if (!PRIMITIVES.has(text) && text !== name) {
|
|
3157
3259
|
schema["x-ts-type"] = text;
|
|
3260
|
+
const declared = writtenTypeText(declaredTypeNode(decl));
|
|
3261
|
+
if (declared && declared !== text && !PRIMITIVES.has(declared)) {
|
|
3262
|
+
schema["x-ts-declared"] = declared;
|
|
3263
|
+
}
|
|
3158
3264
|
}
|
|
3159
3265
|
}
|
|
3160
3266
|
let typeParameters;
|
|
3161
|
-
if (decl && (
|
|
3267
|
+
if (decl && (ts6.isTypeAliasDeclaration(decl) || ts6.isInterfaceDeclaration(decl) || ts6.isClassDeclaration(decl))) {
|
|
3162
3268
|
typeParameters = extractTypeParameters(decl, checker);
|
|
3163
3269
|
}
|
|
3164
3270
|
return {
|
|
3165
|
-
id
|
|
3271
|
+
id,
|
|
3166
3272
|
name,
|
|
3167
3273
|
kind,
|
|
3168
3274
|
...typeParameters && typeParameters.length > 0 ? { typeParameters } : {},
|
|
@@ -3179,14 +3285,14 @@ class TypeRegistry {
|
|
|
3179
3285
|
resolveSelRefSchema(type, checker, ctx) {
|
|
3180
3286
|
if (type.isUnion()) {
|
|
3181
3287
|
const types = type.types;
|
|
3182
|
-
const allStringLiterals = types.every((t) => t.flags &
|
|
3288
|
+
const allStringLiterals = types.every((t) => t.flags & ts6.TypeFlags.StringLiteral);
|
|
3183
3289
|
if (allStringLiterals) {
|
|
3184
3290
|
return {
|
|
3185
3291
|
type: "string",
|
|
3186
3292
|
enum: types.map((t) => t.value)
|
|
3187
3293
|
};
|
|
3188
3294
|
}
|
|
3189
|
-
const allNumberLiterals = types.every((t) => t.flags &
|
|
3295
|
+
const allNumberLiterals = types.every((t) => t.flags & ts6.TypeFlags.NumberLiteral);
|
|
3190
3296
|
if (allNumberLiterals) {
|
|
3191
3297
|
return {
|
|
3192
3298
|
type: "number",
|
|
@@ -3221,18 +3327,18 @@ class TypeRegistry {
|
|
|
3221
3327
|
const elementType = checker.getTypeArguments(type)?.[0];
|
|
3222
3328
|
return elementType ? { type: "array", items: buildSchema(elementType, checker, ctx) } : { type: "array" };
|
|
3223
3329
|
}
|
|
3224
|
-
if (type.flags &
|
|
3330
|
+
if (type.flags & ts6.TypeFlags.Conditional) {
|
|
3225
3331
|
return { "x-ts-type": renderTypeText(type, checker) };
|
|
3226
3332
|
}
|
|
3227
3333
|
const constraint = checker.getBaseConstraintOfType(type);
|
|
3228
|
-
const primitive = constraint && constraint.flags &
|
|
3334
|
+
const primitive = constraint && constraint.flags & ts6.TypeFlags.StringLike ? "string" : constraint && constraint.flags & ts6.TypeFlags.NumberLike ? "number" : constraint && constraint.flags & ts6.TypeFlags.BooleanLike ? "boolean" : undefined;
|
|
3229
3335
|
if (primitive) {
|
|
3230
3336
|
return { type: primitive, "x-ts-type": renderTypeText(type, checker) };
|
|
3231
3337
|
}
|
|
3232
3338
|
return this.buildObjectSchemaFromProperties(type, checker, ctx);
|
|
3233
3339
|
}
|
|
3234
3340
|
buildEnumSchema(symbol, checker) {
|
|
3235
|
-
const decl = symbol.declarations?.find(
|
|
3341
|
+
const decl = symbol.declarations?.find(ts6.isEnumDeclaration);
|
|
3236
3342
|
if (!decl)
|
|
3237
3343
|
return;
|
|
3238
3344
|
const members = [];
|
|
@@ -3259,8 +3365,8 @@ class TypeRegistry {
|
|
|
3259
3365
|
buildObjectSchemaFromProperties(type, checker, ctx) {
|
|
3260
3366
|
const properties = type.getProperties();
|
|
3261
3367
|
const indexInfos = checker.getIndexInfosOfType(type);
|
|
3262
|
-
const stringIndex = indexInfos.find((i) => i.keyType.flags &
|
|
3263
|
-
const numberIndex = indexInfos.find((i) => i.keyType.flags &
|
|
3368
|
+
const stringIndex = indexInfos.find((i) => i.keyType.flags & ts6.TypeFlags.String);
|
|
3369
|
+
const numberIndex = indexInfos.find((i) => i.keyType.flags & ts6.TypeFlags.Number);
|
|
3264
3370
|
if (properties.length === 0 && !stringIndex && !numberIndex) {
|
|
3265
3371
|
return { type: checker.typeToString(type) };
|
|
3266
3372
|
}
|
|
@@ -3268,8 +3374,8 @@ class TypeRegistry {
|
|
|
3268
3374
|
const required = [];
|
|
3269
3375
|
const limit = ctx.maxProperties;
|
|
3270
3376
|
const isArrayLike = checker.isArrayType(type) || checker.isTupleType(type) || type.symbol?.getName() === "Array" && type.symbol?.getDeclarations()?.[0]?.getSourceFile()?.fileName?.includes("/typescript/lib/lib.");
|
|
3271
|
-
const isStringLike = type.flags &
|
|
3272
|
-
const isNumberLike = type.flags &
|
|
3377
|
+
const isStringLike = type.flags & ts6.TypeFlags.StringLike;
|
|
3378
|
+
const isNumberLike = type.flags & ts6.TypeFlags.NumberLike;
|
|
3273
3379
|
const included = properties.filter((prop) => {
|
|
3274
3380
|
const propName = prop.getName();
|
|
3275
3381
|
if (propName.startsWith("__@"))
|
|
@@ -3289,7 +3395,7 @@ class TypeRegistry {
|
|
|
3289
3395
|
for (const prop of included.slice(0, limit)) {
|
|
3290
3396
|
const propName = prop.getName();
|
|
3291
3397
|
const rawPropType = checker.getTypeOfSymbol(prop);
|
|
3292
|
-
const propType = prop.flags &
|
|
3398
|
+
const propType = prop.flags & ts6.SymbolFlags.Optional ? stripUndefinedFromType(rawPropType, checker) : rawPropType;
|
|
3293
3399
|
this.registerType(propType, ctx);
|
|
3294
3400
|
let propSchema = buildSchema(propType, checker, ctx);
|
|
3295
3401
|
const docComment = prop.getDocumentationComment(checker);
|
|
@@ -3306,7 +3412,7 @@ class TypeRegistry {
|
|
|
3306
3412
|
}
|
|
3307
3413
|
propSchema = decoratePropertySchema(propSchema, prop, propType, checker);
|
|
3308
3414
|
props[propName] = propSchema;
|
|
3309
|
-
if (!(prop.flags &
|
|
3415
|
+
if (!(prop.flags & ts6.SymbolFlags.Optional)) {
|
|
3310
3416
|
required.push(propName);
|
|
3311
3417
|
}
|
|
3312
3418
|
}
|
|
@@ -3340,7 +3446,11 @@ function createContext(program, sourceFile, options = {}) {
|
|
|
3340
3446
|
includePrivate: options.includePrivate ?? false,
|
|
3341
3447
|
maxProperties: options.maxProperties ?? 500,
|
|
3342
3448
|
onTruncation: options.onTruncation,
|
|
3343
|
-
shouldExpandExternal: options.shouldExpandExternal
|
|
3449
|
+
shouldExpandExternal: options.shouldExpandExternal,
|
|
3450
|
+
typeIds: new Map,
|
|
3451
|
+
declIds: new Map,
|
|
3452
|
+
idOwner: new Map,
|
|
3453
|
+
workspacePackages: options.workspacePackages ?? new Map
|
|
3344
3454
|
};
|
|
3345
3455
|
}
|
|
3346
3456
|
function getInheritedMembers(classType, ownMemberNames, ctx, isStatic = false) {
|
|
@@ -3409,15 +3519,15 @@ function serializeInheritedMember(symbol, inheritedFrom, ctx, isStatic) {
|
|
|
3409
3519
|
const type = checker.getTypeOfSymbol(symbol);
|
|
3410
3520
|
registerReferencedTypes(type, ctx);
|
|
3411
3521
|
let visibility;
|
|
3412
|
-
if (decl &&
|
|
3413
|
-
const modifiers =
|
|
3522
|
+
if (decl && ts7.canHaveModifiers(decl)) {
|
|
3523
|
+
const modifiers = ts7.getModifiers(decl);
|
|
3414
3524
|
if (modifiers) {
|
|
3415
3525
|
for (const mod of modifiers) {
|
|
3416
|
-
if (mod.kind ===
|
|
3526
|
+
if (mod.kind === ts7.SyntaxKind.PrivateKeyword)
|
|
3417
3527
|
visibility = "private";
|
|
3418
|
-
else if (mod.kind ===
|
|
3528
|
+
else if (mod.kind === ts7.SyntaxKind.ProtectedKeyword)
|
|
3419
3529
|
visibility = "protected";
|
|
3420
|
-
else if (mod.kind ===
|
|
3530
|
+
else if (mod.kind === ts7.SyntaxKind.PublicKeyword)
|
|
3421
3531
|
visibility = "public";
|
|
3422
3532
|
}
|
|
3423
3533
|
}
|
|
@@ -3429,17 +3539,17 @@ function serializeInheritedMember(symbol, inheritedFrom, ctx, isStatic) {
|
|
|
3429
3539
|
const callSigs = type.getCallSignatures();
|
|
3430
3540
|
if (callSigs.length > 0) {
|
|
3431
3541
|
kind = "method";
|
|
3432
|
-
} else if (
|
|
3542
|
+
} else if (ts7.isGetAccessorDeclaration(decl)) {
|
|
3433
3543
|
kind = "getter";
|
|
3434
|
-
} else if (
|
|
3544
|
+
} else if (ts7.isSetAccessorDeclaration(decl)) {
|
|
3435
3545
|
kind = "setter";
|
|
3436
3546
|
}
|
|
3437
3547
|
const flags = {};
|
|
3438
3548
|
if (isStatic)
|
|
3439
3549
|
flags.static = true;
|
|
3440
|
-
if (decl &&
|
|
3441
|
-
const modifiers =
|
|
3442
|
-
if (modifiers?.some((m) => m.kind ===
|
|
3550
|
+
if (decl && ts7.canHaveModifiers(decl)) {
|
|
3551
|
+
const modifiers = ts7.getModifiers(decl);
|
|
3552
|
+
if (modifiers?.some((m) => m.kind === ts7.SyntaxKind.ReadonlyKeyword)) {
|
|
3443
3553
|
flags.readonly = true;
|
|
3444
3554
|
}
|
|
3445
3555
|
}
|
|
@@ -3516,11 +3626,11 @@ function serializeClass(node, ctx) {
|
|
|
3516
3626
|
const memberName = getMemberName(member);
|
|
3517
3627
|
if (memberName?.startsWith("#"))
|
|
3518
3628
|
continue;
|
|
3519
|
-
if (
|
|
3629
|
+
if (ts8.isPropertyDeclaration(member)) {
|
|
3520
3630
|
const propMember = serializeProperty(member, ctx);
|
|
3521
3631
|
if (propMember)
|
|
3522
3632
|
members.push(propMember);
|
|
3523
|
-
} else if (
|
|
3633
|
+
} else if (ts8.isMethodDeclaration(member)) {
|
|
3524
3634
|
const methodMember = serializeMethod(member, ctx);
|
|
3525
3635
|
if (methodMember?.name) {
|
|
3526
3636
|
if (!methodsByName.has(methodMember.name)) {
|
|
@@ -3537,11 +3647,11 @@ function serializeClass(node, ctx) {
|
|
|
3537
3647
|
}
|
|
3538
3648
|
}
|
|
3539
3649
|
}
|
|
3540
|
-
} else if (
|
|
3650
|
+
} else if (ts8.isConstructorDeclaration(member)) {
|
|
3541
3651
|
const ctorSig = serializeConstructor(member, ctx);
|
|
3542
3652
|
if (ctorSig)
|
|
3543
3653
|
signatures.push(ctorSig);
|
|
3544
|
-
} else if (
|
|
3654
|
+
} else if (ts8.isGetAccessorDeclaration(member) || ts8.isSetAccessorDeclaration(member)) {
|
|
3545
3655
|
const accessorMember = serializeAccessor(member, ctx);
|
|
3546
3656
|
if (accessorMember)
|
|
3547
3657
|
members.push(accessorMember);
|
|
@@ -3565,8 +3675,8 @@ function serializeClass(node, ctx) {
|
|
|
3565
3675
|
const extendsClause = getExtendsClause(node, checker);
|
|
3566
3676
|
const implementsClause = getImplementsClause(node, checker);
|
|
3567
3677
|
const classFlags = {};
|
|
3568
|
-
const classModifiers =
|
|
3569
|
-
if (classModifiers?.some((m) => m.kind ===
|
|
3678
|
+
const classModifiers = ts8.getModifiers(node);
|
|
3679
|
+
if (classModifiers?.some((m) => m.kind === ts8.SyntaxKind.AbstractKeyword)) {
|
|
3570
3680
|
classFlags.abstract = true;
|
|
3571
3681
|
}
|
|
3572
3682
|
return {
|
|
@@ -3587,37 +3697,37 @@ function serializeClass(node, ctx) {
|
|
|
3587
3697
|
};
|
|
3588
3698
|
}
|
|
3589
3699
|
function getMemberName(member) {
|
|
3590
|
-
if (
|
|
3700
|
+
if (ts8.isConstructorDeclaration(member))
|
|
3591
3701
|
return "constructor";
|
|
3592
3702
|
if (!member.name)
|
|
3593
3703
|
return;
|
|
3594
|
-
if (
|
|
3704
|
+
if (ts8.isIdentifier(member.name))
|
|
3595
3705
|
return member.name.text;
|
|
3596
|
-
if (
|
|
3706
|
+
if (ts8.isPrivateIdentifier(member.name))
|
|
3597
3707
|
return member.name.text;
|
|
3598
3708
|
return member.name.getText();
|
|
3599
3709
|
}
|
|
3600
3710
|
function getVisibility(member) {
|
|
3601
|
-
const modifiers =
|
|
3711
|
+
const modifiers = ts8.canHaveModifiers(member) ? ts8.getModifiers(member) : undefined;
|
|
3602
3712
|
if (!modifiers)
|
|
3603
3713
|
return;
|
|
3604
3714
|
for (const mod of modifiers) {
|
|
3605
|
-
if (mod.kind ===
|
|
3715
|
+
if (mod.kind === ts8.SyntaxKind.PrivateKeyword)
|
|
3606
3716
|
return "private";
|
|
3607
|
-
if (mod.kind ===
|
|
3717
|
+
if (mod.kind === ts8.SyntaxKind.ProtectedKeyword)
|
|
3608
3718
|
return "protected";
|
|
3609
|
-
if (mod.kind ===
|
|
3719
|
+
if (mod.kind === ts8.SyntaxKind.PublicKeyword)
|
|
3610
3720
|
return "public";
|
|
3611
3721
|
}
|
|
3612
3722
|
return;
|
|
3613
3723
|
}
|
|
3614
3724
|
function isStatic(member) {
|
|
3615
|
-
const modifiers =
|
|
3616
|
-
return modifiers?.some((m) => m.kind ===
|
|
3725
|
+
const modifiers = ts8.canHaveModifiers(member) ? ts8.getModifiers(member) : undefined;
|
|
3726
|
+
return modifiers?.some((m) => m.kind === ts8.SyntaxKind.StaticKeyword) ?? false;
|
|
3617
3727
|
}
|
|
3618
3728
|
function isReadonly(member) {
|
|
3619
|
-
const modifiers =
|
|
3620
|
-
return modifiers?.some((m) => m.kind ===
|
|
3729
|
+
const modifiers = ts8.canHaveModifiers(member) ? ts8.getModifiers(member) : undefined;
|
|
3730
|
+
return modifiers?.some((m) => m.kind === ts8.SyntaxKind.ReadonlyKeyword) ?? false;
|
|
3621
3731
|
}
|
|
3622
3732
|
function serializeProperty(node, ctx) {
|
|
3623
3733
|
const { typeChecker: checker } = ctx;
|
|
@@ -3675,11 +3785,11 @@ function serializeMethod(node, ctx) {
|
|
|
3675
3785
|
if (node.asteriskToken)
|
|
3676
3786
|
flags.generator = true;
|
|
3677
3787
|
flags.methodSyntax = true;
|
|
3678
|
-
const modifiers =
|
|
3679
|
-
if (modifiers?.some((m) => m.kind ===
|
|
3788
|
+
const modifiers = ts8.getModifiers(node);
|
|
3789
|
+
if (modifiers?.some((m) => m.kind === ts8.SyntaxKind.AsyncKeyword)) {
|
|
3680
3790
|
flags.async = true;
|
|
3681
3791
|
}
|
|
3682
|
-
if (modifiers?.some((m) => m.kind ===
|
|
3792
|
+
if (modifiers?.some((m) => m.kind === ts8.SyntaxKind.AbstractKeyword)) {
|
|
3683
3793
|
flags.abstract = true;
|
|
3684
3794
|
}
|
|
3685
3795
|
const symbol = checker.getSymbolAtLocation(node.name ?? node);
|
|
@@ -3716,7 +3826,7 @@ function serializeConstructorSignature(node, sig, ctx) {
|
|
|
3716
3826
|
}
|
|
3717
3827
|
function serializeInheritedConstructors(node, ctx) {
|
|
3718
3828
|
const { typeChecker: checker } = ctx;
|
|
3719
|
-
const hasExtends = node.heritageClauses?.some((c) => c.token ===
|
|
3829
|
+
const hasExtends = node.heritageClauses?.some((c) => c.token === ts8.SyntaxKind.ExtendsKeyword);
|
|
3720
3830
|
if (!hasExtends)
|
|
3721
3831
|
return [];
|
|
3722
3832
|
const symbol = checker.getSymbolAtLocation(node.name ?? node);
|
|
@@ -3725,11 +3835,11 @@ function serializeInheritedConstructors(node, ctx) {
|
|
|
3725
3835
|
const staticType = checker.getTypeOfSymbolAtLocation(symbol, node);
|
|
3726
3836
|
const ctorSigs = staticType.getConstructSignatures().filter((sig) => {
|
|
3727
3837
|
const decl = sig.getDeclaration();
|
|
3728
|
-
return decl !== undefined &&
|
|
3838
|
+
return decl !== undefined && ts8.isConstructorDeclaration(decl);
|
|
3729
3839
|
});
|
|
3730
3840
|
return ctorSigs.map((sig, index) => {
|
|
3731
3841
|
const decl = sig.getDeclaration();
|
|
3732
|
-
const owner =
|
|
3842
|
+
const owner = ts8.isClassLike(decl.parent) ? decl.parent.name?.text : undefined;
|
|
3733
3843
|
return {
|
|
3734
3844
|
...serializeConstructorSignature(decl, sig, ctx),
|
|
3735
3845
|
...owner ? { inheritedFrom: owner } : {},
|
|
@@ -3750,12 +3860,12 @@ function serializeAccessor(node, ctx) {
|
|
|
3750
3860
|
const type = checker.getTypeAtLocation(node);
|
|
3751
3861
|
const schema = buildSchema(type, checker, ctx);
|
|
3752
3862
|
registerReferencedTypes(type, ctx);
|
|
3753
|
-
const kind =
|
|
3863
|
+
const kind = ts8.isGetAccessorDeclaration(node) ? "getter" : "setter";
|
|
3754
3864
|
const flags = {};
|
|
3755
3865
|
if (isStatic(node))
|
|
3756
3866
|
flags.static = true;
|
|
3757
3867
|
let signatures;
|
|
3758
|
-
if (
|
|
3868
|
+
if (ts8.isSetAccessorDeclaration(node) && node.parameters.length > 0) {
|
|
3759
3869
|
const param = node.parameters[0];
|
|
3760
3870
|
const paramName = param.name.getText();
|
|
3761
3871
|
const paramType = checker.getTypeAtLocation(param);
|
|
@@ -3787,7 +3897,7 @@ function getExtendsClause(node, checker) {
|
|
|
3787
3897
|
if (!node.heritageClauses)
|
|
3788
3898
|
return;
|
|
3789
3899
|
for (const clause of node.heritageClauses) {
|
|
3790
|
-
if (clause.token ===
|
|
3900
|
+
if (clause.token === ts8.SyntaxKind.ExtendsKeyword) {
|
|
3791
3901
|
const expr = clause.types[0];
|
|
3792
3902
|
if (expr) {
|
|
3793
3903
|
const type = checker.getTypeAtLocation(expr);
|
|
@@ -3802,7 +3912,7 @@ function getImplementsClause(node, checker) {
|
|
|
3802
3912
|
if (!node.heritageClauses)
|
|
3803
3913
|
return;
|
|
3804
3914
|
for (const clause of node.heritageClauses) {
|
|
3805
|
-
if (clause.token ===
|
|
3915
|
+
if (clause.token === ts8.SyntaxKind.ImplementsKeyword) {
|
|
3806
3916
|
return clause.types.map((expr) => {
|
|
3807
3917
|
const type = checker.getTypeAtLocation(expr);
|
|
3808
3918
|
const symbol = type.getSymbol();
|
|
@@ -3833,13 +3943,16 @@ function serializeEnum(node, ctx) {
|
|
|
3833
3943
|
} else if (member.initializer) {
|
|
3834
3944
|
schema = { type: member.initializer.getText() };
|
|
3835
3945
|
}
|
|
3836
|
-
const { description: memberDesc } = getJSDocComment(member);
|
|
3946
|
+
const { description: memberDesc, tags: memberTags } = getJSDocComment(member);
|
|
3947
|
+
const { deprecated: deprecated2, reason: deprecationReason2 } = isSymbolDeprecated(memberSymbol);
|
|
3837
3948
|
return {
|
|
3838
3949
|
id: memberName,
|
|
3839
3950
|
name: memberName,
|
|
3840
3951
|
kind: "enum-member",
|
|
3841
3952
|
...schema ? { schema } : {},
|
|
3842
|
-
...memberDesc ? { description: memberDesc } : {}
|
|
3953
|
+
...memberDesc ? { description: memberDesc } : {},
|
|
3954
|
+
...memberTags.length > 0 ? { tags: memberTags } : {},
|
|
3955
|
+
...deprecated2 ? { deprecated: true, deprecationReason: deprecationReason2 } : {}
|
|
3843
3956
|
};
|
|
3844
3957
|
});
|
|
3845
3958
|
return {
|
|
@@ -3856,16 +3969,16 @@ function serializeEnum(node, ctx) {
|
|
|
3856
3969
|
}
|
|
3857
3970
|
|
|
3858
3971
|
// src/serializers/functions.ts
|
|
3859
|
-
import
|
|
3972
|
+
import ts9 from "typescript";
|
|
3860
3973
|
function buildReturnSchema(sig, ctx) {
|
|
3861
3974
|
const returnType = ctx.typeChecker.getReturnTypeOfSignature(sig);
|
|
3862
3975
|
registerReferencedTypes(returnType, ctx);
|
|
3863
3976
|
const schema = buildSchema(returnType, ctx.typeChecker, ctx);
|
|
3864
3977
|
const declaration = sig.getDeclaration();
|
|
3865
|
-
if (declaration &&
|
|
3978
|
+
if (declaration && ts9.isFunctionLike(declaration) && declaration.type) {
|
|
3866
3979
|
const returnTypeNode = declaration.type;
|
|
3867
|
-
if (
|
|
3868
|
-
const parameterName =
|
|
3980
|
+
if (ts9.isTypePredicateNode(returnTypeNode)) {
|
|
3981
|
+
const parameterName = ts9.isIdentifier(returnTypeNode.parameterName) ? returnTypeNode.parameterName.text : returnTypeNode.parameterName.getText();
|
|
3869
3982
|
let predicateTypeSchema = { type: "unknown" };
|
|
3870
3983
|
if (returnTypeNode.type) {
|
|
3871
3984
|
const predicateType = ctx.typeChecker.getTypeAtLocation(returnTypeNode.type);
|
|
@@ -3909,8 +4022,8 @@ function serializeFunctionExport(node, ctx, nameOverride) {
|
|
|
3909
4022
|
};
|
|
3910
4023
|
});
|
|
3911
4024
|
const flags = {};
|
|
3912
|
-
const modifiers =
|
|
3913
|
-
if (modifiers?.some((m) => m.kind ===
|
|
4025
|
+
const modifiers = ts9.getModifiers(node);
|
|
4026
|
+
if (modifiers?.some((m) => m.kind === ts9.SyntaxKind.AsyncKeyword)) {
|
|
3914
4027
|
flags.async = true;
|
|
3915
4028
|
}
|
|
3916
4029
|
if (node.asteriskToken) {
|
|
@@ -3932,7 +4045,7 @@ function serializeFunctionExport(node, ctx, nameOverride) {
|
|
|
3932
4045
|
}
|
|
3933
4046
|
|
|
3934
4047
|
// src/serializers/interfaces.ts
|
|
3935
|
-
import
|
|
4048
|
+
import ts10 from "typescript";
|
|
3936
4049
|
function serializeInterface(node, ctx) {
|
|
3937
4050
|
const { typeChecker: checker } = ctx;
|
|
3938
4051
|
const symbol = checker.getSymbolAtLocation(node.name ?? node);
|
|
@@ -3945,11 +4058,11 @@ function serializeInterface(node, ctx) {
|
|
|
3945
4058
|
const methodsByName = new Map;
|
|
3946
4059
|
let callSignatureMember = null;
|
|
3947
4060
|
for (const member of node.members) {
|
|
3948
|
-
if (
|
|
4061
|
+
if (ts10.isPropertySignature(member)) {
|
|
3949
4062
|
const propMember = serializePropertySignature(member, ctx);
|
|
3950
4063
|
if (propMember)
|
|
3951
4064
|
members.push(propMember);
|
|
3952
|
-
} else if (
|
|
4065
|
+
} else if (ts10.isMethodSignature(member)) {
|
|
3953
4066
|
const methodMember = serializeMethodSignature(member, ctx);
|
|
3954
4067
|
if (methodMember?.name && methodMember.signatures) {
|
|
3955
4068
|
const existing = methodsByName.get(methodMember.name);
|
|
@@ -3970,7 +4083,7 @@ function serializeInterface(node, ctx) {
|
|
|
3970
4083
|
methodsByName.set(methodMember.name, methodMember);
|
|
3971
4084
|
}
|
|
3972
4085
|
}
|
|
3973
|
-
} else if (
|
|
4086
|
+
} else if (ts10.isCallSignatureDeclaration(member)) {
|
|
3974
4087
|
const callSig = serializeCallSignature(member, ctx);
|
|
3975
4088
|
if (callSig?.signatures) {
|
|
3976
4089
|
if (callSignatureMember?.signatures) {
|
|
@@ -3993,7 +4106,7 @@ function serializeInterface(node, ctx) {
|
|
|
3993
4106
|
callSignatureMember = callSig;
|
|
3994
4107
|
}
|
|
3995
4108
|
}
|
|
3996
|
-
} else if (
|
|
4109
|
+
} else if (ts10.isIndexSignatureDeclaration(member)) {
|
|
3997
4110
|
const indexMember = serializeIndexSignature(member, ctx);
|
|
3998
4111
|
if (indexMember)
|
|
3999
4112
|
members.push(indexMember);
|
|
@@ -4031,7 +4144,7 @@ function serializePropertySignature(node, ctx) {
|
|
|
4031
4144
|
const flags = {};
|
|
4032
4145
|
if (node.questionToken)
|
|
4033
4146
|
flags.optional = true;
|
|
4034
|
-
if (node.modifiers?.some((m) => m.kind ===
|
|
4147
|
+
if (node.modifiers?.some((m) => m.kind === ts10.SyntaxKind.ReadonlyKeyword)) {
|
|
4035
4148
|
flags.readonly = true;
|
|
4036
4149
|
}
|
|
4037
4150
|
const symbol = checker.getSymbolAtLocation(node.name);
|
|
@@ -4119,7 +4232,7 @@ function getInterfaceExtends(node, checker) {
|
|
|
4119
4232
|
if (!node.heritageClauses)
|
|
4120
4233
|
return;
|
|
4121
4234
|
for (const clause of node.heritageClauses) {
|
|
4122
|
-
if (clause.token ===
|
|
4235
|
+
if (clause.token === ts10.SyntaxKind.ExtendsKeyword && clause.types.length > 0) {
|
|
4123
4236
|
const names = clause.types.map((expr) => {
|
|
4124
4237
|
const type = checker.getTypeAtLocation(expr);
|
|
4125
4238
|
return type.getSymbol()?.getName() ?? expr.expression.getText();
|
|
@@ -4131,7 +4244,7 @@ function getInterfaceExtends(node, checker) {
|
|
|
4131
4244
|
}
|
|
4132
4245
|
|
|
4133
4246
|
// src/serializers/type-aliases.ts
|
|
4134
|
-
import
|
|
4247
|
+
import ts11 from "typescript";
|
|
4135
4248
|
function buildIntersectionSchemaFromNode(node, ctx) {
|
|
4136
4249
|
const types = node.types;
|
|
4137
4250
|
const schemas = [];
|
|
@@ -4159,14 +4272,17 @@ function serializeTypeAlias(node, ctx) {
|
|
|
4159
4272
|
registerReferencedTypes(type, ctx);
|
|
4160
4273
|
let schema;
|
|
4161
4274
|
let members;
|
|
4162
|
-
if (
|
|
4275
|
+
if (ts11.isIntersectionTypeNode(node.type)) {
|
|
4163
4276
|
schema = buildIntersectionSchemaFromNode(node.type, ctx);
|
|
4164
4277
|
if (type.getProperties().length > 0 && type.getCallSignatures().length === 0) {
|
|
4165
4278
|
members = serializeResolvedMembers(type, node, ctx);
|
|
4166
4279
|
}
|
|
4167
4280
|
} else if (isInlineFunctionAlias(node.type) && type.getCallSignatures().length > 0) {
|
|
4168
4281
|
schema = buildFunctionSchema(type.getCallSignatures(), ctx.typeChecker, ctx);
|
|
4169
|
-
|
|
4282
|
+
if (type.getProperties().length > 0) {
|
|
4283
|
+
members = serializeResolvedMembers(type, node, ctx);
|
|
4284
|
+
}
|
|
4285
|
+
} else if ((ts11.isMappedTypeNode(node.type) || ts11.isConditionalTypeNode(node.type)) && type.getProperties().length > 0 && type.getCallSignatures().length === 0 && !(type.flags & ts11.TypeFlags.Conditional) && !(type.flags & (ts11.TypeFlags.StringLike | ts11.TypeFlags.NumberLike))) {
|
|
4170
4286
|
schema = buildObjectSchema(type.getProperties(), ctx.typeChecker, ctx, type);
|
|
4171
4287
|
members = serializeResolvedMembers(type, node, ctx);
|
|
4172
4288
|
} else {
|
|
@@ -4176,9 +4292,13 @@ function serializeTypeAlias(node, ctx) {
|
|
|
4176
4292
|
}
|
|
4177
4293
|
}
|
|
4178
4294
|
if (shouldEmitAliasTypeText(node.type) && typeof schema === "object" && schema !== null && !("x-ts-type" in schema)) {
|
|
4179
|
-
const text = renderTypeText(type, ctx.typeChecker, node,
|
|
4295
|
+
const text = renderTypeText(type, ctx.typeChecker, node, ts11.TypeFormatFlags.InTypeAlias);
|
|
4180
4296
|
if (!PRIMITIVES.has(text) && text !== name) {
|
|
4181
4297
|
schema["x-ts-type"] = text;
|
|
4298
|
+
const declared = writtenTypeText(declaredTypeNode(node));
|
|
4299
|
+
if (declared && declared !== text && !PRIMITIVES.has(declared)) {
|
|
4300
|
+
schema["x-ts-declared"] = declared;
|
|
4301
|
+
}
|
|
4182
4302
|
}
|
|
4183
4303
|
}
|
|
4184
4304
|
return {
|
|
@@ -4197,12 +4317,12 @@ function serializeTypeAlias(node, ctx) {
|
|
|
4197
4317
|
}
|
|
4198
4318
|
function isObjectShapedAlias(type, ctx) {
|
|
4199
4319
|
const { typeChecker: checker } = ctx;
|
|
4200
|
-
if (!(type.flags &
|
|
4320
|
+
if (!(type.flags & ts11.TypeFlags.Object))
|
|
4201
4321
|
return false;
|
|
4202
4322
|
if (checker.isArrayType(type) || checker.isTupleType(type))
|
|
4203
4323
|
return false;
|
|
4204
4324
|
const objectFlags = type.objectFlags;
|
|
4205
|
-
if (!(objectFlags &
|
|
4325
|
+
if (!(objectFlags & ts11.ObjectFlags.Mapped)) {
|
|
4206
4326
|
const targetSymbol = type.target?.getSymbol?.() ?? type.getSymbol();
|
|
4207
4327
|
if (isBuiltinSymbol(targetSymbol))
|
|
4208
4328
|
return false;
|
|
@@ -4210,19 +4330,19 @@ function isObjectShapedAlias(type, ctx) {
|
|
|
4210
4330
|
return type.getProperties().length > 0 && type.getCallSignatures().length === 0;
|
|
4211
4331
|
}
|
|
4212
4332
|
function isInlineFunctionAlias(typeNode) {
|
|
4213
|
-
return
|
|
4333
|
+
return ts11.isFunctionTypeNode(typeNode) || ts11.isTypeLiteralNode(typeNode) && typeNode.members.some(ts11.isCallSignatureDeclaration);
|
|
4214
4334
|
}
|
|
4215
4335
|
function buildConditionalArmDocs(typeNode, checker) {
|
|
4216
4336
|
const docs = new Map;
|
|
4217
|
-
if (!
|
|
4337
|
+
if (!ts11.isMappedTypeNode(typeNode) || !typeNode.type)
|
|
4218
4338
|
return docs;
|
|
4219
4339
|
const literalKeys = (extendsType) => {
|
|
4220
4340
|
const keys = [];
|
|
4221
4341
|
const visit = (n) => {
|
|
4222
|
-
if (
|
|
4342
|
+
if (ts11.isUnionTypeNode(n)) {
|
|
4223
4343
|
for (const member of n.types)
|
|
4224
4344
|
visit(member);
|
|
4225
|
-
} else if (
|
|
4345
|
+
} else if (ts11.isLiteralTypeNode(n) && ts11.isStringLiteral(n.literal)) {
|
|
4226
4346
|
keys.push(n.literal.text);
|
|
4227
4347
|
}
|
|
4228
4348
|
};
|
|
@@ -4230,12 +4350,12 @@ function buildConditionalArmDocs(typeNode, checker) {
|
|
|
4230
4350
|
return keys;
|
|
4231
4351
|
};
|
|
4232
4352
|
const armDoc = (armType) => {
|
|
4233
|
-
if (!
|
|
4353
|
+
if (!ts11.isTypeReferenceNode(armType))
|
|
4234
4354
|
return;
|
|
4235
4355
|
const symbol = checker.getSymbolAtLocation(armType.typeName);
|
|
4236
4356
|
if (!symbol)
|
|
4237
4357
|
return;
|
|
4238
|
-
const target = symbol.flags &
|
|
4358
|
+
const target = symbol.flags & ts11.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
4239
4359
|
const { deprecated, reason } = isSymbolDeprecated(target);
|
|
4240
4360
|
const targetDecl = target.getDeclarations()?.[0];
|
|
4241
4361
|
const description = targetDecl ? getJSDocComment(targetDecl, target, checker).description : undefined;
|
|
@@ -4244,7 +4364,7 @@ function buildConditionalArmDocs(typeNode, checker) {
|
|
|
4244
4364
|
return { deprecated, deprecationReason: reason, description };
|
|
4245
4365
|
};
|
|
4246
4366
|
let current = typeNode.type;
|
|
4247
|
-
while (current &&
|
|
4367
|
+
while (current && ts11.isConditionalTypeNode(current)) {
|
|
4248
4368
|
const doc = armDoc(current.trueType);
|
|
4249
4369
|
if (doc) {
|
|
4250
4370
|
for (const key of literalKeys(current.extendsType)) {
|
|
@@ -4263,10 +4383,10 @@ function serializeResolvedMembers(type, node, ctx) {
|
|
|
4263
4383
|
for (const prop of type.getProperties()) {
|
|
4264
4384
|
const decl = prop.getDeclarations()?.[0] ?? node;
|
|
4265
4385
|
const rawPropType = checker.getTypeOfSymbolAtLocation(prop, decl);
|
|
4266
|
-
const propType = prop.flags &
|
|
4386
|
+
const propType = prop.flags & ts11.SymbolFlags.Optional ? stripUndefinedFromType(rawPropType, checker) : rawPropType;
|
|
4267
4387
|
registerReferencedTypes(propType, ctx);
|
|
4268
4388
|
const callSigs = propType.getCallSignatures();
|
|
4269
|
-
const isMethodDecl =
|
|
4389
|
+
const isMethodDecl = ts11.isMethodSignature(decl) || ts11.isMethodDeclaration(decl) || ts11.isFunctionDeclaration(decl);
|
|
4270
4390
|
const kind = callSigs.length > 0 && isMethodDecl ? "method" : "property";
|
|
4271
4391
|
let { description, tags } = getJSDocComment(decl, prop, checker);
|
|
4272
4392
|
let { deprecated, reason: deprecationReason } = isSymbolDeprecated(prop);
|
|
@@ -4284,11 +4404,11 @@ function serializeResolvedMembers(type, node, ctx) {
|
|
|
4284
4404
|
({ deprecated, reason: deprecationReason } = isSymbolDeprecated(armAlias));
|
|
4285
4405
|
}
|
|
4286
4406
|
const flags = {};
|
|
4287
|
-
if (prop.flags &
|
|
4407
|
+
if (prop.flags & ts11.SymbolFlags.Optional)
|
|
4288
4408
|
flags.optional = true;
|
|
4289
4409
|
if (isReadonlyPropertySymbol(prop))
|
|
4290
4410
|
flags.readonly = true;
|
|
4291
|
-
if (prop.flags &
|
|
4411
|
+
if (prop.flags & ts11.SymbolFlags.Method)
|
|
4292
4412
|
flags.methodSyntax = true;
|
|
4293
4413
|
const schema = kind === "property" ? decoratePropertySchema(buildSchema(propType, checker, ctx), prop, propType, checker) : decoratePropertySchema({ "x-ts-function": true }, prop, propType, checker);
|
|
4294
4414
|
members.push({
|
|
@@ -5066,7 +5186,7 @@ async function getExport(options) {
|
|
|
5066
5186
|
ctx.exportedIds = exportedIds;
|
|
5067
5187
|
try {
|
|
5068
5188
|
const originalDecls = targetSymbol.declarations ?? [];
|
|
5069
|
-
const isNamespaceExportDecl = originalDecls.some((d) =>
|
|
5189
|
+
const isNamespaceExportDecl = originalDecls.some((d) => ts12.isNamespaceExport(d) || ts12.isNamespaceImport(d));
|
|
5070
5190
|
if (isNamespaceExportDecl) {
|
|
5071
5191
|
const spec2 = serializeNamespaceForGet(targetSymbol, exportName, ctx);
|
|
5072
5192
|
const types2 = ctx.typeRegistry.getAll().map((t) => normalizeType(t));
|
|
@@ -5117,42 +5237,42 @@ function resolveExportTarget(symbol, checker) {
|
|
|
5117
5237
|
let isTypeOnly = false;
|
|
5118
5238
|
const declarations = symbol.declarations ?? [];
|
|
5119
5239
|
for (const decl of declarations) {
|
|
5120
|
-
if (
|
|
5240
|
+
if (ts12.isExportSpecifier(decl)) {
|
|
5121
5241
|
if (decl.isTypeOnly)
|
|
5122
5242
|
isTypeOnly = true;
|
|
5123
5243
|
const exportDecl = decl.parent?.parent;
|
|
5124
|
-
if (exportDecl &&
|
|
5244
|
+
if (exportDecl && ts12.isExportDeclaration(exportDecl) && exportDecl.isTypeOnly) {
|
|
5125
5245
|
isTypeOnly = true;
|
|
5126
5246
|
}
|
|
5127
5247
|
}
|
|
5128
5248
|
}
|
|
5129
|
-
if (symbol.flags &
|
|
5249
|
+
if (symbol.flags & ts12.SymbolFlags.Alias) {
|
|
5130
5250
|
const aliased = checker.getAliasedSymbol(symbol);
|
|
5131
5251
|
if (aliased && aliased !== symbol) {
|
|
5132
5252
|
resolvedSymbol = aliased;
|
|
5133
5253
|
}
|
|
5134
5254
|
}
|
|
5135
5255
|
const targetDeclarations = resolvedSymbol.declarations ?? [];
|
|
5136
|
-
const declaration = resolvedSymbol.valueDeclaration || targetDeclarations.find((d) => d.kind !==
|
|
5256
|
+
const declaration = resolvedSymbol.valueDeclaration || targetDeclarations.find((d) => d.kind !== ts12.SyntaxKind.ExportSpecifier) || targetDeclarations[0];
|
|
5137
5257
|
return { declaration, resolvedSymbol, isTypeOnly };
|
|
5138
5258
|
}
|
|
5139
5259
|
function serializeDeclaration(declaration, _exportSymbol, _targetSymbol, exportName, ctx, isTypeOnly) {
|
|
5140
5260
|
let result = null;
|
|
5141
|
-
if (
|
|
5261
|
+
if (ts12.isFunctionDeclaration(declaration)) {
|
|
5142
5262
|
result = serializeFunctionExport(declaration, ctx);
|
|
5143
|
-
} else if (
|
|
5263
|
+
} else if (ts12.isClassDeclaration(declaration)) {
|
|
5144
5264
|
result = serializeClass(declaration, ctx);
|
|
5145
|
-
} else if (
|
|
5265
|
+
} else if (ts12.isInterfaceDeclaration(declaration)) {
|
|
5146
5266
|
result = serializeInterface(declaration, ctx);
|
|
5147
|
-
} else if (
|
|
5267
|
+
} else if (ts12.isTypeAliasDeclaration(declaration)) {
|
|
5148
5268
|
result = serializeTypeAlias(declaration, ctx);
|
|
5149
|
-
} else if (
|
|
5269
|
+
} else if (ts12.isEnumDeclaration(declaration)) {
|
|
5150
5270
|
result = serializeEnum(declaration, ctx);
|
|
5151
|
-
} else if (
|
|
5271
|
+
} else if (ts12.isVariableDeclaration(declaration)) {
|
|
5152
5272
|
const varStatement = declaration.parent?.parent;
|
|
5153
|
-
if (varStatement &&
|
|
5154
|
-
if (declaration.initializer && (
|
|
5155
|
-
const varName =
|
|
5273
|
+
if (varStatement && ts12.isVariableStatement(varStatement)) {
|
|
5274
|
+
if (declaration.initializer && (ts12.isArrowFunction(declaration.initializer) || ts12.isFunctionExpression(declaration.initializer))) {
|
|
5275
|
+
const varName = ts12.isIdentifier(declaration.name) ? declaration.name.text : declaration.name.getText();
|
|
5156
5276
|
result = serializeFunctionExport(declaration.initializer, ctx, varName);
|
|
5157
5277
|
} else {
|
|
5158
5278
|
const checker = ctx.program.getTypeChecker();
|
|
@@ -5166,7 +5286,7 @@ function serializeDeclaration(declaration, _exportSymbol, _targetSymbol, exportN
|
|
|
5166
5286
|
}
|
|
5167
5287
|
}
|
|
5168
5288
|
}
|
|
5169
|
-
} else if (
|
|
5289
|
+
} else if (ts12.isNamespaceExport(declaration) || ts12.isModuleDeclaration(declaration) || ts12.isNamespaceImport(declaration) || ts12.isSourceFile(declaration)) {
|
|
5170
5290
|
result = serializeNamespaceForGet(_exportSymbol, exportName, ctx);
|
|
5171
5291
|
}
|
|
5172
5292
|
if (result) {
|
|
@@ -5182,7 +5302,7 @@ function serializeDeclaration(declaration, _exportSymbol, _targetSymbol, exportN
|
|
|
5182
5302
|
function serializeNamespaceForGet(symbol, exportName, ctx) {
|
|
5183
5303
|
const checker = ctx.program.getTypeChecker();
|
|
5184
5304
|
let targetSymbol = symbol;
|
|
5185
|
-
if (symbol.flags &
|
|
5305
|
+
if (symbol.flags & ts12.SymbolFlags.Alias) {
|
|
5186
5306
|
const aliased = checker.getAliasedSymbol(symbol);
|
|
5187
5307
|
if (aliased && aliased !== symbol) {
|
|
5188
5308
|
targetSymbol = aliased;
|
|
@@ -5212,7 +5332,7 @@ function serializeNamespaceForGet(symbol, exportName, ctx) {
|
|
|
5212
5332
|
}
|
|
5213
5333
|
function detectExternalPackage(symbol, checker) {
|
|
5214
5334
|
let targetSymbol = symbol;
|
|
5215
|
-
if (symbol.flags &
|
|
5335
|
+
if (symbol.flags & ts12.SymbolFlags.Alias) {
|
|
5216
5336
|
const aliased = checker.getAliasedSymbol(symbol);
|
|
5217
5337
|
if (aliased && aliased !== symbol) {
|
|
5218
5338
|
targetSymbol = aliased;
|
|
@@ -5226,9 +5346,9 @@ function detectExternalPackage(symbol, checker) {
|
|
|
5226
5346
|
if (match)
|
|
5227
5347
|
return match[1];
|
|
5228
5348
|
}
|
|
5229
|
-
if (
|
|
5349
|
+
if (ts12.isExportSpecifier(decl)) {
|
|
5230
5350
|
const exportDecl = decl.parent?.parent;
|
|
5231
|
-
if (exportDecl &&
|
|
5351
|
+
if (exportDecl && ts12.isExportDeclaration(exportDecl) && exportDecl.moduleSpecifier) {
|
|
5232
5352
|
const moduleText = exportDecl.moduleSpecifier.text;
|
|
5233
5353
|
if (!moduleText.startsWith(".") && !moduleText.startsWith("/")) {
|
|
5234
5354
|
return moduleText;
|
|
@@ -5239,8 +5359,8 @@ function detectExternalPackage(symbol, checker) {
|
|
|
5239
5359
|
return;
|
|
5240
5360
|
}
|
|
5241
5361
|
// src/primitives/list.ts
|
|
5242
|
-
import * as
|
|
5243
|
-
import
|
|
5362
|
+
import * as path5 from "node:path";
|
|
5363
|
+
import ts13 from "typescript";
|
|
5244
5364
|
async function listExports(options) {
|
|
5245
5365
|
const { entryFile, baseDir, content } = options;
|
|
5246
5366
|
const errors = [];
|
|
@@ -5283,16 +5403,16 @@ async function listExports(options) {
|
|
|
5283
5403
|
}
|
|
5284
5404
|
function extractExportItem(symbol, checker, entryFile, entrySourceFile) {
|
|
5285
5405
|
const name = symbol.getName();
|
|
5286
|
-
const isReexport = !!(symbol.flags &
|
|
5406
|
+
const isReexport = !!(symbol.flags & ts13.SymbolFlags.Alias);
|
|
5287
5407
|
let targetSymbol = symbol;
|
|
5288
|
-
if (symbol.flags &
|
|
5408
|
+
if (symbol.flags & ts13.SymbolFlags.Alias) {
|
|
5289
5409
|
const aliased = checker.getAliasedSymbol(symbol);
|
|
5290
5410
|
if (aliased && aliased !== symbol) {
|
|
5291
5411
|
targetSymbol = aliased;
|
|
5292
5412
|
}
|
|
5293
5413
|
}
|
|
5294
5414
|
const declarations = targetSymbol.declarations ?? [];
|
|
5295
|
-
const declaration = targetSymbol.valueDeclaration || declarations.find((d) => d.kind !==
|
|
5415
|
+
const declaration = targetSymbol.valueDeclaration || declarations.find((d) => d.kind !== ts13.SyntaxKind.ExportSpecifier) || declarations[0];
|
|
5296
5416
|
if (!declaration) {
|
|
5297
5417
|
return {
|
|
5298
5418
|
name,
|
|
@@ -5302,11 +5422,11 @@ function extractExportItem(symbol, checker, entryFile, entrySourceFile) {
|
|
|
5302
5422
|
reexport: true
|
|
5303
5423
|
};
|
|
5304
5424
|
}
|
|
5305
|
-
if (
|
|
5425
|
+
if (ts13.isSourceFile(declaration)) {
|
|
5306
5426
|
return {
|
|
5307
5427
|
name,
|
|
5308
5428
|
kind: "namespace",
|
|
5309
|
-
file:
|
|
5429
|
+
file: path5.relative(path5.dirname(entryFile), declaration.fileName),
|
|
5310
5430
|
line: 1,
|
|
5311
5431
|
reexport: true
|
|
5312
5432
|
};
|
|
@@ -5320,7 +5440,7 @@ function extractExportItem(symbol, checker, entryFile, entrySourceFile) {
|
|
|
5320
5440
|
return {
|
|
5321
5441
|
name,
|
|
5322
5442
|
kind,
|
|
5323
|
-
file:
|
|
5443
|
+
file: path5.relative(path5.dirname(entryFile), sourceFile.fileName),
|
|
5324
5444
|
line: line + 1,
|
|
5325
5445
|
...description ? { description } : {},
|
|
5326
5446
|
...deprecated ? { deprecated: true } : {},
|
|
@@ -5341,21 +5461,21 @@ function getDescriptionPreview(symbol, checker) {
|
|
|
5341
5461
|
return `${firstLine.slice(0, 77)}...`;
|
|
5342
5462
|
}
|
|
5343
5463
|
// src/builder/spec-builder.ts
|
|
5344
|
-
import * as
|
|
5464
|
+
import * as fs6 from "node:fs";
|
|
5345
5465
|
import * as path9 from "node:path";
|
|
5346
5466
|
import { SCHEMA_URL, SCHEMA_VERSION } from "@openpkg-ts/spec";
|
|
5347
|
-
import
|
|
5467
|
+
import ts18 from "typescript";
|
|
5348
5468
|
|
|
5349
5469
|
// src/ast/resolve.ts
|
|
5350
|
-
import
|
|
5470
|
+
import ts14 from "typescript";
|
|
5351
5471
|
function isTypeOnlyExport(symbol) {
|
|
5352
5472
|
const declarations = symbol.declarations ?? [];
|
|
5353
5473
|
for (const decl of declarations) {
|
|
5354
|
-
if (
|
|
5474
|
+
if (ts14.isExportSpecifier(decl)) {
|
|
5355
5475
|
if (decl.isTypeOnly)
|
|
5356
5476
|
return true;
|
|
5357
5477
|
const exportDecl = decl.parent?.parent;
|
|
5358
|
-
if (exportDecl &&
|
|
5478
|
+
if (exportDecl && ts14.isExportDeclaration(exportDecl) && exportDecl.isTypeOnly) {
|
|
5359
5479
|
return true;
|
|
5360
5480
|
}
|
|
5361
5481
|
}
|
|
@@ -5365,14 +5485,14 @@ function isTypeOnlyExport(symbol) {
|
|
|
5365
5485
|
function resolveExportTarget2(symbol, checker) {
|
|
5366
5486
|
let targetSymbol = symbol;
|
|
5367
5487
|
const isTypeOnly = isTypeOnlyExport(symbol);
|
|
5368
|
-
if (symbol.flags &
|
|
5488
|
+
if (symbol.flags & ts14.SymbolFlags.Alias) {
|
|
5369
5489
|
const aliasTarget = checker.getAliasedSymbol(symbol);
|
|
5370
5490
|
if (aliasTarget && aliasTarget !== symbol) {
|
|
5371
5491
|
targetSymbol = aliasTarget;
|
|
5372
5492
|
}
|
|
5373
5493
|
}
|
|
5374
5494
|
const declarations = targetSymbol.declarations ?? [];
|
|
5375
|
-
const declaration = targetSymbol.valueDeclaration || declarations.find((decl) => decl.kind !==
|
|
5495
|
+
const declaration = targetSymbol.valueDeclaration || declarations.find((decl) => decl.kind !== ts14.SyntaxKind.ExportSpecifier) || declarations[0];
|
|
5376
5496
|
return { declaration, targetSymbol, isTypeOnly };
|
|
5377
5497
|
}
|
|
5378
5498
|
|
|
@@ -5380,7 +5500,7 @@ function resolveExportTarget2(symbol, checker) {
|
|
|
5380
5500
|
import { spawn, spawnSync } from "node:child_process";
|
|
5381
5501
|
import * as fs4 from "node:fs";
|
|
5382
5502
|
import * as os from "node:os";
|
|
5383
|
-
import * as
|
|
5503
|
+
import * as path6 from "node:path";
|
|
5384
5504
|
var MAX_BUFFER_SIZE = 10 * 1024 * 1024;
|
|
5385
5505
|
function isStandardJSONSchema(obj) {
|
|
5386
5506
|
if (typeof obj !== "object" || obj === null)
|
|
@@ -5639,16 +5759,16 @@ async function extractStandardSchemasFromTs(tsFilePath, options = {}) {
|
|
|
5639
5759
|
return result;
|
|
5640
5760
|
}
|
|
5641
5761
|
const tempDir = os.tmpdir();
|
|
5642
|
-
const workerPath =
|
|
5762
|
+
const workerPath = path6.join(tempDir, `openpkg-extract-worker-${Date.now()}.ts`);
|
|
5643
5763
|
try {
|
|
5644
5764
|
fs4.writeFileSync(workerPath, TS_WORKER_SCRIPT);
|
|
5645
5765
|
const optionsJson = JSON.stringify({ target, libraryOptions });
|
|
5646
5766
|
const args = [...runtime.args, workerPath, tsFilePath, optionsJson];
|
|
5647
|
-
return await new Promise((
|
|
5767
|
+
return await new Promise((resolve3) => {
|
|
5648
5768
|
const child = spawn(runtime.cmd, args, {
|
|
5649
5769
|
timeout,
|
|
5650
5770
|
stdio: ["ignore", "pipe", "pipe"],
|
|
5651
|
-
cwd:
|
|
5771
|
+
cwd: path6.dirname(tsFilePath)
|
|
5652
5772
|
});
|
|
5653
5773
|
let stdout = "";
|
|
5654
5774
|
let stderr = "";
|
|
@@ -5680,14 +5800,14 @@ async function extractStandardSchemasFromTs(tsFilePath, options = {}) {
|
|
|
5680
5800
|
}
|
|
5681
5801
|
if (code !== 0) {
|
|
5682
5802
|
result.errors.push(`Extraction failed (${runtime.name}): ${stderr || `exit code ${code}`}`);
|
|
5683
|
-
|
|
5803
|
+
resolve3(result);
|
|
5684
5804
|
return;
|
|
5685
5805
|
}
|
|
5686
5806
|
try {
|
|
5687
5807
|
const parsed = JSON.parse(stdout);
|
|
5688
5808
|
if (!parsed.success) {
|
|
5689
5809
|
result.errors.push(`Extraction failed: ${parsed.error}`);
|
|
5690
|
-
|
|
5810
|
+
resolve3(result);
|
|
5691
5811
|
return;
|
|
5692
5812
|
}
|
|
5693
5813
|
for (const item of parsed.results) {
|
|
@@ -5722,7 +5842,7 @@ async function extractStandardSchemasFromTs(tsFilePath, options = {}) {
|
|
|
5722
5842
|
message: "stderr exceeded 10MB buffer limit"
|
|
5723
5843
|
});
|
|
5724
5844
|
}
|
|
5725
|
-
|
|
5845
|
+
resolve3(result);
|
|
5726
5846
|
});
|
|
5727
5847
|
child.on("error", (err) => {
|
|
5728
5848
|
try {
|
|
@@ -5733,7 +5853,7 @@ async function extractStandardSchemasFromTs(tsFilePath, options = {}) {
|
|
|
5733
5853
|
}
|
|
5734
5854
|
}
|
|
5735
5855
|
result.errors.push(`Subprocess error: ${err.message}`);
|
|
5736
|
-
|
|
5856
|
+
resolve3(result);
|
|
5737
5857
|
});
|
|
5738
5858
|
});
|
|
5739
5859
|
} catch (e) {
|
|
@@ -5749,7 +5869,7 @@ async function extractStandardSchemasFromTs(tsFilePath, options = {}) {
|
|
|
5749
5869
|
}
|
|
5750
5870
|
}
|
|
5751
5871
|
function readTsconfigOutDir(baseDir) {
|
|
5752
|
-
const tsconfigPath =
|
|
5872
|
+
const tsconfigPath = path6.join(baseDir, "tsconfig.json");
|
|
5753
5873
|
try {
|
|
5754
5874
|
if (!fs4.existsSync(tsconfigPath)) {
|
|
5755
5875
|
return null;
|
|
@@ -5764,7 +5884,7 @@ function readTsconfigOutDir(baseDir) {
|
|
|
5764
5884
|
return null;
|
|
5765
5885
|
}
|
|
5766
5886
|
function resolveCompiledPath(tsPath, baseDir) {
|
|
5767
|
-
const relativePath =
|
|
5887
|
+
const relativePath = path6.relative(baseDir, tsPath);
|
|
5768
5888
|
const withoutExt = relativePath.replace(/\.tsx?$/, "");
|
|
5769
5889
|
const srcPrefix = withoutExt.replace(/^src\//, "");
|
|
5770
5890
|
const tsconfigOutDir = readTsconfigOutDir(baseDir);
|
|
@@ -5772,7 +5892,7 @@ function resolveCompiledPath(tsPath, baseDir) {
|
|
|
5772
5892
|
const candidates = [];
|
|
5773
5893
|
if (tsconfigOutDir) {
|
|
5774
5894
|
for (const ext of extensions) {
|
|
5775
|
-
candidates.push(
|
|
5895
|
+
candidates.push(path6.join(baseDir, tsconfigOutDir, `${srcPrefix}${ext}`));
|
|
5776
5896
|
}
|
|
5777
5897
|
}
|
|
5778
5898
|
const commonOutDirs = ["dist", "build", "lib", "out"];
|
|
@@ -5780,17 +5900,17 @@ function resolveCompiledPath(tsPath, baseDir) {
|
|
|
5780
5900
|
if (outDir === tsconfigOutDir)
|
|
5781
5901
|
continue;
|
|
5782
5902
|
for (const ext of extensions) {
|
|
5783
|
-
candidates.push(
|
|
5903
|
+
candidates.push(path6.join(baseDir, outDir, `${srcPrefix}${ext}`));
|
|
5784
5904
|
}
|
|
5785
5905
|
}
|
|
5786
5906
|
for (const ext of extensions) {
|
|
5787
|
-
candidates.push(
|
|
5907
|
+
candidates.push(path6.join(baseDir, `${withoutExt}${ext}`));
|
|
5788
5908
|
}
|
|
5789
5909
|
const workspaceMatch = baseDir.match(/^(.+\/packages\/[^/]+)$/);
|
|
5790
5910
|
if (workspaceMatch) {
|
|
5791
5911
|
const pkgRoot = workspaceMatch[1];
|
|
5792
5912
|
for (const ext of extensions) {
|
|
5793
|
-
candidates.push(
|
|
5913
|
+
candidates.push(path6.join(pkgRoot, "dist", `${srcPrefix}${ext}`));
|
|
5794
5914
|
}
|
|
5795
5915
|
}
|
|
5796
5916
|
for (const candidate of candidates) {
|
|
@@ -5812,7 +5932,7 @@ async function extractStandardSchemas(compiledJsPath, options = {}) {
|
|
|
5812
5932
|
return result;
|
|
5813
5933
|
}
|
|
5814
5934
|
const optionsJson = JSON.stringify({ target, libraryOptions });
|
|
5815
|
-
return new Promise((
|
|
5935
|
+
return new Promise((resolve3) => {
|
|
5816
5936
|
const child = spawn("node", ["-e", WORKER_SCRIPT, compiledJsPath, optionsJson], {
|
|
5817
5937
|
timeout,
|
|
5818
5938
|
stdio: ["ignore", "pipe", "pipe"]
|
|
@@ -5840,14 +5960,14 @@ async function extractStandardSchemas(compiledJsPath, options = {}) {
|
|
|
5840
5960
|
child.on("close", (code) => {
|
|
5841
5961
|
if (code !== 0) {
|
|
5842
5962
|
result.errors.push(`Extraction process failed: ${stderr || `exit code ${code}`}`);
|
|
5843
|
-
|
|
5963
|
+
resolve3(result);
|
|
5844
5964
|
return;
|
|
5845
5965
|
}
|
|
5846
5966
|
try {
|
|
5847
5967
|
const parsed = JSON.parse(stdout);
|
|
5848
5968
|
if (!parsed.success) {
|
|
5849
5969
|
result.errors.push(`Extraction failed: ${parsed.error}`);
|
|
5850
|
-
|
|
5970
|
+
resolve3(result);
|
|
5851
5971
|
return;
|
|
5852
5972
|
}
|
|
5853
5973
|
for (const item of parsed.results) {
|
|
@@ -5882,11 +6002,11 @@ async function extractStandardSchemas(compiledJsPath, options = {}) {
|
|
|
5882
6002
|
message: "stderr exceeded 10MB buffer limit"
|
|
5883
6003
|
});
|
|
5884
6004
|
}
|
|
5885
|
-
|
|
6005
|
+
resolve3(result);
|
|
5886
6006
|
});
|
|
5887
6007
|
child.on("error", (err) => {
|
|
5888
6008
|
result.errors.push(`Subprocess error: ${err.message}`);
|
|
5889
|
-
|
|
6009
|
+
resolve3(result);
|
|
5890
6010
|
});
|
|
5891
6011
|
});
|
|
5892
6012
|
}
|
|
@@ -5924,9 +6044,9 @@ async function extractStandardSchemasFromProject(entryFile, baseDir, options = {
|
|
|
5924
6044
|
|
|
5925
6045
|
// src/builder/external-resolver.ts
|
|
5926
6046
|
import * as fs5 from "node:fs";
|
|
5927
|
-
import * as
|
|
6047
|
+
import * as path7 from "node:path";
|
|
5928
6048
|
import picomatch from "picomatch";
|
|
5929
|
-
import
|
|
6049
|
+
import ts15 from "typescript";
|
|
5930
6050
|
function matchesExternalPattern(packageName, include, exclude) {
|
|
5931
6051
|
if (!include?.length)
|
|
5932
6052
|
return false;
|
|
@@ -5942,7 +6062,7 @@ function matchesExternalPattern(packageName, include, exclude) {
|
|
|
5942
6062
|
return true;
|
|
5943
6063
|
}
|
|
5944
6064
|
function resolveExternalModule(moduleSpecifier, containingFile, compilerOptions) {
|
|
5945
|
-
const resolved =
|
|
6065
|
+
const resolved = ts15.resolveModuleName(moduleSpecifier, containingFile, compilerOptions, ts15.sys);
|
|
5946
6066
|
if (!resolved.resolvedModule) {
|
|
5947
6067
|
return null;
|
|
5948
6068
|
}
|
|
@@ -5958,11 +6078,11 @@ function findPackageJson(resolvedPath, packageName) {
|
|
|
5958
6078
|
const isScoped = packageName.startsWith("@");
|
|
5959
6079
|
const packageParts = isScoped ? packageName.split("/").slice(0, 2) : [packageName.split("/")[0]];
|
|
5960
6080
|
const packageDir = packageParts.join("/");
|
|
5961
|
-
let dir =
|
|
6081
|
+
let dir = path7.dirname(resolvedPath);
|
|
5962
6082
|
const maxDepth = 10;
|
|
5963
6083
|
for (let i = 0;i < maxDepth; i++) {
|
|
5964
6084
|
if (dir.endsWith(`node_modules/${packageDir}`)) {
|
|
5965
|
-
const pkgPath =
|
|
6085
|
+
const pkgPath = path7.join(dir, "package.json");
|
|
5966
6086
|
if (fs5.existsSync(pkgPath)) {
|
|
5967
6087
|
try {
|
|
5968
6088
|
return JSON.parse(fs5.readFileSync(pkgPath, "utf-8"));
|
|
@@ -5971,7 +6091,7 @@ function findPackageJson(resolvedPath, packageName) {
|
|
|
5971
6091
|
}
|
|
5972
6092
|
}
|
|
5973
6093
|
}
|
|
5974
|
-
const parent =
|
|
6094
|
+
const parent = path7.dirname(dir);
|
|
5975
6095
|
if (parent === dir)
|
|
5976
6096
|
break;
|
|
5977
6097
|
dir = parent;
|
|
@@ -6007,7 +6127,7 @@ function extractExternalExport(exportName, resolvedModule, program, ctx, visited
|
|
|
6007
6127
|
return null;
|
|
6008
6128
|
}
|
|
6009
6129
|
let resolvedSymbol = targetExport;
|
|
6010
|
-
if (targetExport.flags &
|
|
6130
|
+
if (targetExport.flags & ts15.SymbolFlags.Alias) {
|
|
6011
6131
|
const aliased = checker.getAliasedSymbol(targetExport);
|
|
6012
6132
|
if (aliased && aliased !== targetExport) {
|
|
6013
6133
|
resolvedSymbol = aliased;
|
|
@@ -6078,7 +6198,7 @@ function mergeRuntimeSchemas(staticExports, runtimeSchemas) {
|
|
|
6078
6198
|
}
|
|
6079
6199
|
|
|
6080
6200
|
// src/builder/type-cache.ts
|
|
6081
|
-
import
|
|
6201
|
+
import ts16 from "typescript";
|
|
6082
6202
|
|
|
6083
6203
|
// src/utils/cache-manager.ts
|
|
6084
6204
|
class CacheManager {
|
|
@@ -6185,10 +6305,10 @@ function findTypeDefinition(typeName, program, sourceFile) {
|
|
|
6185
6305
|
return typeDefinitionCache.getOrCompute(typeName, () => {
|
|
6186
6306
|
const checker = program.getTypeChecker();
|
|
6187
6307
|
const findInNode = (node) => {
|
|
6188
|
-
if ((
|
|
6308
|
+
if ((ts16.isInterfaceDeclaration(node) || ts16.isTypeAliasDeclaration(node) || ts16.isClassDeclaration(node) || ts16.isEnumDeclaration(node)) && node.name?.text === typeName) {
|
|
6189
6309
|
return node.getSourceFile().fileName;
|
|
6190
6310
|
}
|
|
6191
|
-
return
|
|
6311
|
+
return ts16.forEachChild(node, findInNode);
|
|
6192
6312
|
};
|
|
6193
6313
|
const entryResult = findInNode(sourceFile);
|
|
6194
6314
|
if (entryResult)
|
|
@@ -6200,14 +6320,14 @@ function findTypeDefinition(typeName, program, sourceFile) {
|
|
|
6200
6320
|
return result;
|
|
6201
6321
|
}
|
|
6202
6322
|
}
|
|
6203
|
-
const symbol = checker.resolveName(typeName, sourceFile,
|
|
6323
|
+
const symbol = checker.resolveName(typeName, sourceFile, ts16.SymbolFlags.Type, false);
|
|
6204
6324
|
return symbol?.declarations?.[0]?.getSourceFile().fileName;
|
|
6205
6325
|
});
|
|
6206
6326
|
}
|
|
6207
6327
|
function hasInternalTag(typeName, program, sourceFile) {
|
|
6208
6328
|
return internalTagCache.getOrCompute(typeName, () => {
|
|
6209
6329
|
const checker = program.getTypeChecker();
|
|
6210
|
-
const symbol = checker.resolveName(typeName, sourceFile,
|
|
6330
|
+
const symbol = checker.resolveName(typeName, sourceFile, ts16.SymbolFlags.Type, false);
|
|
6211
6331
|
if (!symbol)
|
|
6212
6332
|
return false;
|
|
6213
6333
|
return symbol.getJsDocTags().some((tag) => tag.name === "internal");
|
|
@@ -6215,21 +6335,8 @@ function hasInternalTag(typeName, program, sourceFile) {
|
|
|
6215
6335
|
}
|
|
6216
6336
|
|
|
6217
6337
|
// src/builder/type-expansion.ts
|
|
6218
|
-
import
|
|
6219
|
-
|
|
6220
|
-
import ts16 from "typescript";
|
|
6221
|
-
function findPackageDir(fromFile) {
|
|
6222
|
-
let dir = path7.dirname(path7.resolve(fromFile));
|
|
6223
|
-
while (true) {
|
|
6224
|
-
if (fs6.existsSync(path7.join(dir, "package.json")))
|
|
6225
|
-
return dir;
|
|
6226
|
-
const parent = path7.dirname(dir);
|
|
6227
|
-
if (parent === dir)
|
|
6228
|
-
return;
|
|
6229
|
-
dir = parent;
|
|
6230
|
-
}
|
|
6231
|
-
}
|
|
6232
|
-
var NODE_MODULES_PKG = /node_modules\/(@[^/]+\/[^/]+|[^/]+)/;
|
|
6338
|
+
import ts17 from "typescript";
|
|
6339
|
+
var NODE_MODULES_PKG2 = /node_modules\/(@[^/]+\/[^/]+|[^/]+)/;
|
|
6233
6340
|
function isLibFile(fileName) {
|
|
6234
6341
|
return fileName.includes("/typescript/lib/lib.") || fileName.includes("\\typescript\\lib\\lib.");
|
|
6235
6342
|
}
|
|
@@ -6256,7 +6363,7 @@ function createExternalExpansionPredicate(opts) {
|
|
|
6256
6363
|
const fileName = decl.getSourceFile().fileName;
|
|
6257
6364
|
if (isLibFile(fileName))
|
|
6258
6365
|
return false;
|
|
6259
|
-
const match = fileName.match(
|
|
6366
|
+
const match = fileName.match(NODE_MODULES_PKG2);
|
|
6260
6367
|
if (match)
|
|
6261
6368
|
return packageAllowed(match[1]);
|
|
6262
6369
|
return true;
|
|
@@ -6268,20 +6375,6 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6268
6375
|
const checker = ctx.typeChecker;
|
|
6269
6376
|
const visited = new Set;
|
|
6270
6377
|
const MAX_DEPTH = 30;
|
|
6271
|
-
const entryPackageDir = findPackageDir(opts.entryFile);
|
|
6272
|
-
const packageLabel = (fileName) => {
|
|
6273
|
-
const match = fileName.match(NODE_MODULES_PKG);
|
|
6274
|
-
let pkg = match?.[1];
|
|
6275
|
-
if (!pkg) {
|
|
6276
|
-
for (const [name, dir] of opts.workspacePackages) {
|
|
6277
|
-
if (fileName.startsWith(`${path7.resolve(dir)}${path7.sep}`)) {
|
|
6278
|
-
pkg = name;
|
|
6279
|
-
break;
|
|
6280
|
-
}
|
|
6281
|
-
}
|
|
6282
|
-
}
|
|
6283
|
-
return (pkg ?? "local").replace(/^@/, "").replace(/\//g, "-");
|
|
6284
|
-
};
|
|
6285
6378
|
const symbolAllowed = createExternalExpansionPredicate(opts);
|
|
6286
6379
|
const visit = (type, depth) => {
|
|
6287
6380
|
if (!type || depth > MAX_DEPTH || visited.has(type))
|
|
@@ -6311,7 +6404,7 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6311
6404
|
visit(t, depth + 1);
|
|
6312
6405
|
}
|
|
6313
6406
|
}
|
|
6314
|
-
if (!allowed || !(type.flags &
|
|
6407
|
+
if (!allowed || !(type.flags & ts17.TypeFlags.Object || type.isClassOrInterface())) {
|
|
6315
6408
|
return;
|
|
6316
6409
|
}
|
|
6317
6410
|
if (type.isClassOrInterface()) {
|
|
@@ -6334,19 +6427,19 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6334
6427
|
visit(info.type, depth + 1);
|
|
6335
6428
|
}
|
|
6336
6429
|
};
|
|
6337
|
-
const TYPE_SYMBOL_FLAGS =
|
|
6430
|
+
const TYPE_SYMBOL_FLAGS = ts17.SymbolFlags.Interface | ts17.SymbolFlags.TypeAlias | ts17.SymbolFlags.Class | ts17.SymbolFlags.RegularEnum | ts17.SymbolFlags.ConstEnum;
|
|
6338
6431
|
const visitedSymbols = new Set;
|
|
6339
6432
|
const symbolKind = (symbol) => {
|
|
6340
|
-
if (symbol.flags &
|
|
6433
|
+
if (symbol.flags & ts17.SymbolFlags.Interface)
|
|
6341
6434
|
return "interface";
|
|
6342
|
-
if (symbol.flags &
|
|
6435
|
+
if (symbol.flags & ts17.SymbolFlags.Class)
|
|
6343
6436
|
return "class";
|
|
6344
|
-
if (symbol.flags & (
|
|
6437
|
+
if (symbol.flags & (ts17.SymbolFlags.RegularEnum | ts17.SymbolFlags.ConstEnum))
|
|
6345
6438
|
return "enum";
|
|
6346
6439
|
return "type";
|
|
6347
6440
|
};
|
|
6348
6441
|
const resolveAlias = (symbol) => {
|
|
6349
|
-
if (symbol.flags &
|
|
6442
|
+
if (symbol.flags & ts17.SymbolFlags.Alias) {
|
|
6350
6443
|
try {
|
|
6351
6444
|
return checker.getAliasedSymbol(symbol);
|
|
6352
6445
|
} catch {
|
|
@@ -6368,37 +6461,16 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6368
6461
|
walkDeclarations(symbol);
|
|
6369
6462
|
return;
|
|
6370
6463
|
}
|
|
6371
|
-
|
|
6372
|
-
|
|
6373
|
-
if (prior !== symbol) {
|
|
6374
|
-
const declFile = symbol.declarations?.[0]?.getSourceFile().fileName ?? "";
|
|
6375
|
-
const foreign = !!entryPackageDir && !declFile.startsWith(`${entryPackageDir}${path7.sep}`);
|
|
6376
|
-
if (prior !== undefined || foreign) {
|
|
6377
|
-
const scopedId = `${packageLabel(declFile)}.${name}`;
|
|
6378
|
-
if (!ctx.typeRegistry.has(scopedId)) {
|
|
6379
|
-
const declared2 = checker.getDeclaredTypeOfSymbol(symbol);
|
|
6380
|
-
const schema = ensureNonEmptySchema(buildSchema(declared2, checker, ctx), declared2, checker);
|
|
6381
|
-
const selfRef = JSON.stringify(schema) === JSON.stringify({ $ref: `#/types/${name}` });
|
|
6382
|
-
if (!selfRef && JSON.stringify(ctx.typeRegistry.get(name)?.schema) !== JSON.stringify(schema)) {
|
|
6383
|
-
ctx.typeRegistry.add({
|
|
6384
|
-
id: scopedId,
|
|
6385
|
-
name,
|
|
6386
|
-
kind: symbolKind(symbol),
|
|
6387
|
-
schema
|
|
6388
|
-
});
|
|
6389
|
-
visit(declared2, 0);
|
|
6390
|
-
}
|
|
6391
|
-
}
|
|
6392
|
-
}
|
|
6393
|
-
}
|
|
6464
|
+
const id = resolveTypeId(symbol, ctx);
|
|
6465
|
+
if (ctx.typeRegistry.has(id)) {
|
|
6394
6466
|
walkDeclarations(symbol);
|
|
6395
6467
|
return;
|
|
6396
6468
|
}
|
|
6397
6469
|
const declared = checker.getDeclaredTypeOfSymbol(symbol);
|
|
6398
6470
|
ctx.typeRegistry.registerType(declared, ctx);
|
|
6399
|
-
if (!ctx.typeRegistry.has(
|
|
6471
|
+
if (!ctx.typeRegistry.has(id)) {
|
|
6400
6472
|
ctx.typeRegistry.add({
|
|
6401
|
-
id
|
|
6473
|
+
id,
|
|
6402
6474
|
name,
|
|
6403
6475
|
kind: symbolKind(symbol),
|
|
6404
6476
|
schema: ensureNonEmptySchema(buildSchema(declared, checker, ctx), declared, checker)
|
|
@@ -6419,7 +6491,7 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6419
6491
|
const target = symbol && resolveAlias(symbol);
|
|
6420
6492
|
if (!target || visitedSymbols.has(target))
|
|
6421
6493
|
return;
|
|
6422
|
-
if (!(target.flags & (
|
|
6494
|
+
if (!(target.flags & (ts17.SymbolFlags.ValueModule | ts17.SymbolFlags.NamespaceModule)))
|
|
6423
6495
|
return;
|
|
6424
6496
|
if (!symbolAllowed(target))
|
|
6425
6497
|
return;
|
|
@@ -6434,22 +6506,22 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6434
6506
|
}
|
|
6435
6507
|
};
|
|
6436
6508
|
const walkNode = (node) => {
|
|
6437
|
-
if (
|
|
6509
|
+
if (ts17.isTypeReferenceNode(node)) {
|
|
6438
6510
|
handleRef(node.typeName);
|
|
6439
|
-
if (
|
|
6511
|
+
if (ts17.isQualifiedName(node.typeName)) {
|
|
6440
6512
|
handleNamespaceRef(node.typeName.left);
|
|
6441
6513
|
}
|
|
6442
|
-
} else if (
|
|
6514
|
+
} else if (ts17.isExpressionWithTypeArguments(node)) {
|
|
6443
6515
|
handleRef(node.expression);
|
|
6444
|
-
} else if (
|
|
6516
|
+
} else if (ts17.isTypeQueryNode(node)) {
|
|
6445
6517
|
handleRef(node.exprName);
|
|
6446
|
-
if (
|
|
6518
|
+
if (ts17.isQualifiedName(node.exprName)) {
|
|
6447
6519
|
handleRef(node.exprName.left);
|
|
6448
6520
|
handleNamespaceRef(node.exprName.left);
|
|
6449
6521
|
}
|
|
6450
|
-
} else if (
|
|
6522
|
+
} else if (ts17.isImportTypeNode(node) && node.qualifier) {
|
|
6451
6523
|
handleRef(node.qualifier);
|
|
6452
|
-
if (
|
|
6524
|
+
if (ts17.isQualifiedName(node.qualifier)) {
|
|
6453
6525
|
handleNamespaceRef(node.qualifier.left);
|
|
6454
6526
|
}
|
|
6455
6527
|
}
|
|
@@ -6465,7 +6537,7 @@ function expandReachableTypes(exportedSymbols, ctx, opts) {
|
|
|
6465
6537
|
};
|
|
6466
6538
|
for (const exportSymbol of exportedSymbols) {
|
|
6467
6539
|
let target = exportSymbol;
|
|
6468
|
-
if (exportSymbol.flags &
|
|
6540
|
+
if (exportSymbol.flags & ts17.SymbolFlags.Alias) {
|
|
6469
6541
|
try {
|
|
6470
6542
|
target = checker.getAliasedSymbol(exportSymbol);
|
|
6471
6543
|
} catch {
|
|
@@ -6798,7 +6870,8 @@ async function extract(options) {
|
|
|
6798
6870
|
shouldExpandExternal: createExternalExpansionPredicate({
|
|
6799
6871
|
followExternal: options.followExternal,
|
|
6800
6872
|
workspacePackages: result.workspacePackages ?? new Map
|
|
6801
|
-
})
|
|
6873
|
+
}),
|
|
6874
|
+
workspacePackages: result.workspacePackages ?? new Map
|
|
6802
6875
|
});
|
|
6803
6876
|
ctx.exportedIds = exportedIds;
|
|
6804
6877
|
const filteredSymbols = exportedSymbols.filter((s) => shouldIncludeExport(s.getName(), only, ignore));
|
|
@@ -6827,9 +6900,9 @@ async function extract(options) {
|
|
|
6827
6900
|
break;
|
|
6828
6901
|
}
|
|
6829
6902
|
}
|
|
6830
|
-
if (
|
|
6903
|
+
if (ts18.isExportSpecifier(decl)) {
|
|
6831
6904
|
const exportDecl = decl.parent?.parent;
|
|
6832
|
-
if (exportDecl &&
|
|
6905
|
+
if (exportDecl && ts18.isExportDeclaration(exportDecl) && exportDecl.moduleSpecifier) {
|
|
6833
6906
|
const moduleText = exportDecl.moduleSpecifier.getText().slice(1, -1);
|
|
6834
6907
|
if (!moduleText.startsWith(".") && !moduleText.startsWith("/")) {
|
|
6835
6908
|
externalPackage = moduleText;
|
|
@@ -6898,7 +6971,7 @@ async function extract(options) {
|
|
|
6898
6971
|
entryFile
|
|
6899
6972
|
});
|
|
6900
6973
|
{
|
|
6901
|
-
const symFlags =
|
|
6974
|
+
const symFlags = ts18.SymbolFlags.Type | ts18.SymbolFlags.Interface | ts18.SymbolFlags.Class;
|
|
6902
6975
|
const maxPasses = 5;
|
|
6903
6976
|
for (let pass = 0;pass < maxPasses; pass++) {
|
|
6904
6977
|
const allRefs = new Map;
|
|
@@ -7045,21 +7118,21 @@ async function extract(options) {
|
|
|
7045
7118
|
}
|
|
7046
7119
|
function serializeDeclaration2(declaration, exportSymbol, exportName, ctx, isTypeOnly = false) {
|
|
7047
7120
|
let result = null;
|
|
7048
|
-
if (
|
|
7121
|
+
if (ts18.isFunctionDeclaration(declaration)) {
|
|
7049
7122
|
result = serializeFunctionExport(declaration, ctx);
|
|
7050
|
-
} else if (
|
|
7123
|
+
} else if (ts18.isClassDeclaration(declaration)) {
|
|
7051
7124
|
result = serializeClass(declaration, ctx);
|
|
7052
|
-
} else if (
|
|
7125
|
+
} else if (ts18.isInterfaceDeclaration(declaration)) {
|
|
7053
7126
|
result = serializeInterface(declaration, ctx);
|
|
7054
|
-
} else if (
|
|
7127
|
+
} else if (ts18.isTypeAliasDeclaration(declaration)) {
|
|
7055
7128
|
result = serializeTypeAlias(declaration, ctx);
|
|
7056
|
-
} else if (
|
|
7129
|
+
} else if (ts18.isEnumDeclaration(declaration)) {
|
|
7057
7130
|
result = serializeEnum(declaration, ctx);
|
|
7058
|
-
} else if (
|
|
7131
|
+
} else if (ts18.isVariableDeclaration(declaration)) {
|
|
7059
7132
|
const varStatement = declaration.parent?.parent;
|
|
7060
|
-
if (varStatement &&
|
|
7061
|
-
if (declaration.initializer && (
|
|
7062
|
-
const varName =
|
|
7133
|
+
if (varStatement && ts18.isVariableStatement(varStatement)) {
|
|
7134
|
+
if (declaration.initializer && (ts18.isArrowFunction(declaration.initializer) || ts18.isFunctionExpression(declaration.initializer))) {
|
|
7135
|
+
const varName = ts18.isIdentifier(declaration.name) ? declaration.name.text : declaration.name.getText();
|
|
7063
7136
|
result = serializeFunctionExport(declaration.initializer, ctx, varName);
|
|
7064
7137
|
} else {
|
|
7065
7138
|
result = serializeVariable(declaration, varStatement, ctx);
|
|
@@ -7071,7 +7144,7 @@ function serializeDeclaration2(declaration, exportSymbol, exportName, ctx, isTyp
|
|
|
7071
7144
|
}
|
|
7072
7145
|
}
|
|
7073
7146
|
}
|
|
7074
|
-
} else if (
|
|
7147
|
+
} else if (ts18.isNamespaceExport(declaration) || ts18.isModuleDeclaration(declaration) || ts18.isNamespaceImport(declaration) || ts18.isSourceFile(declaration)) {
|
|
7075
7148
|
try {
|
|
7076
7149
|
result = serializeNamespaceExport(exportSymbol, exportName, ctx);
|
|
7077
7150
|
} catch {
|
|
@@ -7107,7 +7180,7 @@ function serializeNamespaceExport(symbol, exportName, ctx) {
|
|
|
7107
7180
|
const members = [];
|
|
7108
7181
|
const checker = ctx.program.getTypeChecker();
|
|
7109
7182
|
let targetSymbol = symbol;
|
|
7110
|
-
if (symbol.flags &
|
|
7183
|
+
if (symbol.flags & ts18.SymbolFlags.Alias) {
|
|
7111
7184
|
const aliased = checker.getAliasedSymbol(symbol);
|
|
7112
7185
|
if (aliased && aliased !== symbol) {
|
|
7113
7186
|
targetSymbol = aliased;
|
|
@@ -7134,31 +7207,31 @@ function serializeNamespaceExport(symbol, exportName, ctx) {
|
|
|
7134
7207
|
function serializeNamespaceMember(symbol, memberName, ctx) {
|
|
7135
7208
|
const checker = ctx.program.getTypeChecker();
|
|
7136
7209
|
let targetSymbol = symbol;
|
|
7137
|
-
if (symbol.flags &
|
|
7210
|
+
if (symbol.flags & ts18.SymbolFlags.Alias) {
|
|
7138
7211
|
const aliased = checker.getAliasedSymbol(symbol);
|
|
7139
7212
|
if (aliased && aliased !== symbol) {
|
|
7140
7213
|
targetSymbol = aliased;
|
|
7141
7214
|
}
|
|
7142
7215
|
}
|
|
7143
7216
|
const declarations = targetSymbol.declarations ?? [];
|
|
7144
|
-
const declaration = targetSymbol.valueDeclaration || declarations.find((d) => d.kind !==
|
|
7217
|
+
const declaration = targetSymbol.valueDeclaration || declarations.find((d) => d.kind !== ts18.SyntaxKind.ExportSpecifier) || declarations[0];
|
|
7145
7218
|
if (!declaration)
|
|
7146
7219
|
return null;
|
|
7147
7220
|
const type = checker.getTypeAtLocation(declaration);
|
|
7148
7221
|
const callSignatures = type.getCallSignatures();
|
|
7149
7222
|
const { deprecated } = isSymbolDeprecated(targetSymbol);
|
|
7150
7223
|
let kind = "variable";
|
|
7151
|
-
if (
|
|
7224
|
+
if (ts18.isFunctionDeclaration(declaration) || ts18.isFunctionExpression(declaration)) {
|
|
7152
7225
|
kind = "function";
|
|
7153
|
-
} else if (
|
|
7226
|
+
} else if (ts18.isClassDeclaration(declaration)) {
|
|
7154
7227
|
kind = "class";
|
|
7155
|
-
} else if (
|
|
7228
|
+
} else if (ts18.isInterfaceDeclaration(declaration)) {
|
|
7156
7229
|
kind = "interface";
|
|
7157
|
-
} else if (
|
|
7230
|
+
} else if (ts18.isTypeAliasDeclaration(declaration)) {
|
|
7158
7231
|
kind = "type";
|
|
7159
|
-
} else if (
|
|
7232
|
+
} else if (ts18.isEnumDeclaration(declaration)) {
|
|
7160
7233
|
kind = "enum";
|
|
7161
|
-
} else if (
|
|
7234
|
+
} else if (ts18.isVariableDeclaration(declaration)) {
|
|
7162
7235
|
if (callSignatures.length > 0) {
|
|
7163
7236
|
kind = "function";
|
|
7164
7237
|
}
|
|
@@ -7189,11 +7262,11 @@ function getJSDocFromExportSymbol(symbol) {
|
|
|
7189
7262
|
const examples = [];
|
|
7190
7263
|
const decl = symbol.declarations?.[0];
|
|
7191
7264
|
if (decl) {
|
|
7192
|
-
const exportDecl =
|
|
7193
|
-
if (exportDecl &&
|
|
7194
|
-
const jsDocs =
|
|
7265
|
+
const exportDecl = ts18.isNamespaceExport(decl) ? decl.parent : decl;
|
|
7266
|
+
if (exportDecl && ts18.isExportDeclaration(exportDecl)) {
|
|
7267
|
+
const jsDocs = ts18.getJSDocCommentsAndTags(exportDecl);
|
|
7195
7268
|
for (const doc of jsDocs) {
|
|
7196
|
-
if (
|
|
7269
|
+
if (ts18.isJSDoc(doc) && doc.comment) {
|
|
7197
7270
|
const commentText = typeof doc.comment === "string" ? doc.comment : doc.comment.map((c) => ("text" in c) ? c.text : "").join("");
|
|
7198
7271
|
if (commentText) {
|
|
7199
7272
|
return {
|
|
@@ -7291,8 +7364,8 @@ async function getPackageMeta(entryFile, baseDir) {
|
|
|
7291
7364
|
while (dir !== path9.dirname(dir)) {
|
|
7292
7365
|
const pkgPath = path9.join(dir, "package.json");
|
|
7293
7366
|
try {
|
|
7294
|
-
if (
|
|
7295
|
-
const pkg = JSON.parse(
|
|
7367
|
+
if (fs6.existsSync(pkgPath)) {
|
|
7368
|
+
const pkg = JSON.parse(fs6.readFileSync(pkgPath, "utf-8"));
|
|
7296
7369
|
return {
|
|
7297
7370
|
name: pkg.name ?? path9.basename(dir),
|
|
7298
7371
|
version: pkg.version,
|
|
@@ -7787,12 +7860,12 @@ function toToolSchema(exp, spec, options) {
|
|
|
7787
7860
|
};
|
|
7788
7861
|
}
|
|
7789
7862
|
// src/types/utils.ts
|
|
7790
|
-
import
|
|
7863
|
+
import ts19 from "typescript";
|
|
7791
7864
|
function isExported(node) {
|
|
7792
7865
|
const modifiers = node.modifiers;
|
|
7793
7866
|
if (!modifiers)
|
|
7794
7867
|
return false;
|
|
7795
|
-
return modifiers.some((m) => m.kind ===
|
|
7868
|
+
return modifiers.some((m) => m.kind === ts19.SyntaxKind.ExportKeyword);
|
|
7796
7869
|
}
|
|
7797
7870
|
function getNodeName(node) {
|
|
7798
7871
|
if ("name" in node && node.name) {
|
|
@@ -7803,6 +7876,7 @@ function getNodeName(node) {
|
|
|
7803
7876
|
}
|
|
7804
7877
|
export {
|
|
7805
7878
|
zodAdapter,
|
|
7879
|
+
writtenTypeText,
|
|
7806
7880
|
withDescription,
|
|
7807
7881
|
withDeprecated,
|
|
7808
7882
|
validateSpec2 as validateSpec,
|
|
@@ -7908,6 +7982,7 @@ export {
|
|
|
7908
7982
|
detectTsRuntime,
|
|
7909
7983
|
deduplicateSchemas,
|
|
7910
7984
|
decoratePropertySchema,
|
|
7985
|
+
declaredTypeNode,
|
|
7911
7986
|
createProgram,
|
|
7912
7987
|
createDocs,
|
|
7913
7988
|
categorizeBreakingChanges,
|