Skip to content

Commit 7389922

Browse files
committed
Merge pull request microsoft#5709 from Microsoft/fixPreferConstRule
Fix prefer const rule
2 parents 5e84ed3 + cbb6165 commit 7389922

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

scripts/tslint/preferConstRule.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ class PreferConstWalker extends Lint.RuleWalker {
8585

8686
visitBinaryExpression(node: ts.BinaryExpression) {
8787
if (isAssignmentOperator(node.operatorToken.kind)) {
88-
this.visitLHSExpressions(node.left);
88+
this.visitLeftHandSideExpression(node.left);
8989
}
9090
super.visitBinaryExpression(node);
9191
}
9292

93-
private visitLHSExpressions(node: ts.Expression) {
93+
private visitLeftHandSideExpression(node: ts.Expression) {
9494
while (node.kind === ts.SyntaxKind.ParenthesizedExpression) {
9595
node = (node as ts.ParenthesizedExpression).expression;
9696
}
@@ -106,18 +106,23 @@ class PreferConstWalker extends Lint.RuleWalker {
106106
if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) {
107107
const pattern = node as ts.ObjectLiteralExpression;
108108
for (const element of pattern.properties) {
109-
if (element.name.kind === ts.SyntaxKind.Identifier) {
110-
this.markAssignment(element.name as ts.Identifier);
109+
const kind = element.kind;
110+
111+
if (kind === ts.SyntaxKind.ShorthandPropertyAssignment) {
112+
this.markAssignment((element as ts.ShorthandPropertyAssignment).name);
113+
}
114+
else if (kind === ts.SyntaxKind.PropertyAssignment) {
115+
this.visitLeftHandSideExpression((element as ts.PropertyAssignment).initializer);
111116
}
112-
else if (isBindingPattern(element.name)) {
113-
this.visitBindingPatternIdentifiers(element.name as ts.BindingPattern);
117+
else {
118+
// Should we throw an exception?
114119
}
115120
}
116121
}
117122
else if (node.kind === ts.SyntaxKind.ArrayLiteralExpression) {
118123
const pattern = node as ts.ArrayLiteralExpression;
119124
for (const element of pattern.elements) {
120-
this.visitLHSExpressions(element);
125+
this.visitLeftHandSideExpression(element);
121126
}
122127
}
123128
}
@@ -145,7 +150,7 @@ class PreferConstWalker extends Lint.RuleWalker {
145150

146151
private visitAnyUnaryExpression(node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression) {
147152
if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken) {
148-
this.visitLHSExpressions(node.operand);
153+
this.visitLeftHandSideExpression(node.operand);
149154
}
150155
}
151156

@@ -211,12 +216,12 @@ class PreferConstWalker extends Lint.RuleWalker {
211216
}
212217
}
213218

214-
private collectNameIdentifiers(value: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.Map<DeclarationUsages>) {
219+
private collectNameIdentifiers(declaration: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.Map<DeclarationUsages>) {
215220
if (node.kind === ts.SyntaxKind.Identifier) {
216-
table[(node as ts.Identifier).text] = {declaration: value, usages: 0};
221+
table[(node as ts.Identifier).text] = { declaration, usages: 0 };
217222
}
218223
else {
219-
this.collectBindingPatternIdentifiers(value, node as ts.BindingPattern, table);
224+
this.collectBindingPatternIdentifiers(declaration, node as ts.BindingPattern, table);
220225
}
221226
}
222227

0 commit comments

Comments
 (0)