-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix non-toplevel prototype assignment #27096
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
Conversation
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).
@@ -2522,7 +2522,7 @@ namespace ts { | |||
const isToplevel = isBinaryExpression(propertyAccess.parent) | |||
? 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))) { |
There was a problem hiding this comment.
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.
// @Filename: a.js | ||
// @strict: true | ||
|
||
// non top-level: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/compiler/checker.ts
Outdated
const prototype = forEach(assignmentSymbol.declarations, getAssignedJSPrototype); | ||
if (prototype) { | ||
return checkExpression(prototype); | ||
if (assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.has("prototype" as __String)) { |
There was a problem hiding this comment.
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-get
and just do:
const prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype" as __String);
const init = prototype && getAssignedJSPrototype(prototype.valueDeclaration);
return init && checkExpression(init);
#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