Skip to content

Only functions can be constructor functions #27369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about node.initializer = some Conditional expression that uses functionExpression in each branch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but that doesn't actually work today either, and you can put the annotation on the individual function expressions like so:

const Weird = b ?
    /** @constructor */
    function () { this. x = 1 } :
    /** @constructor */
    function () { this.y = 'yes' };

I'd rather address that as a new feature instead of as a 3.1 bugfix.

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;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/constructorTagOnClassConstructor.symbols
Original file line number Diff line number Diff line change
@@ -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))

20 changes: 20 additions & 0 deletions tests/baselines/reference/constructorTagOnClassConstructor.types
Original file line number Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions tests/cases/conformance/jsdoc/constructorTagOnClassConstructor.ts
Original file line number Diff line number Diff line change
@@ -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];