Skip to content

Commit 28c870d

Browse files
committed
Fix use-before-def and strict property initialization
1 parent abaf262 commit 28c870d

File tree

46 files changed

+681
-360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+681
-360
lines changed

src/compiler/binder.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ namespace ts {
669669
}
670670
// We create a return control flow graph for IIFEs and constructors. For constructors
671671
// we use the return control flow graph in strict property initialization checks.
672-
currentReturnTarget = isIIFE || node.kind === SyntaxKind.Constructor || (isInJSFile(node) && (node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression)) ? createBranchLabel() : undefined;
672+
currentReturnTarget = isIIFE || node.kind === SyntaxKind.Constructor || node.kind === SyntaxKind.ClassStaticBlockDeclaration || (isInJSFile(node) && (node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression)) ? createBranchLabel() : undefined;
673673
currentExceptionTarget = undefined;
674674
currentBreakTarget = undefined;
675675
currentContinueTarget = undefined;
@@ -678,10 +678,10 @@ namespace ts {
678678
bindChildren(node);
679679
// Reset all reachability check related flags on node (for incremental scenarios)
680680
node.flags &= ~NodeFlags.ReachabilityAndEmitFlags;
681-
if (!(currentFlow.flags & FlowFlags.Unreachable) && containerFlags & ContainerFlags.IsFunctionLike && nodeIsPresent((node as FunctionLikeDeclaration).body)) {
681+
if (!(currentFlow.flags & FlowFlags.Unreachable) && containerFlags & ContainerFlags.IsFunctionLike && nodeIsPresent((node as FunctionLikeDeclaration | ClassStaticBlockDeclaration).body)) {
682682
node.flags |= NodeFlags.HasImplicitReturn;
683683
if (hasExplicitReturn) node.flags |= NodeFlags.HasExplicitReturn;
684-
(node as FunctionLikeDeclaration).endFlowNode = currentFlow;
684+
(node as FunctionLikeDeclaration | ClassStaticBlockDeclaration).endFlowNode = currentFlow;
685685
}
686686
if (node.kind === SyntaxKind.SourceFile) {
687687
node.flags |= emitFlags;
@@ -691,8 +691,8 @@ namespace ts {
691691
if (currentReturnTarget) {
692692
addAntecedent(currentReturnTarget, currentFlow);
693693
currentFlow = finishFlowLabel(currentReturnTarget);
694-
if (node.kind === SyntaxKind.Constructor || (isInJSFile(node) && (node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression))) {
695-
(node as FunctionLikeDeclaration).returnFlowNode = currentFlow;
694+
if (node.kind === SyntaxKind.Constructor || node.kind === SyntaxKind.ClassStaticBlockDeclaration || (isInJSFile(node) && (node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression))) {
695+
(node as FunctionLikeDeclaration | ClassStaticBlockDeclaration).returnFlowNode = currentFlow;
696696
}
697697
}
698698
if (!isIIFE) {
@@ -1944,7 +1944,7 @@ namespace ts {
19441944
}
19451945

19461946
function declareClassMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) {
1947-
return hasSyntacticModifier(node, ModifierFlags.Static)
1947+
return isStatic(node)
19481948
? declareSymbol(container.symbol.exports!, container.symbol, node, symbolFlags, symbolExcludes)
19491949
: declareSymbol(container.symbol.members!, container.symbol, node, symbolFlags, symbolExcludes);
19501950
}
@@ -2950,7 +2950,7 @@ namespace ts {
29502950
// this.foo assignment in a JavaScript class
29512951
// Bind this property to the containing class
29522952
const containingClass = thisContainer.parent;
2953-
const symbolTable = hasSyntacticModifier(thisContainer, ModifierFlags.Static) ? containingClass.symbol.exports! : containingClass.symbol.members!;
2953+
const symbolTable = isStatic(thisContainer) ? containingClass.symbol.exports! : containingClass.symbol.members!;
29542954
if (hasDynamicName(node)) {
29552955
bindDynamicallyNamedThisPropertyAssignment(node, containingClass.symbol, symbolTable);
29562956
}

src/compiler/checker.ts

Lines changed: 137 additions & 70 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,10 @@
33483348
"category": "Error",
33493349
"code": 2816
33503350
},
3351+
"Property '{0}' has no initializer and is not definitely assigned in a class static block.": {
3352+
"category": "Error",
3353+
"code": 2817
3354+
},
33513355

33523356
"Import declaration '{0}' is using private name '{1}'.": {
33533357
"category": "Error",

0 commit comments

Comments
 (0)