From 5969ae9ef76f5ae518558d7925fbf1c518060a4b Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Fri, 19 Aug 2022 19:34:42 +0300 Subject: [PATCH] fix(50075): do not strip undefined from the function class properties (#50169) --- src/compiler/checker.ts | 2 +- ...jsdocFunctionClassPropertiesDeclaration.js | 47 +++++++++++++++++++ ...FunctionClassPropertiesDeclaration.symbols | 32 +++++++++++++ ...ocFunctionClassPropertiesDeclaration.types | 38 +++++++++++++++ ...jsdocFunctionClassPropertiesDeclaration.ts | 18 +++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.js create mode 100644 tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.symbols create mode 100644 tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.types create mode 100644 tests/cases/compiler/jsdocFunctionClassPropertiesDeclaration.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8c8101e8737..e2594f6592c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8154,7 +8154,7 @@ namespace ts { factory.createModifiersFromModifierFlags((isReadonlySymbol(p) ? ModifierFlags.Readonly : 0) | flag), name, p.flags & SymbolFlags.Optional ? factory.createToken(SyntaxKind.QuestionToken) : undefined, - isPrivate ? undefined : serializeTypeForDeclaration(context, getTypeOfSymbol(p), p, enclosingDeclaration, includePrivateSymbol, bundled), + isPrivate ? undefined : serializeTypeForDeclaration(context, getWriteTypeOfSymbol(p), p, enclosingDeclaration, includePrivateSymbol, bundled), // TODO: https://github.com/microsoft/TypeScript/pull/32372#discussion_r328386357 // interface members can't have initializers, however class members _can_ /*initializer*/ undefined diff --git a/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.js b/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.js new file mode 100644 index 00000000000..7f4739ad951 --- /dev/null +++ b/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.js @@ -0,0 +1,47 @@ +//// [a.js] +/** + * @param {number | undefined} x + * @param {number | undefined} y + */ +export function Foo(x, y) { + if (!(this instanceof Foo)) { + return new Foo(x, y); + } + this.x = x; + this.y = y; +} + + +//// [a.js] +"use strict"; +exports.__esModule = true; +exports.Foo = void 0; +/** + * @param {number | undefined} x + * @param {number | undefined} y + */ +function Foo(x, y) { + if (!(this instanceof Foo)) { + return new Foo(x, y); + } + this.x = x; + this.y = y; +} +exports.Foo = Foo; + + +//// [a.d.ts] +/** + * @param {number | undefined} x + * @param {number | undefined} y + */ +export function Foo(x: number | undefined, y: number | undefined): Foo; +export class Foo { + /** + * @param {number | undefined} x + * @param {number | undefined} y + */ + constructor(x: number | undefined, y: number | undefined); + x: number | undefined; + y: number | undefined; +} diff --git a/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.symbols b/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.symbols new file mode 100644 index 00000000000..674286c0700 --- /dev/null +++ b/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.symbols @@ -0,0 +1,32 @@ +=== /a.js === +/** + * @param {number | undefined} x + * @param {number | undefined} y + */ +export function Foo(x, y) { +>Foo : Symbol(Foo, Decl(a.js, 0, 0)) +>x : Symbol(x, Decl(a.js, 4, 20)) +>y : Symbol(y, Decl(a.js, 4, 22)) + + if (!(this instanceof Foo)) { +>this : Symbol(Foo, Decl(a.js, 0, 0)) +>Foo : Symbol(Foo, Decl(a.js, 0, 0)) + + return new Foo(x, y); +>Foo : Symbol(Foo, Decl(a.js, 0, 0)) +>x : Symbol(x, Decl(a.js, 4, 20)) +>y : Symbol(y, Decl(a.js, 4, 22)) + } + this.x = x; +>this.x : Symbol(Foo.x, Decl(a.js, 7, 5)) +>this : Symbol(Foo, Decl(a.js, 0, 0)) +>x : Symbol(Foo.x, Decl(a.js, 7, 5)) +>x : Symbol(x, Decl(a.js, 4, 20)) + + this.y = y; +>this.y : Symbol(Foo.y, Decl(a.js, 8, 15)) +>this : Symbol(Foo, Decl(a.js, 0, 0)) +>y : Symbol(Foo.y, Decl(a.js, 8, 15)) +>y : Symbol(y, Decl(a.js, 4, 22)) +} + diff --git a/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.types b/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.types new file mode 100644 index 00000000000..21b1b031944 --- /dev/null +++ b/tests/baselines/reference/jsdocFunctionClassPropertiesDeclaration.types @@ -0,0 +1,38 @@ +=== /a.js === +/** + * @param {number | undefined} x + * @param {number | undefined} y + */ +export function Foo(x, y) { +>Foo : typeof Foo +>x : number | undefined +>y : number | undefined + + if (!(this instanceof Foo)) { +>!(this instanceof Foo) : boolean +>(this instanceof Foo) : boolean +>this instanceof Foo : boolean +>this : this +>Foo : typeof Foo + + return new Foo(x, y); +>new Foo(x, y) : Foo +>Foo : typeof Foo +>x : number | undefined +>y : number | undefined + } + this.x = x; +>this.x = x : number | undefined +>this.x : any +>this : this +>x : any +>x : number | undefined + + this.y = y; +>this.y = y : number | undefined +>this.y : any +>this : this +>y : any +>y : number | undefined +} + diff --git a/tests/cases/compiler/jsdocFunctionClassPropertiesDeclaration.ts b/tests/cases/compiler/jsdocFunctionClassPropertiesDeclaration.ts new file mode 100644 index 00000000000..321e9c7f3c8 --- /dev/null +++ b/tests/cases/compiler/jsdocFunctionClassPropertiesDeclaration.ts @@ -0,0 +1,18 @@ +// @allowJs: true +// @checkJs: true +// @strict: true +// @declaration: true +// @outDir: ./out +// @filename: /a.js + +/** + * @param {number | undefined} x + * @param {number | undefined} y + */ +export function Foo(x, y) { + if (!(this instanceof Foo)) { + return new Foo(x, y); + } + this.x = x; + this.y = y; +}