Skip to content

Commit

Permalink
core: Error when 'then' 2nd form not 'else'
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Jun 2, 2024
1 parent d24e67f commit a6cb5d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 7 additions & 5 deletions core/src/yamlscript/transformers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@
rhs (if (> (count (:pairs v1)) 2)
(update-in rhs [:pairs 0] (fn [_] (Sym 'do)))
(update-in rhs [:pairs 0] (fn [_] (Sym '=>))))]
(if (and (> (count pairs) 2) (= k2 (Sym 'else)))
(if (> (count (:pairs v2)) 2)
[lhs (update-in rhs [:pairs 2] (fn [_] (Sym 'do)))]
[lhs (update-in rhs [:pairs 2] (fn [_] (Sym '=>)))])
[lhs rhs])))
(if (> (count pairs) 2)
(if (= k2 (Sym 'else))
(if (> (count (:pairs v2)) 2)
[lhs (update-in rhs [:pairs 2] (fn [_] (Sym 'do)))]
[lhs (update-in rhs [:pairs 2] (fn [_] (Sym '=>)))])
(die "Form after 'then' must be 'else'"))
[lhs rhs])))

;;-----------------------------------------------------------------------------
;; require
Expand Down
12 changes: 12 additions & 0 deletions core/test/transformer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@
e: f
clojure: |
(if (= x 1) (a b) (do (c d) (e f)))
- name: Error when using then and not else
yamlscript: |
!yamlscript/v0
if (x = 1):
then: a(b)
=>:
c: d
e: f
error: |
Form after 'then' must be 'else'
18 changes: 18 additions & 0 deletions core/test/yamlscript/transformer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@
(-> test
:transform
edn/read-string))})

(test/load-yaml-test-files
["test/transformer.yaml"]
{:add-tests true
:pick #(test/has-keys? [:yamlscript :error] %1)
:test (fn [test]
(try
(-> test
:yamlscript
parser/parse
composer/compose
resolver/resolve
builder/build
transformer/transform)
""
(catch Exception e
(:cause (Throwable->map e)))))
:want :error})

0 comments on commit a6cb5d4

Please sign in to comment.