Skip to content

Commit

Permalink
refactor astUtil to use lodash methods for function definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Feb 25, 2016
1 parent 5054431 commit 2852ede
Showing 1 changed file with 18 additions and 42 deletions.
60 changes: 18 additions & 42 deletions lib/util/astUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,48 @@ var _ = require('lodash');
* @param {Object} node
* @returns {Object|undefined}
*/
function getCaller(node) {
return _.get(node, 'callee.object');
}
var getCaller = _.property(['callee', 'object']);

/**
* Gets the name of a method in a CallExpression
* @param {Object} node
* @returns {string|undefined}
*/
function getMethodName(node) {
return _.get(node, 'callee.property.name');
}
var getMethodName = _.property(['callee', 'property', 'name']);

/**
* Returns whether the node is a method call
* @param {Object} node
* @returns {boolean|undefined}
* @returns {boolean}
*/
function isMethodCall(node) {
return node && node.type === 'CallExpression' && node.callee.type === 'MemberExpression';
}
var isMethodCall = _.matches({type: 'CallExpression', callee: {type: 'MemberExpression'}});

/**
* Returns whether the node is a function declaration that has a block
* @param {Object} node
* @returns {boolean}
*/
function isFunctionDefinitionWithBlock(node) {
return (node.type === 'FunctionExpression' || (node.type === 'ArrowFunctionExpression' && node.body.type === 'BlockStatement'));
}
var isFunctionDefinitionWithBlock = _.overSome(
_.matchesProperty('type', 'FunctionExpression'),
_.matches({type: 'ArrowFunctionExpression', body: {type: 'BlockStatement'}})
);

/**
* If the node specified is a function, returns the node corresponding with the first statement/expression in that function
* @param {Object} node
* @returns {node|null}
* @returns {node|undefined}
*/
function getFirstFunctionLine(node) {
if (node) {
if (isFunctionDefinitionWithBlock(node)) {
return _.get(node, 'body.body[0]');
}
if (node.type === 'ArrowFunctionExpression') {
return node.body;
}
}
return null;
}
var getFirstFunctionLine = _.cond([
[isFunctionDefinitionWithBlock, _.property(['body', 'body', 0])],
[_.matches({type: 'ArrowFunctionExpression'}), _.property('body')]
]);

/**
*
* @param {Object} node
* @returns {boolean|undefined}
*/
function isPropAccess(node) {
return node &&
node.computed === false ||
(node.computed === true && node.property.type === 'Literal');
}
var isPropAccess = _.overSome(_.matches({computed: false}), _.matchesProperty(['property', 'type'], 'Literal'));

/**
* Returns whether the node is a member expression starting with the same object, up to the specified length
Expand Down Expand Up @@ -92,18 +76,14 @@ function isMemberExpOf(node, objectName, maxPropertyPathLength, allowComputed) {
* @param {Object} func
* @returns {string|undefined}
*/
function getFirstParamName(func) {
return _.get(func, 'params[0].name');
}
var getFirstParamName = _.property(['params', 0, 'name']);

/**
* Returns whether or not the expression is a return statement
* @param {Object} exp
* @returns {boolean|undefined}
*/
function isReturnStatement(exp) {
return exp && exp.type === 'ReturnStatement';
}
var isReturnStatement = _.matchesProperty('type', 'ReturnStatement');

/**
* Returns whether the node specified has only one statement
Expand Down Expand Up @@ -144,9 +124,7 @@ function isBinaryExpWithMemberOf(operator, exp, objectName, maxPropertyPathLengt
* @param {Object} exp
* @returns {boolean|undefined}
*/
function isNegationExpression(exp) {
return exp && exp.type === 'UnaryExpression' && exp.operator === '!';
}
var isNegationExpression = _.matches({type: 'UnaryExpression', operator: '!'});

/**
* Returns whether the expression is a negation of a member of objectName, in the specified depth.
Expand Down Expand Up @@ -233,9 +211,7 @@ function isEquivalentExp(a, b) {
* @param {Object} node
* @returns {boolean}
*/
function isEqEqEq(node) {
return node && node.type === 'BinaryExpression' && node.operator === '===';
}
var isEqEqEq = _.matches({type: 'BinaryExpression', operator: '==='});

module.exports = {
getCaller: getCaller,
Expand Down

0 comments on commit 2852ede

Please sign in to comment.