Skip to content

Conversation

@sandersn
Copy link
Member

@sandersn sandersn commented Sep 14, 2018

#26925 didn't fix prototype assignment when not at the toplevel. The binder was using the wrong node to lookup the containing class type for prototype assignment, so it incorrectly put the prototype declaration on the class' symbol.

This correction to the binder in turn required a change in getJSClassType in the checker. It now has to look at the "prototype" property for the prototype instead of looking on the class symbol's exports (which makes no sense).

Note that, like the previous fix, the new test baselines exhibit #26923 since they have noImplicitAny on.

Fixes #27095

binder was using the wrong node to lookup the containing class type for
prototype assignment, so it incorrectly put the prototype declaration on
the class' symbol.

This correction to the binder in turn required a change in
getJSClassType in the checker. It now has to look at the "prototype"
property for the prototype instead of looking on the class symbol's exports
(which makes no sense).
? getParentOfBinaryExpression(propertyAccess.parent).parent.kind === SyntaxKind.SourceFile
: propertyAccess.parent.parent.kind === SyntaxKind.SourceFile;
if (!isPrototypeProperty && (!namespaceSymbol || !(namespaceSymbol.flags & SymbolFlags.Namespace)) && isToplevel) {
if (isToplevel && !isPrototypeProperty && (!namespaceSymbol || !(namespaceSymbol.flags & SymbolFlags.Namespace))) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an unrelated change. I moved this condition to the front because it's the most important check, and it was buried behind the complex predicate at the end.

@sandersn sandersn requested review from a user and RyanCavanaugh September 14, 2018 17:17
// @Filename: a.js
// @strict: true

// non top-level:
Copy link

Choose a reason for hiding this comment

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

Why not just change the old test? This seems identical to that but not top-level.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer duplicating tests because even though this bug didn't depend on the isToplevel code path, one of the two tests may catch some future regression there.

const prototype = forEach(assignmentSymbol.declarations, getAssignedJSPrototype);
if (prototype) {
return checkExpression(prototype);
if (assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.has("prototype" as __String)) {
Copy link

Choose a reason for hiding this comment

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

Nit: I would avoid has-then-getand just do:

const prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype" as __String);
const init = prototype && getAssignedJSPrototype(prototype.valueDeclaration);
return init && checkExpression(init);

@sandersn sandersn merged commit c9f1902 into master Sep 17, 2018
@sandersn sandersn deleted the js/fix-non-toplevel-prototype-assignment branch September 17, 2018 20:07
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

This-type fix for prototypes only works at top-level

2 participants