diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 274a7980ff1..7b78e19c5ef 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16,6 +16,8 @@ module ts { var emptySymbols: SymbolTable = {}; var compilerOptions = host.getCompilerOptions(); + var languageVersion = compilerOptions.target || ScriptTarget.ES3; + var emitResolver = createResolver(); var checker: TypeChecker = { @@ -6340,7 +6342,7 @@ module ts { function checkTaggedTemplateExpression(node: TaggedTemplateExpression): Type { // Grammar checking - if (!(compilerOptions.target >= ScriptTarget.ES6)) { + if (languageVersion < ScriptTarget.ES6) { grammarErrorOnFirstToken(node.template, Diagnostics.Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher); } @@ -10032,7 +10034,7 @@ module ts { globalRegExpType = getGlobalType("RegExp"); // If we're in ES6 mode, load the TemplateStringsArray. // Otherwise, default to 'unknown' for the purposes of type checking in LS scenarios. - globalTemplateStringsArrayType = compilerOptions.target >= ScriptTarget.ES6 + globalTemplateStringsArrayType = languageVersion >= ScriptTarget.ES6 ? getGlobalType("TemplateStringsArray") : unknownType; anyArrayType = createArrayType(anyType); @@ -10401,7 +10403,7 @@ module ts { return; var computedPropertyName = node; - if (!(compilerOptions.target >= ScriptTarget.ES6)) { + if (languageVersion < 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 +10503,7 @@ module ts { function checkGrammarAccessor(accessor: MethodDeclaration): boolean { var kind = accessor.kind; - if (!(compilerOptions.target >= ScriptTarget.ES5)) { + if (languageVersion < ScriptTarget.ES5) { return grammarErrorOnNode(accessor.name, Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); } else if (isInAmbientContext(accessor)) { @@ -10706,7 +10708,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 (languageVersion < ScriptTarget.ES6) { if (isLet(declarationList)) { return grammarErrorOnFirstToken(declarationList, Diagnostics.let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher); } @@ -10808,7 +10810,7 @@ module ts { function grammarErrorOnFirstToken(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean { var sourceFile = getSourceFileOfNode(node); if (!hasParseDiagnostics(sourceFile)) { - var scanner = createScanner(compilerOptions.target, /*skipTrivia*/ true, sourceFile.text); + var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceFile.text); var start = scanToken(scanner, node.pos); diagnostics.push(createFileDiagnostic(sourceFile, start, scanner.getTextPos() - start, message, arg0, arg1, arg2)); return true; @@ -10950,7 +10952,7 @@ module ts { if (node.parserContextFlags & ParserContextFlags.StrictMode) { return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode); } - else if (compilerOptions.target >= ScriptTarget.ES5) { + else if (languageVersion >= ScriptTarget.ES5) { return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); } } @@ -10959,7 +10961,7 @@ module ts { function grammarErrorAfterFirstToken(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean { var sourceFile = getSourceFileOfNode(node); if (!hasParseDiagnostics(sourceFile)) { - var scanner = createScanner(compilerOptions.target, /*skipTrivia*/ true, sourceFile.text); + var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceFile.text); scanToken(scanner, node.pos); diagnostics.push(createFileDiagnostic(sourceFile, scanner.getTextPos(), 0, message, arg0, arg1, arg2)); return true; diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index abef385c76c..25cb7a8b2ee 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -339,6 +339,7 @@ module ts { function emitDeclarations(host: EmitHost, resolver: EmitResolver, diagnostics: Diagnostic[], jsFilePath: string, root?: SourceFile): DeclarationEmit { var newLine = host.getNewLine(); var compilerOptions = host.getCompilerOptions(); + var languageVersion = compilerOptions.target || ScriptTarget.ES3; var write: (s: string) => void; var writeLine: () => void; @@ -1473,6 +1474,7 @@ module ts { export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile?: SourceFile): EmitResult { // var program = resolver.getProgram(); var compilerOptions = host.getCompilerOptions(); + var languageVersion = compilerOptions.target || ScriptTarget.ES3; var sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap ? [] : undefined; var diagnostics: Diagnostic[] = []; var newLine = host.getNewLine(); @@ -2021,14 +2023,14 @@ module ts { } function emitLiteral(node: LiteralExpression) { - var text = !(compilerOptions.target >= ScriptTarget.ES6) && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) : + var text = languageVersion < 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 (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) { write(node.text); } else { @@ -2043,7 +2045,7 @@ module ts { function emitTemplateExpression(node: TemplateExpression): void { // In ES6 mode and above, we can simply emit each portion of a template in order, but in // ES3 & ES5 we must convert the template expression into a series of string concatenations. - if (compilerOptions.target >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES6) { forEachChild(node, emit); return; } @@ -2150,7 +2152,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.ES6)); + Debug.assert(languageVersion < ScriptTarget.ES6); switch (expression.kind) { case SyntaxKind.BinaryExpression: switch ((expression).operator) { @@ -2335,7 +2337,7 @@ module ts { write("[]"); return; } - if (compilerOptions.target >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES6) { write("["); emitList(elements, 0, elements.length, /*multiLine*/(node.flags & NodeFlags.MultiLine) !== 0, /*trailingComma*/ elements.hasTrailingComma); @@ -2385,7 +2387,7 @@ module ts { write(" "); } emitList(properties, 0, properties.length, /*multiLine*/ multiLine, - /*trailingComma*/ properties.hasTrailingComma && compilerOptions.target >= ScriptTarget.ES5); + /*trailingComma*/ properties.hasTrailingComma && languageVersion >= ScriptTarget.ES5); if (!multiLine) { write(" "); } @@ -2405,7 +2407,7 @@ module ts { } emitLeadingComments(node); emit(node.name); - if (!(compilerOptions.target >= ScriptTarget.ES6)) { + if (languageVersion < ScriptTarget.ES6) { write(": function "); } emitSignatureAndBody(node); @@ -2431,7 +2433,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 (languageVersion < ScriptTarget.ES6 || resolver.getExpressionNamePrefix(node.name)) { // Emit identifier as an identifier write(": "); // Even though this is stored as identifier treat it as an expression @@ -2513,7 +2515,7 @@ module ts { } function emitTaggedTemplateExpression(node: TaggedTemplateExpression): void { - Debug.assert(compilerOptions.target >= ScriptTarget.ES6, "Trying to emit a tagged template in pre-ES6 mode."); + Debug.assert(languageVersion >= ScriptTarget.ES6, "Trying to emit a tagged template in pre-ES6 mode."); emit(node.tag); write(" "); emit(node.template); @@ -2605,7 +2607,7 @@ module ts { function emitBinaryExpression(node: BinaryExpression) { - if (!(compilerOptions.target >= ScriptTarget.ES6) && node.operator === SyntaxKind.EqualsToken && + if (languageVersion < ScriptTarget.ES6 && node.operator === SyntaxKind.EqualsToken && (node.left.kind === SyntaxKind.ObjectLiteralExpression || node.left.kind === SyntaxKind.ArrayLiteralExpression)) { emitDestructuring(node); } @@ -3101,7 +3103,7 @@ module ts { function emitVariableDeclaration(node: VariableDeclaration) { emitLeadingComments(node); if (isBindingPattern(node.name)) { - if (!(compilerOptions.target >= ScriptTarget.ES6)) { + if (languageVersion < ScriptTarget.ES6) { emitDestructuring(node); } else { @@ -3136,7 +3138,7 @@ module ts { function emitParameter(node: ParameterDeclaration) { emitLeadingComments(node); - if (!(compilerOptions.target >= ScriptTarget.ES6)) { + if (languageVersion < ScriptTarget.ES6) { if (isBindingPattern(node.name)) { var name = createTempVariable(node); if (!tempParameters) { @@ -3160,7 +3162,7 @@ module ts { } function emitDefaultValueAssignments(node: FunctionLikeDeclaration) { - if (!(compilerOptions.target >= ScriptTarget.ES6)) { + if (languageVersion < ScriptTarget.ES6) { var tempIndex = 0; forEach(node.parameters, p => { if (isBindingPattern(p.name)) { @@ -3190,7 +3192,7 @@ module ts { } function emitRestParameter(node: FunctionLikeDeclaration) { - if (!(compilerOptions.target >= ScriptTarget.ES6) && hasRestParameters(node)) { + if (languageVersion < ScriptTarget.ES6 && hasRestParameters(node)) { var restIndex = node.parameters.length - 1; var restParam = node.parameters[restIndex]; var tempName = createTempVariable(node, /*forLoopVariable*/ true).text; @@ -3269,7 +3271,7 @@ module ts { write("("); if (node) { var parameters = node.parameters; - var omitCount = !(compilerOptions.target >= ScriptTarget.ES6) && hasRestParameters(node) ? 1 : 0; + var omitCount = languageVersion < ScriptTarget.ES6 && hasRestParameters(node) ? 1 : 0; emitList(parameters, 0, parameters.length - omitCount, /*multiLine*/ false, /*trailingComma*/ false); } write(")");