Skip to content

Commit 08c473b

Browse files
authored
Support the optional attr-map? on the ns macro (#1161)
Fixes #1159
1 parent 8ece92f commit 08c473b

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
* Added support for the optional `attr-map?` on the `ns` macro (#1159)
10+
811
### Fixed
912
* Fix a bug where `#` characters were not legal in keywords and symbols (#1149)
1013
* Fix a bug where seqs were not considered valid input for matching clauses of the `case` macro (#1148)

src/basilisp/core.lpy

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5241,10 +5241,14 @@
52415241
You may include an optional docstring for the namespace to describe its purpose. The
52425242
docstring will be applied as the ``:doc`` key on the namespace metadata map.
52435243

5244+
An optional map of metadata can be given after the docstring. If provided, this
5245+
metadata will be merged into the metadata for the resulting namespace.
5246+
52445247
Example::
52455248

52465249
(ns my.namespace
52475250
\"My namespace with code\"
5251+
{:meta-key 1}
52485252
(:refer-basilisp :exclude [get])
52495253
(:require
52505254
[basilisp.string :as str])
@@ -5273,18 +5277,26 @@
52735277
(when-not (and (symbol? name) (nil? (namespace name)))
52745278
(throw (ex-info "Namespace name must be a non-namespaced symbol"
52755279
{:name name})))
5276-
(let [doc (when (string? (first opts))
5277-
(first opts))
5278-
opts (if doc (rest opts) opts)
5279-
opts (reduce* (fn [m opt]
5280-
(let [opt-name (first opt)
5281-
options (rest opt)]
5282-
(when-not (keyword? opt-name)
5283-
(throw (ex-info "Namespace option must be a keyword"
5284-
{:option opt-name})))
5285-
(assoc m opt-name (vec options))))
5286-
{}
5287-
opts)
5280+
(let [doc (when (string? (first opts))
5281+
(first opts))
5282+
opts (if doc (rest opts) opts)
5283+
attr-map (when (map? (first opts))
5284+
(first opts))
5285+
metadata (merge (->> (meta name)
5286+
(remove #(= (namespace (key %)) "basilisp.lang.reader"))
5287+
(apply hash-map))
5288+
{:doc doc}
5289+
attr-map)
5290+
opts (if attr-map (rest opts) opts)
5291+
opts (reduce* (fn [m opt]
5292+
(let [opt-name (first opt)
5293+
options (rest opt)]
5294+
(when-not (keyword? opt-name)
5295+
(throw (ex-info "Namespace option must be a keyword"
5296+
{:option opt-name})))
5297+
(assoc m opt-name (vec options))))
5298+
{}
5299+
opts)
52885300

52895301
refer-filters (when-let [filters (or (:refer-basilisp opts)
52905302
(:refer-clojure opts))]
@@ -5300,8 +5312,8 @@
53005312
`(^:use-var-indirection do
53015313
(in-ns (quote ~name))
53025314
(swap! *loaded-libs* conj (quote ~name))
5303-
~(when doc
5304-
`(alter-meta! (the-ns (quote ~name)) assoc :doc ~doc))
5315+
~(when metadata
5316+
`(reset-meta! (the-ns (quote ~name)) ~metadata))
53055317
(refer-basilisp ~@refer-filters)
53065318
~requires
53075319
~uses

tests/basilisp/testrunner_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class TestTestrunner:
1111
@pytest.fixture
1212
def run_result(self, pytester: pytest.Pytester) -> pytest.RunResult:
13+
runtime.Namespace.remove(sym.symbol("test-testrunner"))
1314
code = """
1415
(ns test-testrunner
1516
(:require
@@ -222,7 +223,7 @@ def test_fixtures(pytester: pytest.Pytester):
222223
[
223224
("error-during-setup", ":once", 2, 0, 0),
224225
("error-during-setup", ":each", 2, 0, 0),
225-
("error-during-teardown", ":once", 3, 0, 0),
226+
("error-during-teardown", ":once", 1, 1, 1),
226227
("error-during-teardown", ":each", 2, 1, 1),
227228
],
228229
)
@@ -234,6 +235,7 @@ def test_fixtures_with_errors(
234235
passes: int,
235236
failures: int,
236237
):
238+
runtime.Namespace.remove(sym.symbol("test-fixtures-with-errors"))
237239
code = f"""
238240
(ns test-fixtures-with-errors
239241
(:require

0 commit comments

Comments
 (0)