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 rules: Handle No Multiple Onyx in file #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint-plugin-expensify/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
NO_NEGATED_VARIABLES: 'Do not use negated variable names.',
NO_THENABLE_ACTIONS_IN_VIEWS: 'Calling .then() on action method {{method}} is forbidden in React views. Relocate this logic into the actions file and pass values via Onyx.',
NO_USELESS_COMPOSE: 'compose() is not necessary when passed a single argument',
NO_MULTIPLE_ONYX_IN_FILE: 'Only use withOnyx() once. Read here about how to properly work with dependent Onyx keys: https://github.com/Expensify/react-native-onyx#dependent-onyx-keys-and-withonyx',
PREFER_ACTIONS_SET_DATA: 'Only actions should directly set or modify Onyx data. Please move this logic into a suitable action.',
PREFER_EARLY_RETURN: 'Prefer an early return to a conditionally-wrapped function body',
PREFER_IMPORT_MODULE_CONTENTS: 'Do not import individual exports from local modules. Prefer \'import * as\' syntax.',
Expand Down
28 changes: 28 additions & 0 deletions eslint-plugin-expensify/no-multiple-onyx-in-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const lodashGet = require('lodash/get');
const message = require('./CONST').MESSAGE.NO_MULTIPLE_ONYX_IN_FILE;

module.exports = {
create: (context) => {
let withOnyxCount = 0;

return {
CallExpression(node) {
const calleeName = lodashGet(node, 'callee.name');

if (calleeName === 'withOnyx') {
withOnyxCount += 1;

if (withOnyxCount > 1) {
context.report({
node,
message,
});
}
}
},
'Program:exit': () => {
withOnyxCount = 0;
},
};
},
};
43 changes: 43 additions & 0 deletions eslint-plugin-expensify/tests/no-multiple-onyx-in-file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../no-multiple-onyx-in-file');
const message = require('../CONST').MESSAGE.NO_MULTIPLE_ONYX_IN_FILE;

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});

ruleTester.run('no-multiple-onyx-in-file', rule, {
valid: [
{
code: `
compose(
withOnyx({
key: 'value',
})
)(Component);
`,
},
],
invalid: [
{
code: `
compose(
withOnyx({
key1: 'value1',
}),
withOnyx({
key1: 'value1',
})
)(Component);
`,
studentofcoding marked this conversation as resolved.
Show resolved Hide resolved
errors: [
{
message,
},
],
},
],
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-expensify",
"version": "2.0.38",
"version": "2.0.39",
"description": "Expensify's ESLint configuration following our style guide",
"main": "index.js",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions rules/expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'rulesdir/no-useless-compose': 'error',
'rulesdir/prefer-import-module-contents': 'error',
'rulesdir/no-multiple-api-calls': 'error',
'rulesdir/no-multiple-onyx-in-file': 'error',
'rulesdir/no-call-actions-from-actions': 'error',
'rulesdir/no-api-side-effects-method': 'error',
'rulesdir/display-name-property': 'error',
Expand Down