Skip to content

Commit 65452aa

Browse files
committed
Hardening compiler to accept empty CompilerOptions object
1 parent f9f95ba commit 65452aa

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

src/compiler/checker.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6340,7 +6340,7 @@ module ts {
63406340

63416341
function checkTaggedTemplateExpression(node: TaggedTemplateExpression): Type {
63426342
// Grammar checking
6343-
if (compilerOptions.target < ScriptTarget.ES6) {
6343+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
63446344
grammarErrorOnFirstToken(node.template, Diagnostics.Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher);
63456345
}
63466346

@@ -10401,7 +10401,7 @@ module ts {
1040110401
return;
1040210402

1040310403
var computedPropertyName = <ComputedPropertyName>node;
10404-
if (compilerOptions.target < ScriptTarget.ES6) {
10404+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
1040510405
grammarErrorOnNode(node, Diagnostics.Computed_property_names_are_only_available_when_targeting_ECMAScript_6_and_higher);
1040610406
}
1040710407
else if (computedPropertyName.expression.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>computedPropertyName.expression).operator === SyntaxKind.CommaToken) {
@@ -10501,7 +10501,7 @@ module ts {
1050110501

1050210502
function checkGrammarAccessor(accessor: MethodDeclaration): boolean {
1050310503
var kind = accessor.kind;
10504-
if (compilerOptions.target < ScriptTarget.ES5) {
10504+
if (!(compilerOptions.target >= ScriptTarget.ES5)) {
1050510505
return grammarErrorOnNode(accessor.name, Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher);
1050610506
}
1050710507
else if (isInAmbientContext(accessor)) {
@@ -10706,7 +10706,7 @@ module ts {
1070610706
return grammarErrorAtPos(getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, Diagnostics.Variable_declaration_list_cannot_be_empty);
1070710707
}
1070810708

10709-
if (compilerOptions.target < ScriptTarget.ES6) {
10709+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
1071010710
if (isLet(declarationList)) {
1071110711
return grammarErrorOnFirstToken(declarationList, Diagnostics.let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
1071210712
}

src/compiler/commandLineParser.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,7 @@ module ts {
157157
];
158158

159159
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
160-
// Set default compiler option values
161-
var options: CompilerOptions = {
162-
target: ScriptTarget.ES3,
163-
module: ModuleKind.None
164-
};
160+
var options: CompilerOptions = {};
165161
var filenames: string[] = [];
166162
var errors: Diagnostic[] = [];
167163
var shortOptionNames: Map<string> = {};

src/compiler/emitter.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -2021,14 +2021,14 @@ module ts {
20212021
}
20222022

20232023
function emitLiteral(node: LiteralExpression) {
2024-
var text = compilerOptions.target < ScriptTarget.ES6 && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) :
2024+
var text = !(compilerOptions.target >= ScriptTarget.ES6) && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) :
20252025
node.parent ? getSourceTextOfNodeFromSourceFile(currentSourceFile, node) :
20262026
node.text;
20272027
if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
20282028
writer.writeLiteral(text);
20292029
}
20302030
// For version below ES6, emit binary integer literal and octal integer literal in canonical form
2031-
else if (compilerOptions.target < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) {
2031+
else if (!(compilerOptions.target >= ScriptTarget.ES6) && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) {
20322032
write(node.text);
20332033
}
20342034
else {
@@ -2150,7 +2150,7 @@ module ts {
21502150
//
21512151
// TODO (drosen): Note that we need to account for the upcoming 'yield' and
21522152
// spread ('...') unary operators that are anticipated for ES6.
2153-
Debug.assert(compilerOptions.target <= ScriptTarget.ES5);
2153+
Debug.assert(!(compilerOptions.target >= ScriptTarget.ES6));
21542154
switch (expression.kind) {
21552155
case SyntaxKind.BinaryExpression:
21562156
switch ((<BinaryExpression>expression).operator) {
@@ -2405,7 +2405,7 @@ module ts {
24052405
}
24062406
emitLeadingComments(node);
24072407
emit(node.name);
2408-
if (compilerOptions.target < ScriptTarget.ES6) {
2408+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
24092409
write(": function ");
24102410
}
24112411
emitSignatureAndBody(node);
@@ -2431,7 +2431,7 @@ module ts {
24312431
// export var obj = { y };
24322432
// }
24332433
// The short-hand property in obj need to emit as such ... = { y : m.y } regardless of the TargetScript version
2434-
if (compilerOptions.target < ScriptTarget.ES6 || resolver.getExpressionNamePrefix(node.name)) {
2434+
if (!(compilerOptions.target >= ScriptTarget.ES6) || resolver.getExpressionNamePrefix(node.name)) {
24352435
// Emit identifier as an identifier
24362436
write(": ");
24372437
// Even though this is stored as identifier treat it as an expression
@@ -2605,7 +2605,7 @@ module ts {
26052605

26062606

26072607
function emitBinaryExpression(node: BinaryExpression) {
2608-
if (compilerOptions.target < ScriptTarget.ES6 && node.operator === SyntaxKind.EqualsToken &&
2608+
if (!(compilerOptions.target >= ScriptTarget.ES6) && node.operator === SyntaxKind.EqualsToken &&
26092609
(node.left.kind === SyntaxKind.ObjectLiteralExpression || node.left.kind === SyntaxKind.ArrayLiteralExpression)) {
26102610
emitDestructuring(node);
26112611
}
@@ -3101,7 +3101,7 @@ module ts {
31013101
function emitVariableDeclaration(node: VariableDeclaration) {
31023102
emitLeadingComments(node);
31033103
if (isBindingPattern(node.name)) {
3104-
if (compilerOptions.target < ScriptTarget.ES6) {
3104+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
31053105
emitDestructuring(node);
31063106
}
31073107
else {
@@ -3136,7 +3136,7 @@ module ts {
31363136

31373137
function emitParameter(node: ParameterDeclaration) {
31383138
emitLeadingComments(node);
3139-
if (compilerOptions.target < ScriptTarget.ES6) {
3139+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
31403140
if (isBindingPattern(node.name)) {
31413141
var name = createTempVariable(node);
31423142
if (!tempParameters) {
@@ -3160,7 +3160,7 @@ module ts {
31603160
}
31613161

31623162
function emitDefaultValueAssignments(node: FunctionLikeDeclaration) {
3163-
if (compilerOptions.target < ScriptTarget.ES6) {
3163+
if (!(compilerOptions.target >= ScriptTarget.ES6)) {
31643164
var tempIndex = 0;
31653165
forEach(node.parameters, p => {
31663166
if (isBindingPattern(p.name)) {
@@ -3190,7 +3190,7 @@ module ts {
31903190
}
31913191

31923192
function emitRestParameter(node: FunctionLikeDeclaration) {
3193-
if (compilerOptions.target < ScriptTarget.ES6 && hasRestParameters(node)) {
3193+
if (!(compilerOptions.target >= ScriptTarget.ES6) && hasRestParameters(node)) {
31943194
var restIndex = node.parameters.length - 1;
31953195
var restParam = node.parameters[restIndex];
31963196
var tempName = createTempVariable(node, /*forLoopVariable*/ true).text;
@@ -3269,7 +3269,7 @@ module ts {
32693269
write("(");
32703270
if (node) {
32713271
var parameters = node.parameters;
3272-
var omitCount = compilerOptions.target < ScriptTarget.ES6 && hasRestParameters(node) ? 1 : 0;
3272+
var omitCount = !(compilerOptions.target >= ScriptTarget.ES6) && hasRestParameters(node) ? 1 : 0;
32733273
emitList(parameters, 0, parameters.length - omitCount, /*multiLine*/ false, /*trailingComma*/ false);
32743274
}
32753275
write(")");

src/compiler/program.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ module ts {
8181
var seenNoDefaultLib = options.noLib;
8282
var commonSourceDirectory: string;
8383

84-
//options = extend(options, {
85-
// module: ModuleKind.None,
86-
// target: ScriptTarget.ES3
87-
//});
88-
8984
forEach(rootNames, name => processRootFile(name, false));
9085
if (!seenNoDefaultLib) {
9186
processRootFile(host.getDefaultLibFilename(options), true);
@@ -347,7 +342,7 @@ module ts {
347342
}
348343

349344
var firstExternalModule = forEach(files, f => isExternalModule(f) ? f : undefined);
350-
if (firstExternalModule && options.module === ModuleKind.None) {
345+
if (firstExternalModule && !options.module) {
351346
// We cannot use createDiagnosticFromNode because nodes do not have parents yet
352347
var externalModuleErrorSpan = getErrorSpanForNode(firstExternalModule.externalModuleIndicator);
353348
var errorStart = skipTrivia(firstExternalModule.text, externalModuleErrorSpan.pos);

src/compiler/scanner.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ module ts {
224224
}
225225

226226
function isUnicodeIdentifierStart(code: number, languageVersion: ScriptTarget) {
227-
return languageVersion === ScriptTarget.ES3 ?
228-
lookupInUnicodeMap(code, unicodeES3IdentifierStart) :
229-
lookupInUnicodeMap(code, unicodeES5IdentifierStart);
227+
return languageVersion >= ScriptTarget.ES5 ?
228+
lookupInUnicodeMap(code, unicodeES5IdentifierStart) :
229+
lookupInUnicodeMap(code, unicodeES3IdentifierStart);
230230
}
231231

232232
function isUnicodeIdentifierPart(code: number, languageVersion: ScriptTarget) {
233-
return languageVersion === ScriptTarget.ES3 ?
234-
lookupInUnicodeMap(code, unicodeES3IdentifierPart) :
235-
lookupInUnicodeMap(code, unicodeES5IdentifierPart);
233+
return languageVersion >= ScriptTarget.ES5 ?
234+
lookupInUnicodeMap(code, unicodeES5IdentifierPart) :
235+
lookupInUnicodeMap(code, unicodeES3IdentifierPart);
236236
}
237237

238238
function makeReverseMap(source: Map<number>): string[] {

0 commit comments

Comments
 (0)