Skip to content
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

Transform relative path imports with absolute path ones #124

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
21 changes: 17 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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] {
Expand Down Expand Up @@ -469,7 +468,7 @@ export default function generate(options: Options): Promise<void> {

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.
Expand All @@ -490,10 +489,24 @@ export default function generate(options: Options): Promise<void> {
) {
// 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}`;
}
}
}
}
});

Expand Down