day8 / re-com

A ClojureScript library of reusable components for Reagent
https://re-com.day8.com.au
MIT License
796 stars 147 forks source link

Fix error when rewrite-clj reads uneval forms #266

Closed MawiraIke closed 3 years ago

MawiraIke commented 3 years ago

Currently reading a file with an uneval form results in an error, operation not permitted. As rewrite-clj loops through files, it skips whitespaces and comments but does not skip uneval forms such as #_[re-com.core ...]. This makes it extremely hard for us to know if the current component we are looping is an uneval form or code to execute. For example if we have the component

  [h-box
    :size "1"
    :children [#_[title :label "Hidden"]
               [v-box :children []]
               [box :child []]]]]]

If rewrite-clj.zip/next would skip uneval forms, we would've expected the next location, as we loop, to follow the path: [h-box ...] -> h-box -> :size -> "1" -> :children -> [#_[title ...]...] -> [v-box...] -> :children -> [] -> etc

If rewrite-clj would have treated uneval forms as flat objects like strings we would've looped through: [h-box ...] -> h-box -> :size -> "1" -> :children -> [#_[title ...]...] -> #_[title...] -> [v-box...] -> :children -> [] -> etc

but instead follows the path [h-box ...] -> h-box -> :size -> "1" -> :children -> [#_[title ...]...] -> #_[title...] -> [title ...] -> :label -> "Hidden" -> [v-box...] -> :children -> [] -> etc

Notice that currently rewrite-clj.zip/next finds #_[title :label "Hidden"] and on the next iteration loops through [title :label "Hidden"]. This makes it hard for us to know if the current component is an uneval form or code to be executed.

This PR prevents the error from being thrown but prints to the console when an uneval form is found. In case the uneval form is a commented re-com component, it gets :src annotations added to it since rewrite-clj does not skip uneval forms as mentioned in this issue

superstructor commented 3 years ago

Thanks @MawiraIke 👍🏻