mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-10 15:25:54 -06:00
Merge pull request #8970 from Microsoft/propertyControlFlow
Fix control flow analysis for property initializers
This commit is contained in:
commit
52d8a78419
@ -8091,13 +8091,22 @@ namespace ts {
|
||||
return expression;
|
||||
}
|
||||
|
||||
function getControlFlowContainer(node: Node): Node {
|
||||
while (true) {
|
||||
node = node.parent;
|
||||
if (isFunctionLike(node) || node.kind === SyntaxKind.ModuleBlock || node.kind === SyntaxKind.SourceFile || node.kind === SyntaxKind.PropertyDeclaration) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isDeclarationIncludedInFlow(reference: Node, declaration: Declaration, includeOuterFunctions: boolean) {
|
||||
const declarationContainer = getContainingFunctionOrModule(declaration);
|
||||
let container = getContainingFunctionOrModule(reference);
|
||||
const declarationContainer = getControlFlowContainer(declaration);
|
||||
let container = getControlFlowContainer(reference);
|
||||
while (container !== declarationContainer &&
|
||||
(container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.ArrowFunction) &&
|
||||
(includeOuterFunctions || getImmediatelyInvokedFunctionExpression(<FunctionExpression>container))) {
|
||||
container = getContainingFunctionOrModule(container);
|
||||
container = getControlFlowContainer(container);
|
||||
}
|
||||
return container === declarationContainer;
|
||||
}
|
||||
|
||||
@ -880,15 +880,6 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
export function getContainingFunctionOrModule(node: Node): Node {
|
||||
while (true) {
|
||||
node = node.parent;
|
||||
if (isFunctionLike(node) || node.kind === SyntaxKind.ModuleDeclaration || node.kind === SyntaxKind.SourceFile) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getContainingClass(node: Node): ClassLikeDeclaration {
|
||||
while (true) {
|
||||
node = node.parent;
|
||||
|
||||
19
tests/baselines/reference/controlFlowPropertyInitializer.js
Normal file
19
tests/baselines/reference/controlFlowPropertyInitializer.js
Normal file
@ -0,0 +1,19 @@
|
||||
//// [controlFlowPropertyInitializer.ts]
|
||||
|
||||
// Repro from #8967
|
||||
|
||||
const LANG = "Turbo Pascal"
|
||||
|
||||
class BestLanguage {
|
||||
name = LANG;
|
||||
}
|
||||
|
||||
//// [controlFlowPropertyInitializer.js]
|
||||
// Repro from #8967
|
||||
var LANG = "Turbo Pascal";
|
||||
var BestLanguage = (function () {
|
||||
function BestLanguage() {
|
||||
this.name = LANG;
|
||||
}
|
||||
return BestLanguage;
|
||||
}());
|
||||
@ -0,0 +1,14 @@
|
||||
=== tests/cases/compiler/controlFlowPropertyInitializer.ts ===
|
||||
|
||||
// Repro from #8967
|
||||
|
||||
const LANG = "Turbo Pascal"
|
||||
>LANG : Symbol(LANG, Decl(controlFlowPropertyInitializer.ts, 3, 5))
|
||||
|
||||
class BestLanguage {
|
||||
>BestLanguage : Symbol(BestLanguage, Decl(controlFlowPropertyInitializer.ts, 3, 27))
|
||||
|
||||
name = LANG;
|
||||
>name : Symbol(BestLanguage.name, Decl(controlFlowPropertyInitializer.ts, 5, 20))
|
||||
>LANG : Symbol(LANG, Decl(controlFlowPropertyInitializer.ts, 3, 5))
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
=== tests/cases/compiler/controlFlowPropertyInitializer.ts ===
|
||||
|
||||
// Repro from #8967
|
||||
|
||||
const LANG = "Turbo Pascal"
|
||||
>LANG : string
|
||||
>"Turbo Pascal" : string
|
||||
|
||||
class BestLanguage {
|
||||
>BestLanguage : BestLanguage
|
||||
|
||||
name = LANG;
|
||||
>name : string
|
||||
>LANG : string
|
||||
}
|
||||
9
tests/cases/compiler/controlFlowPropertyInitializer.ts
Normal file
9
tests/cases/compiler/controlFlowPropertyInitializer.ts
Normal file
@ -0,0 +1,9 @@
|
||||
// @strictNullChecks: true
|
||||
|
||||
// Repro from #8967
|
||||
|
||||
const LANG = "Turbo Pascal"
|
||||
|
||||
class BestLanguage {
|
||||
name = LANG;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user