Skip to content

Commit

Permalink
Merge pull request #89 from gedu/edu/avoid_anonymous_fun
Browse files Browse the repository at this point in the history
Avoid anonymous functions to be used in arguments/callbacks
  • Loading branch information
mountiny authored Apr 5, 2024
2 parents 72d77f0 + 6c8697c commit 61db348
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 1 deletion.
1 change: 1 addition & 0 deletions eslint-plugin-expensify/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ module.exports = {
ONYX_ONE_PARAM: 'The withOnyx HOC must be passed at least one argument.',
MUST_USE_VARIABLE_FOR_ASSIGNMENT: '{{key}} must be assigned as a variable instead of direct assignment.',
NO_DEFAULT_PROPS: 'defaultProps should not be used in function components. Use default Arguments instead.',
AVOID_ANONYMOUS_FUNCTIONS: 'Prefer named functions.',
},
};
32 changes: 32 additions & 0 deletions eslint-plugin-expensify/avoid-anonymous-functions.js
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 eslint-plugin-expensify/tests/avoid-anonymous-functions.test.js
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,
}],
},
],
});
3 changes: 2 additions & 1 deletion rules/expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
'rulesdir/no-api-side-effects-method': 'error',
'rulesdir/prefer-localization': 'error',
'rulesdir/onyx-props-must-have-default': 'error',
'rulesdir/avoid-anonymous-functions': 'error',
'no-restricted-imports': ['error', {
paths: [{
name: 'react-native',
Expand All @@ -28,4 +29,4 @@ module.exports = {
}],
}],
},
};
};

0 comments on commit 61db348

Please sign in to comment.