Skip to content

Commit e9973f1

Browse files
authored
GitHub Issue #187 - fix ns parsing bug when :require has both :rename and :refer (#188)
1 parent 34967b9 commit e9973f1

File tree

3 files changed

+129
-29
lines changed

3 files changed

+129
-29
lines changed

lib/standard-clojure-style.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,8 @@
16361636
let genClassValueLineNo = -1
16371637
let insideReaderComment = false
16381638
let idOfLastNodeInsideReaderComment = -1
1639-
let requireRenameIdx = -1
1640-
let requireRenameParenNestingDepth = -1
1639+
let renameIdx = -1
1640+
let renameParenNestingDepth = -1
16411641
let skipNodesUntilWeReachThisId = -1
16421642
let sectionToAttachEolCommentsTo = null
16431643
let nextTokenIsRequireDefaultSymbol = false
@@ -1702,7 +1702,7 @@
17021702
insideReaderConditional = true
17031703
currentReaderConditionalPlatform = null
17041704
readerConditionalParenNestingDepth = parenNestingDepth
1705-
} else if (insideRequireForm) {
1705+
} else if (insideRequireForm && requireListParenNestingDepth === -1) {
17061706
insideRequireList = true
17071707
requireListParenNestingDepth = parenNestingDepth
17081708
} else if (insideImportForm && parenNestingDepth > importNodeParenNestingDepth) {
@@ -1712,25 +1712,24 @@
17121712
parenNestingDepth = dec(parenNestingDepth)
17131713
stackPop(parenStack)
17141714

1715-
// TODO: should these be "else if"s or just "ifs" ?
1716-
// I think maybe they should be "if"s
1715+
// We can assume there is only one ns form per file and exit the main
1716+
// loop once we have finished parsing it.
1717+
if (insideNsForm && parenNestingDepth === 0) {
1718+
insideNsForm = false
1719+
nsFormEndsLineIdx = lineNo
1720+
}
1721+
17171722
if (insideImportPackageList) {
17181723
insideImportPackageList = false
17191724
importPackageListFirstToken = null
1720-
} else if (insideRequireForm && parenNestingDepth < requireFormParenNestingDepth) {
1725+
}
1726+
if (insideRequireForm && parenNestingDepth < requireFormParenNestingDepth) {
17211727
insideRequireForm = false
1722-
} else if (insideRequireList && parenNestingDepth < requireListParenNestingDepth) {
1723-
insideRequireList = false
1724-
requireListParenNestingDepth = -1
1725-
requireRenameIdx = -1
1726-
} else if (insideReferClojureForm && parenNestingDepth < referClojureParenNestingDepth) {
1728+
requireFormParenNestingDepth = -1
1729+
}
1730+
if (insideReferClojureForm && parenNestingDepth < referClojureParenNestingDepth) {
17271731
insideReferClojureForm = false
17281732
referClojureNodeIdx = -1
1729-
} else if (insideNsForm && parenNestingDepth === 0) {
1730-
// We can assume there is only one ns form per file and exit the main
1731-
// loop once we have finished parsing it.
1732-
insideNsForm = false
1733-
nsFormEndsLineIdx = lineNo
17341733
}
17351734

17361735
if (insideReferClojureForm && parenNestingDepth <= referClojureParenNestingDepth) {
@@ -1739,11 +1738,25 @@
17391738
collectReferClojureRenameSymbols = false
17401739
}
17411740

1742-
if (referIdx > 0 && parenNestingDepth < referParenNestingDepth) {
1741+
// we are finished collecting :refer symbols
1742+
if (referIdx > 0 && parenNestingDepth <= referParenNestingDepth) {
17431743
referIdx = -1
17441744
referParenNestingDepth = -1
1745+
}
1746+
1747+
// we are finished collecting :rename symbols
1748+
if (renameIdx > 0 && parenNestingDepth <= renameParenNestingDepth) {
1749+
renameIdx = -1
1750+
renameParenNestingDepth = -1
1751+
}
1752+
1753+
// we are finished collecting require prefix list symbols
1754+
if (insideRequireList && parenNestingDepth < requireListParenNestingDepth) {
1755+
insideRequireList = false
1756+
requireListParenNestingDepth = -1
17451757
nextTokenIsRequireDefaultSymbol = false
17461758
}
1759+
17471760
if (insideRequireForm && requireSymbolIdx > 0) {
17481761
requireSymbolIdx = -1
17491762
}
@@ -1775,9 +1788,6 @@
17751788
collectRequireExcludeSymbols = false
17761789
requireExcludeSymbolParenDepth = -1
17771790
}
1778-
if (insideRequireForm && parenNestingDepth < requireRenameParenNestingDepth) {
1779-
requireRenameParenNestingDepth = -1
1780-
}
17811791

17821792
requireMacrosReferNodeIdx = -1
17831793
requireMacrosRenameIdx = -1
@@ -2187,8 +2197,14 @@
21872197
result.requires[activeRequireIdx].default = node.text
21882198
nextTokenIsRequireDefaultSymbol = false
21892199

2200+
// :rename keyword inside a :require list
2201+
} else if (insideRequireForm && insideRequireList && renameIdx === -1 && isRenameKeyword(node)) {
2202+
renameIdx = idx
2203+
renameParenNestingDepth = parenNestingDepth
2204+
renamesTmp = []
2205+
21902206
// collect :require renames
2191-
} else if (insideRequireForm && insideRequireList && requireRenameIdx > 0 && idx > requireRenameIdx && parenNestingDepth > requireRenameParenNestingDepth && isTokenNode2 && isTextNode) {
2207+
} else if (insideRequireForm && insideRequireList && renameIdx > 0 && idx > renameIdx && parenNestingDepth > renameParenNestingDepth && isTokenNode2 && isTextNode) {
21922208
stackPush(renamesTmp, node.text)
21932209

21942210
if (arraySize(renamesTmp) === 2) {
@@ -2259,10 +2275,9 @@
22592275
activeRequireIdx = inc(activeRequireIdx)
22602276
requireSymbolIdx = idx
22612277
requireFormLineNo = lineNo
2262-
insidePrefixList = true
22632278

22642279
// collect :require symbol inside of a list / vector
2265-
} else if (insideRequireForm && insideRequireList && idx > requireNodeIdx && isTokenNode2 && isTextNode && requireSymbolIdx === -1 && !isKeywordNode(node)) {
2280+
} else if (insideRequireForm && insideRequireList && idx > requireNodeIdx && referIdx === -1 && renameIdx === -1 && isTokenNode2 && isTextNode && requireSymbolIdx === -1 && !isKeywordNode(node)) {
22662281
if (!isArray(result.requires)) {
22672282
result.requires = []
22682283
}
@@ -2316,12 +2331,6 @@
23162331
}
23172332
}
23182333

2319-
// :rename inside require
2320-
} else if (insideRequireForm && insideRequireList && idx > requireNodeIdx && isRenameKeyword(node)) {
2321-
requireRenameIdx = idx
2322-
requireRenameParenNestingDepth = parenNestingDepth
2323-
renamesTmp = []
2324-
23252334
// collect require Strings in ClojureScript
23262335
} else if (insideRequireForm && insideRequireList && idx > requireNodeIdx && isStringNode(node)) {
23272336
if (!isArray(result.requires)) {

test_format/ns.eno

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,3 +1949,39 @@
19491949
(:require
19501950
[clojure.string :as str]))
19511951
--Expected
1952+
1953+
# GitHub Issue #187 - :require with :refer and :rename 1
1954+
1955+
> https://github.com/oakmac/standard-clojure-style-js/issues/187
1956+
1957+
--Input
1958+
(ns com.example.my-app
1959+
(:require
1960+
[foo.bar :rename {ccc ddd, aaa bbb} :refer [zzz xxx yyy]]
1961+
[biz.baz :refer [iii jjj] :rename {ooo ppp qqq rrr}]
1962+
))
1963+
--Input
1964+
1965+
--Expected
1966+
(ns com.example.my-app
1967+
(:require
1968+
[biz.baz :refer [iii jjj] :rename {ooo ppp, qqq rrr}]
1969+
[foo.bar :refer [xxx yyy zzz] :rename {aaa bbb, ccc ddd}]))
1970+
--Expected
1971+
1972+
# GitHub Issue #187 - :require with :refer and :rename 2
1973+
1974+
> https://github.com/oakmac/standard-clojure-style-js/issues/187
1975+
1976+
--Input
1977+
(ns com.example.my-app
1978+
(:require
1979+
[garden.selectors :refer [&] :rename {& parent}]
1980+
))
1981+
--Input
1982+
1983+
--Expected
1984+
(ns com.example.my-app
1985+
(:require
1986+
[garden.selectors :refer [&] :rename {& parent}]))
1987+
--Expected

test_parse_ns/parse_ns.eno

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,3 +3177,58 @@
31773177
]
31783178
}
31793179
--Expected
3180+
3181+
# GitHub Issue #187 - :require with :refer and :rename
3182+
3183+
> https://github.com/oakmac/standard-clojure-style-js/issues/187
3184+
3185+
--Input
3186+
(ns com.example.my-app
3187+
(:require
3188+
[foo.bar :rename {ccc ddd, aaa bbb} :refer [zzz xxx yyy]]
3189+
[biz.baz :refer [iii jjj] :rename {ooo ppp qqq rrr}]
3190+
))
3191+
--Input
3192+
3193+
--Expected
3194+
{
3195+
"nsSymbol": "com.example.my-app",
3196+
"requires": [
3197+
{
3198+
"symbol": "biz.baz",
3199+
"refer": [
3200+
{"symbol": "iii"},
3201+
{"symbol": "jjj"}
3202+
],
3203+
"rename": [
3204+
{
3205+
"fromSymbol": "ooo",
3206+
"toSymbol": "ppp"
3207+
},
3208+
{
3209+
"fromSymbol": "qqq",
3210+
"toSymbol": "rrr"
3211+
}
3212+
]
3213+
},
3214+
{
3215+
"symbol": "foo.bar",
3216+
"refer": [
3217+
{"symbol": "xxx"},
3218+
{"symbol": "yyy"},
3219+
{"symbol": "zzz"}
3220+
],
3221+
"rename": [
3222+
{
3223+
"fromSymbol": "aaa",
3224+
"toSymbol": "bbb"
3225+
},
3226+
{
3227+
"fromSymbol": "ccc",
3228+
"toSymbol": "ddd"
3229+
}
3230+
]
3231+
}
3232+
]
3233+
}
3234+
--Expected

0 commit comments

Comments
 (0)