Hardening compiler to accept empty CompilerOptions object

This commit is contained in:
Anders Hejlsberg
2015-01-15 15:57:08 -08:00
parent f9f95ba614
commit 65452aa011
5 changed files with 23 additions and 32 deletions

View File

@@ -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 = <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 && (<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);
}

View File

@@ -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<string> = {};

View File

@@ -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 ((<BinaryExpression>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(")");

View File

@@ -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);

View File

@@ -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<number>): string[] {