Skip to content

Commit

Permalink
Avoid nested functions and fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed Sep 5, 2019
1 parent 47e8640 commit bd90a9b
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions lib/util/Components.js
Expand Up @@ -576,65 +576,65 @@ function componentRule(rule, context) {
},

/**
* Get the parent stateless component node from the current scope
*
* @returns {ASTNode} component node, null if we are not in a component
* @param {ASTNode} node
* @returns {boolean}
*/
getParentStatelessComponent() {
/**
* @param {ASTNode} node
* @returns {boolean}
*/
function isInAllowedPositiionForComponent(node) {
switch (node.parent.type) {
case 'VariableDeclarator':
case 'AssignmentExpression':
case 'Property':
case 'ReturnStatement':
case 'ExportDefaultDeclaration': {
return true;
}
case 'SequenceExpression': {
return isInAllowedPositiionForComponent(node.parent) &&
node === node.parent.expressions[node.parent.expressions.length - 1];
}
default:
return false;
isInAllowedPositionForComponent(node) {
switch (node.parent.type) {
case 'VariableDeclarator':
case 'AssignmentExpression':
case 'Property':
case 'ReturnStatement':
case 'ExportDefaultDeclaration': {
return true;
}
case 'SequenceExpression': {
return utils.isInAllowedPositionForComponent(node.parent) &&
node === node.parent.expressions[node.parent.expressions.length - 1];
}
default:
return false;
}
},

/**
* Get node if node is a stateless component, or node.parent in cases like
* `React.memo` or `React.forwardRef`. Otherwise returns `undefined`.
* @param {ASTNode} node
* @returns {ASTNode | undefined}
*/
function getStatelessComponent(node) {
if (node.type === 'FunctionDeclaration') {
if (utils.isReturningJSXOrNull(node)) {
return node;
}
/**
* Get node if node is a stateless component, or node.parent in cases like
* `React.memo` or `React.forwardRef`. Otherwise returns `undefined`.
* @param {ASTNode} node
* @returns {ASTNode | undefined}
*/
getStatelessComponent(node) {
if (node.type === 'FunctionDeclaration') {
if (utils.isReturningJSXOrNull(node)) {
return node;
}
}

if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
if (isInAllowedPositiionForComponent(node) && utils.isReturningJSXOrNull(node)) {
return node;
}

// Case like `React.memo(() => <></>)` or `React.forwardRef(...)`
const pragmaComponentWrapper = utils.getPragmaComponentWrapper(node);
if (pragmaComponentWrapper) {
return pragmaComponentWrapper;
}
if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) {
return node;
}

return undefined;
// Case like `React.memo(() => <></>)` or `React.forwardRef(...)`
const pragmaComponentWrapper = utils.getPragmaComponentWrapper(node);
if (pragmaComponentWrapper) {
return pragmaComponentWrapper;
}
}

return undefined;
},

/**
* Get the parent stateless component node from the current scope
*
* @returns {ASTNode} component node, null if we are not in a component
*/
getParentStatelessComponent() {
let scope = context.getScope();
while (scope) {
const node = scope.block;
const statelessComponent = getStatelessComponent(node);
const statelessComponent = utils.getStatelessComponent(node);
if (statelessComponent) {
return statelessComponent;
}
Expand Down

0 comments on commit bd90a9b

Please sign in to comment.