Skip to content

Adds 'symbol constraints' for easier checking of assignments. #5170

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

Closed
wants to merge 1 commit into from
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
16 changes: 16 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ namespace ts {
addDeclarationToSymbol(symbol, node, includes);
symbol.parent = parent;

if (node.flags & NodeFlags.Const ||
(node.kind === SyntaxKind.VariableDeclaration && node.parent.flags & NodeFlags.Const)) {
if (symbol.flags & SymbolFlags.ConstEnum) {
symbol.constraints |= SymbolConstraints.Immutable;
}
else {
symbol.constraints |= SymbolConstraints.notWritable;
}
}

// Only true for 'const enum' which its members are implicitly not writable
// Note: Immutable class/container could exist iff all its members are notWritable/const
if(symbol.parent && symbol.parent.constraints & SymbolConstraints.Immutable) {
symbol.constraints |= SymbolConstraints.notWritable;
}

return symbol;
}

Expand Down
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9744,7 +9744,7 @@ namespace ts {
return !symbol || symbol === unknownSymbol || (symbol.flags & ~SymbolFlags.EnumMember) !== 0;
}
case SyntaxKind.ElementAccessExpression:
// old compiler doesn't check indexed assess
// old compiler doesn't check indexed access
return true;
case SyntaxKind.ParenthesizedExpression:
return isReferenceOrErrorExpression((<ParenthesizedExpression>n).expression);
Expand All @@ -9753,25 +9753,25 @@ namespace ts {
}
}

function isConstVariableReference(n: Node): boolean {
function isNotWritableVariableReference(n: Node): boolean {
switch (n.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.PropertyAccessExpression: {
let symbol = findSymbol(n);
return symbol && (symbol.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(symbol) & NodeFlags.Const) !== 0;
return symbol && (symbol.constraints & SymbolConstraints.notWritable) !== 0;
}
case SyntaxKind.ElementAccessExpression: {
let index = (<ElementAccessExpression>n).argumentExpression;
let symbol = findSymbol((<ElementAccessExpression>n).expression);
if (symbol && index && index.kind === SyntaxKind.StringLiteral) {
let name = (<LiteralExpression>index).text;
let prop = getPropertyOfType(getTypeOfSymbol(symbol), name);
return prop && (prop.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(prop) & NodeFlags.Const) !== 0;
return prop && (prop.constraints & SymbolConstraints.notWritable) !== 0;
}
return false;
}
case SyntaxKind.ParenthesizedExpression:
return isConstVariableReference((<ParenthesizedExpression>n).expression);
return isNotWritableVariableReference((<ParenthesizedExpression>n).expression);
default:
return false;
}
Expand All @@ -9782,7 +9782,7 @@ namespace ts {
return false;
}

if (isConstVariableReference(n)) {
if (isNotWritableVariableReference(n)) {
error(n, constantVariableMessage);
return false;
}
Expand Down
18 changes: 12 additions & 6 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,11 @@ namespace ts {
createMissingNode(t, reportAtCurrentPosition, diagnosticMessage, arg0);
}

function parseTokenNode<T extends Node>(): T {
function parseTokenNode<T extends Node>(flags?: NodeFlags): T {
let node = <T>createNode(token);
if (flags) {
node.flags = flags;
}
nextToken();
return finishNode(node);
}
Expand Down Expand Up @@ -1849,7 +1852,7 @@ namespace ts {
function parseTemplateExpression(): TemplateExpression {
let template = <TemplateExpression>createNode(SyntaxKind.TemplateExpression);

template.head = parseLiteralNode();
template.head = parseLiteralNode(/*internName*/ false, NodeFlags.ConstValue);
Debug.assert(template.head.kind === SyntaxKind.TemplateHead, "Template head has wrong token kind");

let templateSpans = <NodeArray<TemplateSpan>>[];
Expand Down Expand Up @@ -1884,11 +1887,13 @@ namespace ts {
return finishNode(span);
}

function parseLiteralNode(internName?: boolean): LiteralExpression {
function parseLiteralNode(internName?: boolean, flags?: NodeFlags): LiteralExpression {
let node = <LiteralExpression>createNode(token);
let text = scanner.getTokenValue();
node.text = internName ? internIdentifier(text) : text;

if (flags) {
node.flags = flags;
}
if (scanner.hasExtendedUnicodeEscape()) {
node.hasExtendedUnicodeEscape = true;
}
Expand Down Expand Up @@ -3651,13 +3656,14 @@ namespace ts {
case SyntaxKind.NumericLiteral:
case SyntaxKind.StringLiteral:
case SyntaxKind.NoSubstitutionTemplateLiteral:
return parseLiteralNode();
return parseLiteralNode(/* internName */ false, NodeFlags.ConstValue);
case SyntaxKind.ThisKeyword:
case SyntaxKind.SuperKeyword:
return parseTokenNode<PrimaryExpression>();
case SyntaxKind.NullKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
return parseTokenNode<PrimaryExpression>();
return parseTokenNode<PrimaryExpression>(NodeFlags.ConstValue);
case SyntaxKind.OpenParenToken:
return parseParenthesizedExpression();
case SyntaxKind.OpenBracketToken:
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,12 @@ namespace ts {
Namespace = 0x00020000, // Namespace declaration
ExportContext = 0x00040000, // Export context (initialized by binding)
ContainsThis = 0x00080000, // Interface contains references to "this"
ConstValue = 0x00100000, // Node that represents a constant value (1, "a", true/false, null)

Modifier = Export | Ambient | Public | Private | Protected | Static | Abstract | Default | Async,
AccessibilityModifier = Public | Private | Protected,
BlockScoped = Let | Const
BlockScoped = Let | Const,
Constant = Const | ConstValue
}

/* @internal */
Expand Down Expand Up @@ -1693,6 +1695,7 @@ namespace ts {
name: string; // Name of symbol
declarations?: Declaration[]; // Declarations associated with this symbol
valueDeclaration?: Declaration; // First value declaration of the symbol
constraints?: SymbolConstraints; // Symbol constraints

members?: SymbolTable; // Class, interface or literal instance members
exports?: SymbolTable; // Module exports
Expand All @@ -1703,6 +1706,12 @@ namespace ts {
/* @internal */ constEnumOnlyModule?: boolean; // True if module contains only const enums or other modules with only const enums
}

export const enum SymbolConstraints {
notWritable = 1,
notMutable,
Immutable = notWritable | notMutable,
}

/* @internal */
export interface SymbolLinks {
target?: Symbol; // Resolved (non-alias) target of an alias
Expand Down