-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Bind toplevel this assignments as global declarations #22891
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0dc554
Bind toplevel this assignments as global decls
sandersn e58da91
Improve toplevel this-assignment test
sandersn 5831e80
Bind+check toplevel this in commonjs
sandersn 59e2617
Merge branch 'master' into bind-toplevel-this
sandersn 2050027
Don't create new types for toplevel every time
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//// [a.js] | ||
this.a = 10; | ||
this.a; | ||
a; | ||
|
||
|
||
//// [output.js] | ||
this.a = 10; | ||
this.a; | ||
a; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
=== tests/cases/conformance/salsa/a.js === | ||
this.a = 10; | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
||
this.a; | ||
a; | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
=== tests/cases/conformance/salsa/a.js === | ||
this.a = 10; | ||
>this.a = 10 : 10 | ||
>this.a : any | ||
>this : any | ||
>a : any | ||
>10 : 10 | ||
|
||
this.a; | ||
>this.a : any | ||
>this : any | ||
>a : any | ||
|
||
a; | ||
>a : number | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @out: output.js | ||
// @allowJs: true | ||
// @Filename: a.js | ||
this.a = 10; | ||
this.a; | ||
a; |
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.
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.
The following test doesn't work perfectly:
In
/b.js
,this.a
will beany
, whilea
is stillnumber
.I don't see how this ends up in globals?
declareSourceFileMember
seems to put it in the source file locals, but does that really point to the globals instead?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 seems half working, we either acknowledge that
this
at the top level is the global scope, and mirror that both for declarations and look ups, or we do not do either.in other words, if
this.a = 10
is a declaration of a globala
, then shouldvar a = 10;
and both should be visible onthis.a
.For this bug fix, i would say just avoid the assert by handling the
SourceFile
case.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.
Note that top level
this
in a commonjs file is the initialexports
object.this
is onlyglobal
in a traditional namespace-style file.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.
Binding
@andy-ms the single file case works the same way.
file.locals
declarations on a script (not module) source file will be available in the global namespace. As @Kovensky points out, we should put the declaration onfile.exports
when the source file is a module.Type checking
@mhegazy Regarding the type checking, I guess you want us to start treating
this
as{ [s: string]: any, a: number }
instead ofany
? For the module case, we can treat it as the type ofexports
, which is actually the source file, I think. I guess we can look up members on top level this by looking them up in file.locals or file.exports, and just returning any if the member is not found.Short term fix
@mhegazy It’s easy enough to no-op source files — do you think a short-term fix is worth it here? I guess the customer that @sheetalkamat was working with would benefit. I’ll create a separate PR with just the no-op and keep this one open for the complete fix.