Skip to content

Commit

Permalink
GitHub Issue #142 - ns bug with paren nesting levels (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac authored Oct 29, 2024
1 parent d8507db commit 3eaca9d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,9 @@
const parenStack = []
let insideNsForm = false
let insideReferClojureForm = false
let referClojureParenNestingDepth = -1
let insideRequireForm = false
let requireFormParenNestingDepth = -1
let requireFormLineNo = -1
let insideImportForm = false
let importFormLineNo = -1
Expand Down Expand Up @@ -1576,11 +1578,13 @@
nsNodeIdx = idx
} else if (insideNsForm && isReferClojureNode(node)) {
insideReferClojureForm = true
referClojureParenNestingDepth = parenNestingDepth
sectionToAttachEolCommentsTo = 'refer-clojure'
referClojureNodeIdx = idx
beyondNsMetadata = true
} else if (insideNsForm && isRequireNode(node)) {
insideRequireForm = true
requireFormParenNestingDepth = parenNestingDepth
requireFormLineNo = lineNo
requireNodeIdx = idx
beyondNsMetadata = true
Expand Down Expand Up @@ -1629,13 +1633,13 @@
if (insideImportPackageList) {
insideImportPackageList = false
importPackageListFirstToken = null
} else if (insideRequireForm && parenNestingDepth <= 1) {
} else if (insideRequireForm && parenNestingDepth < requireFormParenNestingDepth) {
insideRequireForm = false
} else if (insideRequireList && parenNestingDepth < requireListParenNestingDepth) {
insideRequireList = false
requireListParenNestingDepth = -1
requireRenameIdx = -1
} else if (insideReferClojureForm && parenNestingDepth <= 1) {
} else if (insideReferClojureForm && parenNestingDepth < referClojureParenNestingDepth) {
insideReferClojureForm = false
referClojureNodeIdx = -1
} else if (insideNsForm && parenNestingDepth === 0) {
Expand All @@ -1645,11 +1649,12 @@
nsFormEndsLineIdx = lineNo
}

if (parenNestingDepth < 3) {
if (insideReferClojureForm && parenNestingDepth <= referClojureParenNestingDepth) {
collectReferClojureExcludeSymbols = false
collectReferClojureOnlySymbols = false
collectReferClojureRenameSymbols = false
}

if (referIdx > 0 && parenNestingDepth < referParenNestingDepth) {
referIdx = -1
referParenNestingDepth = -1
Expand Down
27 changes: 27 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -1826,3 +1826,30 @@
;; bbb
(:refer-clojure :only [-> number? set]))) ;; ccc
--Expected

# GitHub Issue #142 - bug with reader conditionals

--Input
(ns ^{:doc "ClojureScript wrapper functions for math operations"
:author "Paula Gearon" }
cljs.math
;; create space to load this in Clojure for testing
#?@(:clj ((:refer-clojure :exclude [aset aget + unsigned-bit-shift-right bit-shift-right bit-shift-left])
(:require [js :refer [aset aget +]])
(:import [js ArrayBuffer Uint8Array Uint32Array Float64Array]))))
--Input

--Expected
(ns cljs.math
{:doc "ClojureScript wrapper functions for math operations"
:author "Paula Gearon"}
#?(:clj
;; create space to load this in Clojure for testing
(:refer-clojure :exclude [+ aget aset bit-shift-left bit-shift-right unsigned-bit-shift-right]))
#?(:clj
(:require
[js :refer [+ aget aset]]))
#?(:clj
(:import
(js ArrayBuffer Float64Array Uint32Array Uint8Array))))
--Expected
63 changes: 63 additions & 0 deletions test_parse_ns/parse_ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -2818,3 +2818,66 @@
}
}
--Expected

# GitHub Issue #142 - cljs.math reader conditional example

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

--Input
(ns ^{:doc "ClojureScript wrapper functions for math operations"
:author "Paula Gearon" }
cljs.math
;; create space to load this in Clojure for testing
#?@(:clj ((:refer-clojure :exclude [aset aget + unsigned-bit-shift-right bit-shift-right bit-shift-left])
(:require [js :refer [aset aget +]])
(:import [js ArrayBuffer Uint8Array Uint32Array Float64Array]))))

#?(:clj (def number Double))
--Input

--Expected
{
"nsSymbol": "cljs.math",
"nsMetadata": [
{
"key": ":doc",
"value": "\"ClojureScript wrapper functions for math operations\""
},
{
"key": ":author",
"value": "\"Paula Gearon\""
}
],
"referClojureCommentsAbove": [
";; create space to load this in Clojure for testing"
],
"referClojure": {
"exclude": [
{ "symbol": "+", "platform": ":clj" },
{ "symbol": "aget", "platform": ":clj" },
{ "symbol": "aset", "platform": ":clj" },
{ "symbol": "bit-shift-left", "platform": ":clj" },
{ "symbol": "bit-shift-right", "platform": ":clj" },
{ "symbol": "unsigned-bit-shift-right", "platform": ":clj" }
]
},
"requires": [
{
"symbol": "js",
"refer": [
{"symbol": "+"},
{"symbol": "aget"},
{"symbol": "aset"}
],
"platform": ":clj"
}
],
"imports": [
{
"package": "js",
"classes": ["ArrayBuffer", "Float64Array", "Uint32Array", "Uint8Array"],
"platform": ":clj"
}
]
}
--Expected

0 comments on commit 3eaca9d

Please sign in to comment.