1 | /**
|
2 | * Requires space after opening object curly brace and before closing.
|
3 | *
|
4 | * Types: `String` or `Object`
|
5 | *
|
6 | * Values:
|
7 | * - `String`
|
8 | * - `"all"`: strict mode
|
9 | * - `"allButNested"`: (*deprecated* use Object version with `"allExcept": ['}']`) ignores nested
|
10 | * closing object braces in a row
|
11 | * - `Object`:
|
12 | * - `"allExcept"`: Array specifying list of tokens that can occur after an opening object brace or before a
|
13 | * closing object brace without a space
|
14 | *
|
15 | * #### Example
|
16 | *
|
17 | * ```js
|
18 | * "requireSpacesInsideObjectBrackets": {
|
19 | * "allExcept": [ "}", ")" ]
|
20 | * }
|
21 | * ```
|
22 | * ```js
|
23 | * "requireSpacesInsideObjectBrackets": "all"
|
24 | * ```
|
25 | *
|
26 | * ##### Valid for mode `"all"`
|
27 | *
|
28 | * ```js
|
29 | * var x = { a: { b: 1 } };
|
30 | * ```
|
31 | *
|
32 | * ##### Valid for mode `{ "allExcept": [ "}" ] }` or `"allButNested"`
|
33 | *
|
34 | * ```js
|
35 | * var x = { a: { b: 1 }};
|
36 | * ```
|
37 | *
|
38 | * ##### Valid for mode `"allExcept": [ "}", ")" ]`
|
39 | *
|
40 | * ```js
|
41 | * var x = { a: (b ? 1 : 2)};
|
42 | * var x = { a: { b: 1 }};
|
43 | * ```
|
44 | *
|
45 | * ##### Invalid
|
46 | *
|
47 | * ```js
|
48 | * var x = {a: 1};
|
49 | * ```
|
50 | */
|
51 |
|
52 | var assert = require('assert');
|
53 |
|
54 | module.exports = function() {};
|
55 |
|
56 | module.exports.prototype = {
|
57 |
|
58 | configure: function(value) {
|
59 | var mode;
|
60 | var modes = {
|
61 | 'all': true,
|
62 | 'allButNested': true
|
63 | };
|
64 | var isObject = typeof value === 'object';
|
65 |
|
66 | var error = this.getOptionName() + ' rule' +
|
67 | ' requires string value \'all\' or \'allButNested\' or object';
|
68 |
|
69 | if (typeof value === 'string') {
|
70 | assert(modes[value], error);
|
71 |
|
72 | } else if (isObject) {
|
73 | assert('allExcept' in value, error);
|
74 | } else {
|
75 | assert(false, error);
|
76 | }
|
77 |
|
78 | this._exceptions = {};
|
79 |
|
80 | if (isObject) {
|
81 | (value.allExcept || []).forEach(function(value) {
|
82 | this._exceptions[value] = true;
|
83 | }, this);
|
84 |
|
85 | } else {
|
86 | mode = value;
|
87 | }
|
88 |
|
89 | if (mode === 'allButNested') {
|
90 | this._exceptions['}'] = true;
|
91 | }
|
92 | },
|
93 |
|
94 | getOptionName: function() {
|
95 | return 'requireSpacesInsideObjectBrackets';
|
96 | },
|
97 |
|
98 | check: function(file, errors) {
|
99 | var exceptions = this._exceptions;
|
100 |
|
101 | file.iterateNodesByType(['ObjectExpression', 'ObjectPattern'], function(node) {
|
102 | var openingBracket = node.getFirstToken();
|
103 | var nextToken = file.getNextToken(openingBracket);
|
104 |
|
105 | // Don't check empty object
|
106 | if (nextToken.value === '}') {
|
107 | return;
|
108 | }
|
109 |
|
110 | errors.assert.spacesBetween({
|
111 | token: openingBracket,
|
112 | nextToken: nextToken,
|
113 | exactly: 1,
|
114 | message: 'One space required after opening curly brace'
|
115 | });
|
116 |
|
117 | var closingBracket = file.getLastNodeToken(node);
|
118 | var prevToken = file.getPrevToken(closingBracket);
|
119 |
|
120 | if (prevToken.value in exceptions) {
|
121 | return;
|
122 | }
|
123 |
|
124 | errors.assert.spacesBetween({
|
125 | token: prevToken,
|
126 | nextToken: closingBracket,
|
127 | exactly: 1,
|
128 | message: 'One space required before closing curly brace'
|
129 | });
|
130 | });
|
131 | }
|
132 | };
|