diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index 9cb8702..78901b8 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -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) { @@ -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 @@ -1317,14 +1321,14 @@ // 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) } @@ -1332,7 +1336,7 @@ // sort the requires symbols if (isArray(result.requires)) { - result.requires.sort(compareSymbols) + result.requires.sort(compareSymbolsThenPlatform) const numRequires = arraySize(result.requires) let requiresIdx = 0 @@ -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 @@ -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 diff --git a/test_format/ns.eno b/test_format/ns.eno index 54d915f..aec348e 100644 --- a/test_format/ns.eno +++ b/test_format/ns.eno @@ -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 diff --git a/test_parse_ns/parse_ns.eno b/test_parse_ns/parse_ns.eno index 599493f..a913baf 100644 --- a/test_parse_ns/parse_ns.eno +++ b/test_parse_ns/parse_ns.eno @@ -2475,3 +2475,254 @@ ] } --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 +{ + "nsSymbol": "portal.runtime.cson", + "nsMetadata": [ + { + "key": ":no-doc", + "value": "true" + } + ], + "docstring": "Clojure/Script Object Notation", + "referClojure": { + "exclude": [ + { + "symbol": "read" + } + ] + }, + "requires": [ + { + "symbol": "goog.crypt.base64", + "platform": ":cljs", + "as": "Base64" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":clj", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":cljr", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":cljs", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":joyride", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":org.babashka/nbb", + "as": "json" + }, + { + "symbol": "portal.runtime.macros", + "platform": ":cljs", + "as": "m" + }, + { + "symbol": "portal.runtime.macros", + "platform": ":joyride", + "as": "m" + }, + { + "symbol": "portal.runtime.macros", + "platform": ":org.babashka/nbb", + "as": "m" + } + ], + "imports": [ + { + "package": "goog.math", + "classes": [ + "Long" + ], + "platform": ":cljs" + }, + { + "package": "java.net", + "classes": [ + "URL" + ], + "platform": ":clj" + }, + { + "package": "java.util", + "classes": [ + "Base64", + "Date", + "UUID" + ], + "platform": ":clj" + } + ] +} +--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 +{ + "nsSymbol": "portal.runtime.cson", + "nsMetadata": [ + { + "key": ":no-doc", + "value": "true" + } + ], + "docstring": "Clojure/Script Object Notation", + "referClojure": { + "exclude": [ + { + "symbol": "read" + } + ] + }, + "requires": [ + { + "symbol": "goog.crypt.base64", + "platform": ":cljs", + "as": "Base64" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":clj", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":cljr", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":cljs", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":joyride", + "as": "json" + }, + { + "symbol": "portal.runtime.json-buffer", + "platform": ":org.babashka/nbb", + "as": "json" + }, + { + "symbol": "portal.runtime.macros", + "platform": ":cljs", + "as": "m" + }, + { + "symbol": "portal.runtime.macros", + "platform": ":joyride", + "as": "m" + }, + { + "symbol": "portal.runtime.macros", + "platform": ":org.babashka/nbb", + "as": "m" + } + ], + "imports": [ + { + "package": "goog.math", + "classes": [ + "Long" + ], + "platform": ":cljs" + }, + { + "package": "java.net", + "classes": [ + "URL" + ], + "platform": ":clj" + }, + { + "package": "java.util", + "classes": [ + "Base64", + "Date", + "UUID" + ], + "platform": ":clj" + } + ] +} +--Expected