From 6e0dd971c9f3950b8a377a5982dbd7c26ddee252 Mon Sep 17 00:00:00 2001 From: Chris Oakman Date: Mon, 18 Nov 2024 14:34:09 -0600 Subject: [PATCH 1/2] WIP - parse ns for :require :default --- lib/standard-clojure-style.js | 15 +++++++++++++++ test_parse_ns/parse_ns.eno | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index 9873443..b052346 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -746,6 +746,10 @@ return node && isString(node.text) && node.text === ':refer' } + function isDefaultKeyword (node) { + return node && isString(node.text) && node.text === ':default' + } + function isReferMacrosKeyword (node) { return node && isString(node.text) && node.text === ':refer-macros' } @@ -1586,6 +1590,7 @@ let requireRenameIdx = -1 let skipNodesUntilWeReachThisId = -1 let sectionToAttachEolCommentsTo = null + let nextTokenIsRequireDefaultSymbol = false while (continueParsingNsForm) { const node = nodesArr[idx] @@ -1677,6 +1682,7 @@ if (referIdx > 0 && parenNestingDepth < referParenNestingDepth) { referIdx = -1 referParenNestingDepth = -1 + nextTokenIsRequireDefaultSymbol = false } if (insideRequireForm && requireSymbolIdx > 0) { requireSymbolIdx = -1 @@ -2084,6 +2090,10 @@ referIdx = idx referParenNestingDepth = parenNestingDepth + // is this :require :default ? + } else if (idx > requireNodeIdx && insideRequireForm && isTokenNode2 && isDefaultKeyword(node)) { + nextTokenIsRequireDefaultSymbol = true + // collect :require :exclude symbols } else if (idx > requireNodeIdx && insideRequireForm && isTokenNode2 && collectRequireExcludeSymbols && parenNestingDepth > requireExcludeSymbolParenDepth) { const symbolObj = { @@ -2106,6 +2116,11 @@ } else if (idx > referIdx && insideRequireForm && isTokenNode2 && isAllNode(node)) { result.requires[activeRequireIdx].refer = 'all' + // collect :refer :default symbol + } else if (idx > referIdx && insideRequireForm && isTokenNode2 && nextTokenIsRequireDefaultSymbol) { + result.requires[activeRequireIdx].default = node.text + nextTokenIsRequireDefaultSymbol = false + // collect :require :refer symbols } else if (idx > referIdx && insideRequireForm && parenNestingDepth === inc(referParenNestingDepth) && isTokenNode2 && isTextNode) { if (!isArray(result.requires[activeRequireIdx].refer)) { diff --git a/test_parse_ns/parse_ns.eno b/test_parse_ns/parse_ns.eno index dc90a33..fd80856 100644 --- a/test_parse_ns/parse_ns.eno +++ b/test_parse_ns/parse_ns.eno @@ -2881,3 +2881,38 @@ ] } --Expected + +# GitHub Issue #158 - ClojureScript require default + +> https://github.com/oakmac/standard-clojure-style-js/issues/158 + +--Input +(ns com.example.my-app + (:require + ["react-dropzone" :default Dropzone] + ["react-select" :default Select] + ["react-select/creatable" :default CreatableSelect])) +--Input + +--Expected +{ + "nsSymbol": "com.example.my-app", + "requires": [ + { + "symbol": "\"react-dropzone\"", + "symbolIsString": true, + "default": "Dropzone" + }, + { + "symbol": "\"react-select\"", + "symbolIsString": true, + "default": "Select" + }, + { + "symbol": "\"react-select/creatable\"", + "symbolIsString": true, + "default": "CreatableSelect" + } + ] +} +--Expected From 4f988e59d74f9df12c8d8656c1f69ec32708cd96 Mon Sep 17 00:00:00 2001 From: Chris Oakman Date: Mon, 18 Nov 2024 14:37:40 -0600 Subject: [PATCH 2/2] WIP - support :require :default --- lib/standard-clojure-style.js | 6 +++--- test_format/ns.eno | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index b052346..f8960e0 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -2536,10 +2536,10 @@ if (isString(req.as) && req.as !== '') { outTxt = strConcat3(outTxt, ' :as ', req.as) - } - - if (isString(req.asAlias) && req.asAlias !== '') { + } else if (isString(req.asAlias) && req.asAlias !== '') { outTxt = strConcat3(outTxt, ' :as-alias ', req.asAlias) + } else if (isString(req.default) && req.default !== '') { + outTxt = strConcat3(outTxt, ' :default ', req.default) } // NOTE: this will not work if the individual :refer symbols are wrapped in a reader conditional diff --git a/test_format/ns.eno b/test_format/ns.eno index 985aec4..9162a25 100644 --- a/test_format/ns.eno +++ b/test_format/ns.eno @@ -1853,3 +1853,23 @@ (:import (js ArrayBuffer Float64Array Uint32Array Uint8Array)))) --Expected + +# GitHub Issue #158 - ClojureScript require default + +> https://github.com/oakmac/standard-clojure-style-js/issues/158 + +--Input +(ns com.example.my-app + (:require + ["react-select" :default Select] + ["react-dropzone" :default Dropzone] + ["react-select/creatable" :default CreatableSelect])) +--Input + +--Expected +(ns com.example.my-app + (:require + ["react-dropzone" :default Dropzone] + ["react-select" :default Select] + ["react-select/creatable" :default CreatableSelect])) +--Expected