Fix non-toplevel prototype assignment#27096
Merged
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).
sandersn
commented
Sep 14, 2018
| ? 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))) { |
Member
Author
There was a problem hiding this comment.
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.
ghost
reviewed
Sep 14, 2018
| // @Filename: a.js | ||
| // @strict: true | ||
|
|
||
| // non top-level: |
There was a problem hiding this comment.
Why not just change the old test? This seems identical to that but not top-level.
Member
Author
There was a problem hiding this comment.
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.
ghost
reviewed
Sep 14, 2018
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.
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);
ghost
approved these changes
Sep 17, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#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