Skip to content

Commit e88f60a

Browse files
authored
GitHub Issue #210 - fix bug with invalid splicing reader conditionals for refer-clojure (#215)
1 parent 45a65bd commit e88f60a

2 files changed

Lines changed: 37 additions & 31 deletions

File tree

lib/standard-clojure-style.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,35 +2795,33 @@
27952795
s = strConcat(s, '))')
27962796
return s
27972797

2798-
// all symbols are for specific platforms, ie: every symbol is wrapped in a reader conditional
2798+
// all symbols are for specific platforms across multiple platforms:
2799+
// wrap a separate (:refer-clojure) per platform inside #?(...)
2800+
// (We cannot use :exclude #?@(...) here because #?@ splicing outside a
2801+
// collection produces invalid Clojure.)
27992802
} else if (numPlatforms > 1 && numSymbolsForAllPlatforms === 0) {
28002803
let s = '\n'
28012804
s = printCommentsAbove(s, ns.referClojureCommentsAbove, ' ')
2802-
s = strConcat(s, ' (:refer-clojure\n')
2803-
s = strConcat3(s, ' ', kwd)
2804-
s = strConcat(s, ' #?@(')
2805+
s = strConcat(s, ' #?(')
28052806

28062807
let platformIdx = 0
28072808
while (platformIdx < numPlatforms) {
28082809
const platform = platforms[platformIdx]
28092810
const symbolsForPlatform = arrayPluck(filterOnPlatform(symbolsArr, platform), 'symbol')
28102811

2811-
s = strConcat(s, formatKeywordFollowedByListOfSymbols(platform, symbolsForPlatform))
2812-
2813-
if (inc(platformIdx) !== numPlatforms) {
2814-
if (kwd === ':exclude') {
2815-
s = strConcat3(s, '\n', repeatString(' ', 17))
2816-
} else if (kwd === ':only') {
2817-
s = strConcat3(s, '\n', repeatString(' ', 14))
2818-
} else {
2819-
// FIXME: throw error here?
2820-
}
2812+
if (platformIdx === 0) {
2813+
s = strConcat3(s, platform, ' (:refer-clojure ')
2814+
} else {
2815+
s = strConcat(s, '\n ')
2816+
s = strConcat3(s, platform, ' (:refer-clojure ')
28212817
}
2818+
s = strConcat(s, formatKeywordFollowedByListOfSymbols(kwd, symbolsForPlatform))
2819+
s = strConcat(s, ')')
28222820

28232821
platformIdx = inc(platformIdx)
28242822
}
28252823

2826-
s = strConcat(s, '))')
2824+
s = strConcat(s, ')')
28272825

28282826
return s
28292827

test_format/ns.eno

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,6 @@
994994

995995
# refer-clojure with reader conditionals 4
996996

997-
> FIXME: need to double-check this reader conditional syntax
998-
999997
--Input
1000998
(ns com.example.my-app
1001999
(:refer-clojure :only [#?@(:cljs [dissoc conj]
@@ -1004,15 +1002,12 @@
10041002

10051003
--Expected
10061004
(ns com.example.my-app
1007-
(:refer-clojure
1008-
:only #?@(:clj [frequencies int partition]
1009-
:cljs [conj dissoc])))
1005+
#?(:clj (:refer-clojure :only [frequencies int partition])
1006+
:cljs (:refer-clojure :only [conj dissoc])))
10101007
--Expected
10111008

10121009
# refer-clojure with reader conditionals 5
10131010

1014-
> FIXME: need to double-check this reader conditional syntax
1015-
10161011
--Input
10171012
(ns com.example.my-app
10181013
(:refer-clojure :exclude [#?@(:cljs [dissoc conj]
@@ -1021,9 +1016,8 @@
10211016

10221017
--Expected
10231018
(ns com.example.my-app
1024-
(:refer-clojure
1025-
:exclude #?@(:clj [frequencies int partition]
1026-
:cljs [conj dissoc])))
1019+
#?(:clj (:refer-clojure :exclude [frequencies int partition])
1020+
:cljs (:refer-clojure :exclude [conj dissoc])))
10271021
--Expected
10281022

10291023
# refer-clojure with reader conditionals 6
@@ -1378,9 +1372,8 @@
13781372

13791373
--Expected
13801374
(ns com.example.my-app
1381-
(:refer-clojure
1382-
:exclude #?@(:clj [aaa bbb ccc]
1383-
:cljs [aaa ddd])))
1375+
#?(:clj (:refer-clojure :exclude [aaa bbb ccc])
1376+
:cljs (:refer-clojure :exclude [aaa ddd])))
13841377
--Expected
13851378

13861379
# GitHub Issue #55 - metadata in ns 1
@@ -1543,9 +1536,8 @@
15431536

15441537
--Expected
15451538
(ns cljs.analyzer
1546-
(:refer-clojure
1547-
:exclude #?@(:clj [ensure macroexpand-1]
1548-
:cljs [ensure js-reserved macroexpand-1 ns-interns]))
1539+
#?(:clj (:refer-clojure :exclude [ensure macroexpand-1])
1540+
:cljs (:refer-clojure :exclude [ensure js-reserved macroexpand-1 ns-interns]))
15491541
#?(:cljs
15501542
(:require-macros
15511543
[cljs.analyzer.macros :refer [allowing-redef disallowing-ns* disallowing-recur no-warn with-warning-handlers wrapping-errors]]
@@ -2086,3 +2078,19 @@
20862078
:impl-ns iii
20872079
:load-impl-ns false)) ;; hhh
20882080
--Expected
2081+
2082+
# GitHub Issue #210 - bug with refer-clojure and reader conditionals
2083+
2084+
> https://github.com/oakmac/standard-clojure-style-js/issues/210
2085+
2086+
--Input
2087+
(ns repro
2088+
#?(:cljs (:refer-clojure :exclude [juxt])
2089+
:clj (:refer-clojure :exclude [juxt format])))
2090+
--Input
2091+
2092+
--Expected
2093+
(ns repro
2094+
#?(:clj (:refer-clojure :exclude [format juxt])
2095+
:cljs (:refer-clojure :exclude [juxt])))
2096+
--Expected

0 commit comments

Comments
 (0)