This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-spaces-in-function.js
128 lines (113 loc) · 4.07 KB
/
disallow-spaces-in-function.js
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
118
119
120
121
122
123
124
125
126
127
128
/**
* Disallows space before `()` or `{}` in function expressions (both [named](#disallowspacesinnamedfunctionexpression)
* and [anonymous](#disallowspacesinanonymousfunctionexpression)) and function declarations.
*
* Type: `Object`
*
* Values: `"beforeOpeningRoundBrace"` and `"beforeOpeningCurlyBrace"` as child properties.
* Child properties must be set to `true`.
*
* #### Example
*
* ```js
* "disallowSpacesInFunction": {
* "beforeOpeningRoundBrace": true,
* "beforeOpeningCurlyBrace": true
* }
* ```
*
* ##### Valid
*
* ```js
* var x = function(){};
* var x = function a(){};
* function a(){}
* ```
*
* ##### Invalid
*
* ```js
* var x = function() {};
* var x = function (){};
* var x = function () {};
* var x = function a() {};
* var x = function a (){};
* var x = function a () {};
* function a() {}
* function a (){}
* function a () {}
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
typeof options === 'object',
this.getOptionName() + ' option must be the object'
);
if ('beforeOpeningRoundBrace' in options) {
assert(
options.beforeOpeningRoundBrace === true,
this.getOptionName() + '.beforeOpeningRoundBrace ' +
'property requires true value or should be removed'
);
}
if ('beforeOpeningCurlyBrace' in options) {
assert(
options.beforeOpeningCurlyBrace === true,
this.getOptionName() + '.beforeOpeningCurlyBrace ' +
'property requires true value or should be removed'
);
}
assert(
options.beforeOpeningCurlyBrace || options.beforeOpeningRoundBrace,
this.getOptionName() + ' must have beforeOpeningCurlyBrace or beforeOpeningRoundBrace property'
);
this._beforeOpeningRoundBrace = Boolean(options.beforeOpeningRoundBrace);
this._beforeOpeningCurlyBrace = Boolean(options.beforeOpeningCurlyBrace);
},
getOptionName: function() {
return 'disallowSpacesInFunction';
},
check: function(file, errors) {
var beforeOpeningRoundBrace = this._beforeOpeningRoundBrace;
var beforeOpeningCurlyBrace = this._beforeOpeningCurlyBrace;
file.iterateNodesByType(['FunctionDeclaration', 'FunctionExpression'], function(node) {
// for a named function, use node.id
var functionNode = node.id || node;
var parent = node.parentElement;
// Ignore syntactic sugar for getters and setters.
if (parent.type === 'Property' && (parent.kind === 'get' || parent.kind === 'set')) {
return;
}
// shorthand or constructor methods
if (parent.method || parent.type === 'MethodDefinition') {
functionNode = parent.key;
}
if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
message: 'Illegal space before opening round brace'
});
}
if (beforeOpeningCurlyBrace) {
var bodyToken = file.getFirstNodeToken(node.body);
errors.assert.noWhitespaceBetween({
token: file.getPrevToken(bodyToken),
nextToken: bodyToken,
message: 'Illegal space before opening curly brace'
});
}
});
}
};