Skip to content

Commit b7c598e

Browse files
authored
Use ad-hoc code for nested destructuring in require (microsoft#40188)
* Use ad-hoc code for nested destructuring in require Nested destructuring doesn't really map to ES imports: ```js const { utils: { nub, intercalate } } = require('./monopackage') ``` Previously, isRequireVariableDeclaration walked up binding elements until it reached a variable declaration. This change instead only walks up one binding element and stops. Then it's not bound as an alias and uses the checker-only code to produce types for the nested-imported identifiers. Fixes microsoft#40143 * revert binder formatting change
1 parent 45dedd6 commit b7c598e

6 files changed

+138
-1
lines changed

src/compiler/utilities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,9 @@ namespace ts {
19391939
export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: true): node is RequireVariableDeclaration;
19401940
export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: boolean): node is VariableDeclaration;
19411941
export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: boolean): node is VariableDeclaration {
1942-
node = getRootDeclaration(node);
1942+
if (node.kind === SyntaxKind.BindingElement) {
1943+
node = node.parent.parent;
1944+
}
19431945
return isVariableDeclaration(node) && !!node.initializer && isRequireCall(getLeftmostAccessExpression(node.initializer), requireStringLiteralLikeArgument);
19441946
}
19451947

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/conformance/salsa/main.js(5,1): error TS2304: Cannot find name 'chalk'.
2+
3+
4+
==== tests/cases/conformance/salsa/main.js (1 errors) ====
5+
const {
6+
chalk: { grey }
7+
} = require('./mod1');
8+
grey
9+
chalk
10+
~~~~~
11+
!!! error TS2304: Cannot find name 'chalk'.
12+
13+
==== tests/cases/conformance/salsa/mod1.js (0 errors) ====
14+
const chalk = {
15+
grey: {}
16+
};
17+
module.exports.chalk = chalk
18+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/conformance/salsa/nestedDestructuringOfRequire.ts] ////
2+
3+
//// [mod1.js]
4+
const chalk = {
5+
grey: {}
6+
};
7+
module.exports.chalk = chalk
8+
9+
//// [main.js]
10+
const {
11+
chalk: { grey }
12+
} = require('./mod1');
13+
grey
14+
chalk
15+
16+
17+
//// [mod1.js]
18+
var chalk = {
19+
grey: {}
20+
};
21+
module.exports.chalk = chalk;
22+
//// [main.js]
23+
var grey = require('./mod1').chalk.grey;
24+
grey;
25+
chalk;
26+
27+
28+
//// [mod1.d.ts]
29+
export namespace chalk {
30+
const grey: {};
31+
}
32+
//// [main.d.ts]
33+
export {};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/salsa/main.js ===
2+
const {
3+
chalk: { grey }
4+
>chalk : Symbol(chalk, Decl(mod1.js, 2, 2))
5+
>grey : Symbol(grey, Decl(main.js, 1, 12))
6+
7+
} = require('./mod1');
8+
>require : Symbol(require)
9+
>'./mod1' : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0))
10+
11+
grey
12+
>grey : Symbol(grey, Decl(main.js, 1, 12))
13+
14+
chalk
15+
16+
=== tests/cases/conformance/salsa/mod1.js ===
17+
const chalk = {
18+
>chalk : Symbol(chalk, Decl(mod1.js, 0, 5))
19+
20+
grey: {}
21+
>grey : Symbol(grey, Decl(mod1.js, 0, 15))
22+
23+
};
24+
module.exports.chalk = chalk
25+
>module.exports.chalk : Symbol(chalk, Decl(mod1.js, 2, 2))
26+
>module.exports : Symbol(chalk, Decl(mod1.js, 2, 2))
27+
>module : Symbol(module, Decl(mod1.js, 2, 2))
28+
>exports : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0))
29+
>chalk : Symbol(chalk, Decl(mod1.js, 2, 2))
30+
>chalk : Symbol(chalk, Decl(mod1.js, 0, 5))
31+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/salsa/main.js ===
2+
const {
3+
chalk: { grey }
4+
>chalk : any
5+
>grey : {}
6+
7+
} = require('./mod1');
8+
>require('./mod1') : typeof import("tests/cases/conformance/salsa/mod1")
9+
>require : any
10+
>'./mod1' : "./mod1"
11+
12+
grey
13+
>grey : {}
14+
15+
chalk
16+
>chalk : any
17+
18+
=== tests/cases/conformance/salsa/mod1.js ===
19+
const chalk = {
20+
>chalk : { grey: {}; }
21+
>{ grey: {}} : { grey: {}; }
22+
23+
grey: {}
24+
>grey : {}
25+
>{} : {}
26+
27+
};
28+
module.exports.chalk = chalk
29+
>module.exports.chalk = chalk : { grey: {}; }
30+
>module.exports.chalk : { grey: {}; }
31+
>module.exports : typeof import("tests/cases/conformance/salsa/mod1")
32+
>module : { "\"tests/cases/conformance/salsa/mod1\"": typeof import("tests/cases/conformance/salsa/mod1"); }
33+
>exports : typeof import("tests/cases/conformance/salsa/mod1")
34+
>chalk : { grey: {}; }
35+
>chalk : { grey: {}; }
36+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @outDir: out
4+
// @declaration: true
5+
6+
// @filename: mod1.js
7+
const chalk = {
8+
grey: {}
9+
};
10+
module.exports.chalk = chalk
11+
12+
// @filename: main.js
13+
const {
14+
chalk: { grey }
15+
} = require('./mod1');
16+
grey
17+
chalk

0 commit comments

Comments
 (0)