UNPKG

@coffeelint/cli

Version:
145 lines (132 loc) 4.49 kB
(function() { var BracketSpacing; module.exports = BracketSpacing = (function() { class BracketSpacing { distanceBetweenTokens(firstToken, secondToken) { return secondToken[2].first_column - firstToken[2].last_column - 1; } findNearestToken(token, tokenApi, difference) { var nearestToken, totalDifference; totalDifference = 0; while (true) { totalDifference += difference; nearestToken = tokenApi.peek(totalDifference); if (nearestToken != null ? nearestToken[0].startsWith('STRING_') : void 0) { // Render quotes for string interpolation. nearestToken[1] = '"'; } if ((nearestToken != null ? nearestToken[0] : void 0) === 'OUTDENT' || ((nearestToken != null ? nearestToken.generated : void 0) != null)) { continue; } return nearestToken; } } tokensOnSameLine(firstToken, secondToken) { return firstToken[2].first_line === secondToken[2].first_line; } escape(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } getExpectedSpaces(tokenApi, tokens) { var config, except, pattern, ref, ref1; config = tokenApi.config[this.rule.name]; except = this.escape(config.exceptions.join('')); pattern = tokens.map(function(token) { return token != null ? token[1] : void 0; }).join(''); switch (false) { case !(except && RegExp(`^\\[[${except}]|[${except}]\\]$`).test(pattern)): if (config.spaces) { return 0; } else { return 1; } break; case !pattern.includes('[]'): return (ref = config.empty_array_spaces) != null ? ref : config.spaces; case !/\[\w+\]/.test(pattern): return (ref1 = config.mono_array_spaces) != null ? ref1 : config.spaces; default: return config.spaces; } } lintToken(token, tokenApi) { var actual, expected, firstToken, msg, secondToken, tokens; if (token.generated) { return null; } tokens = token[0] === this.tokens[0] ? (firstToken = token, secondToken = this.findNearestToken(token, tokenApi, 1), [firstToken, secondToken, this.findNearestToken(token, tokenApi, 2)]) : (firstToken = this.findNearestToken(token, tokenApi, -1), secondToken = token, [this.findNearestToken(token, tokenApi, -2), firstToken, secondToken]); if (!this.tokensOnSameLine(firstToken, secondToken)) { return null; } expected = this.getExpectedSpaces(tokenApi, tokens); actual = this.distanceBetweenTokens(firstToken, secondToken); if (actual === expected) { return null; } else { msg = `There should be ${expected} space`; if (expected !== 1) { msg += 's'; } msg += ` inside \"${token[0]}\"`; return { token, context: msg }; } } }; BracketSpacing.prototype.rule = { type: 'style', name: 'bracket_spacing', level: 'ignore', spaces: 0, empty_array_spaces: 0, exceptions: [], message: 'Square brackets must have the proper spacing', description: `This rule checks to see that there is the proper spacing inside square brackets. The spacing amount is specified by "spaces". The spacing amount for empty arrays is specified by "empty_array_spaces". The spacing amount for arrays containing a single item is specified by "mono_array_spaces". Specified characters will be ignored if listed in "exceptions". <pre><code> # Spaces is 0 [a, b] # Good [a, b ] # Bad [ a, b] # Bad [ a, b ] # Bad # Except brackets [ [a, b] ] # Good [[ a, b ]] # Bad # Spaces is 1 [a, b] # Bad [a, b ] # Bad [ a, b] # Bad [ a, b ] # Good [ a, b ] # Bad [ a, b ] # Bad [ a, b ] # Bad # Except braces [{ a: b }] # Good [ {a: b} ] # Bad # Empty Array Spaces is 0 [] # Good [ ] # Bad # Empty Array Spaces is 1 [] # Bad [ ] # Good # Mono Array Spaces is 0 [a] # Good [ a ] # Bad # Mono Array Spaces is 1 [a] # Bad [ a ] # Good </code></pre> This rule is disabled by default.` }; BracketSpacing.prototype.tokens = ['[', ']']; return BracketSpacing; }).call(this); }).call(this);