1 | function isFunction(node) {
|
2 | 'use strict';
|
3 | return node.type === 'FunctionExpression';
|
4 | }
|
5 |
|
6 | function isSingleCall(node) {
|
7 | 'use strict';
|
8 | return node.body &&
|
9 | node.body.type === 'BlockStatement' &&
|
10 | node.body.body.length === 1 &&
|
11 | node.body.body[0].type === 'ExpressionStatement' &&
|
12 | node.body.body[0].expression.type === 'CallExpression';
|
13 | }
|
14 |
|
15 | function hasSameArguments(node) {
|
16 | 'use strict';
|
17 | var topArguments = node.params;
|
18 | var innerArguments = node.body.body[0].expression.arguments;
|
19 |
|
20 | return topArguments.length === innerArguments.length &&
|
21 | topArguments.every(function (arg, k) {
|
22 | return innerArguments[k].name === arg.name;
|
23 | });
|
24 | }
|
25 |
|
26 | function isPotentialPointFree(node) {
|
27 | 'use strict';
|
28 | return isFunction(node) &&
|
29 | isSingleCall(node) &&
|
30 | hasSameArguments(node);
|
31 | }
|
32 |
|
33 | module.exports = function (context) {
|
34 | 'use strict';
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | return {
|
40 | FunctionExpression: function (node) {
|
41 |
|
42 |
|
43 | if (isPotentialPointFree(node)) {
|
44 |
|
45 |
|
46 | context.report(node, node.id && node.id.name || 'function' + ' could potentially be point-free');
|
47 | }
|
48 | }
|
49 | };
|
50 | };
|