Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #158 - support :require :default #160

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -1586,6 +1590,7 @@
let requireRenameIdx = -1
let skipNodesUntilWeReachThisId = -1
let sectionToAttachEolCommentsTo = null
let nextTokenIsRequireDefaultSymbol = false

while (continueParsingNsForm) {
const node = nodesArr[idx]
Expand Down Expand Up @@ -1677,6 +1682,7 @@
if (referIdx > 0 && parenNestingDepth < referParenNestingDepth) {
referIdx = -1
referParenNestingDepth = -1
nextTokenIsRequireDefaultSymbol = false
}
if (insideRequireForm && requireSymbolIdx > 0) {
requireSymbolIdx = -1
Expand Down Expand Up @@ -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 = {
Expand All @@ -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)) {
Expand Down Expand Up @@ -2521,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
Expand Down
20 changes: 20 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions test_parse_ns/parse_ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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