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 pathrequire-aligned-object-values.js
118 lines (104 loc) · 3.35 KB
/
require-aligned-object-values.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
/**
* Requires proper alignment in object literals.
*
* Type: `String`
*
* Values:
* - `"all"` for strict mode,
* - `"ignoreFunction"` ignores objects if one of the property values is a function expression,
* - `"ignoreLineBreak"` ignores objects if there are line breaks between properties
*
* #### Example
*
* ```js
* "requireAlignedObjectValues": "all"
* ```
*
* ##### Valid
* ```js
* var x = {
* a : 1,
* bcd : 2,
* ef : 'str'
* };
* ```
* ##### Invalid
* ```js
* var x = {
* a : 1,
* bcd : 2,
* ef : 'str'
* };
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(mode) {
var modes = {
'all': 'all',
'ignoreFunction': 'ignoreFunction',
'ignoreLineBreak': 'ignoreLineBreak',
'skipWithFunction': 'ignoreFunction',
'skipWithLineBreak': 'ignoreLineBreak'
};
assert(
typeof mode === 'string' && modes[mode],
this.getOptionName() + ' requires one of the following values: ' + Object.keys(modes).join(', ')
);
this._mode = modes[mode];
},
getOptionName: function() {
return 'requireAlignedObjectValues';
},
check: function(file, errors) {
var mode = this._mode;
file.iterateNodesByType('ObjectExpression', function(node) {
if (node.getNewlineCount() === 0 || node.properties < 2) {
return;
}
var maxKeyEndPos = 0;
var prevKeyEndPos = 0;
var minColonPos = 0;
var tokens = [];
var skip = node.properties.some(function(property, index) {
if (property.shorthand || property.method ||
property.type === 'SpreadProperty') {
return true;
}
if (mode === 'ignoreFunction' && property.value.type === 'FunctionExpression') {
return true;
}
if (mode === 'ignoreLineBreak' && index > 0 &&
node.properties[index - 1].getLoc().end.line !== property.getLoc().start.line - 1) {
return true;
}
prevKeyEndPos = maxKeyEndPos;
maxKeyEndPos = Math.max(maxKeyEndPos, property.key.getLoc().end.column);
var keyToken = file.getFirstNodeToken(property.key);
if (property.computed === true) {
while (keyToken.value !== ']') {
keyToken = file.getNextToken(keyToken);
}
}
var colon = file.getNextToken(keyToken);
if (prevKeyEndPos < maxKeyEndPos) {
minColonPos = colon.getLoc().start.column;
}
tokens.push({key: keyToken, colon: colon});
});
if (skip) {
return;
}
var space = minColonPos - maxKeyEndPos;
tokens.forEach(function(pair) {
errors.assert.spacesBetween({
token: pair.key,
nextToken: pair.colon,
exactly: maxKeyEndPos - pair.key.getLoc().end.column + space,
message: 'Alignment required'
});
});
});
}
};