Skip to content

Commit e4626f7

Browse files
committed
refactor(complex-types): avoid passing isPropertyBlacklisted to every functions
1 parent c51993f commit e4626f7

File tree

1 file changed

+14
-36
lines changed

1 file changed

+14
-36
lines changed

packages/complex-types/src/core/printer.ts

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import ts from "typescript";
33
import { escapeQuotes } from "./utils";
44

55
export class Printer {
6+
private isPropertyBlacklisted: (prop: string) => boolean = () => false;
7+
68
constructor(private checker: ts.TypeChecker) {}
79

810
private typeToString(type: ts.Type): string {
@@ -16,14 +18,11 @@ export class Printer {
1618
private printUnionOrIntersection(
1719
type: ts.UnionOrIntersectionType,
1820
separator: string,
19-
isPropertyBlacklisted: (prop: string) => boolean,
2021
inner: boolean,
2122
): string {
2223
return [
2324
...new Set(
24-
type.types
25-
.map((t) => this.printType(t, isPropertyBlacklisted, inner))
26-
.filter(Boolean),
25+
type.types.map((t) => this.printType(t, inner)).filter(Boolean),
2726
),
2827
].join(separator);
2928
}
@@ -32,17 +31,13 @@ export class Printer {
3231
return !!(symbol.flags & ts.SymbolFlags.Optional);
3332
}
3433

35-
private printConditionType(
36-
type: ts.ConditionalType,
37-
isPropertyBlacklisted: (prop: string) => boolean,
38-
inner: boolean,
39-
): string {
34+
private printConditionType(type: ts.ConditionalType, inner: boolean): string {
4035
const decl = type.root.node;
4136
const { trueType, falseType } = decl;
4237
const trueTypeNode = this.checker.getTypeAtLocation(trueType);
4338
const falseTypeNode = this.checker.getTypeAtLocation(falseType);
4439

45-
return `${this.printType(trueTypeNode, isPropertyBlacklisted, inner)} | ${this.printType(falseTypeNode, isPropertyBlacklisted, inner)}`;
40+
return `${this.printType(trueTypeNode, inner)} | ${this.printType(falseTypeNode, inner)}`;
4641
}
4742

4843
private printPrimitiveType(type: ts.Type): string {
@@ -65,25 +60,11 @@ export class Printer {
6560
return "";
6661
}
6762

68-
private printType(
69-
type: ts.Type,
70-
isPropertyBlacklisted: (prop: string) => boolean,
71-
inner = false,
72-
): string {
63+
private printType(type: ts.Type, inner = false): string {
7364
if (type.isUnion()) {
74-
return this.printUnionOrIntersection(
75-
type,
76-
" | ",
77-
isPropertyBlacklisted,
78-
inner,
79-
);
65+
return this.printUnionOrIntersection(type, " | ", inner);
8066
} else if (type.isIntersection()) {
81-
return this.printUnionOrIntersection(
82-
type,
83-
" & ",
84-
isPropertyBlacklisted,
85-
inner,
86-
);
67+
return this.printUnionOrIntersection(type, " & ", inner);
8768
}
8869
if (this.checker.isArrayType(type)) {
8970
return "Array";
@@ -108,12 +89,12 @@ export class Printer {
10889
for (const prop of properties) {
10990
const propType = this.checker.getTypeOfSymbol(prop);
11091
const name = prop.getName();
111-
if (isPropertyBlacklisted(name)) {
92+
if (this.isPropertyBlacklisted(name)) {
11293
continue;
11394
}
11495

11596
props[name] = {
116-
value: this.printType(propType, isPropertyBlacklisted, true),
97+
value: this.printType(propType, true),
11798
isOptional: this.isSymbolOptional(prop),
11899
};
119100
}
@@ -139,11 +120,7 @@ export class Printer {
139120
} else if (type.flags & ts.TypeFlags.Undefined) {
140121
return "";
141122
} else if (type.flags & ts.TypeFlags.Conditional) {
142-
return this.printConditionType(
143-
type as ts.ConditionalType,
144-
isPropertyBlacklisted,
145-
inner,
146-
);
123+
return this.printConditionType(type as ts.ConditionalType, inner);
147124
} else if (type.isTypeParameter()) {
148125
const symbol = type.getSymbol();
149126
const decl = symbol?.declarations?.[0];
@@ -156,7 +133,7 @@ export class Printer {
156133
}
157134
const refType = this.checker.getTypeAtLocation(ref);
158135

159-
return this.printType(refType, isPropertyBlacklisted, inner);
136+
return this.printType(refType, inner);
160137
}
161138

162139
return this.typeToString(type);
@@ -166,9 +143,10 @@ export class Printer {
166143
node: ts.Node,
167144
isPropertyBlacklisted: (prop: string) => boolean,
168145
): string {
146+
this.isPropertyBlacklisted = isPropertyBlacklisted;
169147
const type = this.checker.getTypeAtLocation(node);
170148

171-
return this.printType(type, isPropertyBlacklisted);
149+
return this.printType(type);
172150
}
173151

174152
private printEventsByCallSignatures(

0 commit comments

Comments
 (0)