Skip to content

Commit

Permalink
Hardening compiler to accept empty CompilerOptions object
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Jan 15, 2015
1 parent f9f95ba commit 65452aa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 1 addition & 5 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {};
Expand Down
22 changes: 11 additions & 11 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(")");
Expand Down
7 changes: 1 addition & 6 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down

1 comment on commit 65452aa

@DanielRosenwasser
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty gross for the negated cases. I'd rather we just use a helper function.

Please sign in to comment.