Skip to content

Commit

Permalink
GitHub Issue #162 - fix bugs related to commas at the end of lines (#164
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oakmac authored Nov 29, 2024
1 parent 86fcf2a commit 49ee735
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@
return s.replace(find, replace)
}

const runtimeHasStringReplaceAll = isFunction(''.replaceAll)

function strReplaceAll (s, find, replace) {
if (runtimeHasStringReplaceAll) {
return s.replaceAll(find, replace)
} else {
let s2 = s
while (strIncludes(s2, find)) {
s2 = strReplaceFirst(s2, find, replace)
}
return s2
}
}

function crlfToLf (txt) {
return txt.replace(/\r\n/g, '\n')
}
Expand Down Expand Up @@ -1126,6 +1140,20 @@
return result
}

function isNewlineNodeWithCommaOnNextLine (n) {
if (n && isNewlineNode(n)) {
const txtSlices = strSplit(n.text, '\n')
if (txtSlices.length >= 2) {
const lastSlice = arrayLast(txtSlices)
if (strIncludes(lastSlice, ',')) {
return true
}
}
}

return false
}

// Searches forward in the nodes array for closing paren nodes that could potentially
// be slurped up to the current line. Includes whitespace and comment nodes as well.
// returns an array of the nodes (possibly empty)
Expand All @@ -1139,6 +1167,8 @@

if (!node) {
keepSearching = false
} else if (isNewlineNodeWithCommaOnNextLine(node)) {
keepSearching = false
} else if (isWhitespaceNode(node) || isParenCloser(node) || isCommentNode(node)) {
closers.push(node)
keepSearching = true
Expand Down Expand Up @@ -1203,6 +1233,12 @@
return rtrim(strReplaceFirst(txt, /^[, ]*\n+ */, ''))
}

// NOTE: this function does not remove newline characters because it only
// needs to operates on a single line
function removeTrailingWhitespace (txt) {
return txt.replace(/[, ]*$/, '')
}

function txtHasCommasAfterNewline (s) {
return /\n.*,.*$/.test(s)
}
Expand Down Expand Up @@ -3538,6 +3574,11 @@
skipPrintingThisNode = true
}

// do not print a comma at the end of a line
if (currentNodeIsWhitespace && !currentNodeIsNewline && nextTextNode && isCommentNode(nextTextNode)) {
node.text = strReplaceAll(node.text, ',', '')
}

// If we are inside of a parenStack and hit a newline,
// look forward to see if we can close the current parenTrail.
// ie: slurp closing parens onto the current line
Expand All @@ -3564,7 +3605,7 @@
const lastNodeWePrinted = arrayLast(nodesWeHavePrintedOnThisLine)
let lineTxtHasBeenRightTrimmed = false
if (lastNodeWePrinted && isWhitespaceNode(lastNodeWePrinted)) {
lineTxt = rtrim(lineTxt)
lineTxt = removeTrailingWhitespace(lineTxt)
lineTxtHasBeenRightTrimmed = true
}

Expand Down Expand Up @@ -3922,6 +3963,7 @@
API._commentNeedsSpaceBefore = commentNeedsSpaceBefore
API._commentNeedsSpaceInside = commentNeedsSpaceInside
API._removeLeadingWhitespace = removeLeadingWhitespace
API._removeTrailingWhitespace = removeTrailingWhitespace
API._txtHasCommasAfterNewline = txtHasCommasAfterNewline

API._AnyChar = AnyChar
Expand Down
13 changes: 13 additions & 0 deletions test/internals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ test('removeLeadingWhitespace', () => {
expect(scsLib._removeLeadingWhitespace(',, \n\n')).toBe('')
})

test('removeTrailingWhitespace', () => {
expect(isFn(scsLib._removeTrailingWhitespace)).toBe(true)
expect(scsLib._removeTrailingWhitespace('aaa,')).toBe('aaa')
expect(scsLib._removeTrailingWhitespace('aaa , ')).toBe('aaa')
expect(scsLib._removeTrailingWhitespace('aaa')).toBe('aaa')
expect(scsLib._removeTrailingWhitespace(' aaa aaa ,, ')).toBe(' aaa aaa')
expect(scsLib._removeTrailingWhitespace(' , aaa aaa ,, ')).toBe(' , aaa aaa')

// NOTE: this function does not remove newline characters
// it only needs to operate against a single line
expect(scsLib._removeTrailingWhitespace('aaa \n ')).toBe('aaa \n')
})

test('txtHasCommasAfterNewline', () => {
expect(scsLib._txtHasCommasAfterNewline('\n ,,')).toBe(true)
expect(scsLib._txtHasCommasAfterNewline('\n\n ,')).toBe(true)
Expand Down
88 changes: 88 additions & 0 deletions test_format/format.eno
Original file line number Diff line number Diff line change
Expand Up @@ -2005,3 +2005,91 @@ hee, haz
:eee "eee"
:fff "fff"}])
--Expected

# GitHub Issue #162 - bug with commas 1

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

--Input
aaa, ;; foo
bbb,;;bar
--Input

--Expected
aaa ;; foo
bbb ;; bar
--Expected

# GitHub Issue #162 - bug with commas 2

--Input
(def my-map
{:aaa "aaa",
:bbb "bbb", ;; foo
:ccc "ccc",,,,,,,,,,,,})
--Input

--Expected
(def my-map
{:aaa "aaa"
:bbb "bbb" ;; foo
:ccc "ccc"})
--Expected

# GitHub Issue #162 - bug with commas 3

--Input
(def my-vec
[a "a"
b "b"
;; foo
,])
--Input

--Expected
(def my-vec
[a "a"
b "b"
;; foo
,])
--Expected

# GitHub Issue #162 - bug with commas 4

--Input
(def my-vec
[a "a"
b "b",
;; foo
;; bar
,])
--Input

--Expected
(def my-vec
[a "a"
b "b"
;; foo
;; bar
,])
--Expected

# GitHub Issue #162 - bug with commas 5

--Input
(def my-vec
[a "a"
b "b",
;; foo
;; bar
, ])
--Input

--Expected
(def my-vec
[a "a"
b "b"
;; foo
;; bar
,])
--Expected

0 comments on commit 49ee735

Please sign in to comment.