-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from kacper-mikolajczak/reduce-no-acc-spread
No accumulator spread in reduce methods
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const _ = require('lodash'); | ||
const CONST = require('./CONST'); | ||
|
||
// Matches function expression as a direct descendant (argument callback) of "reduce" or "reduceRight" call | ||
const MATCH = 'CallExpression:matches([callee.property.name="reduce"], [callee.property.name="reduceRight"]) > :matches(ArrowFunctionExpression, FunctionExpression)'; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: CONST.MESSAGE.NO_ACC_SPREAD_IN_REDUCE, | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
schema: [], // no options | ||
}, | ||
create(context) { | ||
return { | ||
[MATCH](node) { | ||
// Retrieve accumulator variable | ||
const accumulator = context.getDeclaredVariables(node)[0]; | ||
if (!accumulator) { | ||
return; | ||
} | ||
|
||
// Check if accumulatorVariable has any references (is used in the scope) | ||
if (!accumulator.references.length) { | ||
return; | ||
} | ||
|
||
// Check if any of the references are used in a SpreadElement | ||
const isAccumulatorVariableUsedSpread = _.some( | ||
accumulator.references, | ||
reference => reference.identifier.parent.type === 'SpreadElement', | ||
); | ||
if (!isAccumulatorVariableUsedSpread) { | ||
return; | ||
} | ||
|
||
// Accumulator variable is used in a SpreadElement, report it | ||
context.report({ | ||
node, | ||
message: CONST.MESSAGE.NO_ACC_SPREAD_IN_REDUCE, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
60 changes: 60 additions & 0 deletions
60
eslint-plugin-expensify/tests/no-acc-spread-in-reduce.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../no-acc-spread-in-reduce'); | ||
const CONST = require('../CONST'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
const errors = [ | ||
{ | ||
message: CONST.MESSAGE.NO_ACC_SPREAD_IN_REDUCE, | ||
}, | ||
]; | ||
|
||
ruleTester.run('no-spread-in-reduce', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
array.reduce((acc, item) => { | ||
acc[item.key] = item.value; | ||
return acc; | ||
} | ||
, {}); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
array.reduce((acc, item) => { | ||
const spread = { ...somethingElse }; | ||
return acc[item.key] = item.value; | ||
}, {}); | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
const arr = []; | ||
arr.reduce((acc, i) => ({ ...acc, i }), {}) | ||
`, | ||
errors, | ||
}, | ||
{ | ||
code: ` | ||
const arr = []; | ||
arr.reduceRight((acc, i) => ({ ...acc, i }), {}) | ||
`, | ||
errors, | ||
}, | ||
{ | ||
code: ` | ||
([]).reduce((acc, i) => ({ ...acc, i }), {}) | ||
`, | ||
errors, | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters