Skip to content

Commit 5554bd4

Browse files
nix6839ljharb
authored andcommitted
[Refactor] add AST util isFunctionLike()
1 parent ef733fd commit 5554bd4

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

lib/util/ast.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ function isFunction(node) {
232232
return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration';
233233
}
234234

235+
/**
236+
* Checks if node is a function declaration or expression or arrow function.
237+
* @param {ASTNode} node The node to check
238+
* @return {Boolean} true if it's a function-like
239+
*/
240+
function isFunctionLike(node) {
241+
return node.type === 'FunctionDeclaration' || isFunctionLikeExpression(node);
242+
}
243+
235244
/**
236245
* Checks if the node is a class.
237246
* @param {ASTNode} node The node to check
@@ -432,6 +441,7 @@ module.exports = {
432441
isClass,
433442
isFunction,
434443
isFunctionLikeExpression,
444+
isFunctionLike,
435445
inConstructor,
436446
isNodeFirstInLine,
437447
unwrapTSAsExpression,

tests/util/ast.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const espree = require('espree');
77
const ast = require('../../lib/util/ast');
88

99
const traverseReturns = ast.traverseReturns;
10+
const isFunctionLike = ast.isFunctionLike;
1011

1112
const DEFAULT_CONFIG = {
1213
ecmaVersion: 6,
@@ -101,4 +102,76 @@ describe('ast', () => {
101102
});
102103
});
103104
});
105+
106+
describe('isFunctionLike()', () => {
107+
it('FunctionDeclaration should return true', () => {
108+
const node1 = parseCode(`
109+
function foo(bar) {
110+
const asdf = () => 'zxcv';
111+
return asdf;
112+
}
113+
`);
114+
assert.strictEqual(isFunctionLike(node1), true);
115+
116+
const node2 = parseCode(`
117+
function foo({bar}) {
118+
const asdf = () => 'zxcv';
119+
console.log(bar);
120+
return '5'
121+
}
122+
`);
123+
assert.strictEqual(isFunctionLike(node2), true);
124+
});
125+
126+
it('FunctionExpression should return true', () => {
127+
const node1 = parseCode(`
128+
const foo = function(bar) {
129+
return () => 'zxcv';
130+
}
131+
`).declarations[0].init;
132+
assert.strictEqual(isFunctionLike(node1), true);
133+
134+
const node2 = parseCode(`
135+
const foo = function ({bar}) {
136+
return '5';
137+
}
138+
`).declarations[0].init;
139+
assert.strictEqual(isFunctionLike(node2), true);
140+
});
141+
142+
it('ArrowFunctionExpression should return true', () => {
143+
const node1 = parseCode(`
144+
(bar) => {
145+
return () => 'zxcv';
146+
}
147+
`).expression;
148+
assert.strictEqual(isFunctionLike(node1), true);
149+
150+
const node2 = parseCode(`
151+
({bar}) => '5';
152+
`).expression;
153+
assert.strictEqual(isFunctionLike(node2), true);
154+
155+
const node3 = parseCode(`
156+
bar => '5';
157+
`).expression;
158+
assert.strictEqual(isFunctionLike(node3), true);
159+
});
160+
161+
it('Non-functions should return false', () => {
162+
const node1 = parseCode(`
163+
class bar {
164+
a() {
165+
return 'a';
166+
}
167+
}
168+
`);
169+
assert.strictEqual(isFunctionLike(node1), false);
170+
171+
const node2 = parseCode(`
172+
const a = 5;
173+
`);
174+
assert.strictEqual(isFunctionLike(node2), false);
175+
});
176+
});
104177
});

0 commit comments

Comments
 (0)