Skip to content

Commit b5c0933

Browse files
Max Heibermheiber
Max Heiber
authored andcommitted
update naming and cleanup for check private names
for private name support in the checker: - make names more consistent - remove unnecessary checks - add utility function to public API - other small cleanup Signed-off-by: Max Heiber <[email protected]>
1 parent 60bc29a commit b5c0933

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

src/compiler/binder.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,6 @@ namespace ts {
388388
symbolTable.set(name, symbol = createSymbol(SymbolFlags.None, name));
389389
}
390390
else if (!(includes & SymbolFlags.Variable && symbol.flags & SymbolFlags.Assignment)) {
391-
// Assignment declarations are allowed to merge with variables, no matter what other flags they have.
392-
if (isNamedDeclaration(node)) {
393-
node.name.parent = node;
394-
}
395-
396391
// Report errors every position with duplicate declaration
397392
// Report errors on previous encountered declarations
398393
let message = symbol.flags & SymbolFlags.BlockScopedVariable

src/compiler/checker.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ namespace ts {
122122
getDeclaredTypeOfSymbol,
123123
getPropertiesOfType,
124124
getPropertyOfType: (type, name) => getPropertyOfType(type, escapeLeadingUnderscores(name)),
125+
getPropertyForPrivateName,
125126
getTypeOfPropertyOfType: (type, name) => getTypeOfPropertyOfType(type, escapeLeadingUnderscores(name)),
126127
getIndexInfoOfType,
127128
getSignaturesOfType,
@@ -18447,38 +18448,40 @@ namespace ts {
1844718448
return checkPropertyAccessExpressionOrQualifiedName(node, node.left, node.right);
1844818449
}
1844918450

18450-
function getPropertyByPrivateName(apparentType: Type, leftType: Type, right: PrivateName): Symbol | undefined {
18451+
function getPropertyForPrivateName(apparentType: Type, leftType: Type, right: PrivateName, errorNode: Node | undefined): Symbol | undefined {
1845118452
let classWithShadowedPrivateName;
18452-
let klass = getContainingClass(right);
18453-
while (klass) {
18454-
const symbolTableKey = getPropertyNameForPrivateNameDescription(klass.symbol, right.escapedText);
18453+
let container = getContainingClass(right);
18454+
while (container) {
18455+
const symbolTableKey = getPropertyNameForPrivateNameDescription(container.symbol, right.escapedText);
1845518456
if (symbolTableKey) {
1845618457
const prop = getPropertyOfType(apparentType, symbolTableKey);
1845718458
if (prop) {
1845818459
if (classWithShadowedPrivateName) {
18459-
error(
18460-
right,
18461-
Diagnostics.This_usage_of_0_refers_to_the_private_member_declared_in_its_enclosing_class_While_type_1_has_a_private_member_with_the_same_spelling_its_declaration_and_accessibility_are_distinct,
18462-
diagnosticName(right),
18463-
diagnosticName(classWithShadowedPrivateName.name || ("(anonymous)" as __String))
18464-
);
18460+
if (errorNode) {
18461+
error(
18462+
errorNode,
18463+
Diagnostics.This_usage_of_0_refers_to_the_private_member_declared_in_its_enclosing_class_While_type_1_has_a_private_member_with_the_same_spelling_its_declaration_and_accessibility_are_distinct,
18464+
diagnosticName(right),
18465+
diagnosticName(classWithShadowedPrivateName.name || ("(anonymous)" as __String))
18466+
);
18467+
}
1846518468
return undefined;
1846618469
}
1846718470
return prop;
1846818471
}
1846918472
else {
18470-
classWithShadowedPrivateName = klass;
18473+
classWithShadowedPrivateName = container;
1847118474
}
1847218475
}
18473-
klass = getContainingClass(klass);
18476+
container = getContainingClass(container);
1847418477
}
1847518478
// If this isn't a case of shadowing, and the lhs has a property with the same
1847618479
// private name description, then there is a privacy violation
1847718480
if (leftType.symbol.members) {
1847818481
const symbolTableKey = getPropertyNameForPrivateNameDescription(leftType.symbol, right.escapedText);
18479-
if (symbolTableKey) {
18480-
const prop = getPropertyOfType(apparentType, symbolTableKey);
18481-
if (prop) {
18482+
const prop = getPropertyOfType(apparentType, symbolTableKey);
18483+
if (prop) {
18484+
if (errorNode) {
1848218485
error(right, Diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_name, symbolToString(prop), typeToString(getDeclaringClass(prop)!));
1848318486
}
1848418487
}
@@ -18499,7 +18502,7 @@ namespace ts {
1849918502
return apparentType;
1850018503
}
1850118504
const assignmentKind = getAssignmentTargetKind(node);
18502-
const prop = isPrivateName(right) ? getPropertyByPrivateName(apparentType, leftType, right) : getPropertyOfType(apparentType, right.escapedText);
18505+
const prop = isPrivateName(right) ? getPropertyForPrivateName(apparentType, leftType, right, /* errorNode */ right) : getPropertyOfType(apparentType, right.escapedText);
1850318506
if (isIdentifier(left) && parentSymbol && !(prop && isConstEnumOrConstEnumOnlyModule(prop))) {
1850418507
markAliasReferenced(parentSymbol, node);
1850518508
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,7 @@ namespace ts {
30263026
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
30273027
getPropertiesOfType(type: Type): Symbol[];
30283028
getPropertyOfType(type: Type, propertyName: string): Symbol | undefined;
3029+
getPropertyForPrivateName(apparentType: Type, leftType: Type, right: PrivateName, errorNode: Node | undefined): Symbol | undefined;
30293030
/* @internal */ getTypeOfPropertyOfType(type: Type, propertyName: string): Type | undefined;
30303031
getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo | undefined;
30313032
getSignaturesOfType(type: Type, kind: SignatureKind): ReadonlyArray<Signature>;

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,7 @@ declare namespace ts {
18761876
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
18771877
getPropertiesOfType(type: Type): Symbol[];
18781878
getPropertyOfType(type: Type, propertyName: string): Symbol | undefined;
1879+
getPropertyForPrivateName(apparentType: Type, leftType: Type, right: PrivateName, errorNode: Node | undefined): Symbol | undefined;
18791880
getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo | undefined;
18801881
getSignaturesOfType(type: Type, kind: SignatureKind): ReadonlyArray<Signature>;
18811882
getIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,7 @@ declare namespace ts {
18761876
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
18771877
getPropertiesOfType(type: Type): Symbol[];
18781878
getPropertyOfType(type: Type, propertyName: string): Symbol | undefined;
1879+
getPropertyForPrivateName(apparentType: Type, leftType: Type, right: PrivateName, errorNode: Node | undefined): Symbol | undefined;
18791880
getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo | undefined;
18801881
getSignaturesOfType(type: Type, kind: SignatureKind): ReadonlyArray<Signature>;
18811882
getIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined;

0 commit comments

Comments
 (0)