UNPKG

1.43 kBJavaScriptView Raw
1function isFunction(node) {
2 'use strict';
3 return node.type === 'FunctionExpression';
4}
5
6function 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
15function 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
26function isPotentialPointFree(node) {
27 'use strict';
28 return isFunction(node) &&
29 isSingleCall(node) &&
30 hasSameArguments(node);
31}
32
33module.exports = function (context) {
34 'use strict';
35
36 // read http://bahmutov.calepin.co/point-free-programming-is-not-pointless.html
37 // for advantages of point-free programming
38
39 return {
40 FunctionExpression: function (node) {
41 // console.log('function expression', node.id && node.id.name);
42
43 if (isPotentialPointFree(node)) {
44 // console.log('potential point-free candidate', node.id && node.id.name);
45 // console.log(node.body.body[0].expression.arguments);
46 context.report(node, node.id && node.id.name || 'function' + ' could potentially be point-free');
47 }
48 }
49 };
50};