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
Cleaned up package parameters, added packageDir option, added path co…
…mparison function
  • Loading branch information
rbuckton committed May 14, 2015
commit 5f41f6828d2ee1e80deff3b05895f314a95cf842
20 changes: 16 additions & 4 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,31 @@ module ts {
name: "packageMain",
type: "string",
isFilePath: true,
experimental: true
experimental: true,
description: Diagnostics.Specifies_the_main_module_for_the_package,
paramType: Diagnostics.FILE
},
{
name: "packageName",
type: "string",
isFilePath: true,
experimental: true
experimental: true,
description: Diagnostics.Specifies_the_name_of_the_package,
paramType: Diagnostics.NAME
},
{
name: "packageDeclaration",
type: "string",
isFilePath: true,
experimental: true
experimental: true,
description: Diagnostics.Specifies_the_output_path_for_the_package_declaration,
paramType: Diagnostics.LOCATION
},
{
name: "packageDir",
type: "string",
isFilePath: true,
experimental: true,
description: Diagnostics.Specifies_the_root_directory_of_the_package
},
{
name: "diagnostics",
Expand Down
38 changes: 38 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module ts {
EqualTo = 0,
GreaterThan = 1
}

export const enum StringComparison {
Ordinal = 0,
IgnoreCase = 1,
CurrentCultureIgnoreCase = 2
}

export interface StringSet extends Map<any> { }

Expand Down Expand Up @@ -485,6 +491,38 @@ module ts {

return normalized;
}

export function compareStrings(x: string, y: string, comparison?: StringComparison): Comparison {
if (x === y) return Comparison.EqualTo;
if (x === undefined) return Comparison.LessThan;
if (y === undefined) return Comparison.GreaterThan;
if (comparison === StringComparison.CurrentCultureIgnoreCase) {
x = x.toLocaleLowerCase();
y = y.toLocaleLowerCase();
}
else if (comparison === StringComparison.IgnoreCase) {
x = x.toLowerCase();
y = y.toLowerCase();
}

return x === y ? Comparison.EqualTo : x < y ? Comparison.LessThan : Comparison.GreaterThan;
}

export function comparePaths(path1: string, path2: string, currentDirectory: string, ignoreCase?: boolean): Comparison {
let pathComponents1 = getNormalizedPathComponents(path1, currentDirectory);
let pathComponents2 = getNormalizedPathComponents(path2, currentDirectory);
let sharedLength = Math.min(pathComponents1.length, pathComponents2.length);
for (let i = 0; i < sharedLength; i++) {
let component1 = pathComponents1[i];
let component2 = pathComponents2[i];
let result = compareStrings(component1, component2, ignoreCase ? StringComparison.IgnoreCase : StringComparison.Ordinal);
if (result !== Comparison.EqualTo) {
return result;
}
}

return compareValues(pathComponents1.length, pathComponents2.length);
}

export function normalizePath(path: string): string {
path = normalizeSlashes(path);
Expand Down
26 changes: 16 additions & 10 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ module ts {
let jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, host, ".js");
emitDeclarations(host, resolver, diagnostics, jsFilePath, targetSourceFile);

let packageDeclaration = getPackageDeclaration(host);
let compilerOptions = host.getCompilerOptions();
let packageDeclaration = host.getPackageDeclaration();
if (packageDeclaration) {
if (!targetSourceFile || isPackageMain(targetSourceFile, host)) {
let packageMain = host.getPackageMain();
if (!targetSourceFile ||
comparePaths(targetSourceFile.fileName, host.getPackageMain(), host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo) {
writePackageDeclarationFile(packageDeclaration, host, resolver, diagnostics);
}
}
Expand Down Expand Up @@ -67,6 +68,7 @@ module ts {

let moduleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[] = [];
let asynchronousSubModuleDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[];
let packageMain = isPackageDeclaration ? host.getPackageMain() : undefined;

// Contains the reference paths that needs to go in the declaration file.
// Collecting this separately because reference paths need to be first thing in the declaration file
Expand Down Expand Up @@ -170,14 +172,16 @@ module ts {
function sortSourceFiles(sourceFiles: SourceFile[]) {
let indices = new Array<number>(sourceFiles.length);
for (let i = 0; i < sourceFiles.length; ++i) indices[i] = i;
let currentDirectory = host.getCurrentDirectory();
let ignoreCase = !host.useCaseSensitiveFileNames();
indices.sort((left, right) => {
let leftFile = sourceFiles[left];
if (isPackageMain(leftFile, host)) {
if (comparePaths(leftFile.fileName, packageMain, currentDirectory, ignoreCase) === Comparison.EqualTo) {
return -1;
}

let rightFile = sourceFiles[right];
if (isPackageMain(rightFile, host)) {
if (comparePaths(rightFile.fileName, packageMain, currentDirectory, ignoreCase) === Comparison.EqualTo) {
return +1;
}

Expand Down Expand Up @@ -1659,21 +1663,23 @@ module ts {
}

function getPackageQualifiedPath(host: EmitHost, moduleName: string, basePath: string) {
let packageRoot = getPackageDirectory(host);
let currentDirectory = host.getCurrentDirectory();
let ignoreCase = !host.useCaseSensitiveFileNames();
let packageRoot = host.getPackageDirectory();
let modulePath = combinePaths(basePath, moduleName);
let packageRelativePath = getRelativePathToDirectoryOrUrl(
packageRoot,
modulePath,
host.getCurrentDirectory(),
currentDirectory,
host.getCanonicalFileName,
false);

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

if (packageAbsolutePath + ".ts" === packageMain ||
packageAbsolutePath + ".d.ts" === packageMain) {
if (comparePaths(packageAbsolutePath + ".ts", packageMain, currentDirectory, ignoreCase) === Comparison.EqualTo ||
comparePaths(packageAbsolutePath + ".d.ts", packageMain, currentDirectory, ignoreCase) === Comparison.EqualTo) {
return compilerOptions.packageName;
}

Expand Down
6 changes: 6 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ module ts {
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'." },
Option_noEmit_cannot_be_specified_with_option_packageDeclaration: { code: 5056, category: DiagnosticCategory.Error, key: "Option 'noEmit' cannot be specified with option 'packageDeclaration'." },
Concatenate_and_emit_output_to_single_file: { code: 6001, category: DiagnosticCategory.Message, key: "Concatenate and emit output to single file." },
Generates_corresponding_d_ts_file: { code: 6002, category: DiagnosticCategory.Message, key: "Generates corresponding '.d.ts' file." },
Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations: { code: 6003, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate map files instead of generated locations." },
Expand Down Expand Up @@ -510,6 +511,11 @@ module ts {
Specifies_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix: { code: 6060, category: DiagnosticCategory.Message, key: "Specifies the end of line sequence to be used when emitting files: 'CRLF' (dos) or 'LF' (unix)." },
NEWLINE: { code: 6061, category: DiagnosticCategory.Message, key: "NEWLINE" },
Argument_for_newLine_option_must_be_CRLF_or_LF: { code: 6062, category: DiagnosticCategory.Error, key: "Argument for '--newLine' option must be 'CRLF' or 'LF'." },
Specifies_the_main_module_for_the_package: { code: 6100, category: DiagnosticCategory.Message, key: "Specifies the main module for the package" },
Specifies_the_name_of_the_package: { code: 6101, category: DiagnosticCategory.Message, key: "Specifies the name of the package" },
Specifies_the_output_path_for_the_package_declaration: { code: 6102, category: DiagnosticCategory.Message, key: "Specifies the output path for the package declaration" },
Specifies_the_root_directory_of_the_package: { code: 6103, category: DiagnosticCategory.Message, key: "Specifies the root directory of the package" },
NAME: { code: 6104, category: DiagnosticCategory.Message, key: "NAME" },
Variable_0_implicitly_has_an_1_type: { code: 7005, category: DiagnosticCategory.Error, key: "Variable '{0}' implicitly has an '{1}' type." },
Parameter_0_implicitly_has_an_1_type: { code: 7006, category: DiagnosticCategory.Error, key: "Parameter '{0}' implicitly has an '{1}' type." },
Member_0_implicitly_has_an_1_type: { code: 7008, category: DiagnosticCategory.Error, key: "Member '{0}' implicitly has an '{1}' type." },
Expand Down
27 changes: 25 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@
"category": "Error",
"code": 5050
},
"Option 'inlineSources' can only be used when either option '--inlineSourceMap' or option '--sourceMap' is provided.": {
"Option 'inlineSources' can only be used when either option '--inlineSourceMap' or option '--sourceMap' is provided.": {
"category": "Error",
"code": 5051
},
Expand All @@ -1833,6 +1833,10 @@
"category": "Error",
"code": 5055
},
"Option 'noEmit' cannot be specified with option 'packageDeclaration'.": {
"category": "Error",
"code": 5056
},

"Concatenate and emit output to single file.": {
"category": "Message",
Expand Down Expand Up @@ -2030,7 +2034,26 @@
"category": "Error",
"code": 6062
},

"Specifies the main module for the package": {
"category": "Message",
"code": 6100
},
"Specifies the name of the package": {
"category": "Message",
"code": 6101
},
"Specifies the output path for the package declaration": {
"category": "Message",
"code": 6102
},
"Specifies the root directory of the package": {
"category": "Message",
"code": 6103
},
"NAME": {
"category": "Message",
"code": 6104
},

"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +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);
let packageDeclaration = host.getPackageDeclaration();

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

if (packageDeclaration && isPackageMain(targetSourceFile, host)) {
if (packageDeclaration &&
comparePaths(targetSourceFile.fileName, host.getPackageMain(), host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo) {
writePackageDeclarationFile(packageDeclaration, host, resolver, diagnostics);
}
}
Expand Down
Loading