eslint 0.22.0 → 0.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +111 -95
  3. package/bin/eslint.js +41 -41
  4. package/conf/environments.js +87 -81
  5. package/conf/eslint.json +186 -179
  6. package/lib/api.js +13 -12
  7. package/lib/cli-engine.js +441 -451
  8. package/lib/cli.js +196 -196
  9. package/lib/config-initializer.js +145 -145
  10. package/lib/config-validator.js +110 -110
  11. package/lib/config.js +428 -416
  12. package/lib/eslint.js +1072 -1073
  13. package/lib/file-finder.js +167 -167
  14. package/lib/formatters/checkstyle.js +68 -68
  15. package/lib/formatters/compact.js +53 -53
  16. package/lib/formatters/jslint-xml.js +40 -40
  17. package/lib/formatters/junit.js +63 -63
  18. package/lib/formatters/stylish.js +90 -90
  19. package/lib/formatters/tap.js +86 -86
  20. package/lib/ignored-paths.js +137 -137
  21. package/lib/load-rules.js +39 -39
  22. package/lib/options.js +132 -126
  23. package/lib/rule-context.js +107 -107
  24. package/lib/rules/accessor-pairs.js +65 -65
  25. package/lib/rules/array-bracket-spacing.js +180 -0
  26. package/lib/rules/block-scoped-var.js +339 -320
  27. package/lib/rules/brace-style.js +228 -228
  28. package/lib/rules/camelcase.js +111 -111
  29. package/lib/rules/comma-dangle.js +67 -64
  30. package/lib/rules/comma-spacing.js +191 -191
  31. package/lib/rules/comma-style.js +195 -195
  32. package/lib/rules/complexity.js +94 -94
  33. package/lib/rules/computed-property-spacing.js +144 -0
  34. package/lib/rules/consistent-return.js +75 -75
  35. package/lib/rules/consistent-this.js +119 -119
  36. package/lib/rules/constructor-super.js +108 -0
  37. package/lib/rules/curly.js +109 -109
  38. package/lib/rules/default-case.js +66 -66
  39. package/lib/rules/dot-location.js +63 -63
  40. package/lib/rules/dot-notation.js +119 -119
  41. package/lib/rules/eol-last.js +38 -38
  42. package/lib/rules/eqeqeq.js +96 -96
  43. package/lib/rules/func-names.js +45 -45
  44. package/lib/rules/func-style.js +49 -49
  45. package/lib/rules/generator-star-spacing.js +104 -87
  46. package/lib/rules/generator-star.js +76 -76
  47. package/lib/rules/global-strict.js +49 -49
  48. package/lib/rules/guard-for-in.js +32 -32
  49. package/lib/rules/handle-callback-err.js +81 -124
  50. package/lib/rules/indent.js +486 -486
  51. package/lib/rules/key-spacing.js +325 -325
  52. package/lib/rules/linebreak-style.js +44 -44
  53. package/lib/rules/lines-around-comment.js +228 -160
  54. package/lib/rules/max-depth.js +89 -89
  55. package/lib/rules/max-len.js +76 -76
  56. package/lib/rules/max-nested-callbacks.js +73 -73
  57. package/lib/rules/max-params.js +45 -45
  58. package/lib/rules/max-statements.js +61 -61
  59. package/lib/rules/new-cap.js +224 -224
  60. package/lib/rules/new-parens.js +29 -29
  61. package/lib/rules/newline-after-var.js +127 -127
  62. package/lib/rules/no-alert.js +153 -153
  63. package/lib/rules/no-array-constructor.js +31 -31
  64. package/lib/rules/no-bitwise.js +57 -57
  65. package/lib/rules/no-caller.js +29 -29
  66. package/lib/rules/no-catch-shadow.js +52 -52
  67. package/lib/rules/no-comma-dangle.js +45 -45
  68. package/lib/rules/no-cond-assign.js +123 -123
  69. package/lib/rules/no-console.js +27 -27
  70. package/lib/rules/no-constant-condition.js +73 -73
  71. package/lib/rules/no-continue.js +23 -23
  72. package/lib/rules/no-control-regex.js +58 -58
  73. package/lib/rules/no-debugger.js +22 -22
  74. package/lib/rules/no-delete-var.js +25 -25
  75. package/lib/rules/no-div-regex.js +27 -27
  76. package/lib/rules/no-dupe-args.js +89 -85
  77. package/lib/rules/no-dupe-keys.js +43 -43
  78. package/lib/rules/no-duplicate-case.js +67 -67
  79. package/lib/rules/no-else-return.js +125 -125
  80. package/lib/rules/no-empty-character-class.js +43 -43
  81. package/lib/rules/no-empty-class.js +45 -45
  82. package/lib/rules/no-empty-label.js +27 -27
  83. package/lib/rules/no-empty.js +49 -49
  84. package/lib/rules/no-eq-null.js +29 -29
  85. package/lib/rules/no-eval.js +26 -26
  86. package/lib/rules/no-ex-assign.js +42 -42
  87. package/lib/rules/no-extend-native.js +103 -103
  88. package/lib/rules/no-extra-bind.js +81 -81
  89. package/lib/rules/no-extra-boolean-cast.js +71 -71
  90. package/lib/rules/no-extra-parens.js +368 -355
  91. package/lib/rules/no-extra-semi.js +70 -23
  92. package/lib/rules/no-extra-strict.js +86 -86
  93. package/lib/rules/no-fallthrough.js +97 -97
  94. package/lib/rules/no-floating-decimal.js +30 -30
  95. package/lib/rules/no-func-assign.js +83 -83
  96. package/lib/rules/no-implied-eval.js +76 -76
  97. package/lib/rules/no-inline-comments.js +49 -49
  98. package/lib/rules/no-inner-declarations.js +78 -78
  99. package/lib/rules/no-invalid-regexp.js +53 -53
  100. package/lib/rules/no-irregular-whitespace.js +135 -135
  101. package/lib/rules/no-iterator.js +28 -28
  102. package/lib/rules/no-label-var.js +64 -64
  103. package/lib/rules/no-labels.js +44 -44
  104. package/lib/rules/no-lone-blocks.js +106 -27
  105. package/lib/rules/no-lonely-if.js +30 -30
  106. package/lib/rules/no-loop-func.js +58 -58
  107. package/lib/rules/no-mixed-requires.js +165 -165
  108. package/lib/rules/no-mixed-spaces-and-tabs.js +74 -74
  109. package/lib/rules/no-multi-spaces.js +119 -119
  110. package/lib/rules/no-multi-str.js +43 -43
  111. package/lib/rules/no-multiple-empty-lines.js +98 -98
  112. package/lib/rules/no-native-reassign.js +62 -62
  113. package/lib/rules/no-negated-in-lhs.js +25 -25
  114. package/lib/rules/no-nested-ternary.js +24 -24
  115. package/lib/rules/no-new-func.js +25 -25
  116. package/lib/rules/no-new-object.js +25 -25
  117. package/lib/rules/no-new-require.js +25 -25
  118. package/lib/rules/no-new-wrappers.js +26 -26
  119. package/lib/rules/no-new.js +27 -27
  120. package/lib/rules/no-obj-calls.js +28 -28
  121. package/lib/rules/no-octal-escape.js +39 -39
  122. package/lib/rules/no-octal.js +25 -25
  123. package/lib/rules/no-param-reassign.js +87 -87
  124. package/lib/rules/no-path-concat.js +39 -39
  125. package/lib/rules/no-plusplus.js +24 -24
  126. package/lib/rules/no-process-env.js +30 -30
  127. package/lib/rules/no-process-exit.js +33 -33
  128. package/lib/rules/no-proto.js +28 -28
  129. package/lib/rules/no-redeclare.js +68 -68
  130. package/lib/rules/no-regex-spaces.js +35 -35
  131. package/lib/rules/no-reserved-keys.js +56 -56
  132. package/lib/rules/no-restricted-modules.js +85 -85
  133. package/lib/rules/no-return-assign.js +53 -24
  134. package/lib/rules/no-script-url.js +34 -34
  135. package/lib/rules/no-self-compare.js +29 -29
  136. package/lib/rules/no-sequences.js +94 -94
  137. package/lib/rules/no-shadow-restricted-names.js +51 -51
  138. package/lib/rules/no-shadow.js +181 -136
  139. package/lib/rules/no-space-before-semi.js +98 -98
  140. package/lib/rules/no-spaced-func.js +37 -37
  141. package/lib/rules/no-sparse-arrays.js +33 -33
  142. package/lib/rules/no-sync.js +30 -30
  143. package/lib/rules/no-ternary.js +24 -24
  144. package/lib/rules/no-this-before-super.js +144 -0
  145. package/lib/rules/no-throw-literal.js +33 -33
  146. package/lib/rules/no-trailing-spaces.js +74 -63
  147. package/lib/rules/no-undef-init.js +28 -28
  148. package/lib/rules/no-undef.js +92 -92
  149. package/lib/rules/no-undefined.js +27 -27
  150. package/lib/rules/no-underscore-dangle.js +73 -73
  151. package/lib/rules/no-unexpected-multiline.js +58 -0
  152. package/lib/rules/no-unneeded-ternary.js +48 -48
  153. package/lib/rules/no-unreachable.js +98 -98
  154. package/lib/rules/no-unused-expressions.js +76 -76
  155. package/lib/rules/no-unused-vars.js +252 -250
  156. package/lib/rules/no-use-before-define.js +105 -105
  157. package/lib/rules/no-var.js +26 -26
  158. package/lib/rules/no-void.js +28 -28
  159. package/lib/rules/no-warning-comments.js +102 -102
  160. package/lib/rules/no-with.js +22 -22
  161. package/lib/rules/no-wrap-func.js +65 -65
  162. package/lib/rules/object-curly-spacing.js +231 -206
  163. package/lib/rules/object-shorthand.js +74 -73
  164. package/lib/rules/one-var.js +311 -304
  165. package/lib/rules/operator-assignment.js +118 -118
  166. package/lib/rules/operator-linebreak.js +114 -114
  167. package/lib/rules/padded-blocks.js +98 -98
  168. package/lib/rules/prefer-const.js +91 -0
  169. package/lib/rules/quote-props.js +72 -72
  170. package/lib/rules/quotes.js +92 -92
  171. package/lib/rules/radix.js +41 -41
  172. package/lib/rules/semi-spacing.js +167 -167
  173. package/lib/rules/semi.js +136 -136
  174. package/lib/rules/sort-vars.js +49 -49
  175. package/lib/rules/space-after-function-name.js +49 -49
  176. package/lib/rules/space-after-keywords.js +82 -82
  177. package/lib/rules/space-before-blocks.js +91 -91
  178. package/lib/rules/space-before-function-paren.js +139 -139
  179. package/lib/rules/space-before-function-parentheses.js +139 -139
  180. package/lib/rules/space-in-brackets.js +305 -305
  181. package/lib/rules/space-in-parens.js +281 -281
  182. package/lib/rules/space-infix-ops.js +106 -106
  183. package/lib/rules/space-return-throw-case.js +38 -38
  184. package/lib/rules/space-unary-ops.js +124 -133
  185. package/lib/rules/spaced-comment.js +143 -0
  186. package/lib/rules/spaced-line-comment.js +89 -89
  187. package/lib/rules/strict.js +242 -242
  188. package/lib/rules/use-isnan.js +26 -26
  189. package/lib/rules/valid-jsdoc.js +215 -215
  190. package/lib/rules/valid-typeof.js +42 -42
  191. package/lib/rules/vars-on-top.js +115 -115
  192. package/lib/rules/wrap-iife.js +48 -48
  193. package/lib/rules/wrap-regex.js +38 -38
  194. package/lib/rules/yoda.js +242 -225
  195. package/lib/rules.js +88 -88
  196. package/lib/timing.js +109 -109
  197. package/lib/token-store.js +201 -201
  198. package/lib/util/traverse.js +105 -105
  199. package/lib/util.js +125 -85
  200. package/package.json +6 -6
  201. package/CHANGELOG.md +0 -1638
@@ -1,325 +1,325 @@
1
- /**
2
- * @fileoverview Rule to specify spacing of object literal keys and values
3
- * @author Brandon Mills
4
- * @copyright 2014 Brandon Mills. All rights reserved.
5
- */
6
- "use strict";
7
-
8
- //------------------------------------------------------------------------------
9
- // Helpers
10
- //------------------------------------------------------------------------------
11
-
12
- /**
13
- * Checks whether a string contains a line terminator as defined in
14
- * http://www.ecma-international.org/ecma-262/5.1/#sec-7.3
15
- * @param {string} str String to test.
16
- * @returns {boolean} True if str contains a line terminator.
17
- */
18
- function containsLineTerminator(str) {
19
- return /[\n\r\u2028\u2029]/.test(str);
20
- }
21
-
22
- /**
23
- * Gets the last element of an array.
24
- * @param {Array} arr An array.
25
- * @returns {any} Last element of arr.
26
- */
27
- function last(arr) {
28
- return arr[arr.length - 1];
29
- }
30
-
31
- /**
32
- * Checks whether a property is a member of the property group it follows.
33
- * @param {ASTNode} lastMember The last Property known to be in the group.
34
- * @param {ASTNode} candidate The next Property that might be in the group.
35
- * @returns {boolean} True if the candidate property is part of the group.
36
- */
37
- function continuesPropertyGroup(lastMember, candidate) {
38
- var groupEndLine = lastMember.loc.start.line,
39
- candidateStartLine = candidate.loc.start.line,
40
- comments, i;
41
-
42
- if (candidateStartLine - groupEndLine <= 1) {
43
- return true;
44
- }
45
-
46
- // Check that the first comment is adjacent to the end of the group, the
47
- // last comment is adjacent to the candidate property, and that successive
48
- // comments are adjacent to each other.
49
- comments = candidate.leadingComments;
50
- if (
51
- comments &&
52
- comments[0].loc.start.line - groupEndLine <= 1 &&
53
- candidateStartLine - last(comments).loc.end.line <= 1
54
- ) {
55
- for (i = 1; i < comments.length; i++) {
56
- if (comments[i].loc.start.line - comments[i - 1].loc.end.line > 1) {
57
- return false;
58
- }
59
- }
60
- return true;
61
- }
62
-
63
- return false;
64
- }
65
-
66
- /**
67
- * Checks whether a node is contained on a single line.
68
- * @param {ASTNode} node AST Node being evaluated.
69
- * @returns {boolean} True if the node is a single line.
70
- */
71
- function isSingleLine(node) {
72
- return (node.loc.end.line === node.loc.start.line);
73
- }
74
-
75
- //------------------------------------------------------------------------------
76
- // Rule Definition
77
- //------------------------------------------------------------------------------
78
-
79
- var messages = {
80
- key: "{{error}} space after {{computed}}key \"{{key}}\".",
81
- value: "{{error}} space before value for {{computed}}key \"{{key}}\"."
82
- };
83
-
84
- module.exports = function(context) {
85
-
86
- /**
87
- * OPTIONS
88
- * "key-spacing": [2, {
89
- * beforeColon: false,
90
- * afterColon: true,
91
- * align: "colon" // Optional, or "value"
92
- * }
93
- */
94
-
95
- var options = context.options[0] || {},
96
- align = options.align,
97
- beforeColon = +!!options.beforeColon, // Defaults to false
98
- afterColon = +!(options.afterColon === false); // Defaults to true
99
-
100
- /**
101
- * Gets an object literal property's key as the identifier name or string value.
102
- * @param {ASTNode} property Property node whose key to retrieve.
103
- * @returns {string} The property's key.
104
- */
105
- function getKey(property) {
106
- var key = property.key;
107
-
108
- if (property.computed) {
109
- return context.getSource().slice(key.range[0], key.range[1]);
110
- }
111
-
112
- return property.key.name || property.key.value;
113
- }
114
-
115
- /**
116
- * Reports an appropriately-formatted error if spacing is incorrect on one
117
- * side of the colon.
118
- * @param {ASTNode} property Key-value pair in an object literal.
119
- * @param {string} side Side being verified - either "key" or "value".
120
- * @param {string} whitespace Actual whitespace string.
121
- * @param {int} expected Expected whitespace length.
122
- * @returns {void}
123
- */
124
- function report(property, side, whitespace, expected) {
125
- var diff = whitespace.length - expected,
126
- key = property.key,
127
- firstTokenAfterColon = context.getTokenAfter(key, 1),
128
- location = side === "key" ? key.loc.start : firstTokenAfterColon.loc.start;
129
-
130
- if (diff && !(expected && containsLineTerminator(whitespace))) {
131
- context.report(property[side], location, messages[side], {
132
- error: diff > 0 ? "Extra" : "Missing",
133
- computed: property.computed ? "computed " : "",
134
- key: getKey(property)
135
- });
136
- }
137
- }
138
-
139
- /**
140
- * Gets the number of characters in a key, including quotes around string
141
- * keys and braces around computed property keys.
142
- * @param {ASTNode} property Property of on object literal.
143
- * @returns {int} Width of the key.
144
- */
145
- function getKeyWidth(property) {
146
- var key = property.key,
147
- startToken, endToken;
148
-
149
- // [computed]: value
150
- if (property.computed) {
151
- startToken = context.getTokenBefore(key);
152
- endToken = context.getTokenAfter(key);
153
- return endToken.range[1] - startToken.range[0];
154
- }
155
-
156
- // name: value
157
- if (key.type === "Identifier") {
158
- return key.name.length;
159
- }
160
-
161
- // "literal": value
162
- // 42: value
163
- if (key.type === "Literal") {
164
- return key.raw.length;
165
- }
166
- }
167
-
168
- /**
169
- * Gets the whitespace around the colon in an object literal property.
170
- * @param {ASTNode} property Property node from an object literal.
171
- * @returns {Object} Whitespace before and after the property's colon.
172
- */
173
- function getPropertyWhitespace(property) {
174
- var whitespace = /(\s*):(\s*)/.exec(context.getSource().slice(
175
- property.key.range[1], property.value.range[0]
176
- ));
177
-
178
- if (whitespace) {
179
- return {
180
- beforeColon: whitespace[1],
181
- afterColon: whitespace[2]
182
- };
183
- }
184
- }
185
-
186
- /**
187
- * Creates groups of properties.
188
- * @param {ASTNode} node ObjectExpression node being evaluated.
189
- * @returns {Array.<ASTNode[]>} Groups of property AST node lists.
190
- */
191
- function createGroups(node) {
192
- if (node.properties.length === 1) {
193
- return [node.properties];
194
- }
195
-
196
- return node.properties.reduce(function(groups, property) {
197
- var currentGroup = last(groups),
198
- prev = last(currentGroup);
199
-
200
- if (!prev || continuesPropertyGroup(prev, property)) {
201
- currentGroup.push(property);
202
- } else {
203
- groups.push([property]);
204
- }
205
-
206
- return groups;
207
- }, [[]]);
208
- }
209
-
210
- /**
211
- * Verifies correct vertical alignment of a group of properties.
212
- * @param {ASTNode[]} properties List of Property AST nodes.
213
- * @returns {void}
214
- */
215
- function verifyGroupAlignment(properties) {
216
- var length = properties.length,
217
- widths = properties.map(getKeyWidth), // Width of keys, including quotes
218
- targetWidth = Math.max.apply(null, widths),
219
- i, property, whitespace, width;
220
-
221
- // Conditionally include one space before or after colon
222
- targetWidth += (align === "colon" ? beforeColon : afterColon);
223
-
224
- for (i = 0; i < length; i++) {
225
- property = properties[i];
226
- whitespace = getPropertyWhitespace(property);
227
-
228
- if (!whitespace) {
229
- continue; // Object literal getters/setters lack a colon
230
- }
231
-
232
- width = widths[i];
233
-
234
- if (align === "value") {
235
- report(property, "key", whitespace.beforeColon, beforeColon);
236
- report(property, "value", whitespace.afterColon, targetWidth - width);
237
- } else { // align = "colon"
238
- report(property, "key", whitespace.beforeColon, targetWidth - width);
239
- report(property, "value", whitespace.afterColon, afterColon);
240
- }
241
- }
242
- }
243
-
244
- /**
245
- * Verifies vertical alignment, taking into account groups of properties.
246
- * @param {ASTNode} node ObjectExpression node being evaluated.
247
- * @returns {void}
248
- */
249
- function verifyAlignment(node) {
250
- createGroups(node).forEach(function(group) {
251
- verifyGroupAlignment(group);
252
- });
253
- }
254
-
255
- /**
256
- * Verifies spacing of property conforms to specified options.
257
- * @param {ASTNode} node Property node being evaluated.
258
- * @returns {void}
259
- */
260
- function verifySpacing(node) {
261
- var whitespace = getPropertyWhitespace(node);
262
- if (whitespace) { // Object literal getters/setters lack colons
263
- report(node, "key", whitespace.beforeColon, beforeColon);
264
- report(node, "value", whitespace.afterColon, afterColon);
265
- }
266
- }
267
-
268
- /**
269
- * Verifies spacing of each property in a list.
270
- * @param {ASTNode[]} properties List of Property AST nodes.
271
- * @returns {void}
272
- */
273
- function verifyListSpacing(properties) {
274
- var length = properties.length;
275
-
276
- for (var i = 0; i < length; i++) {
277
- verifySpacing(properties[i]);
278
- }
279
- }
280
-
281
- //--------------------------------------------------------------------------
282
- // Public API
283
- //--------------------------------------------------------------------------
284
-
285
- if (align) { // Verify vertical alignment
286
-
287
- return {
288
- "ObjectExpression": function(node) {
289
- if (isSingleLine(node)) {
290
- verifyListSpacing(node.properties);
291
- } else {
292
- verifyAlignment(node);
293
- }
294
- }
295
- };
296
-
297
- } else { // Strictly obey beforeColon and afterColon in each property
298
-
299
- return {
300
- "Property": function (node) {
301
- verifySpacing(node);
302
- }
303
- };
304
-
305
- }
306
-
307
- };
308
-
309
- module.exports.schema = [
310
- {
311
- "type": "object",
312
- "properties": {
313
- "align": {
314
- "enum": ["colon", "value"]
315
- },
316
- "beforeColon": {
317
- "type": "boolean"
318
- },
319
- "afterColon": {
320
- "type": "boolean"
321
- }
322
- },
323
- "additionalProperties": false
324
- }
325
- ];
1
+ /**
2
+ * @fileoverview Rule to specify spacing of object literal keys and values
3
+ * @author Brandon Mills
4
+ * @copyright 2014 Brandon Mills. All rights reserved.
5
+ */
6
+ "use strict";
7
+
8
+ //------------------------------------------------------------------------------
9
+ // Helpers
10
+ //------------------------------------------------------------------------------
11
+
12
+ /**
13
+ * Checks whether a string contains a line terminator as defined in
14
+ * http://www.ecma-international.org/ecma-262/5.1/#sec-7.3
15
+ * @param {string} str String to test.
16
+ * @returns {boolean} True if str contains a line terminator.
17
+ */
18
+ function containsLineTerminator(str) {
19
+ return /[\n\r\u2028\u2029]/.test(str);
20
+ }
21
+
22
+ /**
23
+ * Gets the last element of an array.
24
+ * @param {Array} arr An array.
25
+ * @returns {any} Last element of arr.
26
+ */
27
+ function last(arr) {
28
+ return arr[arr.length - 1];
29
+ }
30
+
31
+ /**
32
+ * Checks whether a property is a member of the property group it follows.
33
+ * @param {ASTNode} lastMember The last Property known to be in the group.
34
+ * @param {ASTNode} candidate The next Property that might be in the group.
35
+ * @returns {boolean} True if the candidate property is part of the group.
36
+ */
37
+ function continuesPropertyGroup(lastMember, candidate) {
38
+ var groupEndLine = lastMember.loc.start.line,
39
+ candidateStartLine = candidate.loc.start.line,
40
+ comments, i;
41
+
42
+ if (candidateStartLine - groupEndLine <= 1) {
43
+ return true;
44
+ }
45
+
46
+ // Check that the first comment is adjacent to the end of the group, the
47
+ // last comment is adjacent to the candidate property, and that successive
48
+ // comments are adjacent to each other.
49
+ comments = candidate.leadingComments;
50
+ if (
51
+ comments &&
52
+ comments[0].loc.start.line - groupEndLine <= 1 &&
53
+ candidateStartLine - last(comments).loc.end.line <= 1
54
+ ) {
55
+ for (i = 1; i < comments.length; i++) {
56
+ if (comments[i].loc.start.line - comments[i - 1].loc.end.line > 1) {
57
+ return false;
58
+ }
59
+ }
60
+ return true;
61
+ }
62
+
63
+ return false;
64
+ }
65
+
66
+ /**
67
+ * Checks whether a node is contained on a single line.
68
+ * @param {ASTNode} node AST Node being evaluated.
69
+ * @returns {boolean} True if the node is a single line.
70
+ */
71
+ function isSingleLine(node) {
72
+ return (node.loc.end.line === node.loc.start.line);
73
+ }
74
+
75
+ //------------------------------------------------------------------------------
76
+ // Rule Definition
77
+ //------------------------------------------------------------------------------
78
+
79
+ var messages = {
80
+ key: "{{error}} space after {{computed}}key \"{{key}}\".",
81
+ value: "{{error}} space before value for {{computed}}key \"{{key}}\"."
82
+ };
83
+
84
+ module.exports = function(context) {
85
+
86
+ /**
87
+ * OPTIONS
88
+ * "key-spacing": [2, {
89
+ * beforeColon: false,
90
+ * afterColon: true,
91
+ * align: "colon" // Optional, or "value"
92
+ * }
93
+ */
94
+
95
+ var options = context.options[0] || {},
96
+ align = options.align,
97
+ beforeColon = +!!options.beforeColon, // Defaults to false
98
+ afterColon = +!(options.afterColon === false); // Defaults to true
99
+
100
+ /**
101
+ * Gets an object literal property's key as the identifier name or string value.
102
+ * @param {ASTNode} property Property node whose key to retrieve.
103
+ * @returns {string} The property's key.
104
+ */
105
+ function getKey(property) {
106
+ var key = property.key;
107
+
108
+ if (property.computed) {
109
+ return context.getSource().slice(key.range[0], key.range[1]);
110
+ }
111
+
112
+ return property.key.name || property.key.value;
113
+ }
114
+
115
+ /**
116
+ * Reports an appropriately-formatted error if spacing is incorrect on one
117
+ * side of the colon.
118
+ * @param {ASTNode} property Key-value pair in an object literal.
119
+ * @param {string} side Side being verified - either "key" or "value".
120
+ * @param {string} whitespace Actual whitespace string.
121
+ * @param {int} expected Expected whitespace length.
122
+ * @returns {void}
123
+ */
124
+ function report(property, side, whitespace, expected) {
125
+ var diff = whitespace.length - expected,
126
+ key = property.key,
127
+ firstTokenAfterColon = context.getTokenAfter(key, 1),
128
+ location = side === "key" ? key.loc.start : firstTokenAfterColon.loc.start;
129
+
130
+ if (diff && !(expected && containsLineTerminator(whitespace))) {
131
+ context.report(property[side], location, messages[side], {
132
+ error: diff > 0 ? "Extra" : "Missing",
133
+ computed: property.computed ? "computed " : "",
134
+ key: getKey(property)
135
+ });
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Gets the number of characters in a key, including quotes around string
141
+ * keys and braces around computed property keys.
142
+ * @param {ASTNode} property Property of on object literal.
143
+ * @returns {int} Width of the key.
144
+ */
145
+ function getKeyWidth(property) {
146
+ var key = property.key,
147
+ startToken, endToken;
148
+
149
+ // [computed]: value
150
+ if (property.computed) {
151
+ startToken = context.getTokenBefore(key);
152
+ endToken = context.getTokenAfter(key);
153
+ return endToken.range[1] - startToken.range[0];
154
+ }
155
+
156
+ // name: value
157
+ if (key.type === "Identifier") {
158
+ return key.name.length;
159
+ }
160
+
161
+ // "literal": value
162
+ // 42: value
163
+ if (key.type === "Literal") {
164
+ return key.raw.length;
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Gets the whitespace around the colon in an object literal property.
170
+ * @param {ASTNode} property Property node from an object literal.
171
+ * @returns {Object} Whitespace before and after the property's colon.
172
+ */
173
+ function getPropertyWhitespace(property) {
174
+ var whitespace = /(\s*):(\s*)/.exec(context.getSource().slice(
175
+ property.key.range[1], property.value.range[0]
176
+ ));
177
+
178
+ if (whitespace) {
179
+ return {
180
+ beforeColon: whitespace[1],
181
+ afterColon: whitespace[2]
182
+ };
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Creates groups of properties.
188
+ * @param {ASTNode} node ObjectExpression node being evaluated.
189
+ * @returns {Array.<ASTNode[]>} Groups of property AST node lists.
190
+ */
191
+ function createGroups(node) {
192
+ if (node.properties.length === 1) {
193
+ return [node.properties];
194
+ }
195
+
196
+ return node.properties.reduce(function(groups, property) {
197
+ var currentGroup = last(groups),
198
+ prev = last(currentGroup);
199
+
200
+ if (!prev || continuesPropertyGroup(prev, property)) {
201
+ currentGroup.push(property);
202
+ } else {
203
+ groups.push([property]);
204
+ }
205
+
206
+ return groups;
207
+ }, [[]]);
208
+ }
209
+
210
+ /**
211
+ * Verifies correct vertical alignment of a group of properties.
212
+ * @param {ASTNode[]} properties List of Property AST nodes.
213
+ * @returns {void}
214
+ */
215
+ function verifyGroupAlignment(properties) {
216
+ var length = properties.length,
217
+ widths = properties.map(getKeyWidth), // Width of keys, including quotes
218
+ targetWidth = Math.max.apply(null, widths),
219
+ i, property, whitespace, width;
220
+
221
+ // Conditionally include one space before or after colon
222
+ targetWidth += (align === "colon" ? beforeColon : afterColon);
223
+
224
+ for (i = 0; i < length; i++) {
225
+ property = properties[i];
226
+ whitespace = getPropertyWhitespace(property);
227
+
228
+ if (!whitespace) {
229
+ continue; // Object literal getters/setters lack a colon
230
+ }
231
+
232
+ width = widths[i];
233
+
234
+ if (align === "value") {
235
+ report(property, "key", whitespace.beforeColon, beforeColon);
236
+ report(property, "value", whitespace.afterColon, targetWidth - width);
237
+ } else { // align = "colon"
238
+ report(property, "key", whitespace.beforeColon, targetWidth - width);
239
+ report(property, "value", whitespace.afterColon, afterColon);
240
+ }
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Verifies vertical alignment, taking into account groups of properties.
246
+ * @param {ASTNode} node ObjectExpression node being evaluated.
247
+ * @returns {void}
248
+ */
249
+ function verifyAlignment(node) {
250
+ createGroups(node).forEach(function(group) {
251
+ verifyGroupAlignment(group);
252
+ });
253
+ }
254
+
255
+ /**
256
+ * Verifies spacing of property conforms to specified options.
257
+ * @param {ASTNode} node Property node being evaluated.
258
+ * @returns {void}
259
+ */
260
+ function verifySpacing(node) {
261
+ var whitespace = getPropertyWhitespace(node);
262
+ if (whitespace) { // Object literal getters/setters lack colons
263
+ report(node, "key", whitespace.beforeColon, beforeColon);
264
+ report(node, "value", whitespace.afterColon, afterColon);
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Verifies spacing of each property in a list.
270
+ * @param {ASTNode[]} properties List of Property AST nodes.
271
+ * @returns {void}
272
+ */
273
+ function verifyListSpacing(properties) {
274
+ var length = properties.length;
275
+
276
+ for (var i = 0; i < length; i++) {
277
+ verifySpacing(properties[i]);
278
+ }
279
+ }
280
+
281
+ //--------------------------------------------------------------------------
282
+ // Public API
283
+ //--------------------------------------------------------------------------
284
+
285
+ if (align) { // Verify vertical alignment
286
+
287
+ return {
288
+ "ObjectExpression": function(node) {
289
+ if (isSingleLine(node)) {
290
+ verifyListSpacing(node.properties);
291
+ } else {
292
+ verifyAlignment(node);
293
+ }
294
+ }
295
+ };
296
+
297
+ } else { // Strictly obey beforeColon and afterColon in each property
298
+
299
+ return {
300
+ "Property": function (node) {
301
+ verifySpacing(node);
302
+ }
303
+ };
304
+
305
+ }
306
+
307
+ };
308
+
309
+ module.exports.schema = [
310
+ {
311
+ "type": "object",
312
+ "properties": {
313
+ "align": {
314
+ "enum": ["colon", "value"]
315
+ },
316
+ "beforeColon": {
317
+ "type": "boolean"
318
+ },
319
+ "afterColon": {
320
+ "type": "boolean"
321
+ }
322
+ },
323
+ "additionalProperties": false
324
+ }
325
+ ];