Skip to content

align ClassStaticBlockDeclaration with IIFE in CFA #44969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,19 +657,23 @@ namespace ts {
const saveExceptionTarget = currentExceptionTarget;
const saveActiveLabelList = activeLabelList;
const saveHasExplicitReturn = hasExplicitReturn;
const isIIFE = containerFlags & ContainerFlags.IsFunctionExpression && !hasSyntacticModifier(node, ModifierFlags.Async) &&
!(node as FunctionLikeDeclaration).asteriskToken && !!getImmediatelyInvokedFunctionExpression(node);
const isIIFELike =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const isIIFELike =
const isImmediatelyInvoked =

(or isImmediate for even more old-school flavor)

(containerFlags & ContainerFlags.IsFunctionExpression &&
!hasSyntacticModifier(node, ModifierFlags.Async) &&
!(node as FunctionLikeDeclaration).asteriskToken &&
!!getImmediatelyInvokedFunctionExpression(node)) ||
node.kind === SyntaxKind.ClassStaticBlockDeclaration;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how to format these lines, so here is how prettier handles it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have done it slightly differently, but prettier's format is fine.

// A non-async, non-generator IIFE is considered part of the containing control flow. Return statements behave
// similarly to break statements that exit to a label just past the statement body.
if (!isIIFE) {
if (!isIIFELike) {
currentFlow = initFlowNode({ flags: FlowFlags.Start });
if (containerFlags & (ContainerFlags.IsFunctionExpression | ContainerFlags.IsObjectLiteralOrClassExpressionMethod)) {
currentFlow.node = node as FunctionExpression | ArrowFunction | MethodDeclaration;
}
}
// We create a return control flow graph for IIFEs and constructors. For constructors
// we use the return control flow graph in strict property initialization checks.
currentReturnTarget = isIIFE || node.kind === SyntaxKind.Constructor || node.kind === SyntaxKind.ClassStaticBlockDeclaration || (isInJSFile(node) && (node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression)) ? createBranchLabel() : undefined;
currentReturnTarget = isIIFELike || node.kind === SyntaxKind.Constructor || (isInJSFile(node) && (node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression)) ? createBranchLabel() : undefined;
currentExceptionTarget = undefined;
currentBreakTarget = undefined;
currentContinueTarget = undefined;
Expand All @@ -695,7 +699,7 @@ namespace ts {
(node as FunctionLikeDeclaration | ClassStaticBlockDeclaration).returnFlowNode = currentFlow;
}
}
if (!isIIFE) {
if (!isIIFELike) {
currentFlow = saveCurrentFlow;
}
currentBreakTarget = saveBreakTarget;
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/classStaticBlock28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [classStaticBlock28.ts]
let foo: number;

class C {
static {
foo = 1
}
}

console.log(foo)

//// [classStaticBlock28.js]
"use strict";
var foo;
var C = /** @class */ (function () {
function C() {
}
return C;
}());
(function () {
foo = 1;
})();
console.log(foo);
19 changes: 19 additions & 0 deletions tests/baselines/reference/classStaticBlock28.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock28.ts ===
let foo: number;
>foo : Symbol(foo, Decl(classStaticBlock28.ts, 0, 3))

class C {
>C : Symbol(C, Decl(classStaticBlock28.ts, 0, 16))

static {
foo = 1
>foo : Symbol(foo, Decl(classStaticBlock28.ts, 0, 3))
}
}

console.log(foo)
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>foo : Symbol(foo, Decl(classStaticBlock28.ts, 0, 3))

22 changes: 22 additions & 0 deletions tests/baselines/reference/classStaticBlock28.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock28.ts ===
let foo: number;
>foo : number

class C {
>C : C

static {
foo = 1
>foo = 1 : 1
>foo : number
>1 : 1
}
}

console.log(foo)
>console.log(foo) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>foo : number

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @strict: true

let foo: number;

class C {
static {
foo = 1
}
}

console.log(foo)