From 65452aa0114efe610466ff8da29caf5dd9e8ffe4 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 15 Jan 2015 15:57:08 -0800 Subject: [PATCH] Hardening compiler to accept empty CompilerOptions object --- src/compiler/checker.ts | 8 ++++---- src/compiler/commandLineParser.ts | 6 +----- src/compiler/emitter.ts | 22 +++++++++++----------- src/compiler/program.ts | 7 +------ src/compiler/scanner.ts | 12 ++++++------ 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7604c709fbc..274a7980ff1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6340,7 +6340,7 @@ module ts { function checkTaggedTemplateExpression(node: TaggedTemplateExpression): Type { // Grammar checking - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { grammarErrorOnFirstToken(node.template, Diagnostics.Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher); } @@ -10401,7 +10401,7 @@ module ts { return; var computedPropertyName = node; - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { grammarErrorOnNode(node, Diagnostics.Computed_property_names_are_only_available_when_targeting_ECMAScript_6_and_higher); } else if (computedPropertyName.expression.kind === SyntaxKind.BinaryExpression && (computedPropertyName.expression).operator === SyntaxKind.CommaToken) { @@ -10501,7 +10501,7 @@ module ts { function checkGrammarAccessor(accessor: MethodDeclaration): boolean { var kind = accessor.kind; - if (compilerOptions.target < ScriptTarget.ES5) { + if (!(compilerOptions.target >= ScriptTarget.ES5)) { return grammarErrorOnNode(accessor.name, Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); } else if (isInAmbientContext(accessor)) { @@ -10706,7 +10706,7 @@ module ts { return grammarErrorAtPos(getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, Diagnostics.Variable_declaration_list_cannot_be_empty); } - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { if (isLet(declarationList)) { return grammarErrorOnFirstToken(declarationList, Diagnostics.let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher); } diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 22e6919319d..75d18458903 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -157,11 +157,7 @@ module ts { ]; export function parseCommandLine(commandLine: string[]): ParsedCommandLine { - // Set default compiler option values - var options: CompilerOptions = { - target: ScriptTarget.ES3, - module: ModuleKind.None - }; + var options: CompilerOptions = {}; var filenames: string[] = []; var errors: Diagnostic[] = []; var shortOptionNames: Map = {}; diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index d82e577971d..abef385c76c 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2021,14 +2021,14 @@ module ts { } function emitLiteral(node: LiteralExpression) { - var text = compilerOptions.target < ScriptTarget.ES6 && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) : + var text = !(compilerOptions.target >= ScriptTarget.ES6) && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) : node.parent ? getSourceTextOfNodeFromSourceFile(currentSourceFile, node) : node.text; if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) { writer.writeLiteral(text); } // For version below ES6, emit binary integer literal and octal integer literal in canonical form - else if (compilerOptions.target < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) { + else if (!(compilerOptions.target >= ScriptTarget.ES6) && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) { write(node.text); } else { @@ -2150,7 +2150,7 @@ module ts { // // TODO (drosen): Note that we need to account for the upcoming 'yield' and // spread ('...') unary operators that are anticipated for ES6. - Debug.assert(compilerOptions.target <= ScriptTarget.ES5); + Debug.assert(!(compilerOptions.target >= ScriptTarget.ES6)); switch (expression.kind) { case SyntaxKind.BinaryExpression: switch ((expression).operator) { @@ -2405,7 +2405,7 @@ module ts { } emitLeadingComments(node); emit(node.name); - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { write(": function "); } emitSignatureAndBody(node); @@ -2431,7 +2431,7 @@ module ts { // export var obj = { y }; // } // The short-hand property in obj need to emit as such ... = { y : m.y } regardless of the TargetScript version - if (compilerOptions.target < ScriptTarget.ES6 || resolver.getExpressionNamePrefix(node.name)) { + if (!(compilerOptions.target >= ScriptTarget.ES6) || resolver.getExpressionNamePrefix(node.name)) { // Emit identifier as an identifier write(": "); // Even though this is stored as identifier treat it as an expression @@ -2605,7 +2605,7 @@ module ts { function emitBinaryExpression(node: BinaryExpression) { - if (compilerOptions.target < ScriptTarget.ES6 && node.operator === SyntaxKind.EqualsToken && + if (!(compilerOptions.target >= ScriptTarget.ES6) && node.operator === SyntaxKind.EqualsToken && (node.left.kind === SyntaxKind.ObjectLiteralExpression || node.left.kind === SyntaxKind.ArrayLiteralExpression)) { emitDestructuring(node); } @@ -3101,7 +3101,7 @@ module ts { function emitVariableDeclaration(node: VariableDeclaration) { emitLeadingComments(node); if (isBindingPattern(node.name)) { - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { emitDestructuring(node); } else { @@ -3136,7 +3136,7 @@ module ts { function emitParameter(node: ParameterDeclaration) { emitLeadingComments(node); - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { if (isBindingPattern(node.name)) { var name = createTempVariable(node); if (!tempParameters) { @@ -3160,7 +3160,7 @@ module ts { } function emitDefaultValueAssignments(node: FunctionLikeDeclaration) { - if (compilerOptions.target < ScriptTarget.ES6) { + if (!(compilerOptions.target >= ScriptTarget.ES6)) { var tempIndex = 0; forEach(node.parameters, p => { if (isBindingPattern(p.name)) { @@ -3190,7 +3190,7 @@ module ts { } function emitRestParameter(node: FunctionLikeDeclaration) { - if (compilerOptions.target < ScriptTarget.ES6 && hasRestParameters(node)) { + if (!(compilerOptions.target >= ScriptTarget.ES6) && hasRestParameters(node)) { var restIndex = node.parameters.length - 1; var restParam = node.parameters[restIndex]; var tempName = createTempVariable(node, /*forLoopVariable*/ true).text; @@ -3269,7 +3269,7 @@ module ts { write("("); if (node) { var parameters = node.parameters; - var omitCount = compilerOptions.target < ScriptTarget.ES6 && hasRestParameters(node) ? 1 : 0; + var omitCount = !(compilerOptions.target >= ScriptTarget.ES6) && hasRestParameters(node) ? 1 : 0; emitList(parameters, 0, parameters.length - omitCount, /*multiLine*/ false, /*trailingComma*/ false); } write(")"); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 5af676aa575..06e63635232 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -81,11 +81,6 @@ module ts { var seenNoDefaultLib = options.noLib; var commonSourceDirectory: string; - //options = extend(options, { - // module: ModuleKind.None, - // target: ScriptTarget.ES3 - //}); - forEach(rootNames, name => processRootFile(name, false)); if (!seenNoDefaultLib) { processRootFile(host.getDefaultLibFilename(options), true); @@ -347,7 +342,7 @@ module ts { } var firstExternalModule = forEach(files, f => isExternalModule(f) ? f : undefined); - if (firstExternalModule && options.module === ModuleKind.None) { + if (firstExternalModule && !options.module) { // We cannot use createDiagnosticFromNode because nodes do not have parents yet var externalModuleErrorSpan = getErrorSpanForNode(firstExternalModule.externalModuleIndicator); var errorStart = skipTrivia(firstExternalModule.text, externalModuleErrorSpan.pos); diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 427bc0012d6..5d800be0275 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -224,15 +224,15 @@ module ts { } function isUnicodeIdentifierStart(code: number, languageVersion: ScriptTarget) { - return languageVersion === ScriptTarget.ES3 ? - lookupInUnicodeMap(code, unicodeES3IdentifierStart) : - lookupInUnicodeMap(code, unicodeES5IdentifierStart); + return languageVersion >= ScriptTarget.ES5 ? + lookupInUnicodeMap(code, unicodeES5IdentifierStart) : + lookupInUnicodeMap(code, unicodeES3IdentifierStart); } function isUnicodeIdentifierPart(code: number, languageVersion: ScriptTarget) { - return languageVersion === ScriptTarget.ES3 ? - lookupInUnicodeMap(code, unicodeES3IdentifierPart) : - lookupInUnicodeMap(code, unicodeES5IdentifierPart); + return languageVersion >= ScriptTarget.ES5 ? + lookupInUnicodeMap(code, unicodeES5IdentifierPart) : + lookupInUnicodeMap(code, unicodeES3IdentifierPart); } function makeReverseMap(source: Map): string[] {