Skip to content

Commit

Permalink
Merge branch 'master' into destructuring
Browse files Browse the repository at this point in the history
Move downlevel vs. ES6 emit branching into individual emit functions
  • Loading branch information
ahejlsberg committed Dec 8, 2014
2 parents b3dffff + b6f1225 commit 459dee0
Show file tree
Hide file tree
Showing 209 changed files with 5,509 additions and 3,471 deletions.
17 changes: 12 additions & 5 deletions Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ desc("Builds the test infrastructure using the built compiler");
task("tests", ["local", run].concat(libraryTargets));

function exec(cmd, completeHandler) {
var ex = jake.createExec([cmd]);
var ex = jake.createExec([cmd], {windowsVerbatimArguments: true});
// Add listeners for output and error
ex.addListener("stdout", function(output) {
process.stdout.write(output);
Expand Down Expand Up @@ -488,18 +488,25 @@ task("runtests-browser", ["tests", "browserify", builtLocalDirectory], function(
exec(cmd);
}, {async: true});

function getDiffTool() {
var program = process.env['DIFF']
if (!program) {
fail("Add the 'DIFF' environment variable to the path of the program you want to use.")
}
return program;
}

// Baseline Diff
desc("Diffs the compiler baselines using the diff tool specified by the %DIFF% environment variable");
desc("Diffs the compiler baselines using the diff tool specified by the 'DIFF' environment variable");
task('diff', function () {
var cmd = "%DIFF% " + refBaseline + ' ' + localBaseline;
var cmd = '"' + getDiffTool() + '" ' + refBaseline + ' ' + localBaseline;
console.log(cmd)
exec(cmd);
}, {async: true});

desc("Diffs the RWC baselines using the diff tool specified by the %DIFF% environment variable");
desc("Diffs the RWC baselines using the diff tool specified by the 'DIFF' environment variable");
task('diff-rwc', function () {
var cmd = "%DIFF% " + refRwcBaseline + ' ' + localRwcBaseline;
var cmd = '"' + getDiffTool() + '" ' + refRwcBaseline + ' ' + localRwcBaseline;
console.log(cmd)
exec(cmd);
}, {async: true});
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,12 @@ module ts {
bindDeclaration(<Declaration>node, SymbolFlags.Signature, 0, /*isBlockScopeContainer*/ false);
break;
case SyntaxKind.Method:
bindDeclaration(<Declaration>node, SymbolFlags.Method | ((<MethodDeclaration>node).questionToken ? SymbolFlags.Optional : 0), SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
// If this is an ObjectLiteralExpression method, then it sits in the same space
// as other properties in the object literal. So we use SymbolFlags.PropertyExcludes
// so that it will conflict with any other object literal members with the same
// name.
bindDeclaration(<Declaration>node, SymbolFlags.Method | ((<MethodDeclaration>node).questionToken ? SymbolFlags.Optional : 0),
isObjectLiteralMethod(node) ? SymbolFlags.PropertyExcludes : SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
break;
case SyntaxKind.FunctionDeclaration:
bindDeclaration(<Declaration>node, SymbolFlags.Function, SymbolFlags.FunctionExcludes, /*isBlockScopeContainer*/ true);
Expand Down
283 changes: 184 additions & 99 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ module ts {
export interface StringSet extends Map<any> { }

export function forEach<T, U>(array: T[], callback: (element: T) => U): U {
var result: U;
if (array) {
for (var i = 0, len = array.length; i < len; i++) {
if (result = callback(array[i])) {
break;
var result = callback(array[i]);
if (result) {
return result;
}
}
}
return result;
return undefined;
}

export function contains<T>(array: T[], value: T): boolean {
Expand Down
77 changes: 33 additions & 44 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference path="core.ts"/>
/// <reference path="scanner.ts"/>
/// <reference path="parser.ts"/>
/// <reference path="binder.ts"/>

module ts {
interface EmitTextWriter {
Expand Down Expand Up @@ -2281,27 +2282,31 @@ module ts {
emit(node.expression);
write("]");
}

function emitPropertyAssignment(node: PropertyDeclaration) {

function emitMethod(node: MethodDeclaration) {
if (!isObjectLiteralMethod(node)) {
return;
}
emitLeadingComments(node);
emit(node.name);
write(": ");
emit(node.initializer);
if (compilerOptions.target < ScriptTarget.ES6) {
write(": function ");
}
emitSignatureAndBody(node);
emitTrailingComments(node);
}

function emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) {
function emitPropertyAssignment(node: PropertyDeclaration) {
emitLeadingComments(node);
// Emit identifier as an identifier
emit(node.name);
write(": ");
// Even though this is stored as identifier treat it as an expression
// Short-hand, { x }, is equivalent of normal form { x: x }
emitExpressionIdentifier(node.name);
emit(node.initializer);
emitTrailingComments(node);
}

function emitShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) {
function emitShorthandPropertyAssignment(node: ShorthandPropertyAssignment) {
emitLeadingComments(node);
emit(node.name);
// If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment. For example:
// module m {
// export var y;
Expand All @@ -2310,16 +2315,14 @@ 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
var prefix = resolver.getExpressionNamePrefix(node.name);
if (prefix) {
emitDownlevelShorthandPropertyAssignment(node);
}
// If short-hand property has no prefix, emit it as short-hand.
else {
emitLeadingComments(node);
emit(node.name);
emitTrailingComments(node);
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
// Short-hand, { x }, is equivalent of normal form { x: x }
emitExpressionIdentifier(node.name);
}
emitTrailingComments(node);
}

function tryEmitConstantValue(node: PropertyAccessExpression | ElementAccessExpression): boolean {
Expand Down Expand Up @@ -2868,8 +2871,8 @@ module ts {
var p = properties[i];
if (p.kind === SyntaxKind.PropertyAssignment || p.kind === SyntaxKind.ShorthandPropertyAssignment) {
// TODO(andersh): Computed property support
var propName = <Identifier>((<PropertyDeclaration>p).name);
emitDestructuringAssignment((<PropertyDeclaration>p).initializer || propName, createPropertyAccess(value, propName));
var propName = <Identifier>((<PropertyAssignment>p).name);
emitDestructuringAssignment((<PropertyAssignment>p).initializer || propName, createPropertyAccess(value, propName));
}
}
}
Expand Down Expand Up @@ -3139,17 +3142,17 @@ module ts {
scopeEmitStart(node);
increaseIndent();

emitDetachedComments(node.body.kind === SyntaxKind.FunctionBlock ? (<Block>node.body).statements : node.body);
emitDetachedComments(node.body.kind === SyntaxKind.Block ? (<Block>node.body).statements : node.body);

var startIndex = 0;
if (node.body.kind === SyntaxKind.FunctionBlock) {
if (node.body.kind === SyntaxKind.Block) {
startIndex = emitDirectivePrologues((<Block>node.body).statements, /*startWithNewLine*/ true);
}
var outPos = writer.getTextPos();
emitCaptureThisForNodeIfNecessary(node);
emitDefaultValueAssignments(node);
emitRestParameter(node);
if (node.body.kind !== SyntaxKind.FunctionBlock && outPos === writer.getTextPos()) {
if (node.body.kind !== SyntaxKind.Block && outPos === writer.getTextPos()) {
decreaseIndent();
write(" ");
emitStart(node.body);
Expand All @@ -3164,7 +3167,7 @@ module ts {
emitEnd(node.body);
}
else {
if (node.body.kind === SyntaxKind.FunctionBlock) {
if (node.body.kind === SyntaxKind.Block) {
emitLinesStartingAt((<Block>node.body).statements, startIndex);
}
else {
Expand All @@ -3177,7 +3180,7 @@ module ts {
}
emitTempDeclarations(/*newLine*/ true);
writeLine();
if (node.body.kind === SyntaxKind.FunctionBlock) {
if (node.body.kind === SyntaxKind.Block) {
emitLeadingCommentsOfPosition((<Block>node.body).statements.end);
decreaseIndent();
emitToken(SyntaxKind.CloseBraceToken,(<Block>node.body).statements.end);
Expand Down Expand Up @@ -3812,6 +3815,8 @@ module ts {
return emitIdentifier(<Identifier>node);
case SyntaxKind.Parameter:
return emitParameter(<ParameterDeclaration>node);
case SyntaxKind.Method:
return emitMethod(<MethodDeclaration>node);
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return emitAccessor(<AccessorDeclaration>node);
Expand Down Expand Up @@ -3849,6 +3854,8 @@ module ts {
return emitObjectLiteral(<ObjectLiteralExpression>node);
case SyntaxKind.PropertyAssignment:
return emitPropertyAssignment(<PropertyDeclaration>node);
case SyntaxKind.ShorthandPropertyAssignment:
return emitShorthandPropertyAssignment(<ShorthandPropertyAssignment>node);
case SyntaxKind.ComputedPropertyName:
return emitComputedPropertyName(<ComputedPropertyName>node);
case SyntaxKind.PropertyAccessExpression:
Expand Down Expand Up @@ -3888,7 +3895,6 @@ module ts {
case SyntaxKind.Block:
case SyntaxKind.TryBlock:
case SyntaxKind.FinallyBlock:
case SyntaxKind.FunctionBlock:
case SyntaxKind.ModuleBlock:
return emitBlock(<Block>node);
case SyntaxKind.VariableStatement:
Expand Down Expand Up @@ -3944,23 +3950,6 @@ module ts {
case SyntaxKind.SourceFile:
return emitSourceFile(<SourceFile>node);
}

// Emit node which needs to be emitted differently depended on ScriptTarget
if (compilerOptions.target < ScriptTarget.ES6) {
// Emit node down-level
switch (node.kind) {
case SyntaxKind.ShorthandPropertyAssignment:
return emitDownlevelShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
}
}
else {
// Emit node natively
Debug.assert(compilerOptions.target >= ScriptTarget.ES6, "Invalid ScriptTarget. We should emit as ES6 or above");
switch (node.kind) {
case SyntaxKind.ShorthandPropertyAssignment:
return emitShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
}
}
}

function hasDetachedComments(pos: number) {
Expand Down
Loading

0 comments on commit 459dee0

Please sign in to comment.