Skip to content

Fix exported type resolution in commonjs #24495

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

Merged
merged 3 commits into from
May 30, 2018

Conversation

sandersn
Copy link
Member

It is fine to resolve the types of exported classes in ES6:

export class C {
}
var c = new C()

But not for commonjs exported classes:

module.exports.C = class {
}
var c = new C() // should error

JSDoc type aliases are an exception since they are always exported implicitly, not by [property] assignment to module.exports or exports.

Fixes #24492

sandersn added 2 commits May 30, 2018 12:43
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
@sandersn sandersn requested review from a user and mhegazy May 30, 2018 19:50
@sandersn
Copy link
Member Author

@mhegazy do you want this in 2.9 too?

if (name !== InternalSymbolName.Default && (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember))) {
break loop;
if ((location as SourceFile).commonJsModuleIndicator && !result.declarations.some(isJSDocTypeAlias)) {
Copy link

Choose a reason for hiding this comment

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

May be better to do this check before calling lookup.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also check that the location is a soruceFile

Copy link
Member Author

Choose a reason for hiding this comment

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

sure, although it would be really weird for a ModuleDeclaration to have a property named commonJsModuleIndicator

Copy link
Member Author

Choose a reason for hiding this comment

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

@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.

Copy link
Member Author

Choose a reason for hiding this comment

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

(believe me, I tried :)

@mhegazy
Copy link
Contributor

mhegazy commented May 30, 2018

Please port to release-2.9 as well.

@sandersn sandersn merged commit cdfa63a into master May 30, 2018
@sandersn sandersn deleted the fix-exported-type-resolution-in-commonjs branch May 30, 2018 21:12
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
@sandersn
Copy link
Member Author

OK, it's in 2.9 now too.

@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.

In JS, crash on incorrect type reference of a module.exports property assignment
2 participants