forked from TheLartians/TypeScript2Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseProperty.ts
More file actions
36 lines (34 loc) 路 1.3 KB
/
Copy pathparseProperty.ts
File metadata and controls
36 lines (34 loc) 路 1.3 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
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}`;
}
};