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 | var assert = require('assert');
|
49 |
|
50 | module.exports = function() {};
|
51 |
|
52 | module.exports.prototype = {
|
53 | configure: function(options) {
|
54 | this._exceptions = {
|
55 | 'shorthand': false
|
56 | };
|
57 |
|
58 | assert(
|
59 | typeof options === 'object',
|
60 | this.getOptionName() + ' option must be the object'
|
61 | );
|
62 |
|
63 | if ('beforeOpeningRoundBrace' in options) {
|
64 | assert(
|
65 | options.beforeOpeningRoundBrace === true,
|
66 | this.getOptionName() + '.beforeOpeningRoundBrace ' +
|
67 | 'property requires true value or should be removed'
|
68 | );
|
69 | }
|
70 |
|
71 | if ('beforeOpeningCurlyBrace' in options) {
|
72 | assert(
|
73 | options.beforeOpeningCurlyBrace === true,
|
74 | this.getOptionName() + '.beforeOpeningCurlyBrace ' +
|
75 | 'property requires true value or should be removed'
|
76 | );
|
77 | }
|
78 |
|
79 | if ('allExcept' in options) {
|
80 | if (typeof options.allExcept === 'object') {
|
81 | assert(
|
82 | Array.isArray(options.allExcept),
|
83 | this.getOptionName() + ' option requires "allExcept" to be ' +
|
84 | 'an array'
|
85 | );
|
86 | assert(
|
87 | options.allExcept.length > 0,
|
88 | this.getOptionName() + ' option requires "allExcept" to have ' +
|
89 | 'at least one item or be set to `true`'
|
90 | );
|
91 | options.allExcept.forEach(function(except) {
|
92 | if (except === 'shorthand') {
|
93 | this._exceptions.shorthand = true;
|
94 | } else {
|
95 | assert(false, this.getOptionName() + ' option requires ' +
|
96 | '"allExcept" to be an array containing "shorthand"');
|
97 | }
|
98 | }, this);
|
99 | } else {
|
100 | assert(
|
101 | options.allExcept === true,
|
102 | this.getOptionName() + ' option requires a true value or array'
|
103 | );
|
104 | this._exceptions.shorthand = true;
|
105 | }
|
106 | }
|
107 |
|
108 | assert(
|
109 | options.beforeOpeningCurlyBrace || options.beforeOpeningRoundBrace,
|
110 | this.getOptionName() + ' must have beforeOpeningCurlyBrace ' +
|
111 | ' or beforeOpeningRoundBrace property'
|
112 | );
|
113 |
|
114 | this._beforeOpeningRoundBrace = Boolean(options.beforeOpeningRoundBrace);
|
115 | this._beforeOpeningCurlyBrace = Boolean(options.beforeOpeningCurlyBrace);
|
116 | },
|
117 |
|
118 | getOptionName: function() {
|
119 | return 'requireSpacesInAnonymousFunctionExpression';
|
120 | },
|
121 |
|
122 | check: function(file, errors) {
|
123 | var beforeOpeningRoundBrace = this._beforeOpeningRoundBrace;
|
124 | var beforeOpeningCurlyBrace = this._beforeOpeningCurlyBrace;
|
125 | var exceptions = this._exceptions;
|
126 |
|
127 | file.iterateNodesByType(['FunctionExpression'], function(node) {
|
128 | var functionNode = node;
|
129 | var parent = node.parentElement;
|
130 |
|
131 |
|
132 | if (parent.type === 'Property' && (parent.kind === 'get' || parent.kind === 'set')) {
|
133 | return;
|
134 | }
|
135 |
|
136 |
|
137 | if (parent.method || parent.type === 'MethodDefinition') {
|
138 | functionNode = parent.key;
|
139 | if (exceptions.shorthand) {
|
140 | return;
|
141 | }
|
142 | }
|
143 |
|
144 |
|
145 | if (node.id) {
|
146 | return;
|
147 | }
|
148 |
|
149 | if (beforeOpeningRoundBrace) {
|
150 | var functionToken = file.getFirstNodeToken(functionNode);
|
151 | if (node.async && functionToken.value === 'async') {
|
152 | functionToken = file.getNextToken(functionToken);
|
153 | }
|
154 |
|
155 | if (node.generator && functionToken.value === 'function') {
|
156 | functionToken = file.getNextToken(functionToken);
|
157 | }
|
158 | errors.assert.whitespaceBetween({
|
159 | token: functionToken,
|
160 | nextToken: file.getNextToken(functionToken),
|
161 | message: 'Missing space before opening round brace'
|
162 | });
|
163 | }
|
164 |
|
165 | if (beforeOpeningCurlyBrace) {
|
166 | var bodyToken = file.getFirstNodeToken(node.body);
|
167 | errors.assert.whitespaceBetween({
|
168 | token: file.getPrevToken(bodyToken),
|
169 | nextToken: bodyToken,
|
170 | message: 'Missing space before opening curly brace'
|
171 | });
|
172 | }
|
173 | });
|
174 | }
|
175 |
|
176 | };
|