UNPKG

3.75 kBJavaScriptView Raw
1/**
2 * Disallows space before `()` or `{}` in named function expressions.
3 *
4 * Type: `Object`
5 *
6 * Values: `"beforeOpeningRoundBrace"` and `"beforeOpeningCurlyBrace"` as child properties.
7 * Child properties must be set to `true`.
8 *
9 * #### Example
10 *
11 * ```js
12 * "disallowSpacesInNamedFunctionExpression": {
13 * "beforeOpeningRoundBrace": true,
14 * "beforeOpeningCurlyBrace": true
15 * }
16 * ```
17 *
18 * ##### Valid
19 *
20 * ```js
21 * var x = function a(){};
22 * ```
23 *
24 * ##### Invalid
25 *
26 * ```js
27 * var x = function a() {};
28 * var x = function a (){};
29 * var x = function a () {};
30 * ```
31 */
32
33var assert = require('assert');
34
35module.exports = function() {};
36
37module.exports.prototype = {
38 configure: function(options) {
39 assert(
40 typeof options === 'object',
41 this.getOptionName() + ' option must be the object'
42 );
43
44 if ('beforeOpeningRoundBrace' in options) {
45 assert(
46 options.beforeOpeningRoundBrace === true,
47 this.getOptionName() + '.beforeOpeningRoundBrace ' +
48 'property requires true value or should be removed'
49 );
50 }
51
52 if ('beforeOpeningCurlyBrace' in options) {
53 assert(
54 options.beforeOpeningCurlyBrace === true,
55 this.getOptionName() + '.beforeOpeningCurlyBrace ' +
56 'property requires true value or should be removed'
57 );
58 }
59
60 assert(
61 options.beforeOpeningCurlyBrace || options.beforeOpeningRoundBrace,
62 this.getOptionName() + ' must have beforeOpeningCurlyBrace ' +
63 'or beforeOpeningRoundBrace property'
64 );
65
66 this._beforeOpeningRoundBrace = Boolean(options.beforeOpeningRoundBrace);
67 this._beforeOpeningCurlyBrace = Boolean(options.beforeOpeningCurlyBrace);
68 },
69
70 getOptionName: function() {
71 return 'disallowSpacesInNamedFunctionExpression';
72 },
73
74 check: function(file, errors) {
75 var beforeOpeningRoundBrace = this._beforeOpeningRoundBrace;
76 var beforeOpeningCurlyBrace = this._beforeOpeningCurlyBrace;
77
78 file.iterateNodesByType(['FunctionExpression'], function(node) {
79 var functionNode = node.id;
80 var parent = node.parentElement;
81
82 // Ignore syntactic sugar for getters and setters.
83 if (parent.type === 'Property' && (parent.kind === 'get' || parent.kind === 'set')) {
84 return;
85 }
86
87 // shorthand or constructor methods
88 if (parent.method || parent.type === 'MethodDefinition') {
89 functionNode = parent.key;
90 }
91
92 // named function expressions only
93 if (node.id) {
94 if (beforeOpeningRoundBrace) {
95 var functionToken = file.getFirstNodeToken(functionNode);
96 if (node.async && functionToken.value === 'async') {
97 functionToken = file.getNextToken(functionToken);
98 }
99 errors.assert.noWhitespaceBetween({
100 token: functionToken,
101 nextToken: file.getNextToken(functionToken),
102 message: 'Illegal space before opening round brace'
103 });
104 }
105
106 if (beforeOpeningCurlyBrace) {
107 var bodyToken = file.getFirstNodeToken(node.body);
108 errors.assert.noWhitespaceBetween({
109 token: file.getPrevToken(bodyToken),
110 nextToken: bodyToken,
111 message: 'Illegal space before opening curly brace'
112 });
113 }
114 }
115 });
116 }
117
118};