eslint-plugin-react-func
Advanced tools
Comparing version
@@ -207,2 +207,58 @@ /** | ||
function getCurrentFunctionTextLineArray(node) { | ||
return sourceCode.lines.slice(node.loc.start.line - 1, node.loc.end.line); | ||
} | ||
function checkIsCommonFunction(lineTexts) { | ||
return lineTexts[0].includes('function'); | ||
} | ||
function checkIsObjectFunction(lineTexts) { | ||
try { | ||
const isCommonFunction = checkIsCommonFunction(lineTexts); | ||
if (isCommonFunction) return false; | ||
const arrowOperatorIndex = lineTexts.findIndex(line => line.includes('=>')); | ||
const isNotContainArrowFunction = arrowOperatorIndex === -1; | ||
if (isNotContainArrowFunction) return true; | ||
const rightParenthesesIndex = lineTexts.findIndex(line => line.includes(')')); | ||
if (rightParenthesesIndex === -1) return false; | ||
return (rightParenthesesIndex < arrowOperatorIndex); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function getArgumentsLineNum(node) { | ||
try { | ||
if (isReactEle(node)) return 0; | ||
const emptyLine = 0; | ||
let argumentsLineNum = emptyLine; | ||
const lineTexts = getCurrentFunctionTextLineArray(node); | ||
const isCommonFunction = checkIsCommonFunction(lineTexts); | ||
const isObjectFunction = checkIsObjectFunction(lineTexts); | ||
const isArrowFunction = !isCommonFunction && !isObjectFunction; | ||
if (isCommonFunction || isObjectFunction) { | ||
argumentsLineNum = lineTexts.findIndex(line => line.includes(')')); | ||
} | ||
if (isArrowFunction) { | ||
argumentsLineNum = lineTexts.findIndex(line => line.includes('=>')); | ||
} | ||
if (argumentsLineNum === -1) return emptyLine; | ||
return argumentsLineNum; | ||
} catch(e) { | ||
return emptyLine; | ||
} | ||
} | ||
/** | ||
@@ -217,2 +273,3 @@ * Count the lines in the function | ||
const argumentsLineNum = getArgumentsLineNum(node); | ||
@@ -232,3 +289,3 @@ if (!IIFEs && isIIFE(node)) { | ||
} | ||
if (skipBlankLines) { | ||
@@ -243,2 +300,4 @@ if (line.match(/^\s*$/u)) { | ||
lineCount = lineCount - argumentsLineNum; | ||
const isOverLength = lineCount > maxLines; | ||
@@ -245,0 +304,0 @@ const isError = isOverLength && !isReactEle(node); |
@@ -0,1 +1,60 @@ | ||
/** | ||
* Determines whether the given node is a `null` literal. | ||
* @param {ASTNode} node The node to check | ||
* @returns {boolean} `true` if the node is a `null` literal | ||
*/ | ||
function isNullLiteral(node) { | ||
/* | ||
* Checking `node.value === null` does not guarantee that a literal is a null literal. | ||
* When parsing values that cannot be represented in the current environment (e.g. unicode | ||
* regexes in Node 4), `node.value` is set to `null` because it wouldn't be possible to | ||
* set `node.value` to a unicode regex. To make sure a literal is actually `null`, check | ||
* `node.regex` instead. Also see: https://github.com/eslint/eslint/issues/8020 | ||
*/ | ||
return node.type === "Literal" && node.value === null && !node.regex && !node.bigint; | ||
} | ||
/** | ||
* Returns the result of the string conversion applied to the evaluated value of the given expression node, | ||
* if it can be determined statically. | ||
* | ||
* This function returns a `string` value for all `Literal` nodes and simple `TemplateLiteral` nodes only. | ||
* In all other cases, this function returns `null`. | ||
* @param {ASTNode} node Expression node. | ||
* @returns {string|null} String value if it can be determined. Otherwise, `null`. | ||
*/ | ||
function getStaticStringValue(node) { | ||
switch (node.type) { | ||
case "Literal": | ||
if (node.value === null) { | ||
if (isNullLiteral(node)) { | ||
return String(node.value); // "null" | ||
} | ||
if (node.regex) { | ||
return `/${node.regex.pattern}/${node.regex.flags}`; | ||
} | ||
if (node.bigint) { | ||
return node.bigint; | ||
} | ||
// Otherwise, this is an unknown literal. The function will return null. | ||
} else { | ||
return String(node.value); | ||
} | ||
break; | ||
case "TemplateLiteral": | ||
if (node.expressions.length === 0 && node.quasis.length === 1) { | ||
return node.quasis[0].value.cooked; | ||
} | ||
break; | ||
// no default | ||
} | ||
return null; | ||
} | ||
function getStaticPropertyName(node) { | ||
@@ -2,0 +61,0 @@ let prop; |
{ | ||
"name": "eslint-plugin-react-func", | ||
"version": "0.1.15", | ||
"version": "0.1.16", | ||
"description": "max lines per function for react", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
24031
21.85%512
22.49%