Skip to content

Commit

Permalink
fix: Add support for import types
Browse files Browse the repository at this point in the history
Resolves #1453
  • Loading branch information
Gerrit0 committed Jan 5, 2021
1 parent 894f92f commit 37e8a3c
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 34 deletions.
18 changes: 18 additions & 0 deletions src/lib/converter/types.ts
Expand Up @@ -58,6 +58,7 @@ export function loadConverters() {
constructorConverter,
exprWithTypeArgsConverter,
functionTypeConverter,
importType,
indexedAccessConverter,
inferredConverter,
intersectionConverter,
Expand Down Expand Up @@ -343,6 +344,21 @@ const functionTypeConverter: TypeConverter<ts.FunctionTypeNode, ts.Type> = {
},
};

const importType: TypeConverter<ts.ImportTypeNode> = {
kind: [ts.SyntaxKind.ImportType],
convert(context, node) {
const name = node.qualifier?.getText() ?? "__module";
const symbol = context.checker.getSymbolAtLocation(node);
assert(symbol, "Missing symbol when converting import type node");
return new ReferenceType(name, symbol, context.project);
},
convertType(context, type) {
const symbol = type.getSymbol();
assert(symbol, "Missing symbol when converting import type"); // Should be a compiler error
return new ReferenceType("__module", symbol, context.project);
},
};

const indexedAccessConverter: TypeConverter<
ts.IndexedAccessTypeNode,
ts.IndexedAccessType
Expand Down Expand Up @@ -731,6 +747,8 @@ const literalTypeConverter: TypeConverter<
return new LiteralType(
BigInt(node.literal.getText().replace("n", ""))
);
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
return new LiteralType(node.literal.text);
}

return requestBugReport(context, node.literal);
Expand Down
11 changes: 10 additions & 1 deletion src/test/converter/exports/export.ts
Expand Up @@ -15,4 +15,13 @@ export default function (a: number) {}
import * as x from "./test.json";

/** @hidden */
const x2: string = x.issue;
export const x2: string = x.issue;

export namespace GH1453 {
export const Module = Mod;
export const TypedModule: typeof import("./mod") = Mod;
export const Member = Mod.a;
export const TypedMember: typeof import("./mod").a = Mod.a;

export type Foo = import("./mod").GH1453Helper;
}
2 changes: 2 additions & 0 deletions src/test/converter/exports/mod.ts
Expand Up @@ -36,3 +36,5 @@ export { Node } from "typescript";

// TS 3.8 namespace exports
export * as ThisModule from "./mod";

export type GH1453Helper = `1`;
208 changes: 175 additions & 33 deletions src/test/converter/exports/specs.json
Expand Up @@ -12,47 +12,157 @@
"flags": {},
"children": [
{
"id": 31,
"id": 39,
"name": "Mod",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 22
"target": 29
},
{
"id": 32,
"id": 40,
"name": "ModDefault",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 23
"target": 30
},
{
"id": 36,
"id": 44,
"name": "ThisModule",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 22
"target": 29
},
{
"id": 34,
"id": 42,
"name": "b",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 25
"target": 32
},
{
"id": 30,
"id": 38,
"name": "c",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 25
"target": 32
},
{
"id": 33,
"id": 23,
"name": "GH1453",
"kind": 2,
"kindString": "Namespace",
"flags": {},
"children": [
{
"id": 28,
"name": "Foo",
"kind": 4194304,
"kindString": "Type alias",
"flags": {},
"type": {
"type": "reference",
"id": 29,
"name": "GH1453Helper"
}
},
{
"id": 26,
"name": "Member",
"kind": 32,
"kindString": "Variable",
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": 1
},
"defaultValue": "..."
},
{
"id": 24,
"name": "Module",
"kind": 32,
"kindString": "Variable",
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"id": 29,
"name": "__module"
},
"defaultValue": "..."
},
{
"id": 27,
"name": "TypedMember",
"kind": 32,
"kindString": "Variable",
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"id": 29,
"name": "a"
},
"defaultValue": "..."
},
{
"id": 25,
"name": "TypedModule",
"kind": 32,
"kindString": "Variable",
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"id": 29,
"name": "__module"
},
"defaultValue": "..."
}
],
"groups": [
{
"title": "Type aliases",
"kind": 4194304,
"children": [
28
]
},
{
"title": "Variables",
"kind": 32,
"children": [
26,
24,
27,
25
]
}
]
},
{
"id": 45,
"name": "GH1453Helper",
"kind": 4194304,
"kindString": "Type alias",
"flags": {},
"type": {
"type": "literal",
"value": "1"
}
},
{
"id": 41,
"name": "a",
"kind": 32,
"kindString": "Variable",
Expand Down Expand Up @@ -151,18 +261,32 @@
"title": "References",
"kind": 16777216,
"children": [
31,
32,
36,
34,
30
39,
40,
44,
42,
38
]
},
{
"title": "Namespaces",
"kind": 2,
"children": [
23
]
},
{
"title": "Type aliases",
"kind": 4194304,
"children": [
45
]
},
{
"title": "Variables",
"kind": 32,
"children": [
33
41
]
},
{
Expand Down Expand Up @@ -353,38 +477,49 @@
]
},
{
"id": 22,
"id": 29,
"name": "mod",
"kind": 1,
"kindString": "Module",
"flags": {},
"children": [
{
"id": 29,
"id": 36,
"name": "ThisModule",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 22
"target": 29
},
{
"id": 26,
"id": 33,
"name": "b",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 25
"target": 32
},
{
"id": 27,
"id": 34,
"name": "c",
"kind": 16777216,
"kindString": "Reference",
"flags": {},
"target": 25
"target": 32
},
{
"id": 25,
"id": 37,
"name": "GH1453Helper",
"kind": 4194304,
"kindString": "Type alias",
"flags": {},
"type": {
"type": "literal",
"value": "1"
}
},
{
"id": 32,
"name": "a",
"kind": 32,
"kindString": "Variable",
Expand All @@ -401,14 +536,14 @@
"defaultValue": "1"
},
{
"id": 23,
"id": 30,
"name": "default",
"kind": 64,
"kindString": "Function",
"flags": {},
"signatures": [
{
"id": 24,
"id": 31,
"name": "default",
"kind": 4096,
"kindString": "Call signature",
Expand All @@ -429,23 +564,30 @@
"title": "References",
"kind": 16777216,
"children": [
29,
26,
27
36,
33,
34
]
},
{
"title": "Type aliases",
"kind": 4194304,
"children": [
37
]
},
{
"title": "Variables",
"kind": 32,
"children": [
25
32
]
},
{
"title": "Functions",
"kind": 64,
"children": [
23
30
]
}
]
Expand All @@ -460,7 +602,7 @@
1,
6,
8,
22
29
]
}
]
Expand Down

0 comments on commit 37e8a3c

Please sign in to comment.