Skip to content
Open
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 custom-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
'custom-rules/default-usestate' : 'warn',
'custom-rules/hook-sequence' : 'warn',
'custom-rules/variable-name-check' : 'warn',
'custom-rules/no-hook-conditional-import' : 'warn',
'custom-rules/component-pascal' : 'warn',
'custom-rules/variable-value-jsx' : 'warn',
'custom-rules/default-component-props' : 'warn',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const is_empty_use_check = require('./is_empty_use_check');
const key_as_function = require('./key_as_function');
const key_in_for_each_map = require('./key_in_for_each_map');
const nbsp_ensp_check = require('./nbsp_ensp_check');
const no_hook_conditional_import = require('./no_hook_conditional_import');
const regex_check = require('./regex_check');
const uuid_check = require('./uuid_check');
const variable_name_check = require('./variable_name_check');
Expand All @@ -36,6 +37,7 @@ module.exports = {
'default-usestate' : default_usestate,
'hook-sequence' : hook_sequence,
'variable-name-check' : variable_name_check,
'no-hook-conditional-import' : no_hook_conditional_import,
'component-pascal' : component_pascal,
'variable-value-jsx' : variable_value_jsx,
'default-component-props' : default_comp_props,
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint/no_hook_conditional_import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
meta: {
type : 'warn',
docs : {
description : 'Disallow react hooks in conditional statements',
category : 'Best Practices',
recommended : true,
},
schema: [],
},

create(context) {
return {
CallExpression(node) {
const { callee = {} } = node || {};
const { name = '' } = callee || {};
if (name.startsWith('use')) {
const ancestors = context.getAncestors();
const isCondition = ancestors.some((ancestor) => (
ancestor.type === 'IfStatement' || ancestor.type === 'FunctionDeclaration'
));
if (isCondition) {
context.report({
node,
message: 'Do not use react hooks in conditional statements',
});
}
}
},
};
},
};