diff --git a/index.ts b/index.ts index 1b19972..0a0d535 100644 --- a/index.ts +++ b/index.ts @@ -101,7 +101,7 @@ function getFilenames(baseDir: string, files: string[]): string[] { }); } -function processTree(sourceFile: ts.SourceFile, replacer: (node: ts.Node) => string): string { +function processTree(sourceFile: ts.SourceFile, replacer: (node: ts.Node, sourceFile: ts.SourceFile) => string): string { let code = ''; let cursorPosition = 0; @@ -117,7 +117,7 @@ function processTree(sourceFile: ts.SourceFile, replacer: (node: ts.Node) => str function visit(node: ts.Node) { readThrough(node); - const replacement = replacer(node); + const replacement = replacer(node, sourceFile); if (replacement != null) { code += replacement; @@ -136,7 +136,6 @@ function processTree(sourceFile: ts.SourceFile, replacer: (node: ts.Node) => str /** * Load and parse a TSConfig File - * @param options The dts-generator options to load config into * @param fileName The path to the file */ function getTSConfig(fileName: string): [string[], ts.CompilerOptions] { @@ -469,7 +468,7 @@ export default function generate(options: Options): Promise { output.write('declare module \'' + resolvedModuleId + '\' {' + eol + indent); - const content = processTree(declarationFile, function (node) { + const content = processTree(declarationFile, function (node, sourceFile) { if (isNodeKindExternalModuleReference(node)) { // TODO figure out if this branch is possible, and if so, write a test // that covers it. @@ -490,10 +489,24 @@ export default function generate(options: Options): Promise { ) { // This block of code is modifying the names of imported modules const text = node.text; + const resolved: string = resolveModuleImport(text); if (resolved) { return ` '${resolved}'`; } + } else if (isNodeKindExportDeclaration(node) || isNodeKindImportDeclaration(node)) { + if (node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) { + if (node.moduleSpecifier && node.moduleSpecifier.text && node.moduleSpecifier.text[0] === '.') { + if (node.moduleSpecifier && node.moduleSpecifier.text && node.moduleSpecifier.text[0] === '.') { + const absolutePath = pathUtil.posix.join(pathUtil.dirname(resolvedModuleId), node.moduleSpecifier.text); + const start = sourceFile.text.slice(node.pos, node.moduleSpecifier.pos); + const middle = sourceFile.text.slice(node.moduleSpecifier.pos, node.moduleSpecifier.end); + const end = sourceFile.text.slice(node.moduleSpecifier.end, node.end); + + return `${start}${middle.replace(node.moduleSpecifier.text, absolutePath)}${end}`; + } + } + } } });