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
Updated format for submodule urls in a package
  • Loading branch information
rbuckton committed May 8, 2015
commit 03b5cc36ce3b955b911830a11384a5e29c5c52c1
10 changes: 7 additions & 3 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1632,17 +1632,21 @@ module ts {
referencePathsOutput += "/// <reference path=\"" + declFileName + "\" />" + newLine;
}
}

function getPackageQualifiedPath(host: EmitHost, moduleName: string, basePath: string) {
let compilerOptions = host.getCompilerOptions();
let modulePath = normalizePath(combinePaths(basePath, moduleName));
let packageRelativePath = getRelativePathToDirectoryOrUrl(
getDirectoryPath(host.getCanonicalFileName(compilerOptions.packageMain)),
host.getPackagePath(),
modulePath,
host.getCurrentDirectory(),
host.getCanonicalFileName,
false);
return `package://${compilerOptions.packageName}/${packageRelativePath}`;
if (host.getCanonicalFileName(packageRelativePath + ".ts") === host.getCanonicalFileName(compilerOptions.packageMain) ||
host.getCanonicalFileName(packageRelativePath + ".d.ts") === host.getCanonicalFileName(compilerOptions.packageMain)) {
return compilerOptions.packageName;
}
return combinePaths(compilerOptions.packageName, packageRelativePath);
}

function getDeclarationOutput(synchronousDeclarationOutput: string, moduleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[]) {
Expand Down
47 changes: 44 additions & 3 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ module ts {
}
return undefined;
}

export function findPackageFile(searchPath: string): string {
let fileName = "package.json";
while (true) {
if (sys.fileExists(fileName)) {
return fileName;
}

let parentPath = getDirectoryPath(searchPath);
if (parentPath === searchPath) {
break;
}

searchPath = parentPath;
fileName = "../" + fileName;
}

return undefined;
}

export function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost {
let currentDirectory: string;
Expand Down Expand Up @@ -93,6 +112,24 @@ module ts {
}
}
}

function getCurrentDirectory(): string {
return currentDirectory || (currentDirectory = sys.getCurrentDirectory());
}

function getPackagePath(host?: EmitHost): string {
let searchPath = getCurrentDirectory();
let packageFile = findPackageFile(searchPath);
if (packageFile) {
return getDirectoryPath(normalizePath(packageFile));
}

if (host) {
return host.getCommonSourceDirectory();
}

return searchPath;
}

let newLine =
options.newLine === NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
Expand All @@ -103,7 +140,8 @@ module ts {
getSourceFile,
getDefaultLibFileName: options => combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), getDefaultLibFileName(options)),
writeFile,
getCurrentDirectory: () => currentDirectory || (currentDirectory = sys.getCurrentDirectory()),
getCurrentDirectory,
getPackagePath,
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
getCanonicalFileName,
getNewLine: () => newLine
Expand Down Expand Up @@ -178,6 +216,7 @@ module ts {
getTypeChecker,
getDiagnosticsProducingTypeChecker,
getCommonSourceDirectory: () => commonSourceDirectory,
getPackagePath: () => host.getPackagePath(),
emit,
getCurrentDirectory: () => host.getCurrentDirectory(),
getNodeCount: () => getDiagnosticsProducingTypeChecker().getNodeCount(),
Expand All @@ -186,19 +225,21 @@ module ts {
getTypeCount: () => getDiagnosticsProducingTypeChecker().getTypeCount(),
};
return program;

function getEmitHost(writeFileCallback?: WriteFileCallback): EmitHost {
return {
let emitHost: EmitHost = {
getCanonicalFileName: fileName => host.getCanonicalFileName(fileName),
getCommonSourceDirectory: program.getCommonSourceDirectory,
getCompilerOptions: program.getCompilerOptions,
getCurrentDirectory: () => host.getCurrentDirectory(),
getPackagePath: () => host.getPackagePath(emitHost),
getNewLine: () => host.getNewLine(),
getSourceFile: program.getSourceFile,
getSourceFiles: program.getSourceFiles,
writeFile: writeFileCallback || (
(fileName, data, writeByteOrderMark, onError) => host.writeFile(fileName, data, writeByteOrderMark, onError)),
};
return emitHost;
}

function getDiagnosticsProducingTypeChecker() {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,7 @@ module ts {
getCanonicalFileName(fileName: string): string;
useCaseSensitiveFileNames(): boolean;
getNewLine(): string;
/*@internal*/ getPackagePath(host?: EmitHost): string;
}

export interface TextSpan {
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ module ts {
getCommonSourceDirectory(): string;
getCanonicalFileName(fileName: string): string;
getNewLine(): string;
/*@internal*/ getPackagePath(): string;

writeFile: WriteFileCallback;

}

// Pool writers to avoid needing to allocate them for every symbol we write.
Expand Down