UNPKG

@coffeelint/cli

Version:
128 lines (115 loc) 3.89 kB
(function() { var BracesSpacing, indexOf = [].indexOf; module.exports = BracesSpacing = (function() { class BracesSpacing { 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] : 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; } tokenSetsMatch(a, b) { return JSON.stringify(a) === JSON.stringify(b); } getExpectedSpaces(tokenApi, tokens) { var config, mono, ref, ref1; config = tokenApi.config[this.rule.name]; mono = ['IDENTIFIER', ...this.tokens]; tokens = tokens.map(function(token) { return token != null ? token[0] : void 0; }).filter(function(token) { return indexOf.call(mono, token) >= 0; }); if (this.tokenSetsMatch(tokens.slice(0, 2), this.tokens)) { return (ref = config.empty_object_spaces) != null ? ref : config.spaces; } else if (this.tokenSetsMatch(mono, tokens.sort())) { return (ref1 = config.mono_object_spaces) != null ? ref1 : config.spaces; } else { return config.spaces; } } lintToken(token, tokenApi) { var actual, expected, firstToken, msg, secondToken, tokens; if (token.generated) { return null; } [firstToken, secondToken] = tokens = token[0] === '{' ? [token, this.findNearestToken(token, tokenApi, 1), this.findNearestToken(token, tokenApi, 2)] : [this.findNearestToken(token, tokenApi, -1), token, this.findNearestToken(token, tokenApi, -2)]; 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 }; } } }; BracesSpacing.prototype.rule = { type: 'style', name: 'braces_spacing', level: 'ignore', spaces: 0, empty_object_spaces: 0, message: 'Curly braces must have the proper spacing', description: `This rule checks to see that there is the proper spacing inside curly braces. The spacing amount is specified by "spaces". The spacing amount for empty objects is specified by "empty_object_spaces". The spacing amount for objects containing a single item is specified by "mono_object_spaces". <pre><code> # Spaces is 0 {a: b} # Good {a: b } # Bad { a: b} # Bad { 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 # Empty Object Spaces is 0 {} # Good { } # Bad # Empty Object Spaces is 1 {} # Bad { } # Good # Mono Object Spaces is 0 {a} # Good { a } # Bad # Mono Object Spaces is 1 {a} # Bad { a } # Good </code></pre> This rule is disabled by default.` }; BracesSpacing.prototype.tokens = ['{', '}']; return BracesSpacing; }).call(this); }).call(this);