Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-react-helpers

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-react-helpers - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

24

lib/rules/prefer-use-state-lazy-initialization.js

@@ -23,6 +23,3 @@ /**

messages: {
useLazyInitialization: 'To prevent re-computation, consider using lazy initial state for useState. Ex: useState(() => whatever()) instead of useState(whatever()).',
useLazyInitializationForConditionalExpressions: 'To prevent re-computation, consider using lazy initial state for useState. Ex: useState(() => test ? 1 : 0) instead of useState(test ? 1 : 0)',
useLazyInitializationForLogicalExpressions: 'To prevent re-computation, consider using lazy initial state for useState. Ex: useState(() => a && b) instead of useState(a && b)',
useLazyInitializationForBinaryExpressions: 'To prevent re-computation, consider using lazy initial state for useState. Ex: useState(() => a > b) instead of useState(a > b)',
useLazyInitialization: 'To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: useState(() => getValue())',
}

@@ -42,2 +39,13 @@ },

const hasFunctionCall = (node) => {
if (node.type === 'CallExpression' && ALLOW_LIST.indexOf(node.callee.name) === -1) {
return true;
} else if (node.type === 'ConditionalExpression') {
return hasFunctionCall(node.test) || hasFunctionCall(node.consequent) || hasFunctionCall(node.alternate);
} else if (node.type === 'LogicalExpression' || node.type === 'BinaryExpression') {
return hasFunctionCall(node.left) || hasFunctionCall(node.right);
}
return false;
}
//----------------------------------------------------------------------

@@ -52,10 +60,4 @@ // Public

const useStateInput = node.arguments[0];
if (useStateInput.type === 'CallExpression' && ALLOW_LIST.indexOf(useStateInput.callee.name) === -1) {
if (hasFunctionCall(useStateInput)) {
context.report({node, messageId: 'useLazyInitialization' })
} else if (useStateInput.type === 'ConditionalExpression') {
context.report({node, messageId: 'useLazyInitializationForConditionalExpressions' })
} else if (useStateInput.type === 'LogicalExpression') {
context.report({node, messageId: 'useLazyInitializationForLogicalExpressions' })
} else if (useStateInput.type === 'BinaryExpression') {
context.report({node, messageId: 'useLazyInitializationForBinaryExpressions' })
}

@@ -62,0 +64,0 @@ }

{
"name": "eslint-plugin-react-helpers",
"version": "0.1.0",
"version": "0.1.1",
"description": "A set of react eslint rules",

@@ -5,0 +5,0 @@ "keywords": [

@@ -23,5 +23,19 @@ /**

// give me some code that won't trigger a warning
'useState()',
'useState("")',
'useState(true)',
'useState(false)',
'useState(null)',
'useState(undefined)',
'useState(1)',
'useState("test")',
'useState(value)',
'useState(object.value)',
'useState(1 || 2)',
'useState(1 || 2 || 3 < 4)',
'useState(1 && 2)',
'useState(1 < 2)',
'useState(1 < 2 ? 3 : 4)',
'useState(1 == 2 ? 3 : 4)',
'useState(1 === 2 ? 3 : 4)',
],

@@ -31,2 +45,10 @@

{
code: "useState(1 || getValue())",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(2 < getValue())",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(getValue())",

@@ -36,10 +58,30 @@ errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],

{
code: "useState(a ? b : c)",
errors: [{ message: rule.meta.messages.useLazyInitializationForConditionalExpressions, type: "CallExpression" }],
code: "useState(getValue(1, 2, 3))",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(a && b)",
errors: [{ message: rule.meta.messages.useLazyInitializationForLogicalExpressions, type: "CallExpression" }],
code: "useState(a ? b : c())",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(a() ? b : c)",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(a ? (b ? b1() : b2) : c)",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(a() && b)",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(a && b())",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
{
code: "useState(a() && b())",
errors: [{ message: rule.meta.messages.useLazyInitialization, type: "CallExpression" }],
},
],
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc