Skip to content

In source files and blocks, bind function declarations before other statements#22766

Merged
sandersn merged 9 commits into
masterfrom
js/fix-this-assignments-in-ooorder-prototype-assignment
Mar 21, 2018
Merged

In source files and blocks, bind function declarations before other statements#22766
sandersn merged 9 commits into
masterfrom
js/fix-this-assignments-in-ooorder-prototype-assignment

Conversation

@sandersn
Copy link
Copy Markdown
Member

Functions should be bound before other statements to match the hoisting behaviour of the Javascript runtime. In contrast, var declarations should not be bound before other statements because property access isn't legal until after initialisation, and initialisation isn't hoisted the way it is with functions.

This change isn't observable in correct Typescript code since we don't allow assignment of properties on the function. But this is common in Javascript, and some code bases use it frequently (webpack, npm, semver and minimist, in the survey I recently did).

Errors from the binder are somewhat order dependent, especially for complex merges. That means there is some error message churn.

Fixes #22725
Fixes #22438

@sandersn sandersn requested review from mhegazy and weswigham March 21, 2018 18:41

Point.origin = new Point(0, 0);
~~~~~~
!!! error TS2339: Property 'origin' does not exist on type '(x: any, y: any) => any'.
Copy link
Copy Markdown
Member

@weswigham weswigham Mar 21, 2018

Choose a reason for hiding this comment

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

This part of this test should probably be moved into a .js file in the test, to preserve original intent? (Which was that the ambient class declaration above provides contextual types for these functions)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

While I agree that moving this part into a .js file is the right thing, I don't understand why the ambient class should provide contextual types for the functions. At best they should merge, which is what they do now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Eh? Ok. I don't really have a strong opinion on it, since it feels like the kinda thing that maybe just feel out from other more intentioned choices before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm. Well, @DanielRosenwasser convinced me that contextual typing makes sense for the prototype method assignment, but not for the function declaration. I removed the conflicting function instead.



==== tests/cases/conformance/salsa/use.js (1 errors) ====
var mini = require('./minimatch')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe add a .d.ts into the test that declares require and module, just so there's no unnecessary error baseline?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried that already, and, because of the uses of require/exports, these files aren't in the global namespace any more. I forgot that I could use triple-slash references to work around that.

I still couldn't get rid of the error on assignment to exports.

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

Seems OK, just small nits about tests.

@sandersn sandersn merged commit de4a69c into master Mar 21, 2018
@sandersn sandersn deleted the js/fix-this-assignments-in-ooorder-prototype-assignment branch March 21, 2018 21:23
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
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.

2 participants