Skip to content

FIXED: dynamic type imports #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, relative, resolve, extname } from "path";
import ts from "typescript";
import ts, { LiteralTypeNode } from "typescript";
import slash from "slash";
import { parse } from "url";
import { existsSync } from "fs";
Expand Down Expand Up @@ -109,19 +109,51 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
fileLiteral
]);
}

if (ts.isExternalModuleReference(node)) {
return unpathImportEqualsDeclaration(node);
}

if (ts.isImportDeclaration(node)) {
return unpathImportDeclaration(node);
}

if (ts.isExportDeclaration(node)) {
return unpathExportDeclaration(node);
}

if (ts.isImportTypeNode(node)) {
return unpathImportTypeNode(node);
}

return ts.visitEachChild(node, visit, context);
}

function unpathImportTypeNode(node: ts.ImportTypeNode) {
const argument = node.argument as LiteralTypeNode;

if (!ts.isStringLiteral(argument.literal)) {
return node;
}

const file = bindModuleToFile(argument.literal.text);

if (!file) {
return node;
}

const fileLiteral = ts.createLiteral(file);
const fileArgument = ts.updateLiteralTypeNode(argument, fileLiteral);

return ts.updateImportTypeNode(
node,
fileArgument,
node.qualifier,
node.typeArguments,
node.isTypeOf
);
}

function unpathImportEqualsDeclaration(node: ts.ExternalModuleReference) {
if (!ts.isStringLiteral(node.expression)) {
return node;
Expand Down
18 changes: 18 additions & 0 deletions tests/__fixtures/with-path/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import * as path from "path";
import * as b from "circular/a";
import * as c from "../circular/a";
import { myNative } from "@utils/utils.native";
import { Logger } from "@dynamic/logger";
import { LogLevel } from "@dynamic/logger-types";

type LoggerManager = import("@dynamic/manager").LoggerManager;

c.A;
b.A;
Expand All @@ -20,3 +24,17 @@ const n: NoRuntimecodeHere = null as any;

subs(2, 3);
const a = new A("");

const manager: LoggerManager = {
loggers: [
new Logger(),
new Logger(),
]
};

manager.loggers.forEach((logger) => {
logger.log({
level: LogLevel.DEBUG,
text: 'test',
})
})
9 changes: 9 additions & 0 deletions tests/__fixtures/with-path/dynamic/logger-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum LogLevel {
DEBUG = 1,
INFO = 2,
}

export interface Log {
level: LogLevel;
text: string;
}
10 changes: 10 additions & 0 deletions tests/__fixtures/with-path/dynamic/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Log = import("@dynamic/logger-types").Log;
type LogLevel = import("@dynamic/logger-types").LogLevel;

export class Logger {
level: LogLevel;

public log (x: Log): void {
console.log(x);
};
}
5 changes: 5 additions & 0 deletions tests/__fixtures/with-path/dynamic/manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Logger = import("@dynamic/logger").Logger;

export interface LoggerManager {
loggers: Array<Logger>;
}