Skip to content
Merged
  •  
  •  
  •  
44 changes: 41 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ namespace ts {
if (isWellKnownSymbolSyntactically(name)) {
return getPropertyNameForKnownSymbolName(idText(name.name));
}
if (isPrivateIdentifier(name)) {
Comment thread
mheiber marked this conversation as resolved.
Resolved
// containingClass exists because private names only allowed inside classes
const containingClass = getContainingClass(node);
if (!containingClass) {
Comment thread
sandersn marked this conversation as resolved.
Resolved
// we can get here in cases where there is already a parse error.
return undefined;
}
const containingClassSymbol = containingClass.symbol;
return getSymbolNameForPrivateIdentifier(containingClassSymbol, name.escapedText);
}
return isPropertyNameLiteral(name) ? getEscapedTextOfIdentifierOrLiteral(name) : undefined;
}
switch (node.kind) {
Expand Down Expand Up @@ -471,7 +481,6 @@ namespace ts {
if (isNamedDeclaration(node)) {
node.name.parent = node;
}

// Report errors every position with duplicate declaration
// Report errors on previous encountered declarations
let message = symbol.flags & SymbolFlags.BlockScopedVariable
Expand Down Expand Up @@ -1599,7 +1608,7 @@ namespace ts {
}
if (node.expression.kind === SyntaxKind.PropertyAccessExpression) {
const propertyAccess = <PropertyAccessExpression>node.expression;
if (isNarrowableOperand(propertyAccess.expression) && isPushOrUnshiftIdentifier(propertyAccess.name)) {
if (isIdentifier(propertyAccess.name) && isNarrowableOperand(propertyAccess.expression) && isPushOrUnshiftIdentifier(propertyAccess.name)) {
currentFlow = createFlowMutation(FlowFlags.ArrayMutation, currentFlow, node);
}
}
Expand Down Expand Up @@ -2026,6 +2035,18 @@ namespace ts {
return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode;
}

// The binder visits every node, so this is a good place to check for
// the reserved private name (there is only one)
function checkPrivateIdentifier(node: PrivateIdentifier) {
if (node.escapedText === "#constructor") {
// Report error only if there are no parse errors in file
if (!file.parseDiagnostics.length) {
file.bindDiagnostics.push(createDiagnosticForNode(node,
Diagnostics.constructor_is_a_reserved_word, declarationNameToString(node)));
}
}
}

function checkStrictModeBinaryExpression(node: BinaryExpression) {
if (inStrictMode && isLeftHandSideExpression(node.left) && isAssignmentOperator(node.operatorToken.kind)) {
// ECMA 262 (Annex C) The identifier eval or arguments may not appear as the LeftHandSideExpression of an
Expand Down Expand Up @@ -2298,6 +2319,8 @@ namespace ts {
node.flowNode = currentFlow;
}
return checkStrictModeIdentifier(<Identifier>node);
case SyntaxKind.PrivateIdentifier:
return checkPrivateIdentifier(node as PrivateIdentifier);
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
const expr = node as PropertyAccessExpression | ElementAccessExpression;
Expand Down Expand Up @@ -2657,6 +2680,12 @@ namespace ts {

function bindThisPropertyAssignment(node: BindablePropertyAssignmentExpression | PropertyAccessExpression | LiteralLikeElementAccessExpression) {
Debug.assert(isInJSFile(node));
// private identifiers *must* be declared (even in JS files)
const hasPrivateIdentifier = (isBinaryExpression(node) && isPropertyAccessExpression(node.left) && isPrivateIdentifier(node.left.name))
|| (isPropertyAccessExpression(node) && isPrivateIdentifier(node.name));
if (hasPrivateIdentifier) {
return;
}
const thisContainer = getThisContainer(node, /*includeArrowFunctions*/ false);
switch (thisContainer.kind) {
case SyntaxKind.FunctionDeclaration:
Expand Down Expand Up @@ -2959,7 +2988,12 @@ namespace ts {
}
else {
const s = forEachIdentifierInEntityName(e.expression, parent, action);
return action(getNameOrArgument(e), s && s.exports && s.exports.get(getElementOrPropertyAccessName(e)), s);
const name = getNameOrArgument(e);
// unreachable
if (isPrivateIdentifier(name)) {
Debug.fail("unexpected PrivateIdentifier");
}
return action(name, s && s.exports && s.exports.get(getElementOrPropertyAccessName(e)), s);
}
}

Expand Down Expand Up @@ -4131,6 +4165,10 @@ namespace ts {
case SyntaxKind.BreakStatement:
transformFlags |= TransformFlags.ContainsHoistedDeclarationOrCompletion;
break;

case SyntaxKind.PrivateIdentifier:
transformFlags |= TransformFlags.ContainsClassFields;
break;
}

node.transformFlags = transformFlags | TransformFlags.HasComputedFlags;
Expand Down
Loading