Skip to content

Fix exported type resolution in commonjs#24495

Merged
sandersn merged 3 commits into
masterfrom
fix-exported-type-resolution-in-commonjs
May 30, 2018
Merged

Fix exported type resolution in commonjs#24495
sandersn merged 3 commits into
masterfrom
fix-exported-type-resolution-in-commonjs

Conversation

@sandersn
Copy link
Copy Markdown
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
Copy Markdown
Member Author

@mhegazy do you want this in 2.9 too?

Comment thread src/compiler/checker.ts Outdated
// 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)) {
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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