From bb7818b837fb4145f9fb42e64df4484c8547b7a7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 4 Jun 2016 14:42:45 -0700 Subject: [PATCH 1/2] Consider property declarations to be control flow containers --- src/compiler/checker.ts | 15 ++++++++++++--- src/compiler/utilities.ts | 9 --------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c538134e6d8..67bd747a4ae 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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(container))) { - container = getContainingFunctionOrModule(container); + container = getControlFlowContainer(container); } return container === declarationContainer; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 74ea3459b23..e2fa4662c1f 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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; From 3b1effb7df5e6221260652280d47b78ae9ecbfac Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 4 Jun 2016 14:50:37 -0700 Subject: [PATCH 2/2] Adding regression test --- .../controlFlowPropertyInitializer.js | 19 +++++++++++++++++++ .../controlFlowPropertyInitializer.symbols | 14 ++++++++++++++ .../controlFlowPropertyInitializer.types | 15 +++++++++++++++ .../controlFlowPropertyInitializer.ts | 9 +++++++++ 4 files changed, 57 insertions(+) create mode 100644 tests/baselines/reference/controlFlowPropertyInitializer.js create mode 100644 tests/baselines/reference/controlFlowPropertyInitializer.symbols create mode 100644 tests/baselines/reference/controlFlowPropertyInitializer.types create mode 100644 tests/cases/compiler/controlFlowPropertyInitializer.ts diff --git a/tests/baselines/reference/controlFlowPropertyInitializer.js b/tests/baselines/reference/controlFlowPropertyInitializer.js new file mode 100644 index 00000000000..5d8e10651fa --- /dev/null +++ b/tests/baselines/reference/controlFlowPropertyInitializer.js @@ -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; +}()); diff --git a/tests/baselines/reference/controlFlowPropertyInitializer.symbols b/tests/baselines/reference/controlFlowPropertyInitializer.symbols new file mode 100644 index 00000000000..70e6c2da8a1 --- /dev/null +++ b/tests/baselines/reference/controlFlowPropertyInitializer.symbols @@ -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)) +} diff --git a/tests/baselines/reference/controlFlowPropertyInitializer.types b/tests/baselines/reference/controlFlowPropertyInitializer.types new file mode 100644 index 00000000000..d35a4c13698 --- /dev/null +++ b/tests/baselines/reference/controlFlowPropertyInitializer.types @@ -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 +} diff --git a/tests/cases/compiler/controlFlowPropertyInitializer.ts b/tests/cases/compiler/controlFlowPropertyInitializer.ts new file mode 100644 index 00000000000..0b75f280b1d --- /dev/null +++ b/tests/cases/compiler/controlFlowPropertyInitializer.ts @@ -0,0 +1,9 @@ +// @strictNullChecks: true + +// Repro from #8967 + +const LANG = "Turbo Pascal" + +class BestLanguage { + name = LANG; +} \ No newline at end of file