1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/**
* @fileoverview Rule to validate spacing before function paren.
* @author Mathias Schreck <https://github.com/lo1tuma>
* @copyright 2015 Mathias Schreck
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
var configuration = context.options[0],
requireAnonymousFunctionSpacing = true,
requireNamedFunctionSpacing = true;
if (typeof configuration === "object") {
requireAnonymousFunctionSpacing = configuration.anonymous !== "never";
requireNamedFunctionSpacing = configuration.named !== "never";
} else if (configuration === "never") {
requireAnonymousFunctionSpacing = false;
requireNamedFunctionSpacing = false;
}
/**
* Determines whether two adjacent tokens are have whitespace between them.
* @param {Object} left - The left token object.
* @param {Object} right - The right token object.
* @returns {boolean} Whether or not there is space between the tokens.
*/
function isSpaced(left, right) {
return left.range[1] < right.range[0];
}
/**
* Determines whether a function has a name.
* @param {ASTNode} node The function node.
* @returns {boolean} Whether the function has a name.
*/
function isNamedFunction(node) {
var parent;
if (node.id) {
return true;
}
parent = context.getAncestors().pop();
return parent.type === "MethodDefinition" ||
(parent.type === "Property" &&
(
parent.kind === "get" ||
parent.kind === "set" ||
parent.method
)
);
}
/**
* Validates the spacing before function parentheses.
* @param {ASTNode} node The node to be validated.
* @returns {void}
*/
function validateSpacingBeforeParentheses(node) {
var isNamed = isNamedFunction(node),
tokens,
leftToken,
rightToken,
location;
if (node.generator && !isNamed) {
return;
}
tokens = context.getTokens(node);
if (node.generator) {
if (node.id) {
leftToken = tokens[2];
rightToken = tokens[3];
} else {
// Object methods are named but don't have an id
leftToken = context.getTokenBefore(node);
rightToken = tokens[0];
}
} else if (isNamed) {
if (node.id) {
leftToken = tokens[1];
rightToken = tokens[2];
} else {
// Object methods are named but don't have an id
leftToken = context.getTokenBefore(node);
rightToken = tokens[0];
}
} else {
leftToken = tokens[0];
rightToken = tokens[1];
}
location = leftToken.loc.end;
if (isSpaced(leftToken, rightToken)) {
if ((isNamed && !requireNamedFunctionSpacing) || (!isNamed && !requireAnonymousFunctionSpacing)) {
context.report(node, location, "Unexpected space before function parentheses.");
}
} else {
if ((isNamed && requireNamedFunctionSpacing) || (!isNamed && requireAnonymousFunctionSpacing)) {
context.report(node, location, "Missing space before function parentheses.");
}
}
}
return {
"FunctionDeclaration": validateSpacingBeforeParentheses,
"FunctionExpression": validateSpacingBeforeParentheses
};
};
|