Skip to content

Commit 61a8206

Browse files
committed
Omit late bound privates from declaration emit if their name is inaccessable
1 parent 2c8e49f commit 61a8206

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,14 +1237,23 @@ namespace ts {
12371237
enclosingDeclaration = prevEnclosingDeclaration;
12381238
}
12391239

1240-
function emitPropertyDeclaration(node: Declaration) {
1241-
if (hasDynamicName(node) && !resolver.isLateBound(node)) {
1240+
function hasNoncollidingLateBoundPropertyName(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
1241+
if (!hasModifier(node, ModifierFlags.Private)) {
1242+
return false;
1243+
}
1244+
const entityName = (node as NamedDeclaration as LateBoundDeclaration).name.expression;
1245+
const visibilityResult = resolver.isEntityNameVisible(entityName, enclosingDeclaration);
1246+
return visibilityResult.accessibility !== SymbolAccessibility.Accessible;
1247+
}
1248+
1249+
function emitPropertyDeclaration(node: ParameterDeclaration | PropertyDeclaration) {
1250+
if (hasDynamicName(node) && (!resolver.isLateBound(node) || hasNoncollidingLateBoundPropertyName(node))) {
12421251
return;
12431252
}
12441253

12451254
emitJsDocComments(node);
12461255
emitClassMemberDeclarationFlags(getModifierFlags(node));
1247-
emitVariableDeclaration(<VariableDeclaration>node);
1256+
emitVariableDeclaration(node);
12481257
write(";");
12491258
writeLine();
12501259
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [privateSymbolNoDeclarationEmit.ts]
2+
const _data = Symbol('data');
3+
4+
export class User {
5+
private [_data] : any;
6+
};
7+
8+
//// [privateSymbolNoDeclarationEmit.js]
9+
"use strict";
10+
exports.__esModule = true;
11+
var _data = Symbol('data');
12+
var User = /** @class */ (function () {
13+
function User() {
14+
}
15+
return User;
16+
}());
17+
exports.User = User;
18+
;
19+
20+
21+
//// [privateSymbolNoDeclarationEmit.d.ts]
22+
export declare class User {
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/privateSymbolNoDeclarationEmit.ts ===
2+
const _data = Symbol('data');
3+
>_data : Symbol(_data, Decl(privateSymbolNoDeclarationEmit.ts, 0, 5))
4+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
5+
6+
export class User {
7+
>User : Symbol(User, Decl(privateSymbolNoDeclarationEmit.ts, 0, 29))
8+
9+
private [_data] : any;
10+
>_data : Symbol(_data, Decl(privateSymbolNoDeclarationEmit.ts, 0, 5))
11+
12+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/privateSymbolNoDeclarationEmit.ts ===
2+
const _data = Symbol('data');
3+
>_data : unique symbol
4+
>Symbol('data') : unique symbol
5+
>Symbol : SymbolConstructor
6+
>'data' : "data"
7+
8+
export class User {
9+
>User : User
10+
11+
private [_data] : any;
12+
>_data : unique symbol
13+
14+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @lib: es6
2+
// @declaration: true
3+
const _data = Symbol('data');
4+
5+
export class User {
6+
private [_data] : any;
7+
};

0 commit comments

Comments
 (0)