Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Support object-destructuring mutation in prefer-const (#1906)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and nchen63 committed Dec 19, 2016
1 parent bea8193 commit 8eac3db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/rules/preferConstRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ class PreferConstWalker extends Lint.BlockScopeAwareRuleWalker<{}, ScopeInfo> {
// recursively unwrap destructuring arrays
this.handleLHSExpression(child);
});
} else if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) {
for (const prop of (node as ts.ObjectLiteralExpression).properties) {
switch (prop.kind) {
case ts.SyntaxKind.PropertyAssignment:
this.handleLHSExpression(prop.initializer);
break;
case ts.SyntaxKind.ShorthandPropertyAssignment:
this.handleLHSExpression(prop.name);
break;
case ts.SyntaxKind.SpreadAssignment:
this.handleLHSExpression(prop.expression);
break;
default:
break;
}
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions test/rules/prefer-const/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ let c3 = 1, c4 = 2; // 1 failure
c3 = 4;

// destructuring
let destructLater = 4;
[destructLater] = 5;
{
let destructLater = 4;
[destructLater] = 5;
let a: number;
let b: number;
let c: any;
({ eh: a, b, ...c } = { eh: 0, b: 1 });
}

let {h, i} = {h: 1, i: 1}; // failure for 'h'
let [j, k] = [1, 1]; // failure for 'j'
let [x1, x3] = [1, 2], [x2] = [3]; // failure for x1, x3
Expand Down
11 changes: 9 additions & 2 deletions test/rules/prefer-const/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ let c3 = 1, c4 = 2; // 1 failure
c3 = 4;

// destructuring
let destructLater = 4;
[destructLater] = 5;
{
let destructLater = 4;
[destructLater] = 5;
let a: number;
let b: number;
let c: any;
({ eh: a, b, ...c } = { eh: 0, b: 1 });
}

let {h, i} = {h: 1, i: 1}; // failure for 'h'
~ [Identifier 'h' is never reassigned; use 'const' instead of 'let'.]
let [j, k] = [1, 1]; // failure for 'j'
Expand Down

0 comments on commit 8eac3db

Please sign in to comment.