From 3eaca9dd24d7eccd9c8600e17c72e2e090cb8b38 Mon Sep 17 00:00:00 2001 From: Chris Oakman Date: Mon, 28 Oct 2024 23:39:16 -0500 Subject: [PATCH] GitHub Issue #142 - ns bug with paren nesting levels (#147) --- lib/standard-clojure-style.js | 11 ++++-- test_format/ns.eno | 27 +++++++++++++++ test_parse_ns/parse_ns.eno | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index e0a8cc0..1c39a21 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -1498,7 +1498,9 @@ const parenStack = [] let insideNsForm = false let insideReferClojureForm = false + let referClojureParenNestingDepth = -1 let insideRequireForm = false + let requireFormParenNestingDepth = -1 let requireFormLineNo = -1 let insideImportForm = false let importFormLineNo = -1 @@ -1576,11 +1578,13 @@ nsNodeIdx = idx } else if (insideNsForm && isReferClojureNode(node)) { insideReferClojureForm = true + referClojureParenNestingDepth = parenNestingDepth sectionToAttachEolCommentsTo = 'refer-clojure' referClojureNodeIdx = idx beyondNsMetadata = true } else if (insideNsForm && isRequireNode(node)) { insideRequireForm = true + requireFormParenNestingDepth = parenNestingDepth requireFormLineNo = lineNo requireNodeIdx = idx beyondNsMetadata = true @@ -1629,13 +1633,13 @@ if (insideImportPackageList) { insideImportPackageList = false importPackageListFirstToken = null - } else if (insideRequireForm && parenNestingDepth <= 1) { + } else if (insideRequireForm && parenNestingDepth < requireFormParenNestingDepth) { insideRequireForm = false } else if (insideRequireList && parenNestingDepth < requireListParenNestingDepth) { insideRequireList = false requireListParenNestingDepth = -1 requireRenameIdx = -1 - } else if (insideReferClojureForm && parenNestingDepth <= 1) { + } else if (insideReferClojureForm && parenNestingDepth < referClojureParenNestingDepth) { insideReferClojureForm = false referClojureNodeIdx = -1 } else if (insideNsForm && parenNestingDepth === 0) { @@ -1645,11 +1649,12 @@ nsFormEndsLineIdx = lineNo } - if (parenNestingDepth < 3) { + if (insideReferClojureForm && parenNestingDepth <= referClojureParenNestingDepth) { collectReferClojureExcludeSymbols = false collectReferClojureOnlySymbols = false collectReferClojureRenameSymbols = false } + if (referIdx > 0 && parenNestingDepth < referParenNestingDepth) { referIdx = -1 referParenNestingDepth = -1 diff --git a/test_format/ns.eno b/test_format/ns.eno index d69da3c..985aec4 100644 --- a/test_format/ns.eno +++ b/test_format/ns.eno @@ -1826,3 +1826,30 @@ ;; bbb (:refer-clojure :only [-> number? set]))) ;; ccc --Expected + +# GitHub Issue #142 - bug with reader conditionals + +--Input +(ns ^{:doc "ClojureScript wrapper functions for math operations" + :author "Paula Gearon" } + cljs.math + ;; create space to load this in Clojure for testing + #?@(:clj ((:refer-clojure :exclude [aset aget + unsigned-bit-shift-right bit-shift-right bit-shift-left]) + (:require [js :refer [aset aget +]]) + (:import [js ArrayBuffer Uint8Array Uint32Array Float64Array])))) +--Input + +--Expected +(ns cljs.math + {:doc "ClojureScript wrapper functions for math operations" + :author "Paula Gearon"} + #?(:clj + ;; create space to load this in Clojure for testing + (:refer-clojure :exclude [+ aget aset bit-shift-left bit-shift-right unsigned-bit-shift-right])) + #?(:clj + (:require + [js :refer [+ aget aset]])) + #?(:clj + (:import + (js ArrayBuffer Float64Array Uint32Array Uint8Array)))) +--Expected diff --git a/test_parse_ns/parse_ns.eno b/test_parse_ns/parse_ns.eno index 9c9d890..dc90a33 100644 --- a/test_parse_ns/parse_ns.eno +++ b/test_parse_ns/parse_ns.eno @@ -2818,3 +2818,66 @@ } } --Expected + +# GitHub Issue #142 - cljs.math reader conditional example + +> https://github.com/oakmac/standard-clojure-style-js/issues/142 + +--Input +(ns ^{:doc "ClojureScript wrapper functions for math operations" + :author "Paula Gearon" } + cljs.math + ;; create space to load this in Clojure for testing + #?@(:clj ((:refer-clojure :exclude [aset aget + unsigned-bit-shift-right bit-shift-right bit-shift-left]) + (:require [js :refer [aset aget +]]) + (:import [js ArrayBuffer Uint8Array Uint32Array Float64Array])))) + +#?(:clj (def number Double)) +--Input + +--Expected +{ + "nsSymbol": "cljs.math", + "nsMetadata": [ + { + "key": ":doc", + "value": "\"ClojureScript wrapper functions for math operations\"" + }, + { + "key": ":author", + "value": "\"Paula Gearon\"" + } + ], + "referClojureCommentsAbove": [ + ";; create space to load this in Clojure for testing" + ], + "referClojure": { + "exclude": [ + { "symbol": "+", "platform": ":clj" }, + { "symbol": "aget", "platform": ":clj" }, + { "symbol": "aset", "platform": ":clj" }, + { "symbol": "bit-shift-left", "platform": ":clj" }, + { "symbol": "bit-shift-right", "platform": ":clj" }, + { "symbol": "unsigned-bit-shift-right", "platform": ":clj" } + ] + }, + "requires": [ + { + "symbol": "js", + "refer": [ + {"symbol": "+"}, + {"symbol": "aget"}, + {"symbol": "aset"} + ], + "platform": ":clj" + } + ], + "imports": [ + { + "package": "js", + "classes": ["ArrayBuffer", "Float64Array", "Uint32Array", "Uint8Array"], + "platform": ":clj" + } + ] +} +--Expected