-
Notifications
You must be signed in to change notification settings - Fork 24
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 #89 from gedu/edu/avoid_anonymous_fun
Avoid anonymous functions to be used in arguments/callbacks
- Loading branch information
Showing
4 changed files
with
261 additions
and
1 deletion.
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,32 @@ | ||
const message = require('./CONST').MESSAGE.AVOID_ANONYMOUS_FUNCTIONS; | ||
|
||
module.exports = { | ||
create(context) { | ||
return { | ||
"CallExpression > FunctionExpression": function (node) { | ||
if (!node.id && !node.generator && !node.async) { | ||
context.report({ | ||
node, | ||
message, | ||
}); | ||
} | ||
}, | ||
"CallExpression": function (node) { | ||
if (node.arguments && node.arguments.some(arg => arg.type === "ArrowFunctionExpression" && !arg.id)) { | ||
context.report({ | ||
node, | ||
message, | ||
}); | ||
} | ||
}, | ||
"ReturnStatement > FunctionExpression, ReturnStatement > ArrowFunctionExpression": function (node) { | ||
if (!node.id) { | ||
context.report({ | ||
node, | ||
message, | ||
}); | ||
} | ||
} | ||
}; | ||
}, | ||
}; |
226 changes: 226 additions & 0 deletions
226
eslint-plugin-expensify/tests/avoid-anonymous-functions.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,226 @@ | ||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../avoid-anonymous-functions'); | ||
const message = require('../CONST').MESSAGE.AVOID_ANONYMOUS_FUNCTIONS; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('avoid-anonymous-functions', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
function test() { | ||
function innerFunction(node) { | ||
return node.isParent; | ||
} | ||
const onlyParents = nodes.filter(innerFunction); | ||
return onlyParents; | ||
}`, | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const onlyParents = nodes.filter(function innerFunction(node) { | ||
return node.isParent; | ||
}); | ||
return onlyParents; | ||
}`, | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: function named() {}}; | ||
useEffect(function innerFunction() { | ||
node.execute(); | ||
}, []); | ||
return true; | ||
}`, | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: () => {}}; | ||
useEffect(function* () { | ||
node.execute(); | ||
}, []); | ||
return true; | ||
}`, | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: function named() {}}; | ||
useEffect(node.execute, []); | ||
return true; | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: () => {}}; | ||
useEffect(node.execute, []); | ||
return true; | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = () => {}; | ||
useEffect(node, []); | ||
return true; | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const filteringById = () => {}; | ||
parents.filter(filteringById); | ||
return true; | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
function withName() { | ||
return function innerName() {}; | ||
} | ||
withName(); | ||
return true; | ||
} | ||
` | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
function test() { | ||
const onlyParents = nodes.filter((node) => node.isParent); | ||
return onlyParents; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const onlyParents = nodes.filter((node) => { | ||
return node.isParent; | ||
}); | ||
return onlyParents; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: function named() {}}; | ||
useEffect(function () { | ||
node.execute(); | ||
}, []); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: function named() {}}; | ||
useEffect(() => node.execute(), []); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const node = {execute: () => {}}; | ||
useEffect(function () {node.execute();} , []); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
function rightNamed() { | ||
return () => {}; | ||
} | ||
rightNamed(); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
function rightNamed() { | ||
return function () {}; | ||
} | ||
rightNamed(); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const rightNamed = () => { | ||
return function () {}; | ||
} | ||
rightNamed(); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
function test() { | ||
const rightNamed = () => { | ||
return () => {}; | ||
} | ||
rightNamed(); | ||
return true; | ||
} | ||
`, | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
], | ||
}); |
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