Skip to content

Commit

Permalink
GitHub Issue #166 - fix bug with :refer :all :rename (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac authored Dec 3, 2024
1 parent 5544e13 commit ff473d4
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 24 deletions.
53 changes: 29 additions & 24 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1753,6 +1754,9 @@
collectRequireExcludeSymbols = false
requireExcludeSymbolParenDepth = -1
}
if (insideRequireForm && parenNestingDepth < requireRenameParenNestingDepth) {
requireRenameParenNestingDepth = -1
}

requireMacrosReferNodeIdx = -1
requireMacrosRenameIdx = -1
Expand Down Expand Up @@ -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 = []
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 additions & 0 deletions test_parse_ns/parse_ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ff473d4

Please sign in to comment.