Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit late bound privates from declaration emit if their name is inaccessable #20169

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,14 +1237,23 @@ namespace ts {
enclosingDeclaration = prevEnclosingDeclaration;
}

function emitPropertyDeclaration(node: Declaration) {
if (hasDynamicName(node) && !resolver.isLateBound(node)) {
function hasNoncollidingLateBoundPropertyName(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
if (!hasModifier(node, ModifierFlags.Private)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to keep the private in the type.. why remove late-bound names?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the name is impossible to posses outside of the local scope, then it is impossible to conflict with, ergo retaining the private holds no value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus, computed names like these were elided pre-dynamic-names anyway, so this is a conservative behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not know that.. you can have a function that returns you the same symbol.. the symbol can "escape" in multiple ways..

Copy link
Member Author

@weswigham weswigham Nov 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In value-space, yes, the name can escape (all you'd need to do is alias the type or something); but it can't as far as our type system in concerned - if the late bound name is exported then we'll write the private, since it is visible and we know the name can be referenced elsewhere, if it is not visible, then there's no public way to access the same name, ergo you can't conflict with it (or at least we have no way of checking).
For instance:

const _data = Symbol('data');

export function getDataSymbol() {
  return _data;
}

export class User {
    private [_data] : any;
};

getDataSymbol returns symbol (not a reference to _data's unique symbol), even though it's easy to see that you could make a subclass that attempts to mangle the same symbol, we have no way of verifying that it is, in fact, the same symbol (since it didn't come from the same origin).

Copy link
Member Author

@weswigham weswigham Nov 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if you annotate getDataSymbol to explicitly return typeof _data, we'll happily tell you at that point in time that _data isn't exported and cannot be named (and once it is exported we'll write the private member out).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is this case (simplified):

// a.ts
export const x = Symbol();
export function getX(): typeof x { return x; }

// b.ts
import { getX } from "./a";

const x = getX();

export class C {
  private [x]: number = 1;
}

// c.ts
import { getX } from "./a";
import { C } from "./b";

const x = getX();

export class D extends C {
  private [x]: string = "a"; // oops
}

Copy link
Member Author

@weswigham weswigham Nov 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I suppose the correct thing to check isn't visibility from the current declaration, but rather if the the name is reachable via the exports of its containing file at all. And then adding a visibility check to ensure an alias to it is visible in scope and complaining that it cannot be referenced if not. Meaning

import { getX } from "./a";
import { C } from "./b";

const x = getX();

export class D extends C {
  private [x]: string = "a"; // oops
}

is an error because x cannot be named in the declaration file (without modifying its shape, anyway), while

import { getX, x as xsym } from "./a";
import { C } from "./b";

const x = getX();

export class D extends C {
  private [x]: string = "a"; // oops
}

is fine since the name can be written by its alias from a.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so why not just keep the private, and the const with the unique symbol, but just add a export {}; to the file to make the scoping work correctly.. we talked about this before in the context of other declaration emit issues.

return false;
}
const entityName = (node as NamedDeclaration as LateBoundDeclaration).name.expression;
const visibilityResult = resolver.isEntityNameVisible(entityName, enclosingDeclaration);
return visibilityResult.accessibility !== SymbolAccessibility.Accessible;
}

function emitPropertyDeclaration(node: ParameterDeclaration | PropertyDeclaration) {
if (hasDynamicName(node) && (!resolver.isLateBound(node) || hasNoncollidingLateBoundPropertyName(node))) {
return;
}

emitJsDocComments(node);
emitClassMemberDeclarationFlags(getModifierFlags(node));
emitVariableDeclaration(<VariableDeclaration>node);
emitVariableDeclaration(node);
write(";");
writeLine();
}
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/privateSymbolNoDeclarationEmit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [privateSymbolNoDeclarationEmit.ts]
const _data = Symbol('data');

export class User {
private [_data] : any;
};

//// [privateSymbolNoDeclarationEmit.js]
"use strict";
exports.__esModule = true;
var _data = Symbol('data');
var User = /** @class */ (function () {
function User() {
}
return User;
}());
exports.User = User;
;


//// [privateSymbolNoDeclarationEmit.d.ts]
export declare class User {
}
12 changes: 12 additions & 0 deletions tests/baselines/reference/privateSymbolNoDeclarationEmit.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/privateSymbolNoDeclarationEmit.ts ===
const _data = Symbol('data');
>_data : Symbol(_data, Decl(privateSymbolNoDeclarationEmit.ts, 0, 5))
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))

export class User {
>User : Symbol(User, Decl(privateSymbolNoDeclarationEmit.ts, 0, 29))

private [_data] : any;
>_data : Symbol(_data, Decl(privateSymbolNoDeclarationEmit.ts, 0, 5))

};
14 changes: 14 additions & 0 deletions tests/baselines/reference/privateSymbolNoDeclarationEmit.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/privateSymbolNoDeclarationEmit.ts ===
const _data = Symbol('data');
>_data : unique symbol
>Symbol('data') : unique symbol
>Symbol : SymbolConstructor
>'data' : "data"

export class User {
>User : User

private [_data] : any;
>_data : unique symbol

};
7 changes: 7 additions & 0 deletions tests/cases/compiler/privateSymbolNoDeclarationEmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @lib: es6
// @declaration: true
const _data = Symbol('data');

export class User {
private [_data] : any;
};