diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 86bc7ec1d16b5..61df6fcff7ace 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20272,18 +20272,20 @@ namespace ts { * file. */ function isJSConstructor(node: Declaration | undefined): boolean { - if (node && isInJSFile(node)) { + if (!node || !isInJSFile(node)) { + return false; + } + const func = isFunctionDeclaration(node) || isFunctionExpression(node) ? node : + isVariableDeclaration(node) && node.initializer && isFunctionExpression(node.initializer) ? node.initializer : + undefined; + if (func) { // If the node has a @class tag, treat it like a constructor. if (getJSDocClassTag(node)) return true; // If the symbol of the node has members, treat it like a constructor. - const symbol = isFunctionDeclaration(node) || isFunctionExpression(node) ? getSymbolOfNode(node) : - isVariableDeclaration(node) && node.initializer && isFunctionExpression(node.initializer) ? getSymbolOfNode(node.initializer) : - undefined; - + const symbol = getSymbolOfNode(func); return !!symbol && symbol.members !== undefined; } - return false; } diff --git a/tests/baselines/reference/constructorTagOnClassConstructor.symbols b/tests/baselines/reference/constructorTagOnClassConstructor.symbols new file mode 100644 index 0000000000000..25e9b10bcba5d --- /dev/null +++ b/tests/baselines/reference/constructorTagOnClassConstructor.symbols @@ -0,0 +1,19 @@ +=== tests/cases/conformance/jsdoc/bug27025.js === +export class Alpha { } +>Alpha : Symbol(Alpha, Decl(bug27025.js, 0, 0)) + +export class Beta { +>Beta : Symbol(Beta, Decl(bug27025.js, 0, 22)) + + /** + * @constructor + */ + constructor() { + } +} + +const arr = [Alpha, Beta]; +>arr : Symbol(arr, Decl(bug27025.js, 9, 5)) +>Alpha : Symbol(Alpha, Decl(bug27025.js, 0, 0)) +>Beta : Symbol(Beta, Decl(bug27025.js, 0, 22)) + diff --git a/tests/baselines/reference/constructorTagOnClassConstructor.types b/tests/baselines/reference/constructorTagOnClassConstructor.types new file mode 100644 index 0000000000000..f93061704a923 --- /dev/null +++ b/tests/baselines/reference/constructorTagOnClassConstructor.types @@ -0,0 +1,20 @@ +=== tests/cases/conformance/jsdoc/bug27025.js === +export class Alpha { } +>Alpha : Alpha + +export class Beta { +>Beta : Beta + + /** + * @constructor + */ + constructor() { + } +} + +const arr = [Alpha, Beta]; +>arr : (typeof Alpha)[] +>[Alpha, Beta] : (typeof Alpha)[] +>Alpha : typeof Alpha +>Beta : typeof Beta + diff --git a/tests/cases/conformance/jsdoc/constructorTagOnClassConstructor.ts b/tests/cases/conformance/jsdoc/constructorTagOnClassConstructor.ts new file mode 100644 index 0000000000000..6d2f3780eeee3 --- /dev/null +++ b/tests/cases/conformance/jsdoc/constructorTagOnClassConstructor.ts @@ -0,0 +1,14 @@ +// @allowJs: true +// @noEmit: true +// @checkJs: true +// @Filename: bug27025.js +export class Alpha { } +export class Beta { + /** + * @constructor + */ + constructor() { + } +} + +const arr = [Alpha, Beta];