Skip to content

added support for literal paths in requires #169

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ A little Clojure-like LISP in JavaScript.

<!-- Load from a file: -->
<script type="application/wisp" src="my-script.wisp"></script>

<!-- Using from JS: -->
<script>
var wisp = require('wisp/compiler');
var {code, error} = wisp.compile(` ; this transpiles wisp into JS
(alert "Hello world!")
`);
(code ? console.log(code) : console.error(error));
wisp.evaluate(` ; this evaluates the script
(alert "Hello world!")
`);
</script>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```

3. Or install the binary with npm:
Expand Down
35 changes: 19 additions & 16 deletions doc/language-essentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ approach and supports only one essential way of importing modules:
[fs]
[wisp.backend.javascript.writer :as writer]
[wisp.sequence
:refer [first rest]
:rename {first car rest cdr}]))
:refer [first rest]
:rename {first car rest cdr}]))
```

Let's go through the above example to get a complete picture regarding
Expand All @@ -674,20 +674,23 @@ and is completely optional.
3. The `(:require ...)` form defines dependencies that will be
imported at runtime, and the example above imports multiple modules:

1. First it imports the `start-host!` function from the
`interactivate.host` module. That will be loaded from the
`../host` location, since because module paths are resolved
relative to a name, but only if they share the same root.
2. The second form imports `fs` module and makes it available under
the same name. Note that in this case it could have been
written without wrapping it in brackets.
3. The third form imports `wisp.backend.javascript.writer` module
from `wisp/backend/javascript/writer` and makes it available
via the name `writer`.
4. The last and most complex form imports `first` and `rest`
functions from the `wisp.sequence` module, although it also
renames them and there for makes available under different
`car` and `cdr` names.
1. First it imports the `start-host!` function from the
`interactivate.host` module. That will be loaded from the
`../host` location, since because module paths are resolved
relative to a name, but only if they share the same root.
2. The second form imports `fs` module and makes it available under
the same name. Note that in this case it could have been
written without wrapping it in brackets.
3. The third form imports `wisp.backend.javascript.writer` module
from `wisp/backend/javascript/writer` and makes it available
via the name `writer`.
4. The last and most complex form imports `first` and `rest`
functions from the `wisp.sequence` module, although it also
renames them and there for makes available under different
`car` and `cdr` names.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed indent here.


It's also possible to specify a string literal instead of a module name;
this allows for providing custom require paths when you're having trouble with generated ones.

While Clojure has many other kinds of reference forms they are
not recognized by _wisp_ and will therefore be ignored.
Expand Down
38 changes: 20 additions & 18 deletions src/backend/escodegen/writer.wisp
Original file line number Diff line number Diff line change
Expand Up @@ -711,30 +711,32 @@

(defn resolve
[from to]
(let [requirer (split (name from) \.)
requirement (split (name to) \.)
relative? (and (not (identical? (name from)
(name to)))
(identical? (first requirer)
(first requirement)))]
(if relative?
(loop [from requirer
to requirement]
(if (identical? (first from)
(first to))
(recur (rest from) (rest to))
(join \/
(concat [\.]
(repeat (dec (count from)) "..")
to))))
(join \/ requirement))))
(if (string? to)
to
(let [requirer (split (name from) \.)
requirement (split (name to) \.)
relative? (and (not (identical? (name from)
(name to)))
(identical? (first requirer)
(first requirement)))]
(if-not relative?
(join \/ requirement)
(loop [from requirer
to requirement]
(if (identical? (first from)
(first to))
(recur (rest from) (rest to))
(join \/
(concat [\.]
(repeat (dec (count from)) "..")
to))))))))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indented let (as part of adding an if string? check) and swapped branches of if relative?


(defn id->ns
"Takes namespace identifier symbol and translates to new
symbol without . special characters
wisp.core -> wisp*core"
[id]
(symbol nil (join \* (split (name id) \.))))
(symbol nil (join \* (split (name id) #"[./]"))))


(defn write-require
Expand Down
2 changes: 1 addition & 1 deletion test/ast.wisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns wisp.test.ast
(:require [wisp.test.util :refe [is thrown?]]
(:require [wisp.test.util :refer [is thrown?]]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…Apparently importing macros does nothing after all?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it will import all macros regardless. Intention was to only import ones been referred, but that never made it.

[wisp.src.reader :refer [read-from-string]]
[wisp.src.sequence :refer [list]]
[wisp.src.runtime :refer [str =]]
Expand Down
13 changes: 10 additions & 3 deletions test/escodegen.wisp
Original file line number Diff line number Diff line change
Expand Up @@ -910,19 +910,22 @@
;; ns


(is (= (transpile "(ns foo.bar
(is (= (transpile "(ns foo.bar.baz
\"hello world\"
(:require lib.a
[lib.b]
[lib.c :as c]
[lib.d :refer [foo bar]]
[lib.e :refer [beep baz] :as e]
[lib.f :refer [faz] :rename {faz saz}]
[lib.g :refer [beer] :rename {beer coffee} :as booze]))")
[lib.g :refer [beer] :rename {beer coffee} :as booze]
foo.bar.foo
foo.baz
[\"../test/bar\" :as test]))")

"{
var _ns_ = {
id: 'foo.bar',
id: 'foo.bar.baz',
doc: 'hello world'
};
var lib_a = require('lib/a');
Expand All @@ -941,6 +944,10 @@
var lib_g = require('lib/g');
var booze = lib_g;
var coffee = lib_g.beer;
var foo_bar_foo = require('./foo');
var foo_baz = require('./../baz');
var ___test_bar = require('../test/bar');
var test = ___test_bar;
}"))

(is (= (transpile "(ns wisp.example.main
Expand Down
3 changes: 2 additions & 1 deletion test/util.wisp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
(do
(.push *failed* '~form)
(console.error (str "Fail: " ~msg "\n"
"expected: "
(pr-str '~form) "\n"
"expected: "
(pr-str '~expected) "\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way makes it easier to compare long literals.

" actual: "
(pr-str (try ~actual (catch error (list 'throw (list 'Error (.-message error))))))))
false)))))
Expand Down