From 739eb1b87fa1ba56e47ead9180bc63b13226c257 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Fri, 12 Jun 2020 16:57:27 -0400 Subject: [PATCH] Small fix in `getIsContextSensitiveAssignmentOrContextType` Test that `parentSymbol.valueDeclaration` exists. Fixes #38532 --- src/compiler/checker.ts | 2 +- ...doFunctionContextualTypesNoValue.errors.txt | 13 +++++++++++++ .../expandoFunctionContextualTypesNoValue.js | 18 ++++++++++++++++++ ...pandoFunctionContextualTypesNoValue.symbols | 13 +++++++++++++ ...expandoFunctionContextualTypesNoValue.types | 15 +++++++++++++++ .../expandoFunctionContextualTypesNoValue.ts | 6 ++++++ 6 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesNoValue.errors.txt create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesNoValue.js create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesNoValue.symbols create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesNoValue.types create mode 100644 tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 12fbed724ec1f..fbfa5a8cf0ad8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22585,7 +22585,7 @@ namespace ts { const id = lhs.expression; const parentSymbol = resolveName(id, id.escapedText, SymbolFlags.Value, undefined, id.escapedText, /*isUse*/ true); if (parentSymbol) { - const annotated = getEffectiveTypeAnnotationNode(parentSymbol.valueDeclaration); + const annotated = parentSymbol.valueDeclaration && getEffectiveTypeAnnotationNode(parentSymbol.valueDeclaration); if (annotated) { const nameStr = getElementOrPropertyAccessName(lhs); if (nameStr !== undefined) { diff --git a/tests/baselines/reference/expandoFunctionContextualTypesNoValue.errors.txt b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.errors.txt new file mode 100644 index 0000000000000..979816da82835 --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts(2,17): error TS2307: Cannot find module 'blah' or its corresponding type declarations. + + +==== tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts (1 errors) ==== + // GH #38532 + import Foo from "blah"; + ~~~~~~ +!!! error TS2307: Cannot find module 'blah' or its corresponding type declarations. + + export function Foo() { } + + Foo.bar = () => { }; + \ No newline at end of file diff --git a/tests/baselines/reference/expandoFunctionContextualTypesNoValue.js b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.js new file mode 100644 index 0000000000000..ebe8b1c381c04 --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.js @@ -0,0 +1,18 @@ +//// [expandoFunctionContextualTypesNoValue.ts] +// GH #38532 +import Foo from "blah"; + +export function Foo() { } + +Foo.bar = () => { }; + + +//// [expandoFunctionContextualTypesNoValue.js] +"use strict"; +exports.__esModule = true; +exports.Foo = void 0; +// GH #38532 +var blah_1 = require("blah"); +function Foo() { } +exports.Foo = Foo; +blah_1["default"].bar = function () { }; diff --git a/tests/baselines/reference/expandoFunctionContextualTypesNoValue.symbols b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.symbols new file mode 100644 index 0000000000000..b760ad0af5af9 --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.symbols @@ -0,0 +1,13 @@ +=== tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts === +// GH #38532 +import Foo from "blah"; +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesNoValue.ts, 1, 23), Decl(expandoFunctionContextualTypesNoValue.ts, 1, 6)) + +export function Foo() { } +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesNoValue.ts, 1, 23), Decl(expandoFunctionContextualTypesNoValue.ts, 3, 25)) + +Foo.bar = () => { }; +>Foo.bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesNoValue.ts, 3, 25)) +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesNoValue.ts, 1, 23), Decl(expandoFunctionContextualTypesNoValue.ts, 1, 6)) +>bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesNoValue.ts, 3, 25)) + diff --git a/tests/baselines/reference/expandoFunctionContextualTypesNoValue.types b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.types new file mode 100644 index 0000000000000..6eb8918a7a7fd --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesNoValue.types @@ -0,0 +1,15 @@ +=== tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts === +// GH #38532 +import Foo from "blah"; +>Foo : any + +export function Foo() { } +>Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesNoValue").Foo + +Foo.bar = () => { }; +>Foo.bar = () => { } : () => void +>Foo.bar : () => void +>Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesNoValue").Foo +>bar : () => void +>() => { } : () => void + diff --git a/tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts b/tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts new file mode 100644 index 0000000000000..f3700b04149ce --- /dev/null +++ b/tests/cases/compiler/expandoFunctionContextualTypesNoValue.ts @@ -0,0 +1,6 @@ +// GH #38532 +import Foo from "blah"; + +export function Foo() { } + +Foo.bar = () => { };