Skip to content
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

No error for require, module.exports or exports in Javascript #23743

Merged
merged 7 commits into from
Apr 30, 2018

Conversation

sandersn
Copy link
Member

Fixes #21933, although I still intend to add a quick fix for Typescript that runs npm install @types/node, and a quick fix for require("fs") that does the same thing. (That second quick fix would apply to JS and TS.)

Note that I removed declarations for require/module/exports from a couple of the JS tests. There are plenty that still have their own declarations, which tests the rest of the code in isCommonJsRequire after line 18772.

Still errors for module and exports, and require's type is now
incorreclty 'any'; I broke module resolution somehow. Needs
investigation.
Everything passes the tests but the code can be improved
if (!result) {
if (originalLocation && isInJavaScriptFile(originalLocation) && originalLocation.parent) {
if (isRequireCall(originalLocation.parent, /*checkArgumentIsStringLiteralLike*/ false)) {
return requireSymbol;
Copy link
Contributor

Choose a reason for hiding this comment

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

we shoudl change all other uses in the checker for isRequireCall to use the symbol identity check instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

But you only get requireSymbol when @types/node (or other declaration) is not present. See the rest of isCommonJsRequire for the verification that's needed on declarations from things like @types/node.

@@ -4671,6 +4688,10 @@ namespace ts {
if (symbol.flags & SymbolFlags.Prototype) {
return links.type = getTypeOfPrototypeProperty(symbol);
}
// CommonsJS require/module/exports all have type any.
if (symbol === requireSymbol || symbol === moduleSymbol || symbol === exportsSymbol) {
return links.type = anyType;
Copy link
Contributor

Choose a reason for hiding this comment

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

type of export is not any, it is the type of the module object. @andy-ms had a related change a while back, we should consolidate the two.. #17745

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops. Thanks for catching that.

@sandersn sandersn merged commit 5ea4d3b into master Apr 30, 2018
@sandersn sandersn deleted the js/no-error-for-require-et-al branch April 30, 2018 22:48
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 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.

Cannot find name 'require' and 'module' when @ts-check is on
2 participants