Skip to content

Commit d702073

Browse files
committed
WIP: Reduce usage of node APIs in models
Ref: #2528
1 parent de7d448 commit d702073

File tree

14 files changed

+85
-57
lines changed

14 files changed

+85
-57
lines changed

eslint.config.mjs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,33 @@ export default tslint.config(
196196
],
197197
},
198198
},
199+
// {
200+
// files: ["src/lib/models/**/*.ts"],
201+
// rules: {
202+
// "no-restricted-imports": [
203+
// "error",
204+
// {
205+
// paths: nodeModules,
206+
// patterns: [
207+
// "node:*",
208+
// "../*",
209+
// "!../FileRegistry.js",
210+
// "!../types.js",
211+
// "!../comments/index.js",
212+
// "!../sources/file.js",
213+
// ],
214+
// },
215+
// ],
216+
// },
217+
// },
199218
{
200219
files: ["src/lib/utils-common/**/*.ts"],
201220
rules: {
202221
"no-restricted-imports": [
203222
"error",
204223
{
205224
paths: nodeModules,
206-
patterns: ["../*"],
225+
patterns: ["node:*", "../*"],
207226
},
208227
],
209228
},

src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,11 @@ export * as Models from "./lib/models/index.js";
3030
*/
3131
export {
3232
type CommentParserConfig,
33-
type ComponentPath,
3433
Context,
3534
Converter,
3635
type ConverterEvents,
37-
type DeclarationReference,
3836
type ExternalResolveResult,
3937
type ExternalSymbolResolver,
40-
type Meaning,
41-
type MeaningKeyword,
42-
type SymbolReference,
4338
} from "./lib/converter/index.js";
4439
export * from "./lib/models/index.js";
4540
export * as Configuration from "./lib/utils/options/index.js";
@@ -121,7 +116,17 @@ export type {
121116
ValidationOptions,
122117
} from "./lib/utils/index.js";
123118

124-
export { type EnumKeys, EventDispatcher, EventHooks, JSX } from "#utils";
119+
export {
120+
type ComponentPath,
121+
type DeclarationReference,
122+
type EnumKeys,
123+
EventDispatcher,
124+
EventHooks,
125+
JSX,
126+
type MeaningKeyword,
127+
MeaningKeywords,
128+
type SymbolReference,
129+
} from "#utils";
125130

126131
export {
127132
type Deserializable,

src/lib/converter/comments/declarationReferenceResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ReflectionKind,
1010
} from "../../models/index.js";
1111
import { assertNever, filterMap } from "#utils";
12-
import type { ComponentPath, DeclarationReference, Meaning, MeaningKeyword } from "./declarationReference.js";
12+
import type { ComponentPath, DeclarationReference, Meaning, MeaningKeyword } from "#utils";
1313

1414
function resolveReferenceReflection(ref: Reflection): Reflection {
1515
if (ref instanceof ReferenceReflection) {

src/lib/converter/comments/linkResolver.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import {
88
ReflectionKind,
99
ReflectionSymbolId,
1010
} from "../../models/index.js";
11-
import { type DeclarationReference, parseDeclarationReference } from "./declarationReference.js";
1211
import { resolveDeclarationReference } from "./declarationReferenceResolver.js";
13-
import { maxElementByScore } from "#utils";
12+
import { type DeclarationReference, maxElementByScore, parseDeclarationReference } from "#utils";
1413

1514
const urlPrefix = /^(http|ftp)s?:\/\//;
1615

src/lib/converter/converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
resolveLinks,
3737
resolvePartLinks,
3838
} from "./comments/linkResolver.js";
39-
import { type DeclarationReference, meaningToString } from "./comments/declarationReference.js";
39+
import { type DeclarationReference, meaningToString } from "#utils";
4040
import { basename, dirname, resolve } from "path";
4141
import type { FileRegistry } from "../models/FileRegistry.js";
4242

src/lib/converter/factories/types.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import ts from "typescript";
2+
3+
import type { Context } from "../context.js";
4+
import { ReferenceType } from "../../models/types.js";
5+
import { ReflectionSymbolId } from "../../models/index.js";
6+
import { findPackageForPath, getQualifiedName } from "#node-utils";
7+
8+
export function createSymbolReference(
9+
symbol: ts.Symbol,
10+
context: Context,
11+
name?: string,
12+
) {
13+
const ref = ReferenceType.createUnresolvedReference(
14+
name ?? symbol.name,
15+
new ReflectionSymbolId(symbol),
16+
context.project,
17+
getQualifiedName(symbol, name ?? symbol.name),
18+
);
19+
ref.refersToTypeParameter = !!(
20+
symbol.flags & ts.SymbolFlags.TypeParameter
21+
);
22+
23+
const symbolPath = symbol.declarations?.[0]?.getSourceFile().fileName;
24+
if (!symbolPath) return ref;
25+
26+
ref.package = findPackageForPath(symbolPath);
27+
return ref;
28+
}

src/lib/converter/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
export type {
2-
ComponentPath,
3-
DeclarationReference,
4-
Meaning,
5-
MeaningKeyword,
6-
SymbolReference,
7-
} from "./comments/declarationReference.js";
81
export type { CommentParserConfig } from "./comments/index.js";
92
export type { ExternalResolveResult, ExternalSymbolResolver } from "./comments/linkResolver.js";
103
export { Context } from "./context.js";

src/lib/converter/plugins/InheritDocPlugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { ConverterComponent } from "../components.js";
1010
import type { Context } from "../context.js";
1111
import type { Reflection } from "../../models/reflections/abstract.js";
1212
import { Option, type ValidationOptions } from "../../utils/index.js";
13-
import { DefaultMap, zip } from "#utils";
14-
import { parseDeclarationReference } from "../comments/declarationReference.js";
13+
import { DefaultMap, parseDeclarationReference, zip } from "#utils";
1514
import { resolveDeclarationReference } from "../comments/declarationReferenceResolver.js";
1615
import { ApplicationEvents } from "../../application-events.js";
1716
import { ConverterEvents } from "../converter-events.js";

src/lib/converter/types.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { convertSymbol } from "./symbols.js";
3737
import { isObjectType, isTypeReference } from "./utils/nodes.js";
3838
import { removeUndefined } from "./utils/reflections.js";
3939
import type { TranslatedString } from "../internationalization/internationalization.js";
40+
import { createSymbolReference } from "./factories/types.js";
4041

4142
export interface TypeConverter<
4243
TNode extends ts.TypeNode = ts.TypeNode,
@@ -340,7 +341,7 @@ const exprWithTypeArgsConverter: TypeConverter<
340341
);
341342
}
342343
const parameters = node.typeArguments?.map((type) => convertType(context, type)) ?? [];
343-
const ref = ReferenceType.createSymbolReference(
344+
const ref = createSymbolReference(
344345
context.resolveAliasedSymbol(targetSymbol),
345346
context,
346347
);
@@ -439,7 +440,7 @@ const importType: TypeConverter<ts.ImportTypeNode> = {
439440
return new IntrinsicType("any");
440441
}
441442

442-
return ReferenceType.createSymbolReference(
443+
return createSymbolReference(
443444
context.resolveAliasedSymbol(symbol),
444445
context,
445446
name,
@@ -448,7 +449,7 @@ const importType: TypeConverter<ts.ImportTypeNode> = {
448449
convertType(context, type) {
449450
const symbol = type.getSymbol();
450451
assert(symbol, "Missing symbol when converting import type"); // Should be a compiler error
451-
return ReferenceType.createSymbolReference(
452+
return createSymbolReference(
452453
context.resolveAliasedSymbol(symbol),
453454
context,
454455
"__module",
@@ -710,7 +711,7 @@ const queryConverter: TypeConverter<ts.TypeQueryNode> = {
710711
);
711712
}
712713

713-
const ref = ReferenceType.createSymbolReference(
714+
const ref = createSymbolReference(
714715
context.resolveAliasedSymbol(querySymbol),
715716
context,
716717
node.exprName.getText(),
@@ -730,7 +731,7 @@ const queryConverter: TypeConverter<ts.TypeQueryNode> = {
730731
)
731732
}. This is a bug.`,
732733
);
733-
const ref = ReferenceType.createSymbolReference(
734+
const ref = createSymbolReference(
734735
context.resolveAliasedSymbol(symbol),
735736
context,
736737
);
@@ -780,7 +781,7 @@ const referenceConverter: TypeConverter<
780781

781782
const name = node.typeName.getText();
782783

783-
const ref = ReferenceType.createSymbolReference(
784+
const ref = createSymbolReference(
784785
context.resolveAliasedSymbol(symbol),
785786
context,
786787
name,
@@ -828,7 +829,7 @@ const referenceConverter: TypeConverter<
828829
name = node.typeName.right.text;
829830
}
830831

831-
const ref = ReferenceType.createSymbolReference(
832+
const ref = createSymbolReference(
832833
context.resolveAliasedSymbol(symbol),
833834
context,
834835
name,

src/lib/models/reflections/ReflectionSymbolId.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ts from "typescript";
44
import type { JSONOutput, Serializer } from "../../serialization/index.js";
55
import { findPackageForPath, getCommonDirectory, getQualifiedName, normalizePath, readFile } from "#node-utils";
66
import { Validation } from "#utils";
7-
import type { DeclarationReference } from "../../converter/index.js";
7+
import type { DeclarationReference } from "#utils";
88
import { splitUnquotedString } from "./utils.js";
99

1010
/**

src/lib/models/types.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import ts from "typescript";
2-
import type { Context } from "../converter/index.js";
31
import type { Reflection } from "./reflections/abstract.js";
42
import type { DeclarationReflection } from "./reflections/declaration.js";
53
import type { ProjectReflection } from "./reflections/project.js";
64
import type { Deserializer, JSONOutput, Serializer } from "../serialization/index.js";
7-
import { getQualifiedName } from "../utils/tsutils.js";
85
import { ReflectionSymbolId } from "./reflections/ReflectionSymbolId.js";
9-
import type { DeclarationReference } from "../converter/comments/declarationReference.js";
10-
import { findPackageForPath } from "../utils/fs.js";
6+
import type { DeclarationReference } from "#utils";
117
import { ReflectionKind } from "./reflections/kind.js";
128
import { Comment, type CommentDisplayPart } from "./comments/index.js";
139
import { joinArray } from "#utils";
@@ -940,6 +936,15 @@ export class ReferenceType extends Type {
940936
this.qualifiedName = qualifiedName;
941937
}
942938

939+
static createUnresolvedReference(
940+
name: string,
941+
target: ReflectionSymbolId,
942+
project: ProjectReflection,
943+
qualifiedName: string,
944+
) {
945+
return new ReferenceType(name, target, project, qualifiedName);
946+
}
947+
943948
static createResolvedReference(
944949
name: string,
945950
target: Reflection | number,
@@ -948,28 +953,6 @@ export class ReferenceType extends Type {
948953
return new ReferenceType(name, target, project, name);
949954
}
950955

951-
static createSymbolReference(
952-
symbol: ts.Symbol,
953-
context: Context,
954-
name?: string,
955-
) {
956-
const ref = new ReferenceType(
957-
name ?? symbol.name,
958-
new ReflectionSymbolId(symbol),
959-
context.project,
960-
getQualifiedName(symbol, name ?? symbol.name),
961-
);
962-
ref.refersToTypeParameter = !!(
963-
symbol.flags & ts.SymbolFlags.TypeParameter
964-
);
965-
966-
const symbolPath = symbol.declarations?.[0]?.getSourceFile().fileName;
967-
if (!symbolPath) return ref;
968-
969-
ref.package = findPackageForPath(symbolPath);
970-
return ref;
971-
}
972-
973956
/**
974957
* This is used for type parameters, which don't actually point to something,
975958
* and also for temporary references which will be cleaned up with real references

src/lib/converter/comments/declarationReference.ts renamed to src/lib/utils-common/declarationReference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @module
77
*/
88

9-
import type { Chars } from "#utils";
9+
import type { Chars } from "./string.js";
1010

1111
export const MeaningKeywords = [
1212
"class", // SymbolFlags.Class

src/lib/utils-common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// within the models export which is suitable for bundling.
33

44
export * from "./array.js";
5+
export * from "./declarationReference.js";
56
export * from "./enum.js";
67
export * from "./events.js";
78
export * from "./general.js";

src/test/declarationReference.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
parseModuleSource,
88
parseString,
99
parseSymbolReference,
10-
} from "../lib/converter/comments/declarationReference.js";
10+
} from "#utils";
1111

1212
describe("Declaration References", () => {
1313
describe("String parsing", () => {

0 commit comments

Comments
 (0)