Fix exported type resolution in commonjs (#24495)

* 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
This commit is contained in:
Nathan Shively-Sanders 2018-05-30 14:12:38 -07:00 committed by GitHub
parent 43bf039a94
commit cdfa63aa40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 13 deletions

View file

@ -1323,8 +1323,14 @@ namespace ts {
}
}
// 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 (isSourceFile(location) && location.commonJsModuleIndicator && !result.declarations.some(isJSDocTypeAlias)) {
result = undefined;
}
else {
break loop;
}
}
break;
case SyntaxKind.EnumDeclaration:

View file

@ -0,0 +1,9 @@
tests/cases/conformance/salsa/bug24492.js(2,5): error TS2304: Cannot find name 'D'.
==== tests/cases/conformance/salsa/bug24492.js (1 errors) ====
module.exports.D = class { }
new D()
~
!!! error TS2304: Cannot find name 'D'.

View file

@ -0,0 +1,8 @@
=== tests/cases/conformance/salsa/bug24492.js ===
module.exports.D = class { }
>module.exports : Symbol(D, Decl(bug24492.js, 0, 0))
>module : Symbol(module)
>D : Symbol(D, Decl(bug24492.js, 0, 0))
new D()

View file

@ -0,0 +1,14 @@
=== tests/cases/conformance/salsa/bug24492.js ===
module.exports.D = class { }
>module.exports.D = class { } : typeof D
>module.exports.D : any
>module.exports : any
>module : any
>exports : any
>D : any
>class { } : typeof D
new D()
>new D() : any
>D : any

View file

@ -0,0 +1,6 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @Filename: bug24492.js
module.exports.D = class { }
new D()

View file

@ -21,9 +21,7 @@ verify.codeFix({
description: "Convert to ES6 module",
newFileContent:
`export function f() {}
const _C = class {
};
export { _C as C };
export class C {}
export const x = 0;
export function a1() {}
export function a2() { return 0; }

View file

@ -10,12 +10,6 @@
verify.codeFix({
description: "Convert to ES6 module",
newFileContent:
`const _C = class E {
static instance = new E();
};
export { _C as C };
const _D = class D {
static instance = new D();
};
export { _D as D };`,
`export const C = class E { static instance = new E(); }
export class D { static instance = new D(); }`,
});

View file

@ -4,13 +4,17 @@
// @target: esnext
// @Filename: /a.js
////var C = {};
////console.log(C);
////exports.f = async function* f(p) { p; }
////exports.C = class C extends D { m() {} }
verify.codeFix({
description: "Convert to ES6 module",
newFileContent:
`export async function* f(p) { p; }
`var C = {};
console.log(C);
export async function* f(p) { p; }
const _C = class C extends D {
m() { }
};