UNPKG

3.78 kBJavaScriptView Raw
1/**
2 * Disallow space before or after `*` in generator functions
3 *
4 * Types: `Object`
5 *
6 * - `Object` (at least one of properties must be present and it must be set to true):
7 * - `'beforeStar'`
8 * - `true` disallows space before `*`
9 * - `'afterStar'`
10 * - `true` disallows space after `*`
11 *
12 * #### Example
13 *
14 * ```js
15 * "disallowSpacesInGenerator": {
16 * "beforeStar": true,
17 * "afterStar": true
18 * }
19 * ```
20 *
21 * ##### Valid for mode `{ "beforeStar": true, "afterStar": false }`
22 * ```js
23 * var x = function* () {};
24 * function* a() {};
25 * var x = async function* () {};
26 * var x = async function* a () {};
27 * async function* a() {}
28 * var x = async function* (){};
29 * ```
30 *
31 * ##### Valid for mode `{ "beforeStar": false, "afterStar": true }`
32 * ```js
33 * var x = function *() {};
34 * function *a() {};
35 * var x = async function *() {};
36 * var x = async function *a () {};
37 * async function *a() {}
38 * var x = async function *(){};
39 * ```
40 *
41*/
42
43var assert = require('assert');
44
45module.exports = function() {};
46
47module.exports.prototype = {
48 configure: function(options) {
49 assert(
50 typeof options === 'object',
51 this.getOptionName() + ' option must be an object'
52 );
53
54 if ('beforeStar' in options) {
55 assert(
56 options.beforeStar === true,
57 this.getOptionName() + '.beforeStar ' +
58 'property requires true value or should be removed'
59 );
60 }
61 if ('afterStar' in options) {
62 assert(
63 options.afterStar === true,
64 this.getOptionName() + '.afterStar ' +
65 'property requires true value or should be removed'
66 );
67 }
68
69 assert(
70 options.beforeStar || options.afterStar,
71 this.getOptionName() + ' must have beforeStar or afterStar property'
72 );
73
74 this._beforeStar = options.beforeStar;
75 this._afterStar = options.afterStar;
76 },
77
78 getOptionName: function() {
79 return 'disallowSpacesInGenerator';
80 },
81
82 check: function(file, errors) {
83 var beforeStar = this._beforeStar;
84 var afterStar = this._afterStar;
85
86 file.iterateNodesByType(['FunctionDeclaration', 'FunctionExpression'], function(node) {
87 if (!node.generator) {
88 return;
89 }
90
91 var parent = node.parentElement;
92 var shorthand = node.shorthand;
93
94 // shorthand or constructor methods
95 if (parent.method || parent.type === 'MethodDefinition') {
96 node = parent.key;
97 }
98
99 var currentToken = file.getFirstNodeToken(node);
100
101 if (node.async && currentToken.value === 'async') {
102 currentToken = file.getNextToken(currentToken);
103 }
104
105 if (beforeStar && !shorthand) {
106 // currentToken assigned outside of function
107 errors.assert.noWhitespaceBetween({
108 token: currentToken,
109 nextToken: file.getNextToken(currentToken),
110 message: 'Illegal space before star'
111 });
112 }
113
114 if (afterStar) {
115 if (shorthand) {
116 currentToken = file.getPrevToken(currentToken);
117 } else {
118 // currentToken reassigned for star token
119 currentToken = file.getNextToken(currentToken);
120 }
121
122 errors.assert.noWhitespaceBetween({
123 token: currentToken,
124 nextToken: file.getNextToken(currentToken),
125 message: 'Illegal space after star'
126 });
127 }
128 });
129 }
130};