Skip to content

Commit

Permalink
fix: jsx identifier handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Sep 30, 2021
1 parent 56bebff commit 2055ad7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
20 changes: 19 additions & 1 deletion src/ir/jsx/init.fnk
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/ir/jsx/init.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion src/js/jsx/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/js/jsx/init.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe 'jsx', fn:
it 'compiles shorthand', fn:
expect
fink2js 'elem = <a/>'
fink2js 'elem = <a />'
to_match_snapshot


Expand Down Expand Up @@ -75,6 +75,14 @@ describe 'jsx', fn:
'
to_match_snapshot

it 'handles ident clashes for lowercaae elems', fn:
expect
fink2js '
Foo = fn {label}:
<label>{label}</label>
'
to_match_snapshot


describe 'JSX extensions', fn:
it 'compiles shothand props', fn:
Expand Down
8 changes: 8 additions & 0 deletions src/js/jsx/init.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,11 @@ exports[`jsx compiles with str params 1`] = `
"const elem_0 = <a foo={foo} bar=\\"ni\\" />;
export const elem = elem_0;"
`;
exports[`jsx handles ident clashes for lowercaae elems 1`] = `
"const Foo_0 = drec_0 => {
return <label>{drec_0.label}</label>;
};
export const Foo = Foo_0;"
`;

0 comments on commit 2055ad7

Please sign in to comment.