@arthurgeron/eslint-plugin-react-usememo
Advanced tools
Comparing version 0.1.2 to 1.0.0--beta
@@ -72,2 +72,4 @@ 'use strict'; | ||
var node = variable.defs[0].node; | ||
if (node.type === "FunctionDeclaration") | ||
return MemoStatus.UnmemoizedFunction; | ||
if (node.type !== "VariableDeclarator") | ||
@@ -179,2 +181,3 @@ return MemoStatus.Memoized; | ||
var _a, _b, _c, _d; | ||
var ValidExpressions = { | ||
@@ -188,18 +191,60 @@ 'ArrowFunctionExpression': true, | ||
}; | ||
var jsxEmptyExpressionClassData = (_a = {}, | ||
_a[MemoStatus.UnmemoizedObject.toString()] = "object-class-memo-props", | ||
_a[MemoStatus.UnmemoizedArray.toString()] = "array-class-memo-props", | ||
_a[MemoStatus.UnmemoizedNew.toString()] = "instance-class-memo-props", | ||
_a[MemoStatus.UnmemoizedFunction.toString()] = 'instance-class-memo-props', | ||
_a[MemoStatus.UnmemoizedFunctionCall.toString()] = "unknown-class-memo-props", | ||
_a[MemoStatus.UnmemoizedOther.toString()] = "unknown-class-memo-props", | ||
_a); | ||
var jsxEmptyExpressionData = (_b = {}, | ||
_b[MemoStatus.UnmemoizedObject.toString()] = "object-usememo-props", | ||
_b[MemoStatus.UnmemoizedArray.toString()] = "array-usememo-props", | ||
_b[MemoStatus.UnmemoizedNew.toString()] = "instance-usememo-props", | ||
_b[MemoStatus.UnmemoizedFunction.toString()] = "function-usecallback-props", | ||
_b[MemoStatus.UnmemoizedFunctionCall.toString()] = "unknown-usememo-props", | ||
_b[MemoStatus.UnmemoizedOther.toString()] = "unknown-usememo-props", | ||
_b[MemoStatus.UnmemoizedJSX.toString()] = "jsx-usememo-props", | ||
_b); | ||
var hookReturnExpressionData = (_c = {}, | ||
_c[MemoStatus.UnmemoizedObject.toString()] = "object-usememo-hook", | ||
_c[MemoStatus.UnmemoizedArray.toString()] = "array-usememo-hook", | ||
_c[MemoStatus.UnmemoizedNew.toString()] = "instance-usememo-hook", | ||
_c[MemoStatus.UnmemoizedFunction.toString()] = "function-usecallback-hook", | ||
_c[MemoStatus.UnmemoizedFunctionCall.toString()] = "unknown-usememo-hook", | ||
_c[MemoStatus.UnmemoizedOther.toString()] = "unknown-usememo-hook", | ||
_c[MemoStatus.UnmemoizedJSX.toString()] = "jsx-usememo-hook", | ||
_c); | ||
var callExpressionData = (_d = {}, | ||
_d[MemoStatus.UnmemoizedObject.toString()] = "object-usememo-deps", | ||
_d[MemoStatus.UnmemoizedArray.toString()] = "array-usememo-deps", | ||
_d[MemoStatus.UnmemoizedNew.toString()] = "instance-usememo-deps", | ||
_d[MemoStatus.UnmemoizedFunction.toString()] = "function-usecallback-deps", | ||
_d[MemoStatus.UnmemoizedFunctionCall.toString()] = "unknown-usememo-deps", | ||
_d[MemoStatus.UnmemoizedOther.toString()] = "unknown-usememo-deps", | ||
_d[MemoStatus.UnmemoizedJSX.toString()] = "jsx-usememo-deps", | ||
_d); | ||
var MessagesRequireUseMemo = { | ||
"object-usememo-props": "Object literal should be wrapped in useMemo() or be static when used as a prop", | ||
"object-class-memo-props": "Object literal should com from state or be static when used as a prop", | ||
"object-usememo-hook": "Object literal should com from state or be static when returned from a hook", | ||
"object-usememo-deps": "Object literal should be wrapped in useMemo() or be static when used as a hook dependency", | ||
"array-usememo-props": "Array literal should be wrapped in useMemo() or be static when used as a prop", | ||
"array-usememo-hook": "Array literal should be wrapped in useMemo() or be static when returned from a hook", | ||
"array-class-memo-props": "Array literal should be from state and declared in state, constructor, getDerivedStateFromProps or statically when used as a prop", | ||
"array-usememo-deps": "Array literal should be wrapped in useMemo() or be static when used as a hook dependency", | ||
"instance-usememo-props": "Object instantiation should be wrapped in useMemo() or be static when used as a prop", | ||
"instance-usememo-hook": "Object instantiation should be wrapped in useMemo() or be static when returned from a hook", | ||
"instance-class-memo-props": "Object instantiation should be done in state, constructor, getDerivedStateFromProps or statically when used as a prop", | ||
"instance-usememo-deps": "Object instantiation should be wrapped in useMemo() or be static when used as a hook dependency", | ||
"jsx-usememo-props": "JSX should be wrapped in useMemo() when used as a prop", | ||
"jsx-usememo-hook": "JSX should be wrapped in useMemo() when returned from a hook", | ||
"jsx-usememo-deps": "JSX should be wrapped in useMemo() when used as a hook dependency", | ||
"function-usecallback-props": "Function definition should be wrapped in useCallback() or be static when used as a prop", | ||
"function-usecallback-hook": "Function definition should be wrapped in useCallback() or be static when returned from a hook", | ||
"function-class-props": "Function definition should declared as a class property or statically when used as a prop", | ||
"function-usecallback-deps": "Function definition should be wrapped in useCallback() or be static when used as a hook dependency", | ||
"unknown-usememo-props": "Unknown value may need to be wrapped in useMemo() when used as a prop", | ||
"unknown-usememo-hook": "Unknown value may need to be wrapped in useMemo() when returned from a hook", | ||
"unknown-class-memo-props": "Unknown value should be declared in state, constructor, getDerivedStateFromProps or statically when used as a prop", | ||
@@ -219,3 +264,13 @@ "unknown-usememo-deps": "Unknown value may need to be wrapped in useMemo() when used as a hook dependency", | ||
function isHook(node) { | ||
function checkForErrors(data, expressionType, context, node, report) { | ||
var _a, _b; | ||
var errorName = data === null || data === void 0 ? void 0 : data[expressionType.toString()]; | ||
if (errorName) { | ||
var strict = errorName.includes('unknown'); | ||
if (!strict || (strict && ((_b = (_a = context.options) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.strict))) { | ||
report(node, errorName); | ||
} | ||
} | ||
} | ||
function getIsHook(node) { | ||
if (node.type === "Identifier") { | ||
@@ -226,3 +281,3 @@ return node.name[0] === 'u' && node.name[1] === 's' && node.name[2] === 'e'; | ||
!node.computed && | ||
isHook(node.property)) { | ||
getIsHook(node.property)) { | ||
var obj = node.object; | ||
@@ -235,2 +290,3 @@ return obj.type === "Identifier" && obj.name === "React"; | ||
} | ||
var rule$1 = { | ||
@@ -257,9 +313,5 @@ meta: { | ||
} | ||
function process(node, _expression) { | ||
var _a, _b; | ||
function process(node, _expression, expressionData) { | ||
var expression = _expression !== null && _expression !== void 0 ? _expression : (node.value && Object.prototype.hasOwnProperty.call(node.value, 'expression') ? node.value.expression : node.value); | ||
switch (expression === null || expression === void 0 ? void 0 : expression.type) { | ||
case 'JSXEmptyExpression': | ||
process(node, expression); | ||
return; | ||
case 'LogicalExpression': | ||
@@ -272,79 +324,38 @@ !expression.left ? true : process(node, expression.left); | ||
default: | ||
switch (getExpressionMemoStatus(context, expression)) { | ||
case MemoStatus.UnmemoizedObject: | ||
report(node, isClass ? "object-class-memo-props" : "object-usememo-props"); | ||
return; | ||
case MemoStatus.UnmemoizedArray: | ||
report(node, isClass ? "array-class-memo-props" : "array-usememo-props"); | ||
return; | ||
case MemoStatus.UnmemoizedNew: | ||
report(node, isClass ? "instance-class-memo-props" : "instance-usememo-props"); | ||
return; | ||
case MemoStatus.UnmemoizedFunction: | ||
report(node, isClass ? 'instance-class-memo-props' : "function-usecallback-props"); | ||
return; | ||
case MemoStatus.UnmemoizedFunctionCall: | ||
case MemoStatus.UnmemoizedOther: | ||
if ((_b = (_a = context.options) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.strict) { | ||
report(node, isClass ? "unknown-class-memo-props" : "unknown-usememo-props"); | ||
} | ||
return; | ||
case MemoStatus.UnmemoizedJSX: | ||
if (isClass) { | ||
return; | ||
} | ||
report(node, "jsx-usememo-props"); | ||
return; | ||
} | ||
checkForErrors(expressionData || (isClass ? jsxEmptyExpressionClassData : jsxEmptyExpressionData), getExpressionMemoStatus(context, expression), context, node, report); | ||
return; | ||
} | ||
} | ||
function JSXAttribute(node) { | ||
var _a = node, parent = _a.parent, value = _a.value; | ||
if (value === null) | ||
return null; | ||
if (parent && !isComplexComponent(parent)) | ||
return null; | ||
if (value.type === "JSXExpressionContainer") { | ||
process(node); | ||
} | ||
return null; | ||
} | ||
return { | ||
JSXAttribute: function (node) { | ||
var parent = node.parent, value = node.value; | ||
if (value === null) | ||
return; | ||
if (parent && !isComplexComponent(parent)) | ||
return; | ||
if (value.type === "JSXExpressionContainer") { | ||
process(node); | ||
} | ||
}, | ||
JSXAttribute: JSXAttribute, | ||
ClassDeclaration: function () { | ||
isClass = true; | ||
}, | ||
ReturnStatement: function (node) { | ||
if (node.parent.parent.type === 'FunctionDeclaration' && getIsHook(node.parent.parent.id) && node.argument) { | ||
process(node, node.argument, hookReturnExpressionData); | ||
} | ||
}, | ||
CallExpression: function (node) { | ||
var _a, _b; | ||
var callee = node.callee; | ||
if (!isHook(callee)) | ||
if (!getIsHook(callee)) | ||
return; | ||
var _c = node.arguments, dependencies = _c[1]; | ||
var _a = node.arguments, dependencies = _a[1]; | ||
if (dependencies !== undefined && | ||
dependencies.type === "ArrayExpression") { | ||
for (var _i = 0, _d = dependencies.elements; _i < _d.length; _i++) { | ||
var dep = _d[_i]; | ||
for (var _i = 0, _b = dependencies.elements; _i < _b.length; _i++) { | ||
var dep = _b[_i]; | ||
if (dep !== null && ValidExpressions[dep.type]) { | ||
switch (getExpressionMemoStatus(context, dep)) { | ||
case MemoStatus.UnmemoizedObject: | ||
report(node, "object-usememo-deps"); | ||
break; | ||
case MemoStatus.UnmemoizedArray: | ||
report(node, "array-usememo-deps"); | ||
break; | ||
case MemoStatus.UnmemoizedNew: | ||
report(node, "instance-usememo-deps"); | ||
break; | ||
case MemoStatus.UnmemoizedFunction: | ||
report(node, "function-usecallback-deps"); | ||
break; | ||
case MemoStatus.UnmemoizedFunctionCall: | ||
case MemoStatus.UnmemoizedOther: | ||
if ((_b = (_a = context.options) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.strict) { | ||
report(node, "unknown-usememo-deps"); | ||
} | ||
break; | ||
case MemoStatus.UnmemoizedJSX: | ||
report(node, "jsx-usememo-deps"); | ||
break; | ||
} | ||
checkForErrors(callExpressionData, getExpressionMemoStatus(context, dep), context, node, report); | ||
} | ||
@@ -351,0 +362,0 @@ } |
{ | ||
"name": "@arthurgeron/eslint-plugin-react-usememo", | ||
"version": "0.1.2", | ||
"version": "1.0.0--beta", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27763
420