Skip to content

Commit

Permalink
add tests; one still failing
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom committed Jul 26, 2017
1 parent f91434e commit 4251e2a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
19 changes: 11 additions & 8 deletions lib/rules/prefer-const.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ function getDestructuringHost(reference) {
* @param {eslint-scope.Variable[]} variables - Variables to group by destructuring.
* @param {boolean} ignoreReadBeforeAssign -
* The value of `ignoreReadBeforeAssign` option.
* @param {boolean} checkingMixedDestructuring - value on how to report destructuring (any/all)
* @returns {Map<ASTNode, ASTNode[]>} Grouped identifier nodes.
*/
function groupByDestructuring(variables, ignoreReadBeforeAssign) {
function groupByDestructuring(variables, ignoreReadBeforeAssign, checkingMixedDestructuring) {
const identifierMap = new Map();

for (let i = 0; i < variables.length; ++i) {
Expand Down Expand Up @@ -199,13 +200,15 @@ function groupByDestructuring(variables, ignoreReadBeforeAssign) {
}
}

for (const key of identifierMap.keys()) {
const destructureGroup = identifierMap.get(key);
const destructureElement = destructureGroup[0];
if (!checkingMixedDestructuring) {
for (const key of identifierMap.keys()) {
const destructureGroup = identifierMap.get(key);
const destructureElement = destructureGroup[0];

if (destructureElement && destructureElement.parent && destructureElement.parent.elements) {
if (destructureElement.parent.elements.length !== destructureGroup.length) {
identifierMap.delete(key);
if (destructureElement && destructureElement.parent && destructureElement.parent.elements) {
if (destructureElement.parent.elements.length !== destructureGroup.length) {
identifierMap.delete(key);
}
}
}
}
Expand Down Expand Up @@ -311,7 +314,7 @@ module.exports = {

return {
"Program:exit"() {
groupByDestructuring(variables, ignoreReadBeforeAssign).forEach(checkGroup);
groupByDestructuring(variables, ignoreReadBeforeAssign, checkingMixedDestructuring).forEach(checkGroup);
},

VariableDeclaration(node) {
Expand Down
44 changes: 42 additions & 2 deletions tests/lib/rules/prefer-const.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,22 @@ ruleTester.run("prefer-const", rule, {
"let a; function foo() { if (a) {} a = bar(); }",
"let a; function foo() { a = a || bar(); baz(a); }",
"let a; function foo() { bar(++a); }",
"let predicate; [typeNode.returnType, predicate] = foo();",
"let predicate; let rest; [typeNode.returnType, predicate, ...rest] = foo();",
{
code: "let foo; ({ x: bar.baz, y: foo } = qux);",
options: [{ destructuring: "all" }]
},
{
code: "let foo; [foo, [bar.baz]] = qux;",
options: [{ destructuring: "all" }]
},
{
code: "let predicate; [typeNode.returnType, predicate] = foo();",
options: [{ destructuring: "all" }]
},
{
code: "let predicate; let rest; [typeNode.returnType, predicate, ...rest] = foo();",
options: [{ destructuring: "all" }]
},
{
code: [
"let id;",
Expand Down Expand Up @@ -359,6 +369,36 @@ ruleTester.run("prefer-const", rule, {
{ message: "'foo' is never reassigned. Use 'const' instead.", type: "Identifier" },
{ message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" }
]
},
{
code: "let predicate; [predicate, typeNode.returnType] = foo();",
output: null,
errors: [{ message: "'predicate' is never reassigned. Use 'const' instead.", type: "Identifier" }]
},
{
code: "let predicate; [typeNode.returnType, predicate] = foo();",
output: null,
errors: [{ message: "'predicate' is never reassigned. Use 'const' instead.", type: "Identifier" }]
},
{
code: "let predicate; let rest; [typeNode.returnType, predicate, ...rest] = foo();",
output: null,
errors: [
{ message: "'predicate' is never reassigned. Use 'const' instead.", type: "Identifier" },
{ message: "'rest' is never reassigned. Use 'const' instead.", type: "Identifier" }
]
},
{
code: "let foo; [foo, [bar.baz]] = qux;",
output: null,
options: [{ destructuring: "any" }],
errors: [{ message: "'foo' is never reassigned. Use 'const' instead.", type: "Identifier" }]
},
{
code: "let foo; ({ x: bar.baz, y: foo } = qux);",
output: null,
options: [{ destructuring: "any" }],
errors: [{ message: "'foo' is never reassigned. Use 'const' instead.", type: "Identifier" }]
}
]
});

0 comments on commit 4251e2a

Please sign in to comment.