Merge pull request #351 from Microsoft/wontYouPleasePleaseHelpMe

Support the '--help' compiler flag.
This commit is contained in:
Daniel Rosenwasser
2014-08-06 11:59:18 -07:00
8 changed files with 480 additions and 129 deletions

View File

@@ -197,6 +197,8 @@ var processDiagnosticMessagesTs = path.join(scriptsDirectory, "processDiagnostic
var diagnosticMessagesJson = path.join(compilerDirectory, "diagnosticMessages.json");
var diagnosticInfoMapTs = path.join(compilerDirectory, "diagnosticInformationMap.generated.ts");
file(processDiagnosticMessagesTs)
// processDiagnosticMessages script
compileFile(processDiagnosticMessagesJs,
[processDiagnosticMessagesTs],

View File

@@ -39,7 +39,7 @@ function main(): void {
function buildUniqueNameMap(names: string[]): IIndexable<string> {
var nameMap: IIndexable<string> = {};
var uniqueNames = NameGenerator.ensureUniqueness(names, /*isFixed */ undefined, /* isCaseSensitive */ false);
var uniqueNames = NameGenerator.ensureUniqueness(names, /* isCaseSensitive */ false, /* isFixed */ undefined);
for (var i = 0; i < names.length; i++) {
nameMap[names[i]] = uniqueNames[i];
@@ -94,17 +94,17 @@ function convertPropertyName(origName: string): string {
}
module NameGenerator {
export function ensureUniqueness(
names: string[],
isFixed: boolean[]= names.map(() => false),
isCaseSensitive: boolean = true): string[] {
export function ensureUniqueness(names: string[], isCaseSensitive: boolean, isFixed?: boolean[]): string[]{
if (!isFixed) {
isFixed = names.map(() => false)
}
var names = names.map(x => x);
ensureUniquenessInPlace(names, isFixed, isCaseSensitive);
var names = names.slice();
ensureUniquenessInPlace(names, isCaseSensitive, isFixed);
return names;
}
function ensureUniquenessInPlace(names: string[], isFixed: boolean[], isCaseSensitive: boolean): void {
function ensureUniquenessInPlace(names: string[], isCaseSensitive: boolean, isFixed: boolean[]): void {
for (var i = 0; i < names.length; i++) {
var name = names[i];
var collisionIndices = Utilities.collectMatchingIndices(name, names, isCaseSensitive);
@@ -131,9 +131,12 @@ module NameGenerator {
}
while (true) {
var newName = name + suffix++;
var newName = name + suffix;
suffix++;
if (proposedNames.some((name) => Utilities.stringEquals(name, newName, isCaseSensitive))) {
// Check if we've synthesized a unique name, and if so
// replace the conflicting name with the new one.
if (!proposedNames.some(name => Utilities.stringEquals(name, newName, isCaseSensitive))) {
proposedNames[collisionIndex] = newName;
break;
}
@@ -144,7 +147,7 @@ module NameGenerator {
module Utilities {
/// Return a list of all indices where a string occurs.
export function collectMatchingIndices(name: string, proposedNames: string[], isCaseSensitive: boolean = true): number[] {
export function collectMatchingIndices(name: string, proposedNames: string[], isCaseSensitive: boolean): number[] {
var matchingIndices: number[] = [];
for (var i = 0; i < proposedNames.length; i++) {
@@ -156,8 +159,8 @@ module Utilities {
return matchingIndices;
}
export function stringEquals(s1: string, s2: string, caseSensitive: boolean = true): boolean {
if (!caseSensitive) {
export function stringEquals(s1: string, s2: string, caseSensitive: boolean): boolean {
if (caseSensitive) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
}

View File

@@ -4,44 +4,128 @@
/// <reference path="scanner.ts"/>
module ts {
var shortOptionNames: Map<string> = {
"d": "declaration",
"h": "help",
"m": "module",
"o": "out",
"t": "target",
"v": "version",
"w": "watch",
};
var optionDeclarations: CommandLineOption[] = [
{ name: "charset", type: "string" },
{ name: "codepage", type: "number" },
{ name: "declaration", type: "boolean" },
{ name: "diagnostics", type: "boolean" },
{ name: "help", type: "boolean" },
{ name: "locale", type: "string" },
{ name: "mapRoot", type: "string" },
{ name: "module", type: { "commonjs": ModuleKind.CommonJS, "amd": ModuleKind.AMD }, error: Diagnostics.Argument_for_module_option_must_be_commonjs_or_amd },
{ name: "noImplicitAny", type: "boolean" },
{ name: "noLib", type: "boolean" },
{ name: "noLibCheck", type: "boolean" },
{ name: "noResolve", type: "boolean" },
{ name: "out", type: "string" },
{ name: "outDir", type: "string" },
{ name: "removeComments", type: "boolean" },
{ name: "sourceMap", type: "boolean" },
{ name: "sourceRoot", type: "string" },
{ name: "target", type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5 }, error: Diagnostics.Argument_for_target_option_must_be_es3_or_es5 },
{ name: "version", type: "boolean" },
{ name: "watch", type: "boolean" }
export var optionDeclarations: CommandLineOption[] = [
{
name: "charset",
type: "string",
},
{
name: "codepage",
type: "number",
},
{
name: "declaration",
shortName: "d",
type: "boolean",
description: Diagnostics.Generates_corresponding_d_ts_file,
},
{
name: "diagnostics",
type: "boolean",
},
{
name: "help",
shortName: "h",
type: "boolean",
description: Diagnostics.Print_this_message,
},
{
name: "locale",
type: "string",
},
{
name: "mapRoot",
type: "string",
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
paramType: Diagnostics.LOCATION,
},
{
name: "module",
shortName: "m",
type: {
"commonjs": ModuleKind.CommonJS,
"amd": ModuleKind.AMD
},
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_or_amd,
paramType: Diagnostics.KIND,
error: Diagnostics.Argument_for_module_option_must_be_commonjs_or_amd
},
{
name: "noImplicitAny",
type: "boolean",
description: Diagnostics.Warn_on_expressions_and_declarations_with_an_implied_any_type,
},
{
name: "noLib",
type: "boolean",
},
{
name: "noLibCheck",
type: "boolean",
},
{
name: "noResolve",
type: "boolean",
},
{
name: "out",
type: "string",
description: Diagnostics.Concatenate_and_emit_output_to_single_file,
paramType: Diagnostics.FILE,
},
{
name: "outDir",
type: "string",
description: Diagnostics.Redirect_output_structure_to_the_directory,
paramType: Diagnostics.DIRECTORY,
},
{
name: "removeComments",
type: "boolean",
description: Diagnostics.Do_not_emit_comments_to_output,
},
{
name: "sourcemap",
type: "boolean",
description: Diagnostics.Generates_corresponding_map_file,
},
{
name: "sourceRoot",
type: "string",
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
paramType: Diagnostics.LOCATION,
},
{
name: "target",
shortName: "t",
type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5 },
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5,
paramType: Diagnostics.VERSION,
error: Diagnostics.Argument_for_target_option_must_be_es3_or_es5
},
{
name: "version",
shortName: "v",
type: "boolean",
description: Diagnostics.Print_the_compiler_s_version,
},
{
name: "watch",
shortName: "w",
type: "boolean",
description: Diagnostics.Watch_input_files,
}
];
// Map command line switches to compiler options' property descriptors. Keys must be lower case spellings of command line switches.
// The 'name' property specifies the property name in the CompilerOptions type. The 'type' property specifies the type of the option.
var optionMap: Map<CommandLineOption> = {};
var shortOptionNames: Map<string> = {};
var optionNameMap: Map<CommandLineOption> = {};
forEach(optionDeclarations, option => {
optionMap[option.name.toLowerCase()] = option;
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
shortOptionNames[option.shortName] = option.name;
}
});
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
@@ -75,8 +159,8 @@ module ts {
s = shortOptionNames[s];
}
if (hasProperty(optionMap, s)) {
var opt = optionMap[s];
if (hasProperty(optionNameMap, s)) {
var opt = optionNameMap[s];
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
if (!args[i] && opt.type !== "boolean") {

View File

@@ -259,7 +259,7 @@ module ts {
};
}
function compareValues(a: any, b: any): number {
export function compareValues<T>(a: T, b: T): number {
if (a === b) return 0;
if (a === undefined) return -1;
if (b === undefined) return 1;

View File

@@ -238,9 +238,50 @@ module ts {
Could_not_write_file_0_Colon_1: { code: 5033, category: DiagnosticCategory.Error, key: "Could not write file '{0}': {1}" },
Option_mapRoot_cannot_be_specified_without_specifying_sourcemap_option: { code: 5038, category: DiagnosticCategory.Error, key: "Option mapRoot cannot be specified without specifying sourcemap option." },
Option_sourceRoot_cannot_be_specified_without_specifying_sourcemap_option: { code: 5039, category: DiagnosticCategory.Error, key: "Option sourceRoot cannot be specified without specifying sourcemap option." },
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." },
Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations: { code: 6004, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate TypeScript files instead of source locations." },
Watch_input_files: { code: 6005, category: DiagnosticCategory.Message, key: "Watch input files." },
Redirect_output_structure_to_the_directory: { code: 6006, category: DiagnosticCategory.Message, key: "Redirect output structure to the directory." },
Do_not_emit_comments_to_output: { code: 6009, category: DiagnosticCategory.Message, key: "Do not emit comments to output." },
Skip_resolution_and_preprocessing: { code: 6010, category: DiagnosticCategory.Message, key: "Skip resolution and preprocessing." },
Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: 'ES3' (default), or 'ES5'" },
Specify_module_code_generation_Colon_commonjs_or_amd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs' or 'amd'" },
Print_this_message: { code: 6017, category: DiagnosticCategory.Message, key: "Print this message." },
Print_the_compiler_s_version: { code: 6019, category: DiagnosticCategory.Message, key: "Print the compiler's version." },
Allow_use_of_deprecated_0_keyword_when_referencing_an_external_module: { code: 6021, category: DiagnosticCategory.Message, key: "Allow use of deprecated '{0}' keyword when referencing an external module." },
Specify_locale_for_errors_and_messages_For_example_0_or_1: { code: 6022, category: DiagnosticCategory.Message, key: "Specify locale for errors and messages. For example '{0}' or '{1}'" },
Syntax_Colon_0: { code: 6023, category: DiagnosticCategory.Message, key: "Syntax: {0}" },
options: { code: 6024, category: DiagnosticCategory.Message, key: "options" },
file: { code: 6025, category: DiagnosticCategory.Message, key: "file" },
Examples_Colon_0: { code: 6026, category: DiagnosticCategory.Message, key: "Examples: {0}" },
Options_Colon: { code: 6027, category: DiagnosticCategory.Message, key: "Options:" },
Version_0: { code: 6029, category: DiagnosticCategory.Message, key: "Version {0}" },
Insert_command_line_options_and_files_from_a_file: { code: 6030, category: DiagnosticCategory.Message, key: "Insert command line options and files from a file." },
Use_the_0_flag_to_see_options: { code: 6031, category: DiagnosticCategory.Message, key: "Use the '{0}' flag to see options." },
File_change_detected_Compiling: { code: 6032, category: DiagnosticCategory.Message, key: "File change detected. Compiling..." },
STRING: { code: 6033, category: DiagnosticCategory.Message, key: "STRING" },
KIND: { code: 6034, category: DiagnosticCategory.Message, key: "KIND" },
FILE: { code: 6035, category: DiagnosticCategory.Message, key: "FILE" },
VERSION: { code: 6036, category: DiagnosticCategory.Message, key: "VERSION" },
LOCATION: { code: 6037, category: DiagnosticCategory.Message, key: "LOCATION" },
DIRECTORY: { code: 6038, category: DiagnosticCategory.Message, key: "DIRECTORY" },
NUMBER: { code: 6039, category: DiagnosticCategory.Message, key: "NUMBER" },
Specify_the_codepage_to_use_when_opening_source_files: { code: 6040, category: DiagnosticCategory.Message, key: "Specify the codepage to use when opening source files." },
Additional_locations_Colon: { code: 6041, category: DiagnosticCategory.Message, key: "Additional locations:" },
Compilation_complete_Watching_for_file_changes: { code: 6042, category: DiagnosticCategory.Message, key: "Compilation complete. Watching for file changes." },
Generates_corresponding_map_file: { code: 6043, category: DiagnosticCategory.Message, key: "Generates corresponding '.map' file." },
Compiler_option_0_expects_an_argument: { code: 6044, category: DiagnosticCategory.Error, key: "Compiler option '{0}' expects an argument." },
Unterminated_quoted_string_in_response_file_0: { code: 6045, category: DiagnosticCategory.Error, key: "Unterminated quoted string in response file '{0}'." },
Argument_for_module_option_must_be_commonjs_or_amd: { code: 6045, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
Argument_for_target_option_must_be_es3_or_es5: { code: 6046, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: { code: 6047, category: DiagnosticCategory.Error, key: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'." },
Unsupported_locale_0: { code: 6048, category: DiagnosticCategory.Error, key: "Unsupported locale '{0}'." },
Unable_to_open_file_0: { code: 6049, category: DiagnosticCategory.Error, key: "Unable to open file '{0}'." },
Corrupted_locale_file_0: { code: 6050, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
No_input_files_specified: { code: 6051, category: DiagnosticCategory.Error, key: "No input files specified." },
Warn_on_expressions_and_declarations_with_an_implied_any_type: { code: 7004, category: DiagnosticCategory.Message, key: "Warn on expressions and declarations with an implied 'any' type." },
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." },
@@ -326,14 +367,5 @@ module ts {
Import_declaration_conflicts_with_local_declaration_of_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Import declaration conflicts with local declaration of '{0}'" },
Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: -9999999, category: DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" },
Filename_0_differs_from_already_included_filename_1_only_in_casing: { code: -9999999, category: DiagnosticCategory.Error, key: "Filename '{0}' differs from already included filename '{1}' only in casing" },
Argument_for_module_option_must_be_commonjs_or_amd: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
Argument_for_target_option_must_be_es3_or_es5: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },
Compiler_option_0_expects_an_argument: { code: -9999999, category: DiagnosticCategory.Error, key: "Compiler option '{0}' expects an argument." },
Unterminated_quoted_string_in_response_file_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Unterminated quoted string in response file '{0}'." },
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: { code: -9999999, category: DiagnosticCategory.Error, key: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'." },
Unsupported_locale_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Unsupported locale {0}." },
Unable_to_open_file_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Unable to open file {0}." },
Corrupted_locale_file_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
No_input_files_specified: { code: -9999999, category: DiagnosticCategory.Error, key: "No input files specified." },
};
}

View File

@@ -946,23 +946,187 @@
"category": "Error",
"code": 5039
},
"Concatenate and emit output to single file.": {
"category": "Message",
"code": 6001
},
"Generates corresponding '.d.ts' file.": {
"category": "Message",
"code": 6002
},
"Specifies the location where debugger should locate map files instead of generated locations.": {
"category": "Message",
"code": 6003
},
"Specifies the location where debugger should locate TypeScript files instead of source locations.": {
"category": "Message",
"code": 6004
},
"Watch input files.": {
"category": "Message",
"code": 6005
},
"Redirect output structure to the directory.": {
"category": "Message",
"code": 6006
},
"Do not emit comments to output.": {
"category": "Message",
"code": 6009
},
"Skip resolution and preprocessing.": {
"category": "Message",
"code": 6010
},
"Specify ECMAScript target version: 'ES3' (default), or 'ES5'": {
"category": "Message",
"code": 6015
},
"Specify module code generation: 'commonjs' or 'amd'": {
"category": "Message",
"code": 6016
},
"Print this message.": {
"category": "Message",
"code": 6017
},
"Print the compiler's version.": {
"category": "Message",
"code": 6019
},
"Allow use of deprecated '{0}' keyword when referencing an external module.": {
"category": "Message",
"code": 6021
},
"Specify locale for errors and messages. For example '{0}' or '{1}'": {
"category": "Message",
"code": 6022
},
"Syntax: {0}": {
"category": "Message",
"code": 6023
},
"options": {
"category": "Message",
"code": 6024
},
"file": {
"category": "Message",
"code": 6025
},
"Examples: {0}": {
"category": "Message",
"code": 6026
},
"Options:": {
"category": "Message",
"code": 6027
},
"Version {0}": {
"category": "Message",
"code": 6029
"category": "Message",
"code": 6029
},
"Insert command line options and files from a file.": {
"category": "Message",
"code": 6030
},
"Use the '{0}' flag to see options.": {
"category": "Message",
"code": 6031
},
"File change detected. Compiling...": {
"category": "Message",
"code": 6032
},
"STRING": {
"category": "Message",
"code": 6033
},
"Compilation complete. Watching for file changes.": {
"KIND": {
"category": "Message",
"code": 6034
},
"FILE": {
"category": "Message",
"code": 6035
},
"VERSION": {
"category": "Message",
"code": 6036
},
"LOCATION": {
"category": "Message",
"code": 6037
},
"DIRECTORY": {
"category": "Message",
"code": 6038
},
"NUMBER": {
"category": "Message",
"code": 6039
},
"Specify the codepage to use when opening source files.": {
"category": "Message",
"code": 6040
},
"Additional locations:": {
"category": "Message",
"code": 6041
},
"Compilation complete. Watching for file changes.": {
"category": "Message",
"code": 6042
},
"Generates corresponding '.map' file.": {
"category": "Message",
"code": 6043
},
"Compiler option '{0}' expects an argument.": {
"category": "Error",
"code": 6044
},
"Unterminated quoted string in response file '{0}'.": {
"category": "Error",
"code": 6045
},
"Argument for '--module' option must be 'commonjs' or 'amd'.": {
"category": "Error",
"code": 6045
},
"Argument for '--target' option must be 'es3' or 'es5'.": {
"category": "Error",
"code": 6046
},
"Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'.": {
"category": "Error",
"code": 6047
},
"Unsupported locale '{0}'.": {
"category": "Error",
"code": 6048
},
"Unable to open file '{0}'.": {
"category": "Error",
"code": 6049
},
"Corrupted locale file {0}.": {
"category": "Error",
"code": 6050
},
"No input files specified.": {
"category": "Error",
"code": 6051
},
"Warn on expressions and declarations with an implied 'any' type.": {
"category": "Message",
"code": 7004
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
},
"Parameter '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7006
@@ -1322,41 +1486,5 @@
"Filename '{0}' differs from already included filename '{1}' only in casing": {
"category": "Error",
"code": -9999999
},
"Argument for '--module' option must be 'commonjs' or 'amd'.": {
"category": "Error",
"code": -9999999
},
"Argument for '--target' option must be 'es3' or 'es5'.": {
"category": "Error",
"code": -9999999
},
"Compiler option '{0}' expects an argument.": {
"category": "Error",
"code": -9999999
},
"Unterminated quoted string in response file '{0}'.": {
"category": "Error",
"code": -9999999
},
"Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'.": {
"category": "Error",
"code": -9999999
},
"Unsupported locale {0}.": {
"category": "Error",
"code": -9999999
},
"Unable to open file {0}.": {
"category": "Error",
"code": -9999999
},
"Corrupted locale file {0}.": {
"category": "Error",
"code": -9999999
},
"No input files specified.": {
"category": "Error",
"code": -9999999
}
}

View File

@@ -9,10 +9,12 @@
/// <reference path="commandLineParser.ts"/>
module ts {
export var version = "1.1.0.0";
var version = "1.1.0.0";
/// Checks to see if the locale is in the appropriate format,
/// and if it is, attempt to set the appropriate language.
/**
* Checks to see if the locale is in the appropriate format,
* and if it is, attempts to set the appropriate language.
*/
function validateLocaleAndSetLanguage(locale: string, errors: Diagnostic[]): boolean {
var matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());
@@ -78,19 +80,24 @@ module ts {
return count;
}
function reportDiagnostic(error: Diagnostic) {
if (error.file) {
var loc = error.file.getLineAndCharacterFromPosition(error.start);
sys.write(error.file.filename + "(" + loc.line + "," + loc.character + "): " + error.messageText + sys.newLine);
function getDiagnosticText(message: DiagnosticMessage, ...args: any[]): string {
var diagnostic: Diagnostic = createCompilerDiagnostic.apply(undefined, arguments);
return diagnostic.messageText;
}
function reportDiagnostic(diagnostic: Diagnostic) {
if (diagnostic.file) {
var loc = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start);
sys.write(diagnostic.file.filename + "(" + loc.line + "," + loc.character + "): " + diagnostic.messageText + sys.newLine);
}
else {
sys.write(error.messageText + sys.newLine);
sys.write(diagnostic.messageText + sys.newLine);
}
}
function reportDiagnostics(errors: Diagnostic[]) {
for (var i = 0; i < errors.length; i++) {
reportDiagnostic(errors[i]);
function reportDiagnostics(diagnostics: Diagnostic[]) {
for (var i = 0; i < diagnostics.length; i++) {
reportDiagnostic(diagnostics[i]);
}
}
@@ -186,37 +193,37 @@ module ts {
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
}
// If there are any errors due to command line parsing and/or
// setting up localization, report them and quit.
if (commandLine.errors.length > 0) {
reportDiagnostics(commandLine.errors);
return sys.exit(1);
}
if (commandLine.options.version) {
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Version_0, version));
sys.exit(0);
return sys.exit(0);
}
if (commandLine.options.help) {
// TODO (drosen): Usage.
sys.exit(0);
}
if (commandLine.filenames.length === 0) {
commandLine.errors.push(createCompilerDiagnostic(Diagnostics.No_input_files_specified));
}
if (commandLine.errors.length) {
reportDiagnostics(commandLine.errors);
sys.exit(1);
if (commandLine.options.help || commandLine.filenames.length === 0) {
printVersion();
printHelp();
return sys.exit(0);
}
var defaultCompilerHost = createCompilerHost(commandLine.options);
if (commandLine.options.watch) {
if (!sys.watchFile) {
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"));
sys.exit(1);
return sys.exit(1);
}
watchProgram(commandLine, defaultCompilerHost);
}
else {
sys.exit(compile(commandLine, defaultCompilerHost).errors.length > 0 ? 1 : 0);
var result = compile(commandLine, defaultCompilerHost).errors.length > 0 ? 1 : 0;
return sys.exit(result);
}
}
@@ -341,6 +348,98 @@ module ts {
return { program: program, errors: errors };
}
function printVersion() {
sys.write(getDiagnosticText(Diagnostics.Version_0, version) + sys.newLine);
}
function printHelp() {
var output = "";
// We want to align our "syntax" and "examples" commands to a certain margin.
var syntaxLength = getDiagnosticText(Diagnostics.Syntax_Colon_0, "").length
var examplesLength = getDiagnosticText(Diagnostics.Examples_Colon_0, "").length
var marginLength = Math.max(syntaxLength, examplesLength);
// Build up the syntactic skeleton.
var syntax = makePadding(marginLength - syntaxLength);
syntax += "tsc [" + getDiagnosticText(Diagnostics.options) + "] [" + getDiagnosticText(Diagnostics.file) + " ...]";
output += getDiagnosticText(Diagnostics.Syntax_Colon_0, syntax);
output += sys.newLine + sys.newLine;
// Build up the list of examples.
var padding = makePadding(marginLength);
output += getDiagnosticText(Diagnostics.Examples_Colon_0, makePadding(marginLength - examplesLength) + "tsc hello.ts") + sys.newLine;
output += padding + "tsc --out foo.js foo.ts" + sys.newLine;
output += padding + "tsc @args.txt" + sys.newLine;
output += sys.newLine;
output += getDiagnosticText(Diagnostics.Options_Colon) + sys.newLine;
// Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch")
var optsList = optionDeclarations.slice();
optsList.sort((a, b) => compareValues<string>(a.name.toLowerCase(), b.name.toLowerCase()));
// We want our descriptions to align at the same column in our output,
// so we keep track of the longest option usage string.
var marginLength = 0;
var usageColumn: string[] = []; // Things like "-d, --declaration" go in here.
var descriptionColumn: string[] = [];
for (var i = 0; i < optsList.length; i++) {
var option = optsList[i];
// If an option lacks a description,
// it is not officially supported.
if (!option.description) {
continue;
}
var usageText = " ";
if (option.shortName) {
usageText += "-" + option.shortName;
usageText += getParamName(option);
usageText += ", ";
}
usageText += "--" + option.name;
usageText += getParamName(option);
usageColumn.push(usageText);
descriptionColumn.push(getDiagnosticText(option.description));
// Set the new margin for the description column if necessary.
marginLength = Math.max(usageText.length, marginLength);
}
// Special case that can't fit in the loop.
var usageText = " @<" + getDiagnosticText(Diagnostics.file) + ">";
usageColumn.push(usageText);
descriptionColumn.push(getDiagnosticText(Diagnostics.Insert_command_line_options_and_files_from_a_file));
marginLength = Math.max(usageText.length, marginLength);
// Print out each row, aligning all the descriptions on the same column.
for (var i = 0; i < usageColumn.length; i++) {
var usage = usageColumn[i];
var description = descriptionColumn[i];
output += usage + makePadding(marginLength - usage.length + 2) + description + sys.newLine;
}
sys.write(output);
return;
function getParamName(option: CommandLineOption) {
if (option.paramName !== undefined) {
return " " + getDiagnosticText(option.paramName);
}
return "";
}
function makePadding(paddingLength: number): string {
return Array(paddingLength + 1).join(" ");
}
}
}
ts.executeCommandLine(sys.args);

View File

@@ -979,8 +979,11 @@ module ts {
export interface CommandLineOption {
name: string;
type: any;
error?: DiagnosticMessage;
type: any; // "string", "number", "boolean", or an object literal mapping named values to actual values
shortName?: string; // A short pneumonic for convenience - for instance, 'h' can be used in place of 'help'.
description?: DiagnosticMessage; // The message describing what the command line switch does
paramName?: DiagnosticMessage; // The name to be used for a non-boolean option's parameter.
error?: DiagnosticMessage; // The error given when the argument does not fit a customized 'type'.
}
export enum CharacterCodes {