lilactown / helix

A simple, easy to use library for React development in ClojureScript.
Eclipse Public License 2.0
627 stars 52 forks source link

Hooks not found inside JSValue #87

Closed SevereOverfl0w closed 2 years ago

SevereOverfl0w commented 3 years ago

I think this should be trivial to fix after #86 is merged. Right now, hooks can't be found when they hide inside pesky #js tagged literals like so:

(when true
  #js [(use-state 10)])

Both the find-hooks and invalid-hooks-usage functions in the analyser need updating to know how to descend into a JSValue.

lilactown commented 3 years ago

I haven't put code to this yet but if I'm understanding the issue right:

When we're in the macroexpansion phase of compiling a CLJS file, our tagged values i.e. #js [(use-state 10)] have already been read and are now an opaque JSValue type, defined here: https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/tagged_literals.cljc#L68

Thus, we need a way of getting the form that is wrapped in this JSValue in order to continue analyzing.

I can think of two ways of doing this:

  1. Special case the analyzer to detect JSValue and access the field (.-val x)
  2. Define a protocol that we extend to JSValue to handle the field access

Using polymorphism for this makes me feel clever, since it would allow outside extension to other reader tags i.e. #hiccup [:div (when true (use-state "hello"))].

I am dubious of the times that this will actually be useful, though.

SevereOverfl0w commented 3 years ago

I'm not sure if #hiccup would apply here, as that's unable to return a special form without extending the ClojureScript compiler. That means #hiccup has to return a record, list, etc. for the compiler to interpret.

The polymorphic approach was a little annoying because you had to extend it to Object, nil, etc.

lilactown commented 2 years ago

Fixed in https://github.com/lilactown/helix/commit/27371f055afb4b0878aa90efc69a727928259ed7 and https://github.com/lilactown/helix/commit/4cfe1857712f82756473fc02cc45ce147d0bbcd6