Skip to content

Commit ef36ba9

Browse files
committed
Add a helper function for determining function-like expressions
1 parent efe0c0c commit ef36ba9

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

lib/rules/display-name.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const has = require('has');
88
const Components = require('../util/Components');
9+
const astUtil = require('../util/ast');
910
const docsUrl = require('../util/docsUrl');
1011

1112
// ------------------------------------------------------------------------------
@@ -116,7 +117,7 @@ module.exports = {
116117
);
117118

118119
const namedFunctionExpression = (
119-
(node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') &&
120+
astUtil.isFunctionLikeExpression(node) &&
120121
node.parent &&
121122
(node.parent.type === 'VariableDeclarator' || node.parent.method === true) &&
122123
(!node.parent.parent || !utils.isES5Component(node.parent.parent))

lib/rules/no-array-index-key.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
const has = require('has');
8+
const astUtil = require('../util/ast');
89
const docsUrl = require('../util/docsUrl');
910

1011
// ------------------------------------------------------------------------------
@@ -63,11 +64,7 @@ module.exports = {
6364
return null;
6465
}
6566

66-
const isFunction = [
67-
'ArrowFunctionExpression',
68-
'FunctionExpression'
69-
].indexOf(firstArg.type) !== -1;
70-
if (!isFunction) {
67+
if (!astUtil.isFunctionLikeExpression(firstArg)) {
7168
return null;
7269
}
7370

lib/rules/no-unused-prop-types.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const has = require('has');
1111
const Components = require('../util/Components');
1212
const variable = require('../util/variable');
1313
const annotations = require('../util/annotations');
14+
const astUtil = require('../util/ast');
1415
const versionUtil = require('../util/version');
1516
const propsUtil = require('../util/props');
1617
const docsUrl = require('../util/docsUrl');
@@ -835,10 +836,7 @@ module.exports = {
835836
types.node = value;
836837
declaredPropTypes.push(types);
837838
// Handle custom prop validators using props inside
838-
if (
839-
value.type === 'ArrowFunctionExpression'
840-
|| value.type === 'FunctionExpression'
841-
) {
839+
if (astUtil.isFunctionLikeExpression(value)) {
842840
markPropTypesAsUsed(value);
843841
}
844842
});

lib/rules/require-render-return.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = {
4646
if (astUtil.getPropertyName(properties[i]) !== 'render' || !properties[i].value) {
4747
continue;
4848
}
49-
return /FunctionExpression$/.test(properties[i].value.type);
49+
return astUtil.isFunctionLikeExpression(properties[i].value);
5050
}
5151
return false;
5252
}

lib/rules/sort-comp.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,11 @@ module.exports = {
393393
instanceVariable: !node.static &&
394394
node.type === 'ClassProperty' &&
395395
node.value &&
396-
node.value.type !== 'ArrowFunctionExpression' &&
397-
node.value.type !== 'FunctionExpression',
396+
!astUtil.isFunctionLikeExpression(node.value),
398397
instanceMethod: !node.static &&
399398
node.type === 'ClassProperty' &&
400399
node.value &&
401-
(node.value.type === 'ArrowFunctionExpression' ||
402-
node.value.type === 'FunctionExpression'),
400+
(astUtil.isFunctionLikeExpression(node.value)),
403401
typeAnnotation: !!node.typeAnnotation && node.value === null
404402
}));
405403

lib/util/ast.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,18 @@ function getComponentProperties(node) {
6262
case 'ClassExpression':
6363
return node.body.body;
6464
case 'ObjectExpression':
65-
// return node.properties;
6665
return node.properties;
6766
default:
6867
return [];
6968
}
7069
}
7170

7271
/**
73-
* Checks if the node is the first in its line, excluding whitespace.
74-
* @param {Object} context The node to check
75-
* @param {ASTNode} node The node to check
76-
* @return {Boolean} true if its the first node in its line
77-
*/
72+
* Checks if the node is the first in its line, excluding whitespace.
73+
* @param {Object} context The node to check
74+
* @param {ASTNode} node The node to check
75+
* @return {Boolean} true if it's the first node in its line
76+
*/
7877
function isNodeFirstInLine(context, node) {
7978
const sourceCode = context.getSourceCode();
8079
let token = node;
@@ -94,11 +93,20 @@ function isNodeFirstInLine(context, node) {
9493
return startLine !== endLine;
9594
}
9695

96+
/**
97+
* Checks if the node is a function or arrow function expression.
98+
* @param {Object} context The node to check
99+
* @return {Boolean} true if it's a function-like expression
100+
*/
101+
function isFunctionLikeExpression(node) {
102+
return node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
103+
}
97104

98105
module.exports = {
99106
findReturnStatement: findReturnStatement,
100107
getPropertyName: getPropertyName,
101108
getPropertyNameNode: getPropertyNameNode,
102109
getComponentProperties: getComponentProperties,
103-
isNodeFirstInLine: isNodeFirstInLine
110+
isNodeFirstInLine: isNodeFirstInLine,
111+
isFunctionLikeExpression: isFunctionLikeExpression
104112
};

0 commit comments

Comments
 (0)