UNPKG

1.82 kBJavaScriptView Raw
1/**
2 * Disallows multiple indentation characters (tabs or spaces) between identifiers, keywords, and any other token
3 *
4 * Type: `Boolean` or `Object`
5 *
6 * Values: `true` or `{"allowEOLComments": true}` to allow on-line comments to be ignored
7 *
8 * #### Examples
9 *
10 * ```js
11 * "disallowMultipleSpaces": true
12 * // or
13 * "disallowMultipleSpaces": {"allowEOLComments": true}
14 * ```
15 *
16 * ##### Valid
17 * ```js
18 * var x = "hello";
19 * function y() {}
20 * ```
21 *
22 * ##### Valid for `{"allowEOLComments": true}`
23 *
24 * ```js
25 * var x = "hello" // world;
26 * function y() {}
27 * ```
28 *
29 * ##### Invalid
30 * ```js
31 * var x = "hello";
32 * function y() {}
33 * ```
34 */
35
36var assert = require('assert');
37
38module.exports = function() {};
39
40module.exports.prototype = {
41
42 configure: function(options) {
43 assert(
44 options === true ||
45 typeof options === 'object' &&
46 options.allowEOLComments === true,
47 this.getOptionName() + ' option requires true value ' +
48 'or an object with `allowEOLComments` property'
49 );
50
51 this._allowEOLComments = options.allowEOLComments;
52 },
53
54 getOptionName: function() {
55 return 'disallowMultipleSpaces';
56 },
57
58 check: function(file, errors) {
59 var token = file.getProgram().getFirstToken();
60 var nextToken;
61
62 while (token) {
63 nextToken = token.getNextNonWhitespaceToken();
64
65 if (!nextToken) {
66 break;
67 }
68
69 if (!this._allowEOLComments || nextToken.type !== 'CommentLine') {
70 errors.assert.spacesBetween({
71 token: token,
72 nextToken: nextToken,
73 atMost: 1
74 });
75 }
76
77 token = token.getNextNonWhitespaceToken();
78 }
79 }
80
81};