Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
/ rewrite-cljs Public archive

Traverse and rewrite Clojure/ClojureScript/EDN from ClojureScript

License

Notifications You must be signed in to change notification settings

clj-commons/rewrite-cljs

Repository files navigation

rewrite-cljs

This library is a ClojureScript port of it’s fabulous big-brother rewrite-clj. It provides features to traverse and rewrite Clojure/ClojureScript/EDN documents in a whitespace- and comment-aware manner.

Tip
The initial driver for creating the port was to provide Clojure/ClojureScript refactoring support in Light Table. But that is just one of many potentially wickedly cool use-cases for this library.
The library includes:
  • An EDN parser

  • An EDN aware zipper (using clojure.zip for ClojureScript)

  • A customized cljs.reader (based on clojurescript-in-clojurescript) that mimics more of clojure.tools.reader

Build Status
rewrite cljs
Join%20Chat

Usage

The aim is that this lib should work as closely as possible as it’s original Clojure counterpart. I’ll try to document/highlight the individual differences eventually.

Documentation

Sample

Here’s a little teaser on the sort of things you can do with the zipper features.

(ns rewrite-clj.zip-test
  (:require-macros [cemerick.cljs.test :refer (is deftest )])
  (:require [cemerick.cljs.test :as t]
            [rewrite-clj.zip :as z]
            [rewrite-clj.node :as n]))

(deftest manipulate-sexpr
  (let [sexpr "
 ^{:dynamic true} (+ 1 1
   (+ 2 2)
   (reduce + [1 3 4]))"
        expected "
 ^{:dynamic true} (+ 1 1
   (+ 2 2)
   (reduce + [6 7 [1 2]]))"]
    (is (= expected (-> sexpr
                        z/of-string
                        (z/find-tag-by-pos {:row 4 :col 19} :vector)
                        (z/replace [5 6 7])
                        (z/append-child [1 2])
                        z/down
                        z/remove
                        z/root-string)))))

Limitations and omissions

  • There is no support for parsing files (duh)

  • cljs.extended.reader which is used for reading edn/clojure/clojurescript, has lot of limitations. Pls don’t be surprised when encountering errors during reading of perhaps legal but hopefully infrequently used language constructs.

  • Some features in rewrite-clj is "heavily" based on macros, these features have been omitted for now

Warning
The reader makes a captures positional metadata {:row :col :end-row :end-col} for all nodes. As long as you are only traversing the nodes you should be fine using the meta data and functions that depend on them (example zip/find-last-by-pos). However if you perform any form of rewriting the meta-data can’t be trusted any longer. Not sure how to address that tbh. Pull requests are more than welcome !

Rationale

Why not cljx in the rewrite-clj ?

I’m not really prepared to invest heavily in learning cljx for this particular lib. Xsc and I concluded that we could consider backporting when cljc Reader Conditionals becomes more mainstream.

Clojure/ClojureScript support

If you need to use an older version of ClojureScript (like I do for Light Table), you need to use an 0.3.x version of the lib.

License

License for rewrite-cljs

The MIT License (MIT)

Copyright (c) 2015 Magnus Rundberget

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

License for rewrite-clj

The MIT License (MIT)

Copyright (c) 2013-2015 Yannick Scherer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Traverse and rewrite Clojure/ClojureScript/EDN from ClojureScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published