-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcanonicalTypeName.ts
More file actions
22 lines (21 loc) · 881 Bytes
/
Copy pathcanonicalTypeName.ts
File metadata and controls
22 lines (21 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { createNewParserState, ParserState } from "./ParserState";
import ts from "typescript";
import { parseTypeDefinition } from "./parseTypeDefinition";
/**
* A function that creates a unique string for a given interface or object type,
* from a fresh parser state. This should return the same string for two semantically
* identically types, allowing us to re-use existing helper types if the generated
* strings match.
**/
export const getCanonicalTypeName = (state: ParserState, type: ts.Type) => {
const cachedName = state.canonicalTypeNames.get(type);
if (cachedName) {
return cachedName;
} else {
const tmpState = createNewParserState(state.typechecker, state.config);
parseTypeDefinition(tmpState, "TS2PyTmpType", type);
const result = tmpState.statements.join("\n");
state.canonicalTypeNames.set(type, result);
return result;
}
};