UNPKG

@coffeelint/cli

Version:
89 lines (78 loc) 2.72 kB
(function() { var SpacingAfterComma, indexOf = [].indexOf; module.exports = SpacingAfterComma = (function() { class SpacingAfterComma { constructor() { this.inRegex = false; } lintToken(token, tokenApi) { var ignore_elision, type; [type] = token; if (type === 'REGEX_START') { this.inRegex = true; return; } if (type === 'REGEX_END') { this.inRegex = false; return; } ({ignore_elision} = tokenApi.config[this.rule.name]); if (ignore_elision && indexOf.call(tokenApi.peek(1), ',') >= 0) { return null; } if (!(token.spaced || token.newLine || this.isGenerated(token, tokenApi) || this.isRegexFlag(token, tokenApi))) { return {token}; } } // Coffeescript does some code generation when using JSX syntax, and it adds // brackets & commas that are not marked as generated. The only way to check // these is to see if the comma has the same column number as the last token. isGenerated(token, tokenApi) { var offset, pos, prevPos, prevToken; if (token.generated) { return true; } offset = -1; prevToken = tokenApi.peek(offset); while (prevToken.generated) { offset -= 1; prevToken = tokenApi.peek(offset); } pos = token[2]; prevPos = prevToken[2]; if (pos.first_line === prevPos.first_line && pos.first_column === prevPos.first_column) { return true; } return false; } // When generating a regex (///${whatever}///i) CoffeeScript generates tokens // for RegEx(whatever, "i") but doesn't bother to mark that comma as // generated or spaced. Looking 3 tokens ahead skips the STRING and CALL_END isRegexFlag(token, tokenApi) { var maybeEnd; if (!this.inRegex) { return false; } maybeEnd = tokenApi.peek(3); return (maybeEnd != null ? maybeEnd[0] : void 0) === 'REGEX_END'; } }; SpacingAfterComma.prototype.rule = { type: 'style', name: 'spacing_after_comma', level: 'ignore', ignore_elision: false, message: 'a space is required after commas', description: `This rule checks to make sure you have a space after commas. Consecutive commas are allowed when skipping array elements if "ignore_elision" is true. <pre><code> # ignore_elision: true [,, c,, e, f] = [1, 2, 3, 4, 5, 6] </code></pre>` }; SpacingAfterComma.prototype.tokens = [',', 'REGEX_START', 'REGEX_END']; return SpacingAfterComma; }).call(this); }).call(this);