Skip to content

Commit

Permalink
GitHub Issue #5 - support :require :default in ns forms
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Nov 18, 2024
1 parent 6a9a6ac commit f649c73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
21 changes: 19 additions & 2 deletions standard-clojure-style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 [")
Expand Down
5 changes: 5 additions & 0 deletions test_cases/format_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\") ",
Expand Down
5 changes: 5 additions & 0 deletions test_cases/parse_ns_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
]

0 comments on commit f649c73

Please sign in to comment.