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
Some cleanup and reorganization of code
  • Loading branch information
rbuckton committed May 11, 2015
commit 1f86bfa3416f62e71eb74eca00cf31a7b71f7f08
102 changes: 66 additions & 36 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ module ts {
let diagnostics: Diagnostic[] = [];
let jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, host, ".js");
emitDeclarations(host, resolver, diagnostics, jsFilePath, targetSourceFile);

let packageDeclaration = getPackageDeclaration(host);
let compilerOptions = host.getCompilerOptions();
if (packageDeclaration) {
if (!targetSourceFile || isPackageMain(targetSourceFile, host)) {
writePackageDeclarationFile(packageDeclaration, host, resolver, diagnostics);
}
}

return diagnostics;
}

Expand All @@ -42,7 +51,6 @@ module ts {
let compilerOptions = host.getCompilerOptions();
let languageVersion = compilerOptions.target || ScriptTarget.ES3;

let packageMainFile: string;
let write: (s: string) => void;
let writeLine: () => void;
let increaseIndent: () => void;
Expand All @@ -66,10 +74,30 @@ module ts {
let referencePathsOutput = "";

if (isPackageDeclaration) {
packageMainFile = host.getCanonicalFileName(normalizePath(combinePaths(host.getCurrentDirectory(), compilerOptions.packageMain)));
}
// Emitting a package declaration, so emit all of the source declarations
let emittedReferencedFiles: SourceFile[] = [];
for (let sourceFile of sortSourceFiles(host.getSourceFiles())) {
if (!isDeclarationFile(sourceFile)) {
// Check what references need to be added
if (!compilerOptions.noResolve) {
for (let fileReference of sourceFile.referencedFiles) {
let referencedFile = tryResolveScriptReference(host, sourceFile, fileReference);

if (root) {
// If the reference file is a declaration file or an external module, emit that reference
if (referencedFile && (isDeclarationFile(referencedFile) &&
!contains(emittedReferencedFiles, referencedFile))) { // If the file reference was not already emitted
writeReferencePath(referencedFile);
emittedReferencedFiles.push(referencedFile);
}
}
}

writeLine();
emitSourceFile(sourceFile);
}
}
}
else if (root) {
// Emitting just a single file, so emit references in this file only
if (!compilerOptions.noResolve) {
let addedGlobalFileReference = false;
Expand Down Expand Up @@ -109,8 +137,8 @@ module ts {
else {
// Emit references corresponding to this file
let emittedReferencedFiles: SourceFile[] = [];
for (let sourceFile of sortSourceFiles(host.getSourceFiles())) {
if (!isExternalModuleOrDeclarationFile(sourceFile) || (isPackageDeclaration && isExternalModule(sourceFile))) {
for (let sourceFile of host.getSourceFiles()) {
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
// Check what references need to be added
if (!compilerOptions.noResolve) {
for (let fileReference of sourceFile.referencedFiles) {
Expand Down Expand Up @@ -140,32 +168,28 @@ module ts {
}

function sortSourceFiles(sourceFiles: SourceFile[]) {
if (isPackageDeclaration) {
let indices = new Array<number>(sourceFiles.length);
for (let i = 0; i < sourceFiles.length; ++i) indices[i] = i;
indices.sort((left, right) => {
let leftFile = sourceFiles[left];
if (leftFile.fileName === packageMainFile) {
return -1;
}

let rightFile = sourceFiles[right];
if (rightFile.fileName === packageMainFile) {
return +1;
}

return left - right;
});

let sorted = new Array<SourceFile>(sourceFiles.length);
for (let i = 0; i < sourceFiles.length; ++i) {
sorted[i] = sourceFiles[indices[i]];
}

return sorted;
let indices = new Array<number>(sourceFiles.length);
for (let i = 0; i < sourceFiles.length; ++i) indices[i] = i;
indices.sort((left, right) => {
let leftFile = sourceFiles[left];
if (isPackageMain(leftFile, host)) {
return -1;
}

let rightFile = sourceFiles[right];
if (isPackageMain(rightFile, host)) {
return +1;
}

return left - right;
});

let sorted = new Array<SourceFile>(sourceFiles.length);
for (let i = 0; i < sourceFiles.length; ++i) {
sorted[i] = sourceFiles[indices[i]];
}

return sourceFiles;
return sorted;
}

function hasInternalAnnotation(range: CommentRange) {
Expand Down Expand Up @@ -1631,18 +1655,24 @@ module ts {
}

function getPackageQualifiedPath(host: EmitHost, moduleName: string, basePath: string) {
let compilerOptions = host.getCompilerOptions();
let modulePath = normalizePath(combinePaths(basePath, moduleName));
let packageRoot = getPackageDirectory(host);
let modulePath = combinePaths(basePath, moduleName);
let packageRelativePath = getRelativePathToDirectoryOrUrl(
host.getPackagePath(),
packageRoot,
modulePath,
host.getCurrentDirectory(),
host.getCanonicalFileName,
false);
if (host.getCanonicalFileName(packageRelativePath + ".ts") === host.getCanonicalFileName(compilerOptions.packageMain) ||
host.getCanonicalFileName(packageRelativePath + ".d.ts") === host.getCanonicalFileName(compilerOptions.packageMain)) {

let compilerOptions = host.getCompilerOptions();
let packageMain = getPackageMain(host);
let packageAbsolutePath = getNormalizedAbsolutePath(packageRelativePath, packageRoot);

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

return combinePaths(compilerOptions.packageName, packageRelativePath);
}

Expand Down
13 changes: 5 additions & 8 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
let diagnostics: Diagnostic[] = [];
let newLine = host.getNewLine();
let packageDeclaration = getPackageDeclaration(host);

if (targetSourceFile === undefined) {
forEach(host.getSourceFiles(), sourceFile => {
Expand All @@ -66,8 +67,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
emitFile(compilerOptions.out);
}

if (compilerOptions.packageMain && compilerOptions.packageName && compilerOptions.packageDeclaration) {
writePackageDeclarationFile(compilerOptions.packageDeclaration, host, resolver, diagnostics);
if (packageDeclaration) {
writePackageDeclarationFile(packageDeclaration, host, resolver, diagnostics);
}
}
else {
Expand All @@ -80,12 +81,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
emitFile(compilerOptions.out);
}

if (compilerOptions.packageMain && compilerOptions.packageName && compilerOptions.packageDeclaration) {
let packageMainPath = host.getCanonicalFileName(normalizePath(combinePaths(host.getCurrentDirectory(), compilerOptions.packageMain)));
if (targetSourceFile.fileName === packageMainPath) {
let packageDeclarationPath = host.getCanonicalFileName(normalizePath(combinePaths(host.getCurrentDirectory(), compilerOptions.packageDeclaration)));
writePackageDeclarationFile(packageDeclarationPath, host, resolver, diagnostics);
}
if (packageDeclaration && isPackageMain(targetSourceFile, host)) {
writePackageDeclarationFile(packageDeclaration, host, resolver, diagnostics);
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,13 @@ module ts {
return currentDirectory || (currentDirectory = sys.getCurrentDirectory());
}

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

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

return searchPath;
}

Expand All @@ -141,7 +137,7 @@ module ts {
getDefaultLibFileName: options => combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), getDefaultLibFileName(options)),
writeFile,
getCurrentDirectory,
getPackagePath,
getPackageDirectory,
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
getCanonicalFileName,
getNewLine: () => newLine
Expand Down Expand Up @@ -216,9 +212,9 @@ module ts {
getTypeChecker,
getDiagnosticsProducingTypeChecker,
getCommonSourceDirectory: () => commonSourceDirectory,
getPackagePath: () => host.getPackagePath(),
emit,
getCurrentDirectory: () => host.getCurrentDirectory(),
getPackageDirectory: host.getPackageDirectory ? () => host.getPackageDirectory() : () => host.getCurrentDirectory(),
getNodeCount: () => getDiagnosticsProducingTypeChecker().getNodeCount(),
getIdentifierCount: () => getDiagnosticsProducingTypeChecker().getIdentifierCount(),
getSymbolCount: () => getDiagnosticsProducingTypeChecker().getSymbolCount(),
Expand All @@ -227,19 +223,18 @@ module ts {
return program;

function getEmitHost(writeFileCallback?: WriteFileCallback): EmitHost {
let emitHost: EmitHost = {
return {
getCanonicalFileName: fileName => host.getCanonicalFileName(fileName),
getCommonSourceDirectory: program.getCommonSourceDirectory,
getCompilerOptions: program.getCompilerOptions,
getCurrentDirectory: () => host.getCurrentDirectory(),
getPackagePath: () => host.getPackagePath(emitHost),
getPackageDirectory: host.getPackageDirectory ? () => host.getPackageDirectory() : () => host.getCurrentDirectory(),
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
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ module ts {
getCompilerOptions(): CompilerOptions;
getSourceFile(fileName: string): SourceFile;
getCurrentDirectory(): string;
/*@internal*/ getPackageDirectory?(): string;
}

export interface ParseConfigHost {
Expand Down Expand Up @@ -1881,7 +1882,7 @@ module ts {
getCanonicalFileName(fileName: string): string;
useCaseSensitiveFileNames(): boolean;
getNewLine(): string;
/*@internal*/ getPackagePath(host?: EmitHost): string;
/*@internal*/ getPackageDirectory(): string;
}

export interface TextSpan {
Expand Down
31 changes: 30 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ module ts {
getCommonSourceDirectory(): string;
getCanonicalFileName(fileName: string): string;
getNewLine(): string;
/*@internal*/ getPackagePath(): string;

writeFile: WriteFileCallback;

Expand Down Expand Up @@ -1699,6 +1698,36 @@ module ts {
export function getLocalSymbolForExportDefault(symbol: Symbol) {
return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & NodeFlags.Default) ? symbol.valueDeclaration.localSymbol : undefined;
}

export function getPackageDirectory(host: ScriptReferenceHost) {
return host.getPackageDirectory ? host.getPackageDirectory() : host.getCurrentDirectory();
}

export function getPackageDeclaration(host: ScriptReferenceHost) {
let options = host.getCompilerOptions();
if (options.packageDeclaration) {
let packageDeclaration = getNormalizedAbsolutePath(options.packageDeclaration, getPackageDirectory(host));
return removeFileExtension(packageDeclaration) + ".d.ts";
}

return undefined;
}

export function getPackageMain(host: ScriptReferenceHost) {
let options = host.getCompilerOptions();
if (options.packageMain) {
let packageMain = getNormalizedAbsolutePath(options.packageMain, getPackageDirectory(host));
let packageFile = host.getSourceFile(packageMain);
return packageFile;
}

return undefined;
}

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

/**
* Replace each instance of non-ascii characters by one, two, three, or four escape sequences
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ module Harness {
getCanonicalFileName,
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
getNewLine: () => newLine,
getPackagePath: getCurrentDirectory
getPackageDirectory: getCurrentDirectory
};
}

Expand Down
1 change: 1 addition & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ module Harness.LanguageService {
var script = this.getScriptInfo(fileName);
return script ? script.version.toString() : undefined;
}
getPackageDirectory(): string { return ""; }

log(s: string): void { }
trace(s: string): void { }
Expand Down
2 changes: 1 addition & 1 deletion src/harness/projectsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class ProjectRunner extends RunnerBase {
getCanonicalFileName: Harness.Compiler.getCanonicalFileName,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getNewLine: () => ts.sys.newLine,
getPackagePath: getCurrentDirectory
getPackageDirectory: getCurrentDirectory
};
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ module ts {
getCancellationToken?(): CancellationToken;
getCurrentDirectory(): string;
getDefaultLibFileName(options: CompilerOptions): string;
/*@internal*/ getPackageDirectory?(): string;
log? (s: string): void;
trace? (s: string): void;
error? (s: string): void;
Expand Down Expand Up @@ -1794,7 +1795,7 @@ module ts {
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getPackagePath: () => "",
getPackageDirectory: () => "",
getNewLine: () => (sys && sys.newLine) || "\r\n"
};

Expand Down Expand Up @@ -2421,7 +2422,7 @@ module ts {
getDefaultLibFileName: (options) => host.getDefaultLibFileName(options),
writeFile: (fileName, data, writeByteOrderMark) => { },
getCurrentDirectory: () => host.getCurrentDirectory(),
getPackagePath: (emitHost?: EmitHost) => (emitHost && emitHost.getCommonSourceDirectory()) || host.getCurrentDirectory()
getPackageDirectory: () => host.getPackageDirectory ? host.getPackageDirectory() : host.getCurrentDirectory()
});

// Release any files we have acquired in the old program but are
Expand Down
9 changes: 9 additions & 0 deletions src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module ts {
getCurrentDirectory(): string;
getDefaultLibFileName(options: string): string;
getNewLine?(): string;
getPackageDirectory?(): string;
}

/** Public interface of the the of a config service shim instance.*/
Expand Down Expand Up @@ -310,6 +311,14 @@ module ts {
public getCurrentDirectory(): string {
return this.shimHost.getCurrentDirectory();
}

public getPackageDirectory(): string {
if (this.shimHost.getPackageDirectory) {
return this.shimHost.getPackageDirectory();
}

return this.getCurrentDirectory();
}

public getDefaultLibFileName(options: CompilerOptions): string {
// Wrap the API changes for 1.5 release. This try/catch
Expand Down