Fix exported type resolution in commonjs#24495
Merged
Merged
Conversation
It is fine to resolve the types of exported classes in ES6:
```js
export class C {
}
var c = new C()
```
But not for commonjs exported classes:
```js
module.exports.C = class {
}
var c = new C() // should error
```
Fixes #24492
Member
Author
|
@mhegazy do you want this in 2.9 too? |
ghost
reviewed
May 30, 2018
| // ES6 exports are also visible locally (except for 'default'), but commonjs exports are not (except typedefs) | ||
| if (name !== InternalSymbolName.Default && (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember))) { | ||
| break loop; | ||
| if ((location as SourceFile).commonJsModuleIndicator && !result.declarations.some(isJSDocTypeAlias)) { |
There was a problem hiding this comment.
May be better to do this check before calling lookup.
Contributor
There was a problem hiding this comment.
Can you also check that the location is a soruceFile
Member
Author
There was a problem hiding this comment.
sure, although it would be really weird for a ModuleDeclaration to have a property named commonJsModuleIndicator
Member
Author
There was a problem hiding this comment.
@Andy-MS I still want to call lookup, even for commonjs modules, because it might find a jsdoc type alias. I can't unconditionally skip commonjs modules.
Member
Author
There was a problem hiding this comment.
(believe me, I tried :)
ghost
approved these changes
May 30, 2018
mhegazy
approved these changes
May 30, 2018
Contributor
|
Please port to release-2.9 as well. |
sandersn
added a commit
that referenced
this pull request
May 30, 2018
* Fix resolution of exported types in commonjs
It is fine to resolve the types of exported classes in ES6:
```js
export class C {
}
var c = new C()
```
But not for commonjs exported classes:
```js
module.exports.C = class {
}
var c = new C() // should error
```
Fixes #24492
* All jsdoc type aliases are available locally in commonjs modules
* Check that location isSourceFile before commonJsModuleIndicator
Member
Author
|
OK, it's in 2.9 now too. |
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.
It is fine to resolve the types of exported classes in ES6:
But not for commonjs exported classes:
JSDoc type aliases are an exception since they are always exported implicitly, not by [property] assignment to
module.exportsorexports.Fixes #24492