diff --git a/standard-clojure-style.lua b/standard-clojure-style.lua index 949301a..2f91700 100644 --- a/standard-clojure-style.lua +++ b/standard-clojure-style.lua @@ -1077,6 +1077,10 @@ local function isReferKeyword(node) return node and isString(node.text) and node.text == ":refer" end +local function isDefaultKeyword(node) + return node and isString(node.text) and node.text == ":default" +end + local function isReferMacrosKeyword(node) return node and isString(node.text) and node.text == ":refer-macros" end @@ -1916,6 +1920,7 @@ local function parseNs(nodesArr) local requireRenameIdx = -1 local skipNodesUntilWeReachThisId = -1 local sectionToAttachEolCommentsTo = nil + local nextTokenIsRequireDefaultSymbol = false while continueParsingNsForm do local node = nodesArr[idx] @@ -2006,6 +2011,7 @@ local function parseNs(nodesArr) if referIdx > 0 and parenNestingDepth < referParenNestingDepth then referIdx = -1 referParenNestingDepth = -1 + nextTokenIsRequireDefaultSymbol = false end if insideRequireForm and requireSymbolIdx > 0 then requireSymbolIdx = -1 @@ -2482,6 +2488,10 @@ local function parseNs(nodesArr) referIdx = idx referParenNestingDepth = parenNestingDepth + -- is this :require :default ? + elseif idx > requireNodeIdx and insideRequireForm and isTokenNode2 and isDefaultKeyword(node) then + nextTokenIsRequireDefaultSymbol = true + -- collect :require :exclude symbols elseif idx > requireNodeIdx @@ -2510,6 +2520,11 @@ local function parseNs(nodesArr) elseif idx > referIdx and insideRequireForm and isTokenNode2 and isAllNode(node) then result.requires[activeRequireIdx].refer = "all" + -- collect :refer :default symbol + elseif idx > referIdx and insideRequireForm and isTokenNode2 and nextTokenIsRequireDefaultSymbol then + result.requires[activeRequireIdx].default = node.text + nextTokenIsRequireDefaultSymbol = false + -- collect :require :refer symbols elseif idx > referIdx @@ -2979,10 +2994,12 @@ function formatRequireLine(req, initialIndentation) outTxt = strConcat3(outTxt, "[", req.symbol) if isString(req.as) and req.as ~= "" then outTxt = strConcat3(outTxt, " :as ", req.as) - end - if isString(req.asAlias) and req.asAlias ~= "" then + elseif isString(req.asAlias) and req.asAlias ~= "" then outTxt = strConcat3(outTxt, " :as-alias ", req.asAlias) + elseif isString(req.default) and req.default ~= "" then + outTxt = strConcat3(outTxt, " :default ", req.default) end + -- NOTE: this will not work if the individual :refer symbols are wrapped in a reader conditional if isArray(req.refer) and arraySize(req.refer) > 0 then outTxt = strConcat(outTxt, " :refer [") diff --git a/test_cases/format_tests.json b/test_cases/format_tests.json index 81e0a35..594e406 100644 --- a/test_cases/format_tests.json +++ b/test_cases/format_tests.json @@ -764,6 +764,11 @@ "input": "(ns ^{:doc \"ClojureScript wrapper functions for math operations\"\n :author \"Paula Gearon\" }\n cljs.math\n ;; create space to load this in Clojure for testing\n #?@(:clj ((:refer-clojure :exclude [aset aget + unsigned-bit-shift-right bit-shift-right bit-shift-left])\n (:require [js :refer [aset aget +]])\n (:import [js ArrayBuffer Uint8Array Uint32Array Float64Array]))))", "expected": "(ns cljs.math\n {:doc \"ClojureScript wrapper functions for math operations\"\n :author \"Paula Gearon\"}\n #?(:clj\n ;; create space to load this in Clojure for testing\n (:refer-clojure :exclude [+ aget aset bit-shift-left bit-shift-right unsigned-bit-shift-right]))\n #?(:clj\n (:require\n [js :refer [+ aget aset]]))\n #?(:clj\n (:import\n (js ArrayBuffer Float64Array Uint32Array Uint8Array))))" }, + { + "name": "GitHub Issue #158 - ClojureScript require default", + "input": "(ns com.example.my-app\n (:require\n [\"react-select\" :default Select]\n [\"react-dropzone\" :default Dropzone]\n [\"react-select/creatable\" :default CreatableSelect]))", + "expected": "(ns com.example.my-app\n (:require\n [\"react-dropzone\" :default Dropzone]\n [\"react-select\" :default Select]\n [\"react-select/creatable\" :default CreatableSelect]))" + }, { "name": "Trim trailing whitespace 1", "input": "(def aaa \"aaa\") ", diff --git a/test_cases/parse_ns_tests.json b/test_cases/parse_ns_tests.json index 9b0c448..6e9f617 100644 --- a/test_cases/parse_ns_tests.json +++ b/test_cases/parse_ns_tests.json @@ -338,5 +338,10 @@ "name": "GitHub Issue #142 - cljs.math reader conditional example", "input": "(ns ^{:doc \"ClojureScript wrapper functions for math operations\"\n :author \"Paula Gearon\" }\n cljs.math\n ;; create space to load this in Clojure for testing\n #?@(:clj ((:refer-clojure :exclude [aset aget + unsigned-bit-shift-right bit-shift-right bit-shift-left])\n (:require [js :refer [aset aget +]])\n (:import [js ArrayBuffer Uint8Array Uint32Array Float64Array]))))\n\n#?(:clj (def number Double))", "expected": "{\n \"nsSymbol\": \"cljs.math\",\n \"nsMetadata\": [\n {\n \"key\": \":doc\",\n \"value\": \"\\\"ClojureScript wrapper functions for math operations\\\"\"\n },\n {\n \"key\": \":author\",\n \"value\": \"\\\"Paula Gearon\\\"\"\n }\n ],\n \"referClojureCommentsAbove\": [\n \";; create space to load this in Clojure for testing\"\n ],\n \"referClojure\": {\n \"exclude\": [\n { \"symbol\": \"+\", \"platform\": \":clj\" },\n { \"symbol\": \"aget\", \"platform\": \":clj\" },\n { \"symbol\": \"aset\", \"platform\": \":clj\" },\n { \"symbol\": \"bit-shift-left\", \"platform\": \":clj\" },\n { \"symbol\": \"bit-shift-right\", \"platform\": \":clj\" },\n { \"symbol\": \"unsigned-bit-shift-right\", \"platform\": \":clj\" }\n ]\n },\n \"requires\": [\n {\n \"symbol\": \"js\",\n \"refer\": [\n {\"symbol\": \"+\"},\n {\"symbol\": \"aget\"},\n {\"symbol\": \"aset\"}\n ],\n \"platform\": \":clj\"\n }\n ],\n \"imports\": [\n {\n \"package\": \"js\",\n \"classes\": [\"ArrayBuffer\", \"Float64Array\", \"Uint32Array\", \"Uint8Array\"],\n \"platform\": \":clj\"\n }\n ]\n}" + }, + { + "name": "GitHub Issue #158 - ClojureScript require default", + "input": "(ns com.example.my-app\n (:require\n [\"react-dropzone\" :default Dropzone]\n [\"react-select\" :default Select]\n [\"react-select/creatable\" :default CreatableSelect]))", + "expected": "{\n \"nsSymbol\": \"com.example.my-app\",\n \"requires\": [\n {\n \"symbol\": \"\\\"react-dropzone\\\"\",\n \"symbolIsString\": true,\n \"default\": \"Dropzone\"\n },\n {\n \"symbol\": \"\\\"react-select\\\"\",\n \"symbolIsString\": true,\n \"default\": \"Select\"\n },\n {\n \"symbol\": \"\\\"react-select/creatable\\\"\",\n \"symbolIsString\": true,\n \"default\": \"CreatableSelect\"\n }\n ]\n}" } ]