/
comma-spacing.js
60 lines (49 loc) · 1018 Bytes
/
comma-spacing.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
/**
* Internal dependencies.
*/
var Rule = require('../rule');
var Comma = Rule.create({
error: 'Bad comma spacing',
option: {
key: 'commaspace',
type: 'enum',
values: ['after', 'before', 'surround'],
defaults: 'off'
},
on: {
Punctuator: 'check',
},
});
/**
* Perform the check.
*
* @param {Object} token
* @api public
*/
Comma.prototype.check = function(token) {
if (token.value !== ',') return;
var range = [token.range[0] - 1, token.range[1] + 1];
var str = this.source.range(range);
var error = false;
var test = function(char) {
return /\s/.test(char);
};
switch (this.option()) {
case 'after':
if (!test(str[2]) || test(str[0])) error = true;
break;
case 'before':
if (!test(str[0]) || test(str[2])) error = true;
break;
case 'surround':
if (!test(str[0]) || !test(str[2])) error = true;
break;
}
if (error) {
this.badToken(token);
}
};
/**
* Primary export.
*/
module.exports = Comma;