Skip to content

Implement provide-canBeMissing-in-useOnyx rule #142

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
103 changes: 103 additions & 0 deletions eslint-plugin-expensify/provide-canBeMissing-in-useOnyx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const _ = require('underscore');

/**
* @typedef {import('eslint').Rule.RuleModule} RuleModule
* @typedef {import('estree').ObjectExpression} ObjectExpression
*/

/** @type {RuleModule} */
module.exports = {
name: 'provide-canBeMissing-in-useOnyx',
meta: {
type: 'problem',
docs: {
description: 'Enforces use of "canBeMissing" option in useOnyx calls.',
recommended: 'error',
},
schema: [],
messages: {
provideCanBeMissing: 'Provide the "canBeMissing" option to the useOnyx() call.',
},
},
create(context) {
/**
* Find the variable declaration and return the value.
*
* @param {string} name - The name of the variable to resolve.
* @returns {ObjectExpression|null}
*/
function getVariableValue(name) {
try {
const scope = context.getScope();
const variable = scope.set.get(name);

if (variable && variable.defs.length > 0) {
const def = variable.defs[0];
if (def.node.init && def.node.init.type === 'ObjectExpression') {
return def.node.init;
}
}
} catch (e) {
return null;
}

return null;
}

return {
VariableDeclarator(node) {
if (
!node.init
|| node.init.type !== 'CallExpression'
|| node.init.callee.name !== 'useOnyx'
) {
return;
}

if (node.init.arguments.length < 2) {
context.report({
node: node.init,
messageId: 'provideCanBeMissing',
});
return;
}

const optionsArgument = node.init.arguments[1];
switch (optionsArgument.type) {
case 'ObjectExpression': {
if (!_.some(optionsArgument.properties, property => property.type === 'Property' && property.key.name === 'canBeMissing')) {
context.report({
node: node.init,
messageId: 'provideCanBeMissing',
});
}
break;
}

case 'Identifier': {
const resolvedValue = getVariableValue(optionsArgument.name);
if (!resolvedValue) {
context.report({
node: node.init,
messageId: 'provideCanBeMissing',
});
return;
}

if (!_.some(resolvedValue.properties, property => property.type === 'Property' && property.key.name === 'canBeMissing')) {
context.report({
node: node.init,
messageId: 'provideCanBeMissing',
});
}
break;
}

default: {
break;
}
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../provide-canBeMissing-in-useOnyx');

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

ruleTester.run('provide-canBeMissing-in-useOnyx', rule, {
valid: [
{
code: 'const [data] = useOnyx(ONYXKEYS.DATA, {allowStaleData: true, canBeMissing: false});',
},
{
code: 'const [data] = useOnyx(ONYXKEYS.DATA, {canBeMissing: false}, []);',
},
{
code: `
const options = {allowStaleData: true, canBeMissing: false};
const [data] = useOnyx(ONYXKEYS.DATA, options);
`,
},
],
invalid: [
{
code: 'const [data] = useOnyx(ONYXKEYS.DATA);',
errors: [{
messageId: 'provideCanBeMissing',
}],
},
{
code: 'const [data] = useOnyx(ONYXKEYS.DATA, {allowStaleData: true});',
errors: [{
messageId: 'provideCanBeMissing',
}],
},
{
code: 'const [data] = useOnyx(ONYXKEYS.DATA, undefined, []);',
errors: [{
messageId: 'provideCanBeMissing',
}],
},
{
code: `
const options = {allowStaleData: true};
const [data] = useOnyx(ONYXKEYS.DATA, options);
`,
errors: [{
messageId: 'provideCanBeMissing',
}],
},
{
code: `
const undefinedOptions = undefined;
const [data] = useOnyx(ONYXKEYS.DATA, undefinedOptions);
`,
errors: [{
messageId: 'provideCanBeMissing',
}],
},
],
});