UNPKG

1.61 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to check that spaced function application
3 * @author Matt DuVall <http://www.mattduvall.com>
4 */
5
6//------------------------------------------------------------------------------
7// Rule Definition
8//------------------------------------------------------------------------------
9
10module.exports = function(context) {
11
12 "use strict";
13
14 function findOpenParen(tokens) {
15 var i, token, hasArgumentList = false, numOpen = 0;
16
17 // start at the end of the token stream; skip over argument list contents
18 for(i = tokens.length - 1; i >= 0; --i) {
19 token = tokens[i];
20 if (token.value === "(") {
21 --numOpen;
22 } else if (token.value === ")") {
23 hasArgumentList = true;
24 ++numOpen;
25 }
26 if (hasArgumentList && numOpen <= 0) {
27 return i;
28 }
29 }
30 }
31
32 function detectOpenSpaces(node) {
33 var tokens = context.getTokens(node),
34 openParenIndex = findOpenParen(tokens),
35 openParen = tokens[openParenIndex],
36 callee = tokens[openParenIndex - 1];
37
38 // openParenIndex will be undefined for a NewExpression with no argument list
39 if (!openParenIndex) { return; }
40
41 // look for a space between the callee and the open paren
42 if (callee.range[1] !== openParen.range[0]) {
43 context.report(node, "Spaced function application is not allowed.");
44 }
45 }
46
47 return {
48 "CallExpression": detectOpenSpaces,
49 "NewExpression": detectOpenSpaces
50 };
51
52};