Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added support for package paths in ImportEqualsDeclaration emit
  • Loading branch information
rbuckton committed May 13, 2015
commit 073da995ccd146a82399c9a8c6ff25c22c6ad4ac
46 changes: 25 additions & 21 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ module ts {
}
else {
write("require(");
writeTextOfNode(currentSourceFile, getExternalModuleImportEqualsDeclarationExpression(node));
emitModuleSpecifier(getExternalModuleImportEqualsDeclarationExpression(node));
write(");");
}
writer.writeLine();
Expand Down Expand Up @@ -741,10 +741,30 @@ module ts {
}
write(" from ");
}
writeTextOfNode(currentSourceFile, node.moduleSpecifier);
emitModuleSpecifier(node.moduleSpecifier);
write(";");
writer.writeLine();
}

function emitModuleSpecifier(node: Expression) {
if (isPackageDeclaration) {
let moduleNameText = (<LiteralExpression>node).text;
let searchPath = getDirectoryPath(currentSourceFile.fileName);
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (host.getSourceFile(searchName + ".ts") || host.getSourceFile(searchName + ".d.ts")) {
let packageQualifiedPath = getPackageQualifiedPath(host, moduleNameText, searchPath);
write("\"");
write(escapeString(packageQualifiedPath));
write("\"");
}
else {
writeTextOfNode(currentSourceFile, node);
}
}
else {
writeTextOfNode(currentSourceFile, node);
}
}

function emitImportOrExportSpecifier(node: ImportOrExportSpecifier) {
if (node.propertyName) {
Expand Down Expand Up @@ -777,23 +797,7 @@ module ts {
}
if (node.moduleSpecifier) {
write(" from ");
if (isPackageDeclaration) {
let moduleNameText = (<LiteralExpression>node.moduleSpecifier).text;
let searchPath = getDirectoryPath(currentSourceFile.fileName);
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (host.getSourceFile(searchName + ".ts") || host.getSourceFile(searchName + ".d.ts")) {
let packageQualifiedPath = getPackageQualifiedPath(host, moduleNameText, searchPath);
write("\"");
write(escapeString(packageQualifiedPath));
write("\"");
}
else {
writeTextOfNode(currentSourceFile, node.moduleSpecifier);
}
}
else {
writeTextOfNode(currentSourceFile, node.moduleSpecifier);
}
emitModuleSpecifier(node.moduleSpecifier);
}
write(";");
writer.writeLine();
Expand Down Expand Up @@ -1668,8 +1672,8 @@ module ts {
let packageMain = getPackageMain(host);
let packageAbsolutePath = getNormalizedAbsolutePath(packageRelativePath, packageRoot);

if (host.getSourceFile(packageAbsolutePath + ".ts") === packageMain ||
host.getSourceFile(packageAbsolutePath + ".d.ts") === packageMain) {
if (packageAbsolutePath + ".ts" === packageMain ||
packageAbsolutePath + ".d.ts" === packageMain) {
return compilerOptions.packageName;
}

Expand Down
14 changes: 9 additions & 5 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,12 @@ module ts {
}

export function getPackageDirectory(host: ScriptReferenceHost) {
return host.getPackageDirectory ? host.getPackageDirectory() : host.getCurrentDirectory();
let searchPath = host.getCurrentDirectory();
if (host.getPackageDirectory) {
return getNormalizedAbsolutePath(host.getPackageDirectory(), searchPath);
}

return searchPath;
}

export function getPackageDeclaration(host: ScriptReferenceHost) {
Expand All @@ -1717,16 +1722,15 @@ module ts {
let options = host.getCompilerOptions();
if (options.packageMain) {
let packageMain = getNormalizedAbsolutePath(options.packageMain, getPackageDirectory(host));
let packageFile = host.getSourceFile(packageMain);
return packageFile;
return packageMain;
}

return undefined;
}

export function isPackageMain(node: SourceFile, host: ScriptReferenceHost) {
let packageMain = getPackageMain(host);
return node === packageMain;
let packageMain = getPackageMain(host);
return node.fileName === packageMain;
}

/**
Expand Down