Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

In the following, the first and last should throw:

cljs.user=> (set! *print-fn-bodies*) nil cljs.user=> (set! *print-fn-bodies* true) true cljs.user=> (set! target -prop 3) true cljs.user=> (set! *print-fn-bodies* true false true) true

Implementation guidance:

The message thrown should be derived from Clojure, perhaps "Malformed assignment, expecting (set! target val) or (set! target -prop val)"

The check and throw can be added to the analyzer's code which parses 'set!:
https://github.com/clojure/clojurescript/blob/6eedd0a08c49f7b0d4dcb30977b2fb38c90577bd/src/main/clojure/cljs/analyzer.cljc#L2229

An example of a similar throw is in the analyzer code for {{'if}}:
https://github.com/clojure/clojurescript/blob/6eedd0a08c49f7b0d4dcb30977b2fb38c90577bd/src/main/clojure/cljs/analyzer.cljc#L1446-L1449

Tests can be added in {{cljs.analyzer-tests}} (which is run via {{lein test}}): https://github.com/clojure/clojurescript/blob/master/src/test/clojure/cljs/analyzer_tests.clj

9 Answers

0 votes
by

Comment made by: namenu

set! supports 3-arity form so error message from Clojure could be syntatically wrong.

Is it ok to throw an error just like {{'if}}?

0 votes
by

Comment made by: thheller

The code suggests that passing 3 args is actually valid.

(set! o -prop val) (set! (.-prop o) val)

0 votes
by

Comment made by: mfikes

Ahh, right Thomas. I'll update the description.

0 votes
by

Comment made by: mfikes

Hyunwoo: Yes

0 votes
by

Comment made by: sulami

This should do it (I hope). Verified that it builds and works on my machine with all projects I have.

0 votes
by

Comment made by: mfikes

Hi Robin. Thanks for your contribution. Have you signed the CA?

0 votes
by

Comment made by: sulami

I just have, yes.

0 votes
by

Comment made by: mfikes

CLJS-2923.patch LGTM. It behaves properly, and passes in CI and Canary tests.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-2923 (reported by mfikes)
...