2 * @fileoverview Disallow use of multiple spaces.
3 * @author Nicholas C. Zakas
4 * @copyright 2015 Brandon Mills. All rights reserved.
5 * @copyright 2015 Nicholas C. Zakas. All rights reserved.
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
14 module.exports = function(context) {
16 // the index of the last comment that was checked
17 var exceptions = { "Property": true },
19 options = context.options[0],
22 if (options && options.exceptions) {
23 Object.keys(options.exceptions).forEach(function (key) {
24 if (options.exceptions[key]) {
25 exceptions[key] = true;
27 delete exceptions[key];
30 hasExceptions = Object.keys(exceptions).length > 0;
34 * Determines if a given source index is in a comment or not by checking
35 * the index against the comment range. Since the check goes straight
36 * through the file, once an index is passed a certain comment, we can
37 * go to the next comment to check that.
38 * @param {int} index The source index to check.
39 * @param {ASTNode[]} comments An array of comment nodes.
40 * @returns {boolean} True if the index is within a comment, false if not.
43 function isIndexInComment(index, comments) {
47 while (lastCommentIndex < comments.length) {
49 comment = comments[lastCommentIndex];
51 if (comment.range[0] <= index && index < comment.range[1]) {
53 } else if (index > comment.range[1]) {
64 //--------------------------------------------------------------------------
66 //--------------------------------------------------------------------------
69 "Program": function() {
71 var source = context.getSource(),
72 allComments = context.getAllComments(),
73 pattern = /[^\n\r\u2028\u2029 ] {2,}/g, // note: repeating space
77 while (pattern.test(source)) {
79 // do not flag anything inside of comments
80 if (!isIndexInComment(pattern.lastIndex, allComments)) {
82 token = context.getTokenByRangeStart(pattern.lastIndex);
86 parent = context.getNodeByRangeIndex(pattern.lastIndex - 1);
89 if (!parent || !exceptions[parent.type]) {
90 context.report(token, token.loc.start,
91 "Multiple spaces found before '{{value}}'.",
92 { value: token.value });