Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #123 - do not remove eol commas inside comment lines #124

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -1172,10 +1172,6 @@
return rtrim(strReplace(txt, /^[, ]*\n+ */, ''))
}

function removeTrailingWhitespace (txt) {
return strReplace(txt, /[, ]*$/, '')
}

function txtHasCommasAfterNewline (s) {
return /\n.*,.*$/.test(s)
}
Expand Down Expand Up @@ -3350,7 +3346,7 @@
ignoreNodesStartId = startIgnoreNode.id
ignoreNodesEndId = closingNode.id

// if a node without children, then just don't format it
// if a node without children, then just don't format it
} else {
ignoreNodesStartId = startIgnoreNode.id
ignoreNodesEndId = firstNodeInsideIgnoreZone.id
Expand All @@ -3359,8 +3355,8 @@
}

if (isParenOpener(node)) {
// we potentially need to add this opener node to the current openingLineNodes
// before we push into the next parenStack
// we potentially need to add this opener node to the current openingLineNodes
// before we push into the next parenStack
const topOfTheParenStack = stackPeek(parenStack, 0)
if (topOfTheParenStack) {
const onOpeningLineOfParenStack = lineIdx === topOfTheParenStack._parenOpenerLineIdx
Expand Down Expand Up @@ -3449,7 +3445,7 @@
}

if (lookForwardToSlurpNodes) {
// look forward and grab any closers nodes that may be slurped up
// look forward and grab any closers nodes that may be slurped up
const parenTrailClosers = findForwardClosingParens(nodesArr, inc(idx))

// If we have printed a whitespace node just before this, we may need to remove it and then re-print
Expand All @@ -3467,8 +3463,8 @@
const parenTrailCloserNode = parenTrailClosers[parenTrailCloserIdx]

if (isParenCloser(parenTrailCloserNode)) {
// NOTE: we are adjusting the current line here, but we do not update the nodesWeHavePrintedOnThisLine
// because we cannot have a Rule 3 alignment to a closer node
// NOTE: we are adjusting the current line here, but we do not update the nodesWeHavePrintedOnThisLine
// because we cannot have a Rule 3 alignment to a closer node
lineTxt = strConcat(lineTxt, parenTrailCloserNode.text)

parenTrailCloserNode.text = ''
Expand All @@ -3489,7 +3485,7 @@
}

if (currentNodeIsNewline) {
// record the original column indexes for the next line
// record the original column indexes for the next line
nodesArr = recordOriginalColIndexes(nodesArr, idx)

const numSpacesOnNextLine = numSpacesAfterNewline(node)
Expand Down Expand Up @@ -3596,16 +3592,16 @@
if (topOfTheParenStack && topOfTheParenStack._rule3Active) {
numSpaces = topOfTheParenStack._rule3NumSpaces

// Comment lines that are vertically aligned with a node from the line above --> align to that node
// Comment lines that are vertically aligned with a node from the line above --> align to that node
} else if (nextLineContainsOnlyOneComment && commentLooksAlignedWithPreviousForm) {
numSpaces = colIdxOfSingleLineCommentAlignmentNode

// Comment lines that are outside of a parenStack, and have no obvious relation to lines above them:
// keep their current indentation
// Comment lines that are outside of a parenStack, and have no obvious relation to lines above them:
// keep their current indentation
} else if (nextLineContainsOnlyOneComment && !topOfTheParenStack) {
numSpaces = numSpacesOnNextLine

// Else apply regular fixed indentation rules based on the parenStack depth (ie: Tonsky rules)
// Else apply regular fixed indentation rules based on the parenStack depth (ie: Tonsky rules)
} else {
numSpaces = numSpacesForIndentation(topOfTheParenStack)
}
Expand All @@ -3626,8 +3622,7 @@

// add this line to the outTxt and reset lineTxt
if (strTrim(lineTxt) !== '') {
const whitespaceTrimmedLineTxt = removeTrailingWhitespace(lineTxt)
outTxt = strConcat(outTxt, whitespaceTrimmedLineTxt)
outTxt = strConcat(outTxt, lineTxt)
}
outTxt = strConcat(outTxt, newlineStr)

Expand Down
18 changes: 18 additions & 0 deletions test_format/format.eno
Original file line number Diff line number Diff line change
Expand Up @@ -1700,3 +1700,21 @@ false}

"aaa"
--Expected

# GitHub Issue #123 - eol commas inside comments

> https://github.com/oakmac/standard-clojure-style-js/issues/123

--Input
;; foo, bar,
;; biz, baz,
gee, gaz,
hee, haz,
--Input

--Expected
;; foo, bar,
;; biz, baz,
gee, gaz
hee, haz
--Expected