From 57befaefcf1ad034900fab9b86b553967eedf200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20HENAFF?= Date: Thu, 11 Apr 2019 00:51:57 +0200 Subject: [PATCH 1/3] Replace relative paths with absolute --- index.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 1b19972..d03d9bc 100644 --- a/index.ts +++ b/index.ts @@ -48,6 +48,9 @@ export interface Options { // declare some constants so we don't have magic integers without explanation const DTSLEN = '.d.ts'.length; +// used to find relative paths to replace with their absolute +const relativePathsRegex = /(?:from\s+|import\s+|require\()['"]((?:\.|\.\.)\/[^'"]*)['"]/g; + const filenameToMid: (filename: string) => string = (function () { if (pathUtil.sep === '/') { return function (filename: string) { @@ -136,7 +139,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] { @@ -497,7 +499,19 @@ export default function generate(options: Options): Promise { } }); - output.write(content.replace(nonEmptyLineStart, '$&' + indent)); + let match: RegExpExecArray; + let resultContent: string = content; + while ((match = relativePathsRegex.exec(content)) != null) { + const relativePath = match[1]; + const absolutePath = pathUtil.posix.join(pathUtil.dirname(resolvedModuleId), match[1]); + + // replace relative paths with absolute + resultContent = resultContent + .replace(`'${relativePath}'`, `'${absolutePath}'`) + .replace(`"${relativePath}"`, `"${absolutePath}"`); + } + + output.write(resultContent.replace(nonEmptyLineStart, '$&' + indent)); output.write(eol + '}' + eol); } else { From a3d528cd8efb5ad729d2dd37fdabf9c483fd4e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20HENAFF?= Date: Thu, 11 Apr 2019 01:16:33 +0200 Subject: [PATCH 2/3] better regular expression absolute path transform --- index.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/index.ts b/index.ts index d03d9bc..40486c5 100644 --- a/index.ts +++ b/index.ts @@ -49,7 +49,7 @@ export interface Options { const DTSLEN = '.d.ts'.length; // used to find relative paths to replace with their absolute -const relativePathsRegex = /(?:from\s+|import\s+|require\()['"]((?:\.|\.\.)\/[^'"]*)['"]/g; +const relativePathsRegex = /((?:from\s+|import\s+|require\()['"])((?:\.|\.\.)\/[^'"]*)(['"])/g; const filenameToMid: (filename: string) => string = (function () { if (pathUtil.sep === '/') { @@ -499,19 +499,12 @@ export default function generate(options: Options): Promise { } }); - let match: RegExpExecArray; - let resultContent: string = content; - while ((match = relativePathsRegex.exec(content)) != null) { - const relativePath = match[1]; - const absolutePath = pathUtil.posix.join(pathUtil.dirname(resolvedModuleId), match[1]); - - // replace relative paths with absolute - resultContent = resultContent - .replace(`'${relativePath}'`, `'${absolutePath}'`) - .replace(`"${relativePath}"`, `"${absolutePath}"`); - } + const absolutePathContent = content.replace(relativePathsRegex, (match, p1, p2, p3) => { + const absolutePath = pathUtil.posix.join(pathUtil.dirname(resolvedModuleId), p2); + return `${p1}${absolutePath}${p3}` + }); - output.write(resultContent.replace(nonEmptyLineStart, '$&' + indent)); + output.write(absolutePathContent.replace(nonEmptyLineStart, '$&' + indent)); output.write(eol + '}' + eol); } else { From 19ea5bd16d18e35eb3a82d51c2ed76046a780d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20HENAFF?= Date: Thu, 11 Apr 2019 14:26:53 +0200 Subject: [PATCH 3/3] simplify abolute path by doing it in the processTree node browsing --- index.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/index.ts b/index.ts index 40486c5..0a0d535 100644 --- a/index.ts +++ b/index.ts @@ -48,9 +48,6 @@ export interface Options { // declare some constants so we don't have magic integers without explanation const DTSLEN = '.d.ts'.length; -// used to find relative paths to replace with their absolute -const relativePathsRegex = /((?:from\s+|import\s+|require\()['"])((?:\.|\.\.)\/[^'"]*)(['"])/g; - const filenameToMid: (filename: string) => string = (function () { if (pathUtil.sep === '/') { return function (filename: string) { @@ -104,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; @@ -120,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; @@ -471,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. @@ -492,19 +489,28 @@ 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}`; + } + } + } } }); - const absolutePathContent = content.replace(relativePathsRegex, (match, p1, p2, p3) => { - const absolutePath = pathUtil.posix.join(pathUtil.dirname(resolvedModuleId), p2); - return `${p1}${absolutePath}${p3}` - }); - - output.write(absolutePathContent.replace(nonEmptyLineStart, '$&' + indent)); + output.write(content.replace(nonEmptyLineStart, '$&' + indent)); output.write(eol + '}' + eol); } else {