Skip to content

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

Merged
sandersn merged 7 commits into
masterfrom
js/no-error-for-require-et-al
Apr 30, 2018
Merged

No error for require, module.exports or exports in Javascript#23743
sandersn merged 7 commits into
masterfrom
js/no-error-for-require-et-al

Conversation

@sandersn
Copy link
Copy Markdown
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
Comment thread src/compiler/checker.ts
if (!result) {
if (originalLocation && isInJavaScriptFile(originalLocation) && originalLocation.parent) {
if (isRequireCall(originalLocation.parent, /*checkArgumentIsStringLiteralLike*/ false)) {
return requireSymbol;
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread src/compiler/checker.ts
}
// CommonsJS require/module/exports all have type any.
if (symbol === requireSymbol || symbol === moduleSymbol || symbol === exportsSymbol) {
return links.type = anyType;
Copy link
Copy Markdown
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
Copy Markdown
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