Skip to content
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

Add no-reduce rule #704

Merged
merged 35 commits into from May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5690bee
Add docs, tests and empty rules file.
ankeetmaini May 1, 2020
64ad5ee
Add more test cases.
ankeetmaini May 2, 2020
430e0ec
Add the simplest rule ever.
ankeetmaini May 2, 2020
68dc130
Use `medthod-selector` for super tight selections.
ankeetmaini May 3, 2020
26818b0
Incorporate review comments. Handle `apply` and min/max args for reduce.
ankeetmaini May 3, 2020
88217f1
Skip lint for reduce usages in codebase
ankeetmaini May 3, 2020
eecc091
Remove `reduce` usages from codebase
ankeetmaini May 4, 2020
362b074
Incorporate review comments
ankeetmaini May 4, 2020
7f41efb
Remove useless code from tests
ankeetmaini May 4, 2020
3b4964b
Add review comment fix
ankeetmaini May 4, 2020
409c1ee
Update rules/no-reduce.js
fisker May 4, 2020
b525aab
Use messageId, simplify selector
fisker May 4, 2020
293fb7a
More tests
fisker May 4, 2020
394654d
Simplify tests
fisker May 4, 2020
866c8b6
Stricter selector
fisker May 4, 2020
0182ccf
More tests
fisker May 4, 2020
52b9c2b
Remove calls if first arg is a literal or undefined
ankeetmaini May 12, 2020
53b4980
Review comments
ankeetmaini May 12, 2020
3c52eff
Update test
ankeetmaini May 12, 2020
be642a5
Add reduceRight
ankeetmaini May 12, 2020
d32e3f4
Merge branch 'master' into no-reduce
ankeetmaini May 12, 2020
6c20b1e
Remove reduceRight from codebase
ankeetmaini May 12, 2020
f9b75f9
Revert test change
fisker May 13, 2020
9a8d249
`notListedMethod` -> `notListed`
fisker May 13, 2020
dd3f8af
Add `reduceRight` tests and failed tests
fisker May 13, 2020
db0922f
Fix failed tests
fisker May 13, 2020
bca2b85
Use different message
fisker May 13, 2020
417a37d
Update docs
fisker May 13, 2020
6fead63
Tests
fisker May 13, 2020
11bc1b7
Tests
fisker May 13, 2020
6de1268
Revert cartesian-product-samples.js
fisker May 13, 2020
c00fe99
Rewrite `reduceRight`
fisker May 13, 2020
b925b38
Merge branch 'master' into no-reduce
fisker May 13, 2020
fc2434f
Update no-reduce.md
sindresorhus May 13, 2020
17c110c
Update no-reduce.md
sindresorhus May 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions rules/expiring-todo-comments.js
Expand Up @@ -58,11 +58,11 @@ function parseTodoWithArguments(string, {terms}) {
return createArgumentGroup(parsedArguments);
}

function createArgumentGroup(args) {
function createArgumentGroup(arguments_) {
const groups = {};
for (const arg of args) {
groups[arg.type] = groups[arg.type] || [];
groups[arg.type].push(arg.value);
for (const argument of arguments_) {
fisker marked this conversation as resolved.
Show resolved Hide resolved
groups[argument.type] = groups[argument.type] || [];
groups[argument.type].push(argument.value);
}

return groups;
Expand Down
9 changes: 8 additions & 1 deletion rules/no-reduce.js
Expand Up @@ -7,13 +7,20 @@ const message = 'Array.reduce not allowed';
const PROTOTYPE_SELECTOR = [
methodSelector({names: ['call', 'apply']}),
fisker marked this conversation as resolved.
Show resolved Hide resolved
'[callee.object.type="MemberExpression"]',
':matches([callee.object.object.type="ArrayExpression"], [callee.object.object.object.type="Identifier"][callee.object.object.object.name="Array"][callee.object.object.property.name="prototype"])',
fisker marked this conversation as resolved.
Show resolved Hide resolved
'[callee.object.computed=false]',
'[callee.object.property.type="Identifier"]',
'[callee.object.property.name="reduce"]'
fisker marked this conversation as resolved.
Show resolved Hide resolved
].join('');

const METHOD_SELECTOR = [
methodSelector({name: 'reduce', min: 1, max: 2}),
'[callee.object.property.name!="call"]'
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it fails some tests if this is removed

a.b.call.reduce(() => {})

Copy link
Collaborator

Choose a reason for hiding this comment

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

But a.b.call should be a array, otherwise there is no reduce method.

].join('');

const create = context => {
return {
[methodSelector({name: 'reduce', min: 1, max: 2})](node) {
[METHOD_SELECTOR](node) {
// For arr.reduce()
context.report({node: node.callee.property, message});
},
Expand Down
5 changes: 4 additions & 1 deletion test/no-reduce.js
Expand Up @@ -27,7 +27,10 @@ const tests = {
'a(b.reduce)',
'a.reduce()',
'a.reduce(1, 2, 3)',
'a.reduce(b, c, d)'
'a.reduce(b, c, d)',
'a.b.call.reduce(() => {})',
'a.call.reduce(() => {})',
'[][reduce].call()'
],
invalid: [
{
Expand Down