Skip to content

Support the optional attr-map? on the ns macro #1161

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

Merged
merged 2 commits into from
Dec 4, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
* Added support for the optional `attr-map?` on the `ns` macro (#1159)

### Fixed
* Fix a bug where `#` characters were not legal in keywords and symbols (#1149)
* Fix a bug where seqs were not considered valid input for matching clauses of the `case` macro (#1148)
Expand Down
40 changes: 26 additions & 14 deletions src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -5241,10 +5241,14 @@
You may include an optional docstring for the namespace to describe its purpose. The
docstring will be applied as the ``:doc`` key on the namespace metadata map.

An optional map of metadata can be given after the docstring. If provided, this
metadata will be merged into the metadata for the resulting namespace.

Example::

(ns my.namespace
\"My namespace with code\"
{:meta-key 1}
(:refer-basilisp :exclude [get])
(:require
[basilisp.string :as str])
Expand Down Expand Up @@ -5273,18 +5277,26 @@
(when-not (and (symbol? name) (nil? (namespace name)))
(throw (ex-info "Namespace name must be a non-namespaced symbol"
{:name name})))
(let [doc (when (string? (first opts))
(first opts))
opts (if doc (rest opts) opts)
opts (reduce* (fn [m opt]
(let [opt-name (first opt)
options (rest opt)]
(when-not (keyword? opt-name)
(throw (ex-info "Namespace option must be a keyword"
{:option opt-name})))
(assoc m opt-name (vec options))))
{}
opts)
(let [doc (when (string? (first opts))
(first opts))
opts (if doc (rest opts) opts)
attr-map (when (map? (first opts))
(first opts))
metadata (merge (->> (meta name)
(remove #(= (namespace (key %)) "basilisp.lang.reader"))
(apply hash-map))
{:doc doc}
attr-map)
opts (if attr-map (rest opts) opts)
opts (reduce* (fn [m opt]
(let [opt-name (first opt)
options (rest opt)]
(when-not (keyword? opt-name)
(throw (ex-info "Namespace option must be a keyword"
{:option opt-name})))
(assoc m opt-name (vec options))))
{}
opts)

refer-filters (when-let [filters (or (:refer-basilisp opts)
(:refer-clojure opts))]
Expand All @@ -5300,8 +5312,8 @@
`(^:use-var-indirection do
(in-ns (quote ~name))
(swap! *loaded-libs* conj (quote ~name))
~(when doc
`(alter-meta! (the-ns (quote ~name)) assoc :doc ~doc))
~(when metadata
`(reset-meta! (the-ns (quote ~name)) ~metadata))
(refer-basilisp ~@refer-filters)
~requires
~uses
Expand Down
4 changes: 3 additions & 1 deletion tests/basilisp/testrunner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class TestTestrunner:
@pytest.fixture
def run_result(self, pytester: pytest.Pytester) -> pytest.RunResult:
runtime.Namespace.remove(sym.symbol("test-testrunner"))
code = """
(ns test-testrunner
(:require
Expand Down Expand Up @@ -222,7 +223,7 @@ def test_fixtures(pytester: pytest.Pytester):
[
("error-during-setup", ":once", 2, 0, 0),
("error-during-setup", ":each", 2, 0, 0),
("error-during-teardown", ":once", 3, 0, 0),
("error-during-teardown", ":once", 1, 1, 1),
("error-during-teardown", ":each", 2, 1, 1),
],
)
Expand All @@ -234,6 +235,7 @@ def test_fixtures_with_errors(
passes: int,
failures: int,
):
runtime.Namespace.remove(sym.symbol("test-fixtures-with-errors"))
code = f"""
(ns test-fixtures-with-errors
(:require
Expand Down
Loading