Skip to content

Commit

Permalink
GitHub Issue #135 - fix bug with :import and multiple reader conditio…
Browse files Browse the repository at this point in the history
…nals (#134)
  • Loading branch information
oakmac authored Oct 20, 2024
1 parent 7145053 commit cb8f16d
Show file tree
Hide file tree
Showing 3 changed files with 405 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,10 +1227,14 @@
// ---------------------------------------------------------------------------
// Parse Namespace

function compareSymbols (itmA, itmB) {
function compareSymbolsThenPlatform (itmA, itmB) {
if (itmA.symbol > itmB.symbol) return 1
else if (itmA.symbol < itmB.symbol) return -1
else return 0
else if (itmA.symbol === itmB.symbol) {
if (itmA.platform > itmB.platform) return 1
else if (itmA.platform < itmB.platform) return -1
}
return 0
}

function compareFromSymbol (itmA, itmB) {
Expand Down Expand Up @@ -1302,12 +1306,12 @@
function sortNsResult (result, prefixListComments) {
// sort :refer-clojure :exclude symbols
if (result.referClojure && isArray(result.referClojure.exclude)) {
result.referClojure.exclude.sort(compareSymbols)
result.referClojure.exclude.sort(compareSymbolsThenPlatform)
}

// sort :refer-clojure :only symbols
if (result.referClojure && isArray(result.referClojure.only)) {
result.referClojure.only.sort(compareSymbols)
result.referClojure.only.sort(compareSymbolsThenPlatform)
}

// sort :refer-clojure :rename symbols
Expand All @@ -1317,22 +1321,22 @@

// sort :require-macros symbols
if (isArray(result.requireMacros)) {
result.requireMacros.sort(compareSymbols)
result.requireMacros.sort(compareSymbolsThenPlatform)

// sort :refer symbols
let rmIdx = 0
const numRequireMacrosResults = arraySize(result.requireMacros)
while (rmIdx < numRequireMacrosResults) {
if (isArray(result.requireMacros[rmIdx].refer)) {
result.requireMacros[rmIdx].refer.sort(compareSymbols)
result.requireMacros[rmIdx].refer.sort(compareSymbolsThenPlatform)
}
rmIdx = inc(rmIdx)
}
}

// sort the requires symbols
if (isArray(result.requires)) {
result.requires.sort(compareSymbols)
result.requires.sort(compareSymbolsThenPlatform)

const numRequires = arraySize(result.requires)
let requiresIdx = 0
Expand All @@ -1357,12 +1361,12 @@

// sort :require :refer symbols
if (isArray(result.requires[requiresIdx].refer)) {
result.requires[requiresIdx].refer.sort(compareSymbols)
result.requires[requiresIdx].refer.sort(compareSymbolsThenPlatform)
}

// sort :require :exclude symbols
if (isArray(result.requires[requiresIdx].exclude)) {
result.requires[requiresIdx].exclude.sort(compareSymbols)
result.requires[requiresIdx].exclude.sort(compareSymbolsThenPlatform)
}

// sort :require :rename symbols
Expand Down Expand Up @@ -3140,6 +3144,10 @@
outTxt = strConcat(outTxt, ' ')
isImportKeywordPrinted = true
} else if (isFirstPlatform) {
if (!isImportKeywordPrinted) {
outTxt = strConcat(outTxt, '\n (:import')
isImportKeywordPrinted = true
}
outTxt = strConcat3(outTxt, '\n #?@(', platformStr)
outTxt = strConcat(outTxt, '\n [')
isFirstPlatform = false
Expand Down
137 changes: 137 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -1590,3 +1590,140 @@
#?(:clj (def ^:dynamic *unchecked-if* false))
#?(:clj (def ^:dynamic *unchecked-arrays* false))
--Expected

# GitHub Issue #135 - portal.runtime.cson before

--Input
(ns ^:no-doc portal.runtime.cson
"Clojure/Script Object Notation"
(:refer-clojure :exclude [read])
#?(:clj (:require [portal.runtime.json-buffer :as json])
:cljr (:require [portal.runtime.json-buffer :as json])
:joyride
(:require
[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m])
:org.babashka/nbb
(:require
[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m])
:cljs
(:require
[goog.crypt.base64 :as Base64]
[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]))
#?(:clj (:import [java.net URL]
[java.util Base64 Date UUID])
:joyride (:import)
:org.babashka/nbb (:import)
:cljs (:import [goog.math Long])))

(defprotocol ToJson (-to-json [value buffer]))
--Input

--Expected
(ns portal.runtime.cson
"Clojure/Script Object Notation"
{:no-doc true}
(:refer-clojure :exclude [read])
(:require
#?@(:clj
[[portal.runtime.json-buffer :as json]]

:cljr
[[portal.runtime.json-buffer :as json]]

:cljs
[[goog.crypt.base64 :as Base64]
[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]

:joyride
[[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]

:org.babashka/nbb
[[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]))
(:import
#?@(:clj
[(java.net URL)
(java.util Base64 Date UUID)]

:cljs
[(goog.math Long)])))

(defprotocol ToJson (-to-json [value buffer]))
--Expected

# GitHub Issue #135 - portal.runtime.cson after

--Input
(ns portal.runtime.cson
"Clojure/Script Object Notation"
{:no-doc true}
(:refer-clojure :exclude [read])
(:require
#?@(:clj
[[portal.runtime.json-buffer :as json]]

:cljr
[[portal.runtime.json-buffer :as json]]

:cljs
[[goog.crypt.base64 :as Base64]
[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]

:joyride
[[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]

:org.babashka/nbb
[[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]))
(:import
#?@(:clj
[(java.net URL)
(java.util Base64 Date UUID)]

:cljs
[(goog.math Long)])))

(defprotocol ToJson (-to-json [value buffer]))
--Input

--Expected
(ns portal.runtime.cson
"Clojure/Script Object Notation"
{:no-doc true}
(:refer-clojure :exclude [read])
(:require
#?@(:clj
[[portal.runtime.json-buffer :as json]]

:cljr
[[portal.runtime.json-buffer :as json]]

:cljs
[[goog.crypt.base64 :as Base64]
[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]

:joyride
[[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]

:org.babashka/nbb
[[portal.runtime.json-buffer :as json]
[portal.runtime.macros :as m]]))
(:import
#?@(:clj
[(java.net URL)
(java.util Base64 Date UUID)]

:cljs
[(goog.math Long)])))

(defprotocol ToJson (-to-json [value buffer]))
--Expected
Loading

0 comments on commit cb8f16d

Please sign in to comment.