From d02c8153a528936f8c8e4946c6e83745113ed9bb Mon Sep 17 00:00:00 2001 From: Amin Mirzaee Date: Tue, 16 Jan 2018 22:02:41 -0500 Subject: [PATCH] Fix Space after Comma rule with CSX format described in #629 --- src/rules/spacing_after_comma.coffee | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/rules/spacing_after_comma.coffee b/src/rules/spacing_after_comma.coffee index 009fd2b5..ddc5725a 100644 --- a/src/rules/spacing_after_comma.coffee +++ b/src/rules/spacing_after_comma.coffee @@ -22,10 +22,28 @@ module.exports = class SpacingAfterComma @inRegex = false return - unless token.spaced or token.newLine or token.generated or + unless token.spaced or token.newLine or @isGenerated(token, tokenApi) or @isRegexFlag(token, tokenApi) return true + # Coffeescript does some code generation when using CSX 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) -> + return true if token.generated + 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 and + 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