UNPKG

1.61 kBJavaScriptView Raw
1/**
2 * Requires space after object keys.
3 *
4 * Type: `Boolean`
5 *
6 * Value: `true`
7 *
8 * #### Example
9 *
10 * ```js
11 * "requireSpaceAfterObjectKeys": true
12 * ```
13 *
14 * ##### Valid
15 * ```js
16 * var x = {a : 1};
17 * ```
18 * ##### Invalid
19 * ```js
20 * var x = {a: 1};
21 * ```
22 */
23
24var assert = require('assert');
25
26module.exports = function() {};
27
28module.exports.prototype = {
29
30 configure: function(options) {
31 assert(
32 options === true,
33 this.getOptionName() + ' option requires a true value or should be removed'
34 );
35 },
36
37 getOptionName: function() {
38 return 'requireSpaceAfterObjectKeys';
39 },
40
41 check: function(file, errors) {
42 file.iterateNodesByType('ObjectExpression', function(node) {
43 node.properties.forEach(function(property) {
44 if (
45 property.type === 'ObjectMethod' &&
46 (property.kind === 'get' || property.kind === 'set')
47 ) {
48 return;
49 }
50
51 if (property.shorthand ||
52 property.type === 'SpreadProperty') {
53 return;
54 }
55
56 var token = file.getLastNodeToken(property.key);
57
58 if (property.computed === true) {
59 token = file.getNextToken(token);
60 }
61
62 errors.assert.whitespaceBetween({
63 token: token,
64 nextToken: file.getNextToken(token),
65 message: 'Missing space after key'
66 });
67 });
68 });
69 }
70
71};