Skip to content

Commit b0c0234

Browse files
committed
Fix #17085
1 parent 960d114 commit b0c0234

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,10 +1249,18 @@ namespace ts {
12491249
writeLine();
12501250
}
12511251

1252+
function bindingNameContainsVisibleBindingElement(node: BindingName): boolean {
1253+
return !!node && isBindingPattern(node) && some(node.elements, elem => !isOmittedExpression(elem) && (resolver.isDeclarationVisible(elem) || bindingNameContainsVisibleBindingElement(elem.name)));
1254+
}
1255+
1256+
function isVariableDeclarationVisible(node: VariableDeclaration) {
1257+
return resolver.isDeclarationVisible(node) || bindingNameContainsVisibleBindingElement(node.name);
1258+
}
1259+
12521260
function emitVariableDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
12531261
// If we are emitting property it isn't moduleElement and hence we already know it needs to be emitted
12541262
// so there is no check needed to see if declaration is visible
1255-
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
1263+
if (node.kind !== SyntaxKind.VariableDeclaration || isVariableDeclarationVisible(node)) {
12561264
if (isBindingPattern(node.name)) {
12571265
emitBindingPattern(<BindingPattern>node.name);
12581266
}
@@ -1324,14 +1332,14 @@ namespace ts {
13241332
}
13251333

13261334
function emitBindingPattern(bindingPattern: BindingPattern) {
1327-
// Only select non-omitted expression from the bindingPattern's elements.
1335+
// Only select visible, non-omitted expression from the bindingPattern's elements.
13281336
// We have to do this to avoid emitting trailing commas.
13291337
// For example:
13301338
// original: var [, c,,] = [ 2,3,4]
13311339
// emitted: declare var c: number; // instead of declare var c:number, ;
13321340
const elements: Node[] = [];
13331341
for (const element of bindingPattern.elements) {
1334-
if (element.kind !== SyntaxKind.OmittedExpression) {
1342+
if (element.kind !== SyntaxKind.OmittedExpression && resolver.isDeclarationVisible(element)) {
13351343
elements.push(element);
13361344
}
13371345
}
@@ -1371,7 +1379,7 @@ namespace ts {
13711379
}
13721380

13731381
function isVariableStatementVisible(node: VariableStatement) {
1374-
return forEach(node.declarationList.declarations, varDeclaration => resolver.isDeclarationVisible(varDeclaration));
1382+
return forEach(node.declarationList.declarations, varDeclaration => isVariableDeclarationVisible(varDeclaration));
13751383
}
13761384

13771385
function writeVariableStatement(node: VariableStatement) {
@@ -1390,7 +1398,7 @@ namespace ts {
13901398
else {
13911399
write("var ");
13921400
}
1393-
emitCommaList(node.declarationList.declarations, emitVariableDeclaration, resolver.isDeclarationVisible);
1401+
emitCommaList(node.declarationList.declarations, emitVariableDeclaration, isVariableDeclarationVisible);
13941402
write(";");
13951403
writeLine();
13961404
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [tests/cases/compiler/destructuredDeclarationEmit.ts] ////
2+
3+
//// [foo.ts]
4+
const foo = { bar: 'hello', bat: 'world' };
5+
export { foo };
6+
//// [index.ts]
7+
import { foo } from './foo';
8+
export { foo };
9+
10+
const { bar: baz, bat } = foo;
11+
export { baz };
12+
13+
//// [foo.js]
14+
"use strict";
15+
exports.__esModule = true;
16+
var foo = { bar: 'hello', bat: 'world' };
17+
exports.foo = foo;
18+
//// [index.js]
19+
"use strict";
20+
exports.__esModule = true;
21+
var foo_1 = require("./foo");
22+
exports.foo = foo_1.foo;
23+
var baz = foo_1.foo.bar, bat = foo_1.foo.bat;
24+
exports.baz = baz;
25+
26+
27+
//// [foo.d.ts]
28+
declare const foo: {
29+
bar: string;
30+
bat: string;
31+
};
32+
export { foo };
33+
//// [index.d.ts]
34+
import { foo } from './foo';
35+
export { foo };
36+
declare const baz: string;
37+
export { baz };
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/foo.ts ===
2+
const foo = { bar: 'hello', bat: 'world' };
3+
>foo : Symbol(foo, Decl(foo.ts, 0, 5))
4+
>bar : Symbol(bar, Decl(foo.ts, 0, 13))
5+
>bat : Symbol(bat, Decl(foo.ts, 0, 27))
6+
7+
export { foo };
8+
>foo : Symbol(foo, Decl(foo.ts, 1, 8))
9+
10+
=== tests/cases/compiler/index.ts ===
11+
import { foo } from './foo';
12+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
13+
14+
export { foo };
15+
>foo : Symbol(foo, Decl(index.ts, 1, 8))
16+
17+
const { bar: baz, bat } = foo;
18+
>bar : Symbol(bar, Decl(foo.ts, 0, 13))
19+
>baz : Symbol(baz, Decl(index.ts, 3, 7))
20+
>bat : Symbol(bat, Decl(index.ts, 3, 17))
21+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
22+
23+
export { baz };
24+
>baz : Symbol(baz, Decl(index.ts, 4, 8))
25+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/foo.ts ===
2+
const foo = { bar: 'hello', bat: 'world' };
3+
>foo : { bar: string; bat: string; }
4+
>{ bar: 'hello', bat: 'world' } : { bar: string; bat: string; }
5+
>bar : string
6+
>'hello' : "hello"
7+
>bat : string
8+
>'world' : "world"
9+
10+
export { foo };
11+
>foo : { bar: string; bat: string; }
12+
13+
=== tests/cases/compiler/index.ts ===
14+
import { foo } from './foo';
15+
>foo : { bar: string; bat: string; }
16+
17+
export { foo };
18+
>foo : { bar: string; bat: string; }
19+
20+
const { bar: baz, bat } = foo;
21+
>bar : any
22+
>baz : string
23+
>bat : string
24+
>foo : { bar: string; bat: string; }
25+
26+
export { baz };
27+
>baz : string
28+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @declaration: true
2+
// @filename: foo.ts
3+
const foo = { bar: 'hello', bat: 'world' };
4+
export { foo };
5+
// @filename: index.ts
6+
import { foo } from './foo';
7+
export { foo };
8+
9+
const { bar: baz, bat } = foo;
10+
export { baz };

0 commit comments

Comments
 (0)