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
26 lines (24 loc) 路 859 Bytes
/
Copy pathparseProperty.ts
File metadata and controls
26 lines (24 loc) 路 859 Bytes
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
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 """`
: "";
const definition = parseInlineType(
state,
state.typechecker.getTypeOfSymbol(symbol),
);
if (symbol.flags & ts.SymbolFlags.Optional) {
state.imports.add("NotRequired");
state.imports.add("Optional");
return `${name}: NotRequired[Optional[${definition}]]${documentationSuffix}`;
} else {
return `${name}: ${definition}${documentationSuffix}`;
}
};