Skip to content

Fix prefer const rule #5709

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

Merged
merged 3 commits into from
Nov 18, 2015
Merged
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
27 changes: 16 additions & 11 deletions scripts/tslint/preferConstRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class PreferConstWalker extends Lint.RuleWalker {

visitBinaryExpression(node: ts.BinaryExpression) {
if (isAssignmentOperator(node.operatorToken.kind)) {
this.visitLHSExpressions(node.left);
this.visitLeftHandSideExpression(node.left);
}
super.visitBinaryExpression(node);
}

private visitLHSExpressions(node: ts.Expression) {
private visitLeftHandSideExpression(node: ts.Expression) {
while (node.kind === ts.SyntaxKind.ParenthesizedExpression) {
node = (node as ts.ParenthesizedExpression).expression;
}
Expand All @@ -106,18 +106,23 @@ class PreferConstWalker extends Lint.RuleWalker {
if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) {
const pattern = node as ts.ObjectLiteralExpression;
for (const element of pattern.properties) {
if (element.name.kind === ts.SyntaxKind.Identifier) {
this.markAssignment(element.name as ts.Identifier);
const kind = element.kind;

if (kind === ts.SyntaxKind.ShorthandPropertyAssignment) {
this.markAssignment((element as ts.ShorthandPropertyAssignment).name);
}
else if (kind === ts.SyntaxKind.PropertyAssignment) {
this.visitLeftHandSideExpression((element as ts.PropertyAssignment).initializer);
}
else if (isBindingPattern(element.name)) {
this.visitBindingPatternIdentifiers(element.name as ts.BindingPattern);
else {
// Should we throw an exception?
Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing that we can parse as property of ObjectLiteralExpression except PropertyAssignment and ShorthandPropertyAssignment is MethodDeclaration and it is already a grammar error to have it in destructuring assignment. So I think it is safe to ignore other syntax kinds

}
}
}
else if (node.kind === ts.SyntaxKind.ArrayLiteralExpression) {
const pattern = node as ts.ArrayLiteralExpression;
for (const element of pattern.elements) {
this.visitLHSExpressions(element);
this.visitLeftHandSideExpression(element);
}
}
}
Expand Down Expand Up @@ -145,7 +150,7 @@ class PreferConstWalker extends Lint.RuleWalker {

private visitAnyUnaryExpression(node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression) {
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken) {
this.visitLHSExpressions(node.operand);
this.visitLeftHandSideExpression(node.operand);
}
}

Expand Down Expand Up @@ -211,12 +216,12 @@ class PreferConstWalker extends Lint.RuleWalker {
}
}

private collectNameIdentifiers(value: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.Map<DeclarationUsages>) {
private collectNameIdentifiers(declaration: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.Map<DeclarationUsages>) {
if (node.kind === ts.SyntaxKind.Identifier) {
table[(node as ts.Identifier).text] = {declaration: value, usages: 0};
table[(node as ts.Identifier).text] = { declaration, usages: 0 };
}
else {
this.collectBindingPatternIdentifiers(value, node as ts.BindingPattern, table);
this.collectBindingPatternIdentifiers(declaration, node as ts.BindingPattern, table);
}
}

Expand Down