From fe788769f0cf6dbe664c21db973368583279edaf Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 6 Feb 2023 12:48:14 -0800 Subject: [PATCH 1/3] Add test showing use of jsdoc comment in TS --- ...unctionContextualTypesJSDocInTs.errors.txt | 18 +++++++++++++++ ...expandoFunctionContextualTypesJSDocInTs.js | 22 +++++++++++++++++++ ...doFunctionContextualTypesJSDocInTs.symbols | 15 +++++++++++++ ...andoFunctionContextualTypesJSDocInTs.types | 17 ++++++++++++++ ...expandoFunctionContextualTypesJSDocInTs.ts | 8 +++++++ 5 files changed, 80 insertions(+) create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols create mode 100644 tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types create mode 100644 tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt new file mode 100644 index 0000000000000..68c98868dfa9b --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt @@ -0,0 +1,18 @@ +tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts(2,17): error TS2307: Cannot find module 'blah' or its corresponding type declarations. +tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts(8,1): error TS2322: Type '() => void' is not assignable to type 'never'. + + +==== tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts (2 errors) ==== + // GH #38532 + import Foo from "blah"; + ~~~~~~ +!!! error TS2307: Cannot find module 'blah' or its corresponding type declarations. + + export function Foo() { } + + // This comment should have no effect; this is a TS file. + /** @type {never} */ + Foo.bar = () => { }; + ~~~~~~~ +!!! error TS2322: Type '() => void' is not assignable to type 'never'. + \ No newline at end of file diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js new file mode 100644 index 0000000000000..5e38831eb5ede --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js @@ -0,0 +1,22 @@ +//// [expandoFunctionContextualTypesJSDocInTs.ts] +// GH #38532 +import Foo from "blah"; + +export function Foo() { } + +// This comment should have no effect; this is a TS file. +/** @type {never} */ +Foo.bar = () => { }; + + +//// [expandoFunctionContextualTypesJSDocInTs.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Foo = void 0; +// GH #38532 +var blah_1 = require("blah"); +function Foo() { } +exports.Foo = Foo; +// This comment should have no effect; this is a TS file. +/** @type {never} */ +blah_1.default.bar = function () { }; diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols new file mode 100644 index 0000000000000..d9c843fb9c9b8 --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols @@ -0,0 +1,15 @@ +=== tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts === +// GH #38532 +import Foo from "blah"; +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 23), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 6)) + +export function Foo() { } +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 23), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 3, 25)) + +// This comment should have no effect; this is a TS file. +/** @type {never} */ +Foo.bar = () => { }; +>Foo.bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 3, 25)) +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 23), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 6)) +>bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 3, 25)) + diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types new file mode 100644 index 0000000000000..3380b44db5ca9 --- /dev/null +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types @@ -0,0 +1,17 @@ +=== tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts === +// GH #38532 +import Foo from "blah"; +>Foo : any + +export function Foo() { } +>Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs").Foo + +// This comment should have no effect; this is a TS file. +/** @type {never} */ +Foo.bar = () => { }; +>Foo.bar = () => { } : () => void +>Foo.bar : never +>Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs").Foo +>bar : never +>() => { } : () => void + diff --git a/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts b/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts new file mode 100644 index 0000000000000..98b94e6931d9c --- /dev/null +++ b/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts @@ -0,0 +1,8 @@ +// GH #38532 +import Foo from "blah"; + +export function Foo() { } + +// This comment should have no effect; this is a TS file. +/** @type {never} */ +Foo.bar = () => { }; From 5ce04bcb50634730ab35c8034885840e2d189aae Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 6 Feb 2023 12:56:05 -0800 Subject: [PATCH 2/3] Don't consult JSDoc outside of JS in expando function assignment declarations --- src/compiler/checker.ts | 2 +- .../expandoFunctionContextualTypesJSDocInTs.errors.txt | 5 +---- .../reference/expandoFunctionContextualTypesJSDocInTs.types | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 62aab9ca5c062..44904e0aae7ee 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10519,7 +10519,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // function/class/{} initializers are themselves containers, so they won't merge in the same way as other initializers const container = getAssignedExpandoInitializer(symbol.valueDeclaration); if (container) { - const tag = getJSDocTypeTag(container); + const tag = isInJSFile(container) ? getJSDocTypeTag(container) : undefined; if (tag && tag.typeExpression) { return getTypeFromTypeNode(tag.typeExpression); } diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt index 68c98868dfa9b..e3971255df11c 100644 --- a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt @@ -1,8 +1,7 @@ tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts(2,17): error TS2307: Cannot find module 'blah' or its corresponding type declarations. -tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts(8,1): error TS2322: Type '() => void' is not assignable to type 'never'. -==== tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts (2 errors) ==== +==== tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts (1 errors) ==== // GH #38532 import Foo from "blah"; ~~~~~~ @@ -13,6 +12,4 @@ tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts(8,1): error TS23 // This comment should have no effect; this is a TS file. /** @type {never} */ Foo.bar = () => { }; - ~~~~~~~ -!!! error TS2322: Type '() => void' is not assignable to type 'never'. \ No newline at end of file diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types index 3380b44db5ca9..00515ebd75816 100644 --- a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types @@ -10,8 +10,8 @@ export function Foo() { } /** @type {never} */ Foo.bar = () => { }; >Foo.bar = () => { } : () => void ->Foo.bar : never +>Foo.bar : () => void >Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs").Foo ->bar : never +>bar : () => void >() => { } : () => void From 26c71ad8ae28a7d08411a7c74ccd3ba571af68dd Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 6 Feb 2023 12:59:43 -0800 Subject: [PATCH 3/3] Simplify test --- ...ndoFunctionContextualTypesJSDocInTs.errors.txt | 15 --------------- .../expandoFunctionContextualTypesJSDocInTs.js | 7 +------ ...xpandoFunctionContextualTypesJSDocInTs.symbols | 12 ++++-------- .../expandoFunctionContextualTypesJSDocInTs.types | 8 ++------ .../expandoFunctionContextualTypesJSDocInTs.ts | 3 --- 5 files changed, 7 insertions(+), 38 deletions(-) delete mode 100644 tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt deleted file mode 100644 index e3971255df11c..0000000000000 --- a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.errors.txt +++ /dev/null @@ -1,15 +0,0 @@ -tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts(2,17): error TS2307: Cannot find module 'blah' or its corresponding type declarations. - - -==== tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts (1 errors) ==== - // GH #38532 - import Foo from "blah"; - ~~~~~~ -!!! error TS2307: Cannot find module 'blah' or its corresponding type declarations. - - export function Foo() { } - - // This comment should have no effect; this is a TS file. - /** @type {never} */ - Foo.bar = () => { }; - \ No newline at end of file diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js index 5e38831eb5ede..a9693850eec11 100644 --- a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.js @@ -1,7 +1,4 @@ //// [expandoFunctionContextualTypesJSDocInTs.ts] -// GH #38532 -import Foo from "blah"; - export function Foo() { } // This comment should have no effect; this is a TS file. @@ -13,10 +10,8 @@ Foo.bar = () => { }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Foo = void 0; -// GH #38532 -var blah_1 = require("blah"); function Foo() { } exports.Foo = Foo; // This comment should have no effect; this is a TS file. /** @type {never} */ -blah_1.default.bar = function () { }; +Foo.bar = function () { }; diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols index d9c843fb9c9b8..667bc1dd8c341 100644 --- a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.symbols @@ -1,15 +1,11 @@ === tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts === -// GH #38532 -import Foo from "blah"; ->Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 23), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 6)) - export function Foo() { } ->Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 23), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 3, 25)) +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 0, 0), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 0, 25)) // This comment should have no effect; this is a TS file. /** @type {never} */ Foo.bar = () => { }; ->Foo.bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 3, 25)) ->Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 23), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 1, 6)) ->bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 3, 25)) +>Foo.bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 0, 25)) +>Foo : Symbol(Foo, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 0, 0), Decl(expandoFunctionContextualTypesJSDocInTs.ts, 0, 25)) +>bar : Symbol(Foo.bar, Decl(expandoFunctionContextualTypesJSDocInTs.ts, 0, 25)) diff --git a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types index 00515ebd75816..7e818015f1a79 100644 --- a/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types +++ b/tests/baselines/reference/expandoFunctionContextualTypesJSDocInTs.types @@ -1,17 +1,13 @@ === tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts === -// GH #38532 -import Foo from "blah"; ->Foo : any - export function Foo() { } ->Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs").Foo +>Foo : typeof Foo // This comment should have no effect; this is a TS file. /** @type {never} */ Foo.bar = () => { }; >Foo.bar = () => { } : () => void >Foo.bar : () => void ->Foo : typeof import("tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs").Foo +>Foo : typeof Foo >bar : () => void >() => { } : () => void diff --git a/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts b/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts index 98b94e6931d9c..c26397a764a80 100644 --- a/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts +++ b/tests/cases/compiler/expandoFunctionContextualTypesJSDocInTs.ts @@ -1,6 +1,3 @@ -// GH #38532 -import Foo from "blah"; - export function Foo() { } // This comment should have no effect; this is a TS file.