Skip to content

Commit 08e9d0b

Browse files
Extract util functions to AST utils
1 parent 68d6384 commit 08e9d0b

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

lib/util/Components.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,13 @@ function componentRule(rule, context) {
457457
while (scope) {
458458
const node = scope.block;
459459
const isFunction = /Function/.test(node.type); // Functions
460-
const isArrowFunction = node.type === 'ArrowFunctionExpression';
460+
const isArrowFunction = astUtil.isArrowFunction(node);
461461
let enclosingScope = scope;
462462
if (isArrowFunction) {
463463
enclosingScope = utils.getArrowFunctionScope(scope);
464464
}
465-
const enclosingScopeType = enclosingScope && enclosingScope.block.type;
466465
const enclosingScopeParent = enclosingScope && enclosingScope.block.parent;
467-
const isClass = enclosingScopeType === 'ClassDeclaration' || enclosingScopeType === 'ClassExpression';
466+
const isClass = enclosingScope && astUtil.isClass(enclosingScope.block);
468467
const isMethod = enclosingScopeParent && enclosingScopeParent.type === 'MethodDefinition'; // Classes methods
469468
const isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.)
470469
// Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>)
@@ -490,9 +489,7 @@ function componentRule(rule, context) {
490489
getArrowFunctionScope(scope) {
491490
scope = scope.upper;
492491
while (scope) {
493-
const type = scope.block.type;
494-
if (type === 'FunctionExpression' || type === 'FunctionDeclaration'
495-
|| type === 'ClassDeclaration' || type === 'ClassExpression') {
492+
if (astUtil.isFunction(scope.block) || astUtil.isClass(scope.block)) {
496493
return scope;
497494
}
498495
scope = scope.upper;

lib/util/ast.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,41 @@ function isFunctionLikeExpression(node) {
102102
return node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
103103
}
104104

105+
/**
106+
* Checks if the node is a function.
107+
* @param {Object} context The node to check
108+
* @return {Boolean} true if it's a function
109+
*/
110+
function isFunction(node) {
111+
return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
112+
}
113+
114+
/**
115+
* Checks if the node is an arrow function.
116+
* @param {Object} context The node to check
117+
* @return {Boolean} true if it's an arrow function
118+
*/
119+
function isArrowFunction(node) {
120+
return node.type === 'ArrowFunctionExpression';
121+
}
122+
123+
/**
124+
* Checks if the node is a class.
125+
* @param {Object} context The node to check
126+
* @return {Boolean} true if it's a class
127+
*/
128+
function isClass(node) {
129+
return node.type === 'ClassDeclaration' || node.type === 'ClassExpression';
130+
}
131+
105132
module.exports = {
106133
findReturnStatement: findReturnStatement,
107134
getPropertyName: getPropertyName,
108135
getPropertyNameNode: getPropertyNameNode,
109136
getComponentProperties: getComponentProperties,
110-
isNodeFirstInLine: isNodeFirstInLine,
111-
isFunctionLikeExpression: isFunctionLikeExpression
137+
isArrowFunction: isArrowFunction,
138+
isClass: isClass,
139+
isFunction: isFunction,
140+
isFunctionLikeExpression: isFunctionLikeExpression,
141+
isNodeFirstInLine: isNodeFirstInLine
112142
};

0 commit comments

Comments
 (0)