mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Switch to arrow for ts class wrapper IIFE
This commit is contained in:
+42
-1
@@ -2372,6 +2372,24 @@ namespace ts {
|
||||
);
|
||||
}
|
||||
|
||||
export function createImmediatelyInvokedArrowFunction(statements: Statement[]): CallExpression;
|
||||
export function createImmediatelyInvokedArrowFunction(statements: Statement[], param: ParameterDeclaration, paramValue: Expression): CallExpression;
|
||||
export function createImmediatelyInvokedArrowFunction(statements: Statement[], param?: ParameterDeclaration, paramValue?: Expression) {
|
||||
return createCall(
|
||||
createArrowFunction(
|
||||
/*modifiers*/ undefined,
|
||||
/*typeParameters*/ undefined,
|
||||
/*parameters*/ param ? [param] : [],
|
||||
/*type*/ undefined,
|
||||
/*equalsGreaterThanToken*/ undefined,
|
||||
createBlock(statements, /*multiLine*/ true)
|
||||
),
|
||||
/*typeArguments*/ undefined,
|
||||
/*argumentsArray*/ paramValue ? [paramValue] : []
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function createComma(left: Expression, right: Expression) {
|
||||
return <Expression>createBinary(left, SyntaxKind.CommaToken, right);
|
||||
}
|
||||
@@ -4040,8 +4058,31 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a node is a parenthesized expression that can be ignored when recreating outer expressions.
|
||||
*
|
||||
* A parenthesized expression can be ignored when all of the following are true:
|
||||
*
|
||||
* - It's `pos` and `end` are not -1
|
||||
* - It does not have a custom source map range
|
||||
* - It does not have a custom comment range
|
||||
* - It does not have synthetic leading or trailing comments
|
||||
*
|
||||
* If an outermost parenthesized expression is ignored, but the containing expression requires a parentheses around
|
||||
* the expression to maintain precedence, a new parenthesized expression should be created automatically when
|
||||
* the containing expression is created/updated.
|
||||
*/
|
||||
function isIgnorableParen(node: Expression) {
|
||||
return node.kind === SyntaxKind.ParenthesizedExpression
|
||||
&& nodeIsSynthesized(node)
|
||||
&& nodeIsSynthesized(getSourceMapRange(node))
|
||||
&& nodeIsSynthesized(getCommentRange(node))
|
||||
&& !some(getSyntheticLeadingComments(node))
|
||||
&& !some(getSyntheticTrailingComments(node));
|
||||
}
|
||||
|
||||
export function recreateOuterExpressions(outerExpression: Expression | undefined, innerExpression: Expression, kinds = OuterExpressionKinds.All): Expression {
|
||||
if (outerExpression && isOuterExpression(outerExpression, kinds)) {
|
||||
if (outerExpression && isOuterExpression(outerExpression, kinds) && !isIgnorableParen(outerExpression)) {
|
||||
return updateOuterExpression(
|
||||
outerExpression,
|
||||
recreateOuterExpressions(outerExpression.expression, innerExpression)
|
||||
|
||||
@@ -339,64 +339,12 @@ namespace ts {
|
||||
&& !(<ReturnStatement>node).expression;
|
||||
}
|
||||
|
||||
function isClassLikeVariableStatement(node: Node) {
|
||||
if (!isVariableStatement(node)) return false;
|
||||
const variable = singleOrUndefined((<VariableStatement>node).declarationList.declarations);
|
||||
return variable
|
||||
&& variable.initializer
|
||||
&& isIdentifier(variable.name)
|
||||
&& (isClassLike(variable.initializer)
|
||||
|| (isAssignmentExpression(variable.initializer)
|
||||
&& isIdentifier(variable.initializer.left)
|
||||
&& isClassLike(variable.initializer.right)));
|
||||
}
|
||||
|
||||
function isTypeScriptClassWrapper(node: Node) {
|
||||
const call = tryCast(node, isCallExpression);
|
||||
if (!call || isParseTreeNode(call) ||
|
||||
some(call.typeArguments) ||
|
||||
some(call.arguments)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const func = tryCast(skipOuterExpressions(call.expression), isFunctionExpression);
|
||||
if (!func || isParseTreeNode(func) ||
|
||||
some(func.typeParameters) ||
|
||||
some(func.parameters) ||
|
||||
func.type ||
|
||||
!func.body) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const statements = func.body.statements;
|
||||
if (statements.length < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const firstStatement = statements[0];
|
||||
if (isParseTreeNode(firstStatement) ||
|
||||
!isClassLike(firstStatement) &&
|
||||
!isClassLikeVariableStatement(firstStatement)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const lastStatement = elementAt(statements, -1);
|
||||
const returnStatement = tryCast(isVariableStatement(lastStatement) ? elementAt(statements, -2) : lastStatement, isReturnStatement);
|
||||
if (!returnStatement ||
|
||||
!returnStatement.expression ||
|
||||
!isIdentifier(skipOuterExpressions(returnStatement.expression))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function shouldVisitNode(node: Node): boolean {
|
||||
return (node.transformFlags & TransformFlags.ContainsES2015) !== 0
|
||||
|| convertedLoopState !== undefined
|
||||
|| (hierarchyFacts & HierarchyFacts.ConstructorWithCapturedSuper && (isStatement(node) || (node.kind === SyntaxKind.Block)))
|
||||
|| (isIterationStatement(node, /*lookInLabeledStatements*/ false) && shouldConvertIterationStatementBody(node))
|
||||
|| isTypeScriptClassWrapper(node);
|
||||
|| (getEmitFlags(node) & EmitFlags.TypeScriptClassWrapper) !== 0;
|
||||
}
|
||||
|
||||
function visitor(node: Node): VisitResult<Node> {
|
||||
@@ -3308,13 +3256,14 @@ namespace ts {
|
||||
* @param node a CallExpression.
|
||||
*/
|
||||
function visitCallExpression(node: CallExpression) {
|
||||
if (isTypeScriptClassWrapper(node)) {
|
||||
if (getEmitFlags(node) & EmitFlags.TypeScriptClassWrapper) {
|
||||
return visitTypeScriptClassWrapper(node);
|
||||
}
|
||||
|
||||
if (node.transformFlags & TransformFlags.ES2015) {
|
||||
return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ true);
|
||||
}
|
||||
|
||||
return updateCall(
|
||||
node,
|
||||
visitNode(node.expression, callExpressionVisitor, isExpression),
|
||||
@@ -3357,7 +3306,7 @@ namespace ts {
|
||||
|
||||
// We skip any outer expressions in a number of places to get to the innermost
|
||||
// expression, but we will restore them later to preserve comments and source maps.
|
||||
const body = cast(skipOuterExpressions(node.expression), isFunctionExpression).body;
|
||||
const body = cast(cast(skipOuterExpressions(node.expression), isArrowFunction).body, isBlock);
|
||||
|
||||
// The class statements are the statements generated by visiting the first statement of the
|
||||
// body (1), while all other statements are added to remainingStatements (2)
|
||||
|
||||
@@ -613,13 +613,16 @@ namespace ts {
|
||||
|
||||
addRange(statements, context.endLexicalEnvironment());
|
||||
|
||||
const iife = createImmediatelyInvokedArrowFunction(statements);
|
||||
setEmitFlags(iife, EmitFlags.TypeScriptClassWrapper);
|
||||
|
||||
const varStatement = createVariableStatement(
|
||||
/*modifiers*/ undefined,
|
||||
createVariableDeclarationList([
|
||||
createVariableDeclaration(
|
||||
getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ false),
|
||||
/*type*/ undefined,
|
||||
createImmediatelyInvokedFunctionExpression(statements)
|
||||
iife
|
||||
)
|
||||
])
|
||||
);
|
||||
|
||||
@@ -4170,6 +4170,7 @@ namespace ts {
|
||||
HasEndOfDeclarationMarker = 1 << 22, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
|
||||
Iterator = 1 << 23, // The expression to a `yield*` should be treated as an Iterator when down-leveling, not an Iterable.
|
||||
NoAsciiEscaping = 1 << 24, // When synthesizing nodes that lack an original node or textSourceNode, we want to write the text on the node with ASCII escaping substitutions.
|
||||
/*@internal*/ TypeScriptClassWrapper = 1 << 25, // The node is an IIFE class wrapper created by the ts transform.
|
||||
}
|
||||
|
||||
export interface EmitHelper {
|
||||
|
||||
@@ -2068,9 +2068,9 @@ namespace ts {
|
||||
|| kind === SyntaxKind.SourceFile;
|
||||
}
|
||||
|
||||
export function nodeIsSynthesized(node: TextRange): boolean {
|
||||
return positionIsSynthesized(node.pos)
|
||||
|| positionIsSynthesized(node.end);
|
||||
export function nodeIsSynthesized(range: TextRange): boolean {
|
||||
return positionIsSynthesized(range.pos)
|
||||
|| positionIsSynthesized(range.end);
|
||||
}
|
||||
|
||||
export function getOriginalSourceFile(sourceFile: SourceFile) {
|
||||
|
||||
Reference in New Issue
Block a user