Skip to content

Commit

Permalink
GitHub Issue #48 - support :refer-clojure :rename with multiple reade…
Browse files Browse the repository at this point in the history
…r conditionals (#84)
  • Loading branch information
oakmac authored Sep 6, 2024
1 parent 5ee0642 commit bb54882
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2318,21 +2318,46 @@
} else if (numKeys === 1 && keys[0] === ':rename') {
const platforms = getPlatformsFromArray(referClojure.rename)
const numPlatforms = arraySize(platforms)
const nonPlatformSpecificRenames = filterOnPlatform(referClojure.rename, false)
const numNonPlatformSpecificRenames = arraySize(nonPlatformSpecificRenames)
const allRenamesForSamePlatform = numNonPlatformSpecificRenames === 0 && arraySize(platforms) > 0

if (numPlatforms === 0) {
let s = '\n (:refer-clojure :rename {'
s = strConcat(s, formatRenamesList(referClojure.rename))
s = strConcat(s, '})')
return s
} else if (numPlatforms === 1) {
} else if (numPlatforms === 1 && allRenamesForSamePlatform) {
let s = strConcat3('\n #?(', platforms[0], '\n')
s = strConcat(s, ' (:refer-clojure :rename {')
s = strConcat(s, formatRenamesList(referClojure.rename))
s = strConcat(s, '}))')
return s
} else {
// FIXME: implement this
throw new Error('namespaces with :refer-clojure :rename containing reader conditionals on multiple platforms is not supported yet')
let s = '\n (:refer-clojure\n :rename {'
s = strConcat(s, formatRenamesList(nonPlatformSpecificRenames))
s = strConcat(s, '\n #?@(')

let platformIdx = 0
while (platformIdx < numPlatforms) {
const platformStr = platforms[platformIdx]
const platformRenames = filterOnPlatform(referClojure.rename, platformStr)

if (platformIdx === 0) {
s = strConcat3(s, platformStr, ' [')
} else {
s = strConcat(s, '\n ')
s = strConcat3(s, platformStr, ' [')
}
s = strConcat(s, formatRenamesList(platformRenames))
s = strConcat(s, ']')

platformIdx = inc(platformIdx)
}

s = strConcat(s, ')})')

return s
}

// there are multiple keys, put each one on it's own line
Expand Down
38 changes: 38 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,41 @@
[bbb.ccc :refer :all :exclude [eee hhh yyy]]
[incanter.core :as-alias ic]))
--Expected

# refer-clojure rename with comment and reader conditional

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

--Input
(ns com.example.my-app
(:refer-clojure :rename
{get core-get
#?@(:clj [conj core-conj])}))
;:cljs [conj core-conj2])}))
--Input

--Expected
(ns com.example.my-app
(:refer-clojure
:rename {get core-get
#?@(:clj [conj core-conj])}))
;:cljs [conj core-conj2])}))
--Expected

# refer-clojure rename with reader conditional and multiple platforms

--Input
(ns com.example.my-app
(:refer-clojure :rename
{get core-get
#?@(:clj [conj core-conj]
:cljs [conj core-conj2])}))
--Input

--Expected
(ns com.example.my-app
(:refer-clojure
:rename {get core-get
#?@(:clj [conj core-conj]
:cljs [conj core-conj2])}))
--Expected
31 changes: 31 additions & 0 deletions test_parse_ns/parse_ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -1934,3 +1934,34 @@
]
}
--Expected

# refer-clojure rename with comment and reader conditional

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

--Input
(ns com.example.my-app
(:refer-clojure
:rename {get core-get
#?@(:clj [conj core-conj])}))
;:cljs [conj core-conj2])}))
--Input

--Expected
{
"nsSymbol": "com.example.my-app",
"referClojure": {
"rename": [
{
"fromSymbol": "conj",
"toSymbol": "core-conj",
"platform": ":clj"
},
{
"fromSymbol": "get",
"toSymbol": "core-get"
}
]
}
}
--Expected

0 comments on commit bb54882

Please sign in to comment.