Skip to content

Commit 9fb6acf

Browse files
authored
Add missed resolveSymbol in commonjs import resolution (#41479)
Fixes resolution of export aliases in the postfix-property-access case of commonjs require: ```js const { x } = require('./foo').nested x ``` This program would previously fail if `x` was an export alias. Fixes #41422
1 parent 728c9cc commit 9fb6acf

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,7 @@ namespace ts {
27902790
const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias);
27912791
const name = node.propertyName || node.name;
27922792
if (commonJSPropertyAccess && resolved && isIdentifier(name)) {
2793-
return getPropertyOfType(getTypeOfSymbol(resolved), name.escapedText);
2793+
return resolveSymbol(getPropertyOfType(getTypeOfSymbol(resolved), name.escapedText), dontResolveAlias);
27942794
}
27952795
markSymbolOfAliasDeclarationIfTypeOnly(node, /*immediateTarget*/ undefined, resolved, /*overwriteEmpty*/ false);
27962796
return resolved;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/conformance/salsa/main.js ===
2+
const { hardline } = require('./second').nested;
3+
>hardline : Symbol(hardline, Decl(main.js, 0, 7))
4+
>require('./second').nested : Symbol(nested, Decl(second.js, 0, 18))
5+
>require : Symbol(require)
6+
>'./second' : Symbol("tests/cases/conformance/salsa/second", Decl(second.js, 0, 0))
7+
>nested : Symbol(nested, Decl(second.js, 0, 18))
8+
9+
hardline
10+
>hardline : Symbol(hardline, Decl(main.js, 0, 7))
11+
12+
=== tests/cases/conformance/salsa/first.js ===
13+
// #41422, based on prettier's exports
14+
15+
const hardline = { type: "hard" }
16+
>hardline : Symbol(hardline, Decl(first.js, 2, 5))
17+
>type : Symbol(type, Decl(first.js, 2, 18))
18+
19+
module.exports = {
20+
>module.exports : Symbol("tests/cases/conformance/salsa/first", Decl(first.js, 0, 0))
21+
>module : Symbol(module, Decl(first.js, 2, 33))
22+
>exports : Symbol("tests/cases/conformance/salsa/first", Decl(first.js, 0, 0))
23+
24+
hardline
25+
>hardline : Symbol(hardline, Decl(first.js, 3, 18))
26+
}
27+
28+
29+
=== tests/cases/conformance/salsa/second.js ===
30+
module.exports = {
31+
>module.exports : Symbol("tests/cases/conformance/salsa/second", Decl(second.js, 0, 0))
32+
>module : Symbol(export=, Decl(second.js, 0, 0))
33+
>exports : Symbol(export=, Decl(second.js, 0, 0))
34+
35+
nested: require('./first')
36+
>nested : Symbol(nested, Decl(second.js, 0, 18))
37+
>require : Symbol(require)
38+
>'./first' : Symbol("tests/cases/conformance/salsa/first", Decl(first.js, 0, 0))
39+
40+
};
41+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
=== tests/cases/conformance/salsa/main.js ===
2+
const { hardline } = require('./second').nested;
3+
>hardline : { type: string; }
4+
>require('./second').nested : typeof import("tests/cases/conformance/salsa/first")
5+
>require('./second') : { nested: typeof import("tests/cases/conformance/salsa/first"); }
6+
>require : any
7+
>'./second' : "./second"
8+
>nested : typeof import("tests/cases/conformance/salsa/first")
9+
10+
hardline
11+
>hardline : { type: string; }
12+
13+
=== tests/cases/conformance/salsa/first.js ===
14+
// #41422, based on prettier's exports
15+
16+
const hardline = { type: "hard" }
17+
>hardline : { type: string; }
18+
>{ type: "hard" } : { type: string; }
19+
>type : string
20+
>"hard" : "hard"
21+
22+
module.exports = {
23+
>module.exports = { hardline} : typeof import("tests/cases/conformance/salsa/first")
24+
>module.exports : typeof import("tests/cases/conformance/salsa/first")
25+
>module : { "\"tests/cases/conformance/salsa/first\"": typeof import("tests/cases/conformance/salsa/first"); }
26+
>exports : typeof import("tests/cases/conformance/salsa/first")
27+
>{ hardline} : { hardline: { type: string; }; }
28+
29+
hardline
30+
>hardline : { type: string; }
31+
}
32+
33+
34+
=== tests/cases/conformance/salsa/second.js ===
35+
module.exports = {
36+
>module.exports = { nested: require('./first')} : { nested: typeof import("tests/cases/conformance/salsa/first"); }
37+
>module.exports : { nested: typeof import("tests/cases/conformance/salsa/first"); }
38+
>module : { "\"tests/cases/conformance/salsa/second\"": { nested: typeof import("tests/cases/conformance/salsa/first"); }; }
39+
>exports : { nested: typeof import("tests/cases/conformance/salsa/first"); }
40+
>{ nested: require('./first')} : { nested: typeof import("tests/cases/conformance/salsa/first"); }
41+
42+
nested: require('./first')
43+
>nested : typeof import("tests/cases/conformance/salsa/first")
44+
>require('./first') : typeof import("tests/cases/conformance/salsa/first")
45+
>require : any
46+
>'./first' : "./first"
47+
48+
};
49+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// #41422, based on prettier's exports
2+
// @noEmit: true
3+
// @checkJS: true
4+
5+
// @filename: first.js
6+
const hardline = { type: "hard" }
7+
module.exports = {
8+
hardline
9+
}
10+
11+
12+
// @filename: second.js
13+
module.exports = {
14+
nested: require('./first')
15+
};
16+
17+
// @filename: main.js
18+
const { hardline } = require('./second').nested;
19+
hardline

0 commit comments

Comments
 (0)