Skip to content

Commit cdfa63a

Browse files
authored
Fix exported type resolution in commonjs (microsoft#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 microsoft#24492 * All jsdoc type aliases are available locally in commonjs modules * Check that location isSourceFile before commonJsModuleIndicator
1 parent 43bf039 commit cdfa63a

8 files changed

+52
-13
lines changed

src/compiler/checker.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1323,8 +1323,14 @@ namespace ts {
13231323
}
13241324
}
13251325

1326+
// ES6 exports are also visible locally (except for 'default'), but commonjs exports are not (except typedefs)
13261327
if (name !== InternalSymbolName.Default && (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember))) {
1327-
break loop;
1328+
if (isSourceFile(location) && location.commonJsModuleIndicator && !result.declarations.some(isJSDocTypeAlias)) {
1329+
result = undefined;
1330+
}
1331+
else {
1332+
break loop;
1333+
}
13281334
}
13291335
break;
13301336
case SyntaxKind.EnumDeclaration:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/salsa/bug24492.js(2,5): error TS2304: Cannot find name 'D'.
2+
3+
4+
==== tests/cases/conformance/salsa/bug24492.js (1 errors) ====
5+
module.exports.D = class { }
6+
new D()
7+
~
8+
!!! error TS2304: Cannot find name 'D'.
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/salsa/bug24492.js ===
2+
module.exports.D = class { }
3+
>module.exports : Symbol(D, Decl(bug24492.js, 0, 0))
4+
>module : Symbol(module)
5+
>D : Symbol(D, Decl(bug24492.js, 0, 0))
6+
7+
new D()
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/salsa/bug24492.js ===
2+
module.exports.D = class { }
3+
>module.exports.D = class { } : typeof D
4+
>module.exports.D : any
5+
>module.exports : any
6+
>module : any
7+
>exports : any
8+
>D : any
9+
>class { } : typeof D
10+
11+
new D()
12+
>new D() : any
13+
>D : any
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @noEmit: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @Filename: bug24492.js
5+
module.exports.D = class { }
6+
new D()

tests/cases/fourslash/refactorConvertToEs6Module_export_named.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ verify.codeFix({
2121
description: "Convert to ES6 module",
2222
newFileContent:
2323
`export function f() {}
24-
const _C = class {
25-
};
26-
export { _C as C };
24+
export class C {}
2725
export const x = 0;
2826
export function a1() {}
2927
export function a2() { return 0; }

tests/cases/fourslash/refactorConvertToEs6Module_export_namedClassExpression.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
verify.codeFix({
1111
description: "Convert to ES6 module",
1212
newFileContent:
13-
`const _C = class E {
14-
static instance = new E();
15-
};
16-
export { _C as C };
17-
const _D = class D {
18-
static instance = new D();
19-
};
20-
export { _D as D };`,
13+
`export const C = class E { static instance = new E(); }
14+
export class D { static instance = new D(); }`,
2115
});

tests/cases/fourslash/refactorConvertToEs6Module_expressionToDeclaration.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
// @target: esnext
55

66
// @Filename: /a.js
7+
////var C = {};
8+
////console.log(C);
79
////exports.f = async function* f(p) { p; }
810
////exports.C = class C extends D { m() {} }
911

1012
verify.codeFix({
1113
description: "Convert to ES6 module",
1214
newFileContent:
13-
`export async function* f(p) { p; }
15+
`var C = {};
16+
console.log(C);
17+
export async function* f(p) { p; }
1418
const _C = class C extends D {
1519
m() { }
1620
};

0 commit comments

Comments
 (0)