Skip to content

Commit 52d8a78

Browse files
committed
Merge pull request microsoft#8970 from Microsoft/propertyControlFlow
Fix control flow analysis for property initializers
2 parents bfafecc + 3b1effb commit 52d8a78

6 files changed

Lines changed: 69 additions & 12 deletions

File tree

src/compiler/checker.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8091,13 +8091,22 @@ namespace ts {
80918091
return expression;
80928092
}
80938093

8094+
function getControlFlowContainer(node: Node): Node {
8095+
while (true) {
8096+
node = node.parent;
8097+
if (isFunctionLike(node) || node.kind === SyntaxKind.ModuleBlock || node.kind === SyntaxKind.SourceFile || node.kind === SyntaxKind.PropertyDeclaration) {
8098+
return node;
8099+
}
8100+
}
8101+
}
8102+
80948103
function isDeclarationIncludedInFlow(reference: Node, declaration: Declaration, includeOuterFunctions: boolean) {
8095-
const declarationContainer = getContainingFunctionOrModule(declaration);
8096-
let container = getContainingFunctionOrModule(reference);
8104+
const declarationContainer = getControlFlowContainer(declaration);
8105+
let container = getControlFlowContainer(reference);
80978106
while (container !== declarationContainer &&
80988107
(container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.ArrowFunction) &&
80998108
(includeOuterFunctions || getImmediatelyInvokedFunctionExpression(<FunctionExpression>container))) {
8100-
container = getContainingFunctionOrModule(container);
8109+
container = getControlFlowContainer(container);
81018110
}
81028111
return container === declarationContainer;
81038112
}

src/compiler/utilities.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -880,15 +880,6 @@ namespace ts {
880880
}
881881
}
882882

883-
export function getContainingFunctionOrModule(node: Node): Node {
884-
while (true) {
885-
node = node.parent;
886-
if (isFunctionLike(node) || node.kind === SyntaxKind.ModuleDeclaration || node.kind === SyntaxKind.SourceFile) {
887-
return node;
888-
}
889-
}
890-
}
891-
892883
export function getContainingClass(node: Node): ClassLikeDeclaration {
893884
while (true) {
894885
node = node.parent;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [controlFlowPropertyInitializer.ts]
2+
3+
// Repro from #8967
4+
5+
const LANG = "Turbo Pascal"
6+
7+
class BestLanguage {
8+
name = LANG;
9+
}
10+
11+
//// [controlFlowPropertyInitializer.js]
12+
// Repro from #8967
13+
var LANG = "Turbo Pascal";
14+
var BestLanguage = (function () {
15+
function BestLanguage() {
16+
this.name = LANG;
17+
}
18+
return BestLanguage;
19+
}());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/controlFlowPropertyInitializer.ts ===
2+
3+
// Repro from #8967
4+
5+
const LANG = "Turbo Pascal"
6+
>LANG : Symbol(LANG, Decl(controlFlowPropertyInitializer.ts, 3, 5))
7+
8+
class BestLanguage {
9+
>BestLanguage : Symbol(BestLanguage, Decl(controlFlowPropertyInitializer.ts, 3, 27))
10+
11+
name = LANG;
12+
>name : Symbol(BestLanguage.name, Decl(controlFlowPropertyInitializer.ts, 5, 20))
13+
>LANG : Symbol(LANG, Decl(controlFlowPropertyInitializer.ts, 3, 5))
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/controlFlowPropertyInitializer.ts ===
2+
3+
// Repro from #8967
4+
5+
const LANG = "Turbo Pascal"
6+
>LANG : string
7+
>"Turbo Pascal" : string
8+
9+
class BestLanguage {
10+
>BestLanguage : BestLanguage
11+
12+
name = LANG;
13+
>name : string
14+
>LANG : string
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @strictNullChecks: true
2+
3+
// Repro from #8967
4+
5+
const LANG = "Turbo Pascal"
6+
7+
class BestLanguage {
8+
name = LANG;
9+
}

0 commit comments

Comments
 (0)