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
Cleanup, bug fixes
  • Loading branch information
rbuckton committed May 9, 2015
commit 895a096ad554c9400963059ddb2018bf7a5d47c5
43 changes: 20 additions & 23 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ module ts {
return diagnostics;
}

function emitDeclarations(host: EmitHost, resolver: EmitResolver, diagnostics: Diagnostic[], jsFilePath: string, root?: SourceFile): DeclarationEmit {
function emitDeclarations(host: EmitHost, resolver: EmitResolver, diagnostics: Diagnostic[], jsFilePath: string, root?: SourceFile, isPackageDeclaration?: boolean): DeclarationEmit {
let newLine = host.getNewLine();
let compilerOptions = host.getCompilerOptions();
let languageVersion = compilerOptions.target || ScriptTarget.ES3;

let isPackage = (compilerOptions.packageMain && compilerOptions.packageName && compilerOptions.packageDeclaration) != undefined;
let packageMainFile: string;
let write: (s: string) => void;
let writeLine: () => void;
Expand All @@ -66,8 +65,8 @@ module ts {
// and we could be collecting these paths from multiple files into single one with --out option
let referencePathsOutput = "";

if (isPackage) {
packageMainFile = host.getCanonicalFileName(compilerOptions.packageMain);
if (isPackageDeclaration) {
packageMainFile = host.getCanonicalFileName(normalizePath(combinePaths(host.getCurrentDirectory(), compilerOptions.packageMain)));
}

if (root) {
Expand Down Expand Up @@ -111,7 +110,7 @@ module ts {
// Emit references corresponding to this file
let emittedReferencedFiles: SourceFile[] = [];
for (let sourceFile of sortSourceFiles(host.getSourceFiles())) {
if (!isExternalModuleOrDeclarationFile(sourceFile) || (isPackage && isExternalModule(sourceFile))) {
if (!isExternalModuleOrDeclarationFile(sourceFile) || (isPackageDeclaration && isExternalModule(sourceFile))) {
// Check what references need to be added
if (!compilerOptions.noResolve) {
for (let fileReference of sourceFile.referencedFiles) {
Expand All @@ -123,13 +122,13 @@ module ts {

writeReferencePath(referencedFile);
emittedReferencedFiles.push(referencedFile);
}
}
}
}

writeLine();
emitSourceFile(sourceFile);
}
}
}
}

Expand All @@ -141,7 +140,7 @@ module ts {
}

function sortSourceFiles(sourceFiles: SourceFile[]) {
if (isPackage) {
if (isPackageDeclaration) {
let indices = new Array<number>(sourceFiles.length);
for (let i = 0; i < sourceFiles.length; ++i) indices[i] = i;
indices.sort((left, right) => {
Expand Down Expand Up @@ -476,24 +475,20 @@ module ts {
currentSourceFile = node;
enclosingDeclaration = node;

if (isPackage) {
if (isPackageDeclaration && isExternalModule(node)) {
// compute file name relative to main
let packageQualifiedModuleName: string;
if (packageMainFile === node.fileName) {
packageQualifiedModuleName = compilerOptions.packageName;
}
else {
let sourcePath = removeFileExtension(node.fileName);
packageQualifiedModuleName = getPackageQualifiedPath(host, sourcePath, ".");
}
write(`declare module "${packageQualifiedModuleName}" {`);
let sourcePath = removeFileExtension(node.fileName);
let packageQualifiedModuleName = getPackageQualifiedPath(host, sourcePath, ".");
write("declare module \"");
write(escapeString(packageQualifiedModuleName));
write("\" {");
increaseIndent();
writeLine();
}

emitLines(node.statements);

if (isPackage) {
if (isPackageDeclaration && isExternalModule(node)) {
decreaseIndent();
writeLine();
write("}");
Expand Down Expand Up @@ -621,7 +616,7 @@ module ts {

function emitModuleElementDeclarationFlags(node: Node) {
// If the node is parented in the current source file we need to emit export declare or just export
if (node.parent === currentSourceFile && !isPackage) {
if (node.parent === currentSourceFile && !(isPackageDeclaration && isExternalModule(currentSourceFile))) {
// If the node is exported
if (node.flags & NodeFlags.Export) {
write("export ");
Expand Down Expand Up @@ -758,13 +753,15 @@ module ts {
}
if (node.moduleSpecifier) {
write(" from ");
if (isPackage) {
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(`"${packageQualifiedPath}"`);
write("\"");
write(escapeString(packageQualifiedPath));
write("\"");
}
else {
writeTextOfNode(currentSourceFile, node.moduleSpecifier);
Expand Down Expand Up @@ -1667,7 +1664,7 @@ module ts {
/* @internal */
export function writePackageDeclarationFile(dtsFilePath: string, host: EmitHost, resolver: EmitResolver, diagnostics: Diagnostic[]) {
let compilerOptions = host.getCompilerOptions();
let emitDeclarationResult = emitDeclarations(host, resolver, diagnostics, dtsFilePath);
let emitDeclarationResult = emitDeclarations(host, resolver, diagnostics, dtsFilePath, /*root*/ undefined, /*isPackageDeclaration*/ true);
if (!emitDeclarationResult.reportedDeclarationError) {
let declarationOutput = emitDeclarationResult.referencePathsOutput
+ getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo);
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ module ts {
Option_sourceRoot_cannot_be_specified_with_option_inlineSourceMap: { code: 5049, category: DiagnosticCategory.Error, key: "Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'." },
Option_mapRoot_cannot_be_specified_with_option_inlineSourceMap: { code: 5050, category: DiagnosticCategory.Error, key: "Option 'mapRoot' cannot be specified with option 'inlineSourceMap'." },
Option_inlineSources_can_only_be_used_when_either_option_inlineSourceMap_or_option_sourceMap_is_provided: { code: 5051, category: DiagnosticCategory.Error, key: "Option 'inlineSources' can only be used when either option '--inlineSourceMap' or option '--sourceMap' is provided." },
Options_packageName_packageMain_and_packageDeclaration_must_all_be_specified_with_option_0: { code: 5052, category: DiagnosticCategory.Error, key: "Options 'packageName', 'packageMain', and 'packageDeclaration' must all be specified with option '{0}'." },
Option_declaration_must_be_specified_with_options_packageName_packageMain_and_packageDeclaration: { code: 5053, category: DiagnosticCategory.Error, key: "Option 'declaration' must be specified with options 'packageName', 'packageMain', and 'packageDeclaration'." },
Options_0_and_1_must_also_be_specified_with_option_2: { code: 5052, category: DiagnosticCategory.Error, key: "Options '{0}' and '{1}' must also be specified with option '{2}'." },
Options_packageName_packageMain_and_packageDeclaration_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES6_or_higher: { code: 5054, category: DiagnosticCategory.Error, key: "Options 'packageName', 'packageMain', and 'packageDeclaration' can only be used when either option 'module' is provided or option 'target' is 'ES6' or higher." },
Option_0_cannot_be_specified_with_option_separateCompilation: { code: 5055, category: DiagnosticCategory.Error, key: "Option '{0}' cannot be specified with option 'separateCompilation'." },
Concatenate_and_emit_output_to_single_file: { code: 6001, category: DiagnosticCategory.Message, key: "Concatenate and emit output to single file." },
Expand Down
6 changes: 1 addition & 5 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1821,14 +1821,10 @@
"category": "Error",
"code": 5051
},
"Options 'packageName', 'packageMain', and 'packageDeclaration' must all be specified with option '{0}'.": {
"Options '{0}' and '{1}' must also be specified with option '{2}'.": {
"category": "Error",
"code": 5052
},
"Option 'declaration' must be specified with options 'packageName', 'packageMain', and 'packageDeclaration'.": {
"category": "Error",
"code": 5053
},
"Options 'packageName', 'packageMain', and 'packageDeclaration' can only be used when either option 'module' is provided or option 'target' is 'ES6' or higher.": {
"category": "Error",
"code": 5054
Expand Down
17 changes: 9 additions & 8 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
else if (!isDeclarationFile(targetSourceFile) && compilerOptions.out) {
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);
}
}
}

// Sort and make the unique list of diagnostics
Expand Down Expand Up @@ -6051,14 +6059,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
emitJavaScript(jsFilePath, sourceFile);

if (compilerOptions.declaration) {
if (compilerOptions.packageMain && compilerOptions.packageName && compilerOptions.packageDeclaration) {
if (sourceFile.fileName === host.getCanonicalFileName(compilerOptions.packageMain)) {
writeDeclarationFile(jsFilePath, sourceFile, host, resolver, diagnostics);
}
}
else {
writeDeclarationFile(jsFilePath, sourceFile, host, resolver, diagnostics);
}
writeDeclarationFile(jsFilePath, sourceFile, host, resolver, diagnostics);
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,23 +627,20 @@ module ts {

if (options.packageMain || options.packageName || options.packageDeclaration) {
if (!options.packageMain) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_packageName_packageMain_and_packageDeclaration_must_all_be_specified_with_option_0, "packageMain"));
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_0_and_1_must_also_be_specified_with_option_2, "packageName", "packageDeclaration", "packageMain"));
return;
}
if (!options.packageName) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_packageName_packageMain_and_packageDeclaration_must_all_be_specified_with_option_0, "packageName"));
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_0_and_1_must_also_be_specified_with_option_2, "packageDeclaration", "packageMain", "packageName"));
return;
}
if (!options.packageDeclaration) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_packageName_packageMain_and_packageDeclaration_must_all_be_specified_with_option_0, "packageDeclaration"));
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_0_and_1_must_also_be_specified_with_option_2, "packageMain", "packageName", "packageDeclaration"));
return;
}
if (options.module === ModuleKind.None && options.target < ScriptTarget.ES6) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Options_packageName_packageMain_and_packageDeclaration_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES6_or_higher));
}
if (!options.declaration) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_declaration_must_be_specified_with_options_packageName_packageMain_and_packageDeclaration));
}
}

let languageVersion = options.target || ScriptTarget.ES3;
Expand Down