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 | var assert = require('assert');
|
57 |
|
58 | module.exports = function() {};
|
59 |
|
60 | module.exports.prototype = {
|
61 | configure: function(options) {
|
62 | assert(
|
63 | options === true || Array.isArray(options.allExcept),
|
64 | this.getOptionName() + ' option requires a true value or an object of exceptions'
|
65 | );
|
66 |
|
67 | this._isSameLine = false;
|
68 | if (Array.isArray(options.allExcept)) {
|
69 | this._isSameLine = options.allExcept[0] === 'sameLine';
|
70 | }
|
71 | },
|
72 |
|
73 | getOptionName: function() {
|
74 | return 'requireObjectKeysOnNewLine';
|
75 | },
|
76 |
|
77 | check: function(file, errors) {
|
78 | var message = 'Object keys must go on a new line';
|
79 | var isSameLine = this._isSameLine;
|
80 |
|
81 | if (isSameLine) {
|
82 | message = 'Object keys must go on a new line if they aren\'t all on the same line';
|
83 | }
|
84 |
|
85 | file.iterateNodesByType('ObjectExpression', function(node) {
|
86 | var firstKeyToken;
|
87 | var lastValueToken;
|
88 | var lastProp;
|
89 |
|
90 | if (isSameLine) {
|
91 | if (node.properties.length > 1) {
|
92 | firstKeyToken = file.getFirstNodeToken(node.properties[0].key);
|
93 | lastProp = node.properties[node.properties.length - 1];
|
94 | lastValueToken = file.getLastNodeToken(lastProp.value || lastProp.key);
|
95 |
|
96 | if (firstKeyToken.getLoc().end.line === lastValueToken.getLoc().start.line) {
|
97 |
|
98 | return;
|
99 | }
|
100 | }
|
101 | }
|
102 |
|
103 | for (var i = 1; i < node.properties.length; i++) {
|
104 | var prop = node.properties[i - 1];
|
105 | lastValueToken = file.getLastNodeToken(prop.value || prop.body);
|
106 | var comma = file.findNextToken(lastValueToken, 'Punctuator', ',');
|
107 |
|
108 | firstKeyToken = file.getFirstNodeToken(node.properties[i].key);
|
109 |
|
110 | errors.assert.differentLine({
|
111 | token: comma,
|
112 | nextToken: firstKeyToken,
|
113 | message: message
|
114 | });
|
115 | }
|
116 | });
|
117 | }
|
118 | };
|