From ff473d439842db75e5f988a403da662d7b7571ce Mon Sep 17 00:00:00 2001 From: Chris Oakman Date: Mon, 2 Dec 2024 18:33:40 -0600 Subject: [PATCH] GitHub Issue #166 - fix bug with :refer :all :rename (#167) --- lib/standard-clojure-style.js | 53 ++++++++++++++++++---------------- test_format/ns.eno | 30 +++++++++++++++++++ test_parse_ns/parse_ns.eno | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 24 deletions(-) diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index 144ba1a..177f2ee 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -1626,6 +1626,7 @@ let insideReaderComment = false let idOfLastNodeInsideReaderComment = -1 let requireRenameIdx = -1 + let requireRenameParenNestingDepth = -1 let skipNodesUntilWeReachThisId = -1 let sectionToAttachEolCommentsTo = null let nextTokenIsRequireDefaultSymbol = false @@ -1753,6 +1754,9 @@ collectRequireExcludeSymbols = false requireExcludeSymbolParenDepth = -1 } + if (insideRequireForm && parenNestingDepth < requireRenameParenNestingDepth) { + requireRenameParenNestingDepth = -1 + } requireMacrosReferNodeIdx = -1 requireMacrosRenameIdx = -1 @@ -2159,8 +2163,30 @@ result.requires[activeRequireIdx].default = node.text nextTokenIsRequireDefaultSymbol = false + // collect :require renames + } else if (insideRequireForm && insideRequireList && requireRenameIdx > 0 && idx > requireRenameIdx && parenNestingDepth > requireRenameParenNestingDepth && isTokenNode2 && isTextNode) { + stackPush(renamesTmp, node.text) + + if (arraySize(renamesTmp) === 2) { + const itm = {} + itm.fromSymbol = renamesTmp[0] + itm.toSymbol = renamesTmp[1] + + if (insideReaderConditional && currentReaderConditionalPlatform) { + itm.platform = currentReaderConditionalPlatform + } + + if (!isArray(result.requires[activeRequireIdx].rename)) { + result.requires[activeRequireIdx].rename = [] + } + + stackPush(result.requires[activeRequireIdx].rename, itm) + + renamesTmp = [] + } + // collect :require :refer symbols - } else if (idx > referIdx && insideRequireForm && parenNestingDepth === inc(referParenNestingDepth) && isTokenNode2 && isTextNode) { + } else if (idx > referIdx && insideRequireForm && referParenNestingDepth !== -1 && parenNestingDepth > referParenNestingDepth && isTokenNode2 && isTextNode) { if (!isArray(result.requires[activeRequireIdx].refer)) { result.requires[activeRequireIdx].refer = [] } @@ -2211,28 +2237,6 @@ requireFormLineNo = lineNo insidePrefixList = true - // collect :require renames - } else if (insideRequireForm && insideRequireList && requireRenameIdx > 0 && idx > requireRenameIdx && isTokenNode2 && isTextNode) { - stackPush(renamesTmp, node.text) - - if (arraySize(renamesTmp) === 2) { - const itm = {} - itm.fromSymbol = renamesTmp[0] - itm.toSymbol = renamesTmp[1] - - if (insideReaderConditional && currentReaderConditionalPlatform) { - itm.platform = currentReaderConditionalPlatform - } - - if (!isArray(result.requires[activeRequireIdx].rename)) { - result.requires[activeRequireIdx].rename = [] - } - - stackPush(result.requires[activeRequireIdx].rename, itm) - - renamesTmp = [] - } - // collect :require symbol inside of a list / vector } else if (insideRequireForm && insideRequireList && idx > requireNodeIdx && isTokenNode2 && isTextNode && requireSymbolIdx === -1 && !isKeywordNode(node)) { if (!isArray(result.requires)) { @@ -2288,9 +2292,10 @@ } } - // :rename inside require + // :rename inside require } else if (insideRequireForm && insideRequireList && idx > requireNodeIdx && isRenameKeyword(node)) { requireRenameIdx = idx + requireRenameParenNestingDepth = parenNestingDepth renamesTmp = [] // collect require Strings in ClojureScript diff --git a/test_format/ns.eno b/test_format/ns.eno index 9162a25..53b9e47 100644 --- a/test_format/ns.eno +++ b/test_format/ns.eno @@ -1873,3 +1873,33 @@ ["react-select" :default Select] ["react-select/creatable" :default CreatableSelect])) --Expected + +# GitHub Issue #166 - :require :refer :all bug 1 + +> https://github.com/oakmac/standard-clojure-style-js/issues/166 + +--Input +(ns com.example.my-app + (:require [other :refer :all :rename {foo bar}])) +--Input + +--Expected +(ns com.example.my-app + (:require + [other :refer :all :rename {foo bar}])) +--Expected + +# GitHub Issue #166 - :require :refer :all bug 2 + +> https://github.com/oakmac/standard-clojure-style-js/issues/166 + +--Input +(ns com.example.my-app + (:require [other :rename {foo bar} :refer :all])) +--Input + +--Expected +(ns com.example.my-app + (:require + [other :refer :all :rename {foo bar}])) +--Expected diff --git a/test_parse_ns/parse_ns.eno b/test_parse_ns/parse_ns.eno index fd80856..e9ee977 100644 --- a/test_parse_ns/parse_ns.eno +++ b/test_parse_ns/parse_ns.eno @@ -2916,3 +2916,57 @@ ] } --Expected + +# GitHub Issue #166 - :require :refer :all bug 1 + +> https://github.com/oakmac/standard-clojure-style-js/issues/166 + +--Input +(ns com.example.my-app + (:require [other :refer :all :rename {foo bar}])) +--Input + +--Expected +{ + "nsSymbol": "com.example.my-app", + "requires": [ + { + "symbol": "other", + "refer": "all", + "rename": [ + { + "fromSymbol": "foo", + "toSymbol": "bar" + } + ] + } + ] +} +--Expected + +# GitHub Issue #166 - :require :refer :all bug 2 + +> https://github.com/oakmac/standard-clojure-style-js/issues/166 + +--Input +(ns com.example.my-app + (:require [other :rename {foo bar} :refer :all])) +--Input + +--Expected +{ + "nsSymbol": "com.example.my-app", + "requires": [ + { + "symbol": "other", + "refer": "all", + "rename": [ + { + "fromSymbol": "foo", + "toSymbol": "bar" + } + ] + } + ] +} +--Expected