-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparseProperty.ts
More file actions
83 lines (78 loc) · 2.6 KB
/
Copy pathparseProperty.ts
File metadata and controls
83 lines (78 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import ts from "typescript";
import { ParserState } from "./ParserState";
import { parseInlineType } from "./parseInlineType";
export const parseProperty = (state: ParserState, symbol: ts.Symbol) => {
const name = symbol.getName();
const documentation = symbol
.getDocumentationComment(state.typechecker)
.map((v) => v.text)
.join("\n");
const documentationSuffix = documentation
? `\n """\n ${documentation.replaceAll("\n", "\n ")}\n """`
: "";
if (symbol.flags & ts.SymbolFlags.Optional) {
state.imports.add("NotRequired");
const definition = parseInlineType(
state,
// since the entry is already options, the inner type can be non-nullable
state.typechecker.getNonNullableType(
state.typechecker.getTypeOfSymbol(symbol),
),
);
if (state.config.nullableOptionals) {
state.imports.add("Optional");
return `${name}: NotRequired[Optional[${definition}]]${documentationSuffix}`;
} else {
return `${name}: NotRequired[${definition}]${documentationSuffix}`;
}
} else {
const definition = parseInlineType(
state,
// since the entry is already options, the inner type can be non-nullable
state.typechecker.getTypeOfSymbol(symbol),
);
return `${name}: ${definition}${documentationSuffix}`;
}
};
export const parsePropertyForDict = (state: ParserState, symbol: ts.Symbol) => {
const name = JSON.stringify(symbol.getName());
if (symbol.flags & ts.SymbolFlags.Optional) {
state.imports.add("NotRequired");
const definition = parseInlineType(
state,
// since the entry is already options, the inner type can be non-nullable
state.typechecker.getNonNullableType(
state.typechecker.getTypeOfSymbol(symbol),
),
);
if (state.config.nullableOptionals) {
state.imports.add("Optional");
return `${name}: NotRequired[Optional[${definition}]]`;
} else {
return `${name}: NotRequired[${definition}]`;
}
} else {
const definition = parseInlineType(
state,
// since the entry is already options, the inner type can be non-nullable
state.typechecker.getTypeOfSymbol(symbol),
);
return `${name}: ${definition}`;
}
};
export const getDocumentationStringForDict = (
state: ParserState,
symbol: ts.Symbol,
) => {
const name = symbol.getName();
const documentation = symbol
.getDocumentationComment(state.typechecker)
.map((v) => v.text)
.filter((v) => !!v)
.join(" \n");
if (documentation.length > 0) {
return `\`${JSON.stringify(name)}\`: ${documentation}`;
} else {
return undefined;
}
};