Merge branch 'transforms-transformer-es6' into transforms-transformer-module

This commit is contained in:
Ron Buckton 2016-03-02 15:47:46 -08:00
commit c4a75babc6
5 changed files with 129 additions and 212 deletions

View File

@ -152,8 +152,7 @@ const _super = (function (geti, seti) {
let isEmitNotificationEnabled: (node: Node) => boolean;
let expressionSubstitution: (node: Expression) => Expression;
let identifierSubstitution: (node: Identifier) => Identifier;
let onBeforeEmitNode: (node: Node) => void;
let onAfterEmitNode: (node: Node) => void;
let onEmitNode: (node: Node, emit: (node: Node) => void) => void;
let nodeToGeneratedName: string[];
let generatedNameSet: Map<string>;
let tempFlags: TempFlags;
@ -215,8 +214,7 @@ const _super = (function (geti, seti) {
isEmitNotificationEnabled = undefined;
expressionSubstitution = undefined;
identifierSubstitution = undefined;
onBeforeEmitNode = undefined;
onAfterEmitNode = undefined;
onEmitNode = undefined;
tempFlags = TempFlags.Auto;
currentSourceFile = undefined;
currentText = undefined;
@ -237,8 +235,7 @@ const _super = (function (geti, seti) {
isEmitNotificationEnabled = context.isEmitNotificationEnabled;
expressionSubstitution = context.expressionSubstitution;
identifierSubstitution = context.identifierSubstitution;
onBeforeEmitNode = context.onBeforeEmitNode;
onAfterEmitNode = context.onAfterEmitNode;
onEmitNode = context.onEmitNode;
return printSourceFile;
}
@ -252,21 +249,52 @@ const _super = (function (geti, seti) {
return node;
}
/**
* Emits a node.
*/
function emit(node: Node) {
emitWithWorker(node, emitWorker);
emitNodeWithNotificationOption(node, emitWithoutNotificationOption);
}
/**
* Emits a node without calling onEmitNode.
* NOTE: Do not call this method directly.
*/
function emitWithoutNotificationOption(node: Node) {
emitNodeWithWorker(node, emitWorker);
}
/**
* Emits an expression node.
*/
function emitExpression(node: Expression) {
emitWithWorker(node, emitExpressionWorker);
emitNodeWithNotificationOption(node, emitExpressionWithoutNotificationOption);
}
function emitWithWorker(node: Node, emitWorker: (node: Node) => void) {
if (node) {
const adviseOnEmit = isEmitNotificationEnabled(node);
if (adviseOnEmit && onBeforeEmitNode) {
onBeforeEmitNode(node);
}
/**
* Emits an expression without calling onEmitNode.
* NOTE: Do not call this method directly.
*/
function emitExpressionWithoutNotificationOption(node: Expression) {
emitNodeWithWorker(node, emitExpressionWorker);
}
/**
* Emits a node with emit notification if available.
*/
function emitNodeWithNotificationOption(node: Node, emit: (node: Node) => void) {
if (node) {
if (isEmitNotificationEnabled(node)) {
onEmitNode(node, emit);
}
else {
emit(node);
}
}
}
function emitNodeWithWorker(node: Node, emitWorker: (node: Node) => void) {
if (node) {
const leadingComments = getLeadingComments(node, getNotEmittedParent);
const trailingComments = getTrailingComments(node, getNotEmittedParent);
emitLeadingComments(node, leadingComments);
@ -274,24 +302,9 @@ const _super = (function (geti, seti) {
emitWorker(node);
emitEnd(node);
emitTrailingComments(node, trailingComments);
if (adviseOnEmit && onAfterEmitNode) {
onAfterEmitNode(node);
}
}
}
function getNotEmittedParent(node: Node): Node {
if (getNodeEmitFlags(node) & NodeEmitFlags.EmitCommentsOfNotEmittedParent) {
const parent = getOriginalNode(node).parent;
if (getNodeEmitFlags(parent) & NodeEmitFlags.IsNotEmittedNode) {
return parent;
}
}
return undefined;
}
function emitWorker(node: Node): void {
const kind = node.kind;
switch (kind) {
@ -2367,6 +2380,17 @@ const _super = (function (geti, seti) {
&& rangeEndIsOnSameLineAsRangeStart(block, block);
}
function getNotEmittedParent(node: Node): Node {
if (getNodeEmitFlags(node) & NodeEmitFlags.EmitCommentsOfNotEmittedParent) {
const parent = getOriginalNode(node).parent;
if (getNodeEmitFlags(parent) & NodeEmitFlags.IsNotEmittedNode) {
return parent;
}
}
return undefined;
}
function isUniqueName(name: string): boolean {
return !resolver.hasGlobalName(name) &&
!hasProperty(currentFileIdentifiers, name) &&

View File

@ -78,8 +78,7 @@ namespace ts {
expressionSubstitution: node => node,
enableExpressionSubstitution,
isExpressionSubstitutionEnabled,
onBeforeEmitNode: node => { },
onAfterEmitNode: node => { },
onEmitNode: (node, emit) => emit(node),
enableEmitNotification,
isEmitNotificationEnabled,
};

View File

@ -22,10 +22,8 @@ namespace ts {
const resolver = context.getEmitResolver();
const previousIdentifierSubstitution = context.identifierSubstitution;
const previousExpressionSubstitution = context.expressionSubstitution;
const previousOnBeforeEmitNode = context.onBeforeEmitNode;
const previousOnAfterEmitNode = context.onAfterEmitNode;
context.onBeforeEmitNode = onBeforeEmitNode;
context.onAfterEmitNode = onAfterEmitNode;
const previousOnEmitNode = context.onEmitNode;
context.onEmitNode = onEmitNode;
context.identifierSubstitution = substituteIdentifier;
context.expressionSubstitution = substituteExpression;
@ -44,23 +42,9 @@ namespace ts {
let enabledSubstitutions: ES6SubstitutionFlags;
/**
* Keeps track of how deeply nested we are within function-likes when printing
* nodes. This is used to determine whether we need to emit `_this` instead of
* `this`.
* This is used to determine whether we need to emit `_this` instead of `this`.
*/
let containingFunctionDepth: number;
/**
* The first 31 bits are used to determine whether a containing function is an
* arrow function.
*/
let containingFunctionState: number;
/**
* If the containingFunctionDepth grows beyond 31 nested function-likes, this
* array is used as a stack to track deeper levels of nesting.
*/
let containingFunctionStack: number[];
let useCapturedThis: boolean;
return transformSourceFile;
@ -1715,28 +1699,18 @@ namespace ts {
*
* @param node The node to be printed.
*/
function onBeforeEmitNode(node: Node) {
previousOnBeforeEmitNode(node);
function onEmitNode(node: Node, emit: (node: Node) => void) {
const savedUseCapturedThis = useCapturedThis;
if (enabledSubstitutions & ES6SubstitutionFlags.CapturedThis && isFunctionLike(node)) {
// If we are tracking a captured `this`, push a bit that indicates whether the
// containing function is an arrow function.
pushContainingFunction(node.kind === SyntaxKind.ArrowFunction);
useCapturedThis = node.kind === SyntaxKind.ArrowFunction;
}
}
/**
* Called by the printer just after a node is printed.
*
* @param node The node that was printed.
*/
function onAfterEmitNode(node: Node) {
previousOnAfterEmitNode(node);
previousOnEmitNode(node, emit);
if (enabledSubstitutions & ES6SubstitutionFlags.CapturedThis && isFunctionLike(node)) {
// If we are tracking a captured `this`, pop the last containing function bit.
popContainingFunction();
}
useCapturedThis = savedUseCapturedThis;
}
/**
@ -1757,7 +1731,6 @@ namespace ts {
function enableSubstitutionsForCapturedThis() {
if ((enabledSubstitutions & ES6SubstitutionFlags.CapturedThis) === 0) {
enabledSubstitutions |= ES6SubstitutionFlags.CapturedThis;
containingFunctionDepth = 0;
context.enableExpressionSubstitution(SyntaxKind.ThisKeyword);
context.enableEmitNotification(SyntaxKind.Constructor);
context.enableEmitNotification(SyntaxKind.MethodDeclaration);
@ -1850,68 +1823,13 @@ namespace ts {
* @param node The ThisKeyword node.
*/
function substituteThisKeyword(node: PrimaryExpression): PrimaryExpression {
if (enabledSubstitutions & ES6SubstitutionFlags.CapturedThis && isContainedInArrowFunction()) {
if (enabledSubstitutions & ES6SubstitutionFlags.CapturedThis && useCapturedThis) {
return createIdentifier("_this", /*location*/ node);
}
return node;
}
/**
* Pushes a value onto a stack that indicates whether we are currently printing a node
* within an arrow function. This is used to determine whether we need to capture `this`.
*
* @param isArrowFunction A value indicating whether the current function container is
* an arrow function.
*/
function pushContainingFunction(isArrowFunction: boolean) {
// Encode whether the containing function is an arrow function in the first 31 bits of
// an integer. If the stack grows beyond a depth of 31 functions, use an array.
if (containingFunctionDepth > 0 && containingFunctionDepth % 31 === 0) {
if (!containingFunctionStack) {
containingFunctionStack = [containingFunctionState];
}
else {
containingFunctionStack.push(containingFunctionState);
}
containingFunctionState = 0;
}
if (isArrowFunction) {
containingFunctionState |= 1 << (containingFunctionDepth % 31);
}
containingFunctionDepth++;
}
/**
* Pops a value off of the containing function stack.
*/
function popContainingFunction() {
if (containingFunctionDepth > 0) {
containingFunctionDepth--;
if (containingFunctionDepth === 0) {
containingFunctionState = 0;
}
else if (containingFunctionDepth % 31 === 0) {
containingFunctionState = containingFunctionStack.pop();
}
else {
containingFunctionState &= ~(1 << containingFunctionDepth % 31);
}
}
}
/**
* Gets a value indicating whether we are currently printing a node inside of an arrow
* function.
*/
function isContainedInArrowFunction() {
return containingFunctionDepth > 0
&& containingFunctionState & (1 << (containingFunctionDepth - 1) % 31);
}
function getDeclarationName(node: ClassExpression | ClassDeclaration | FunctionDeclaration) {
return node.name ? getSynthesizedClone(node.name) : getGeneratedNameForNode(node);
}

View File

@ -28,13 +28,11 @@ namespace ts {
const languageVersion = getEmitScriptTarget(compilerOptions);
// Save the previous transformation hooks.
const previousOnBeforeEmitNode = context.onBeforeEmitNode;
const previousOnAfterEmitNode = context.onAfterEmitNode;
const previousOnEmitNode = context.onEmitNode;
const previousExpressionSubstitution = context.expressionSubstitution;
// Set new transformation hooks.
context.onBeforeEmitNode = onBeforeEmitNode;
context.onAfterEmitNode = onAfterEmitNode;
context.onEmitNode = onEmitNode;
context.expressionSubstitution = substituteExpression;
// These variables contain state that changes as we descend into the tree.
@ -64,19 +62,16 @@ namespace ts {
let currentDecoratedClassAliases: Map<Identifier>;
/**
* Keeps track of how deeply nested we are within any containing namespaces
* when performing just-in-time substitution while printing an expression identifier.
* If the nest level is greater than zero, then we are performing a substitution
* inside of a namespace and we should perform the more costly checks to determine
* whether the identifier points to an exported declaration.
* Keeps track of whether we are within any containing namespaces when performing
* just-in-time substitution while printing an expression identifier.
*/
let namespaceNestLevel: number;
let isEnclosedInNamespace: boolean;
/**
* This array keeps track of containers where `super` is valid, for use with
* This keeps track of containers where `super` is valid, for use with
* just-in-time substitution for `super` expressions inside of async methods.
*/
let superContainerStack: SuperContainer[];
let currentSuperContainer: SuperContainer;
return transformSourceFile;
@ -2415,21 +2410,24 @@ namespace ts {
// x_1.y = ...;
// })(x || (x = {}));
statements.push(
setOriginalNode(
createStatement(
createCall(
createParen(
createFunctionExpression(
/*asteriskToken*/ undefined,
/*name*/ undefined,
[createParameter(currentNamespaceLocalName)],
transformModuleBody(node)
)
),
[moduleParam]
)
setNodeEmitFlags(
setOriginalNode(
createStatement(
createCall(
createParen(
createFunctionExpression(
/*asteriskToken*/ undefined,
/*name*/ undefined,
[createParameter(currentNamespaceLocalName)],
transformModuleBody(node)
)
),
[moduleParam]
)
),
node
),
node
NodeEmitFlags.AdviseOnEmitNode
)
);
@ -2628,62 +2626,51 @@ namespace ts {
: getClassPrototype(node);
}
function onBeforeEmitNode(node: Node): void {
previousOnBeforeEmitNode(node);
function isClassWithDecorators(node: Node): node is ClassDeclaration {
return node.kind === SyntaxKind.ClassDeclaration && node.decorators !== undefined;
}
function isSuperContainer(node: Node): node is SuperContainer {
const kind = node.kind;
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses
&& kind === SyntaxKind.ClassDeclaration
&& node.decorators) {
return kind === SyntaxKind.ClassDeclaration
|| kind === SyntaxKind.Constructor
|| kind === SyntaxKind.MethodDeclaration
|| kind === SyntaxKind.GetAccessor
|| kind === SyntaxKind.SetAccessor;
}
function isTransformedModuleDeclaration(node: Node): boolean {
return getOriginalNode(node).kind === SyntaxKind.ModuleDeclaration;
}
function onEmitNode(node: Node, emit: (node: Node) => void): void {
const savedIsEnclosedInNamespace = isEnclosedInNamespace;
const savedCurrentSuperContainer = currentSuperContainer;
// If we need support substitutions for aliases for decorated classes,
// we should enable it here.
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses && isClassWithDecorators(node)) {
currentDecoratedClassAliases[getOriginalNodeId(node)] = decoratedClassAliases[getOriginalNodeId(node)];
}
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
&& (kind === SyntaxKind.ClassDeclaration
|| kind === SyntaxKind.Constructor
|| kind === SyntaxKind.MethodDeclaration
|| kind === SyntaxKind.GetAccessor
|| kind === SyntaxKind.SetAccessor)) {
if (!superContainerStack) {
superContainerStack = [];
}
superContainerStack.push(<SuperContainer>node);
// If we need to support substitutions for `super` in an async method,
// we should track it here.
if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper && isSuperContainer(node)) {
currentSuperContainer = node;
}
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
&& kind === SyntaxKind.ModuleDeclaration) {
namespaceNestLevel++;
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports && isTransformedModuleDeclaration(node)) {
isEnclosedInNamespace = true;
}
}
function onAfterEmitNode(node: Node): void {
previousOnAfterEmitNode(node);
previousOnEmitNode(node, emit);
const kind = node.kind;
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses
&& kind === SyntaxKind.ClassDeclaration
&& node.decorators) {
if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses && isClassWithDecorators(node)) {
currentDecoratedClassAliases[getOriginalNodeId(node)] = undefined;
}
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
&& (kind === SyntaxKind.ClassDeclaration
|| kind === SyntaxKind.Constructor
|| kind === SyntaxKind.MethodDeclaration
|| kind === SyntaxKind.GetAccessor
|| kind === SyntaxKind.SetAccessor)) {
if (superContainerStack) {
superContainerStack.pop();
}
}
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
&& kind === SyntaxKind.ModuleDeclaration) {
namespaceNestLevel--;
}
isEnclosedInNamespace = savedIsEnclosedInNamespace;
currentSuperContainer = savedCurrentSuperContainer;
}
function substituteExpression(node: Expression): Expression {
@ -2694,7 +2681,7 @@ namespace ts {
return substituteExpressionIdentifier(<Identifier>node);
}
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports) {
if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper) {
switch (node.kind) {
case SyntaxKind.CallExpression:
return substituteCallExpression(<CallExpression>node);
@ -2727,8 +2714,7 @@ namespace ts {
}
}
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports
&& namespaceNestLevel > 0) {
if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports && isEnclosedInNamespace) {
// If we are nested within a namespace declaration, we may need to qualifiy
// an identifier that is exported from a merged namespace.
const original = getOriginalNode(node);
@ -2794,8 +2780,8 @@ namespace ts {
}
function enableExpressionSubstitutionForAsyncMethodsWithSuper() {
if ((enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports) === 0) {
enabledSubstitutions |= TypeScriptSubstitutionFlags.NamespaceExports;
if ((enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper) === 0) {
enabledSubstitutions |= TypeScriptSubstitutionFlags.AsyncMethodsWithSuper;
// We need to enable substitutions for call, property access, and element access
// if we need to rewrite super calls.
@ -2836,9 +2822,6 @@ namespace ts {
// We need to be notified when entering and exiting namespaces.
context.enableEmitNotification(SyntaxKind.ModuleDeclaration);
// Keep track of namespace nesting depth
namespaceNestLevel = 0;
}
}
@ -2863,9 +2846,8 @@ namespace ts {
}
function getSuperContainerAsyncMethodFlags() {
const container = lastOrUndefined(superContainerStack);
return container !== undefined
&& resolver.getNodeCheckFlags(getOriginalNode(container)) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding);
return currentSuperContainer !== undefined
&& resolver.getNodeCheckFlags(getOriginalNode(currentSuperContainer)) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding);
}
}
}

View File

@ -2867,16 +2867,10 @@ namespace ts {
isEmitNotificationEnabled(node: Node): boolean;
/**
* Hook used to notify transformers immediately before the pretty printer
* emits a node.
* Hook used to allow transformers to capture state before or after
* the printer emits a node.
*/
onBeforeEmitNode?: (node: Node) => void;
/**
* Hook used to notify transformers immediately after the pretty printer
* emits a node.
*/
onAfterEmitNode?: (node: Node) => void;
onEmitNode?: (node: Node, emit: (node: Node) => void) => void;
}
/* @internal */