From 2055ad7a896e6970bdb992c5b863886985755425 Mon Sep 17 00:00:00 2001 From: Jan Klaas Kollhof Date: Thu, 30 Sep 2021 19:51:44 +0200 Subject: [PATCH] fix: jsx identifier handling --- src/ir/jsx/init.fnk | 20 +++++++++++++++++++- src/ir/jsx/init.test.fnk.snap | 20 ++++++++++---------- src/js/jsx/init.fnk | 9 ++++++++- src/js/jsx/init.test.fnk | 10 +++++++++- src/js/jsx/init.test.fnk.snap | 8 ++++++++ 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/ir/jsx/init.fnk b/src/ir/jsx/init.fnk index 6a48688..40da46c 100644 --- a/src/ir/jsx/init.fnk +++ b/src/ir/jsx/init.fnk @@ -1,3 +1,5 @@ +{lower_case} = import '@fink/std-lib/str.fnk' + {add, any, ir_fn} = import '../context.fnk' {transform} = import '../transform.fnk' {lst, lst_a} = import '../literals/list.fnk' @@ -84,8 +86,24 @@ transform_children = fn {children, loc}, ctx: +first_is_lower = fn [s]: + match lower_case s: + s: true + else: false + + + +transform_elem_ident = fn expr, ctx: + match expr: + {value: first_is_lower ?}: + str expr.value, 'jsxi', expr, ctx + else: + transform expr, 'jsxi', ctx + + + transform_jsx_elem = fn node, res_id, ctx: - [name, name_id, props_ctx] = transform node.name, 'jsxi', ctx + [name, name_id, props_ctx] = transform_elem_ident node.name, ctx [props, props_id, chldrn_ctx] = transform_props node, props_ctx [chldrn, chldrn_id, jxe_ctx] = transform_children node, chldrn_ctx [elem, , next_ctx] = jxe name_id, props_id, chldrn_id, res_id, node, jxe_ctx diff --git a/src/ir/jsx/init.test.fnk.snap b/src/ir/jsx/init.test.fnk.snap index 84844c6..cb68834 100644 --- a/src/ir/jsx/init.test.fnk.snap +++ b/src/ir/jsx/init.test.fnk.snap @@ -151,7 +151,7 @@ rec_e fn exports_0: exports[`jsx compiles empty elem 1`] = ` " rec_e fn exports_0: - id b, fn jsxi_0: + str 'b', fn jsxi_0: rec_e fn props_0: lst_e fn chldrn_0: jxe jsxi_0, props_0, chldrn_0, fn elem_0: @@ -178,7 +178,7 @@ rec_e fn exports_0: lst_e fn chldrn_0: str '\\\\n foo\\\\n ', fn chld_0: lst_a chldrn_0, chld_0, fn chldrn_1: - id p, fn jsxi_0: + str 'p', fn jsxi_0: rec_e fn props_0: lst_e fn chldrn_2: str 'bar', fn chld_2: @@ -197,7 +197,7 @@ rec_e fn exports_0: exports[`jsx compiles hypenate props 1`] = ` " rec_e fn exports_0: - id a, fn jsxi_0: + str 'a', fn jsxi_0: rec_e fn props_0: str 'foo-bar', fn prpn_0: str '1234', fn str_0: @@ -214,7 +214,7 @@ rec_e fn exports_0: exports[`jsx compiles shorthand 1`] = ` " rec_e fn exports_0: - id a, fn jsxi_0: + str 'a', fn jsxi_0: rec_e fn props_0: lst_e fn chldrn_0: jxe jsxi_0, props_0, chldrn_0, fn elem_0: @@ -227,7 +227,7 @@ rec_e fn exports_0: exports[`jsx compiles with children and expr 1`] = ` " rec_e fn exports_0: - id a, fn jsxi_0: + str 'a', fn jsxi_0: rec_e fn props_0: lst_e fn chldrn_0: str '\\\\n foo ', fn chld_0: @@ -236,14 +236,14 @@ rec_e fn exports_0: lst_a chldrn_1, chld_1, fn chldrn_2: str '\\\\n ', fn chld_2: lst_a chldrn_2, chld_2, fn chldrn_3: - id b, fn jsxi_1: + str 'b', fn jsxi_1: rec_e fn props_1: lst_e fn chldrn_4: jxe jsxi_1, props_1, chldrn_4, fn chld_3: lst_a chldrn_3, chld_3, fn chldrn_5: str ' ham\\\\n spam\\\\n ', fn chld_4: lst_a chldrn_5, chld_4, fn chldrn_6: - id c, fn jsxi_2: + str 'c', fn jsxi_2: rec_e fn props_2: lst_e fn chldrn_7: jxe jsxi_2, props_2, chldrn_7, fn chld_5: @@ -260,7 +260,7 @@ rec_e fn exports_0: exports[`jsx compiles with expr params 1`] = ` " rec_e fn exports_0: - id a, fn jsxi_0: + str 'a', fn jsxi_0: rec_e fn props_0: str 'foo', fn prpn_0: id foo, fn prpv_0: @@ -279,7 +279,7 @@ rec_e fn exports_0: exports[`jsx compiles with expr params 2`] = ` " rec_e fn exports_0: - id a, fn jsxi_0: + str 'a', fn jsxi_0: rec_e fn props_0: str 'foo', fn prpn_0: id foo, fn prpv_0: @@ -299,7 +299,7 @@ rec_e fn exports_0: exports[`jsx compiles with str params 1`] = ` " rec_e fn exports_0: - id a, fn jsxi_0: + str 'a', fn jsxi_0: rec_e fn props_0: str 'foo', fn prpn_0: id foo, fn prpv_0: diff --git a/src/js/jsx/init.fnk b/src/js/jsx/init.fnk index c4e889a..e78855a 100644 --- a/src/js/jsx/init.fnk +++ b/src/js/jsx/init.fnk @@ -71,7 +71,14 @@ transform_children = fn [chld=false, ...children], out=[]: transform_jxe = fn expr, ctx: [{args: [name_id, props_id, chldrn_id]}] = expr - id = jsxIdentifier (get_js name_id, ctx).name + id_v = get_js_literal name_id, ctx + + id = match id_v: + {type: 'TemplateLiteral'}: + {quasis: [{value: {raw: name}}]} = id_v + with_loc name_id, jsxIdentifier name + else: + jsxIdentifier (get_js name_id, ctx).name props = pipe props_id: get_js_literal ?, ctx diff --git a/src/js/jsx/init.test.fnk b/src/js/jsx/init.test.fnk index d6e1131..56e0bac 100644 --- a/src/js/jsx/init.test.fnk +++ b/src/js/jsx/init.test.fnk @@ -5,7 +5,7 @@ describe 'jsx', fn: it 'compiles shorthand', fn: expect - fink2js 'elem = ' + fink2js 'elem = ' to_match_snapshot @@ -75,6 +75,14 @@ describe 'jsx', fn: ' to_match_snapshot + it 'handles ident clashes for lowercaae elems', fn: + expect + fink2js ' + Foo = fn {label}: + + ' + to_match_snapshot + describe 'JSX extensions', fn: it 'compiles shothand props', fn: diff --git a/src/js/jsx/init.test.fnk.snap b/src/js/jsx/init.test.fnk.snap index c774796..f6c8b7b 100644 --- a/src/js/jsx/init.test.fnk.snap +++ b/src/js/jsx/init.test.fnk.snap @@ -104,3 +104,11 @@ exports[`jsx compiles with str params 1`] = ` "const elem_0 = ; export const elem = elem_0;" `; + +exports[`jsx handles ident clashes for lowercaae elems 1`] = ` +"const Foo_0 = drec_0 => { + return ; +}; + +export const Foo = Foo_0;" +`;