Skip to content

Commit

Permalink
feat(jsx): use identifier expr for jsx elem and attr names
Browse files Browse the repository at this point in the history
BREAKING CHANGE: JSX elem and attr names are now identifiers, values are false if they are not present
  • Loading branch information
kollhof committed Nov 19, 2020
1 parent d862121 commit e666274
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
67 changes: 41 additions & 26 deletions src/lang/jsx/init.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

exports[`JSX extensions parses expr group with call for attr value 1`] = `
jsx:elem (1:0-1:35) Foobar
jsx:attr (1:8-1:21) spam
group (1:13-1:21)
jsx:attr (1:8-1:21)
ident (1:8-1:12) spam
group (1:13-1:21)
call (1:14-1:20)
ident (1:14-1:17) ham
ident (1:18-1:20) ni
jsx:attr (1:22-1:32) shrub
number (1:28-1:32) 1234
jsx:attr (1:22-1:32)
ident (1:22-1:27) shrub
number (1:28-1:32) 1234
`;

exports[`JSX extensions parses fink expr as attr values 1`] = `
jsx:elem (1:0-1:50) Foobar
jsx:attr (1:8-1:16) spam
ident (1:13-1:16) ham
jsx:attr (1:17-1:27) shrub
number (1:23-1:27) 1234
jsx:attr (1:28-1:35) ni
arithm:prefix - (1:31-1:35)
jsx:attr (1:8-1:16)
ident (1:8-1:12) spam
ident (1:13-1:16) ham
jsx:attr (1:17-1:27)
ident (1:17-1:22) shrub
number (1:23-1:27) 1234
jsx:attr (1:28-1:35)
ident (1:28-1:30) ni
arithm:prefix - (1:31-1:35)
number (1:32-1:35) 123
:
jsx:text (1:36-1:41)
Expand All @@ -27,30 +32,35 @@ jsx:elem (1:0-1:50) Foobar

exports[`JSX extensions parses fink expr with gt comparison 1`] = `
jsx:elem (1:0-1:49) Foobar
jsx:attr (1:8-1:24) spam
group (1:13-1:24)
jsx:attr (1:8-1:24)
ident (1:8-1:12) spam
group (1:13-1:24)
comp > (1:14-1:23)
ident (1:14-1:17) foo
number (1:20-1:23) 123
jsx:attr (1:25-1:35) shrub
number (1:31-1:35) 1234
jsx:attr (1:25-1:35)
ident (1:25-1:30) shrub
number (1:31-1:35) 1234
:
jsx:text (1:36-1:40)
" ni "
`;

exports[`JSX extensions parses spread 1`] = `
jsx:elem (1:0-1:28) Foobar
jsx:attr (1:8-1:12) spam
jsx:attr (1:8-1:12)
ident (1:8-1:12) spam
spread ... (1:13-1:19)
ident (1:16-1:19) ham
jsx:attr (1:20-1:25) shrub
jsx:attr (1:20-1:25)
ident (1:20-1:25) shrub
`;

exports[`JSX extensions parses template str attr 1`] = `
jsx:elem (1:0-1:30) Foobar
jsx:attr (1:8-1:27) spam
string ' (1:13-1:27)
jsx:attr (1:8-1:27)
ident (1:8-1:12) spam
string ' (1:13-1:27)
'ni: '
arithm + (1:20-1:25)
number (1:20-1:21) 1
Expand All @@ -60,7 +70,8 @@ jsx:elem (1:0-1:30) Foobar

exports[`JSX parses elem with children: <Foobar><Spam /></Foobar> 1`] = `
jsx:elem (1:0-3:9) Foobar
jsx:attr (1:8-1:10) ni
jsx:attr (1:8-1:10)
ident (1:8-1:10) ni
:
jsx:text (1:11-2:2)
"\\n "
Expand Down Expand Up @@ -113,28 +124,32 @@ jsx:elem (1:0-3:6) div

exports[`JSX parses self closing elem with expr attr: <Foobar spam={ni} /> 1`] = `
jsx:elem (1:0-1:20) Foobar
jsx:attr (1:8-1:17) spam
jsx:expr (1:13-1:17)
jsx:attr (1:8-1:17)
ident (1:8-1:12) spam
jsx:expr (1:13-1:17)
block (1:13-1:17)
ident (1:14-1:16) ni
`;

exports[`JSX parses self closing elem with short attr: <Foobar spam /> 1`] = `
jsx:elem (1:0-1:15) Foobar
jsx:attr (1:8-1:12) spam
jsx:attr (1:8-1:12)
ident (1:8-1:12) spam
`;

exports[`JSX parses self closing elem with str attr: <Foobar spam="ni" /> 1`] = `
jsx:elem (1:0-1:20) Foobar
jsx:attr (1:8-1:17) spam
string " (1:13-1:17)
jsx:attr (1:8-1:17)
ident (1:8-1:12) spam
string " (1:13-1:17)
'ni'
`;

exports[`JSX parses self closing elem with str attr: <Foobar spam='ni' /> 1`] = `
jsx:elem (1:0-1:20) Foobar
jsx:attr (1:8-1:17) spam
string ' (1:13-1:17)
jsx:attr (1:8-1:17)
ident (1:8-1:12) spam
string ' (1:13-1:17)
'ni'
`;

Expand Down
29 changes: 16 additions & 13 deletions src/lang/jsx/jsx.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{terminated_block} = import '../block/init.fnk'
{single_expression} = import '../block/expr.fnk'
{get_unindented_text} = import '../literals/string.fnk'
{identifier} = import '../identifier/init.fnk'



Expand Down Expand Up @@ -54,10 +55,15 @@ get_children = fn ctx:



jsx_ident = fn ctx:
identifier advance ctx



jsx_body = fn name, ctx:
[children, name_ctx] = get_children ctx

end_ctx = advance_expected name_ctx, name
end_ctx = advance_expected name_ctx, name.value
[children, advance_expected end_ctx, '>']


Expand Down Expand Up @@ -89,20 +95,18 @@ jsx_attr_value = fn ctx:


jsx_attr = fn ctx:
name = curr_value ctx
{start} = curr_loc ctx
[name, value_ctx] = jsx_ident ctx
{start} = name.loc

[value, next_ctx] = match ctx:
[value, next_ctx] = match value_ctx:
next_is ?, '=':
[value, next_ctx] = jsx_attr_value advance ctx
[{value}, next_ctx]
jsx_attr_value advance value_ctx

else:
# TODO: should we use identifier as value?
[{}, ctx]
[false, value_ctx]

{end} = curr_loc next_ctx
[{type: 'jsx:attr', name, ...value, loc: {start, end}}, next_ctx]
[{type: 'jsx:attr', name, value, loc: {start, end}}, next_ctx]



Expand Down Expand Up @@ -131,7 +135,7 @@ jsx_props = fn ctx:
jsx_spread_attr advance prop_ctx

else:
jsx_attr advance prop_ctx
jsx_attr prop_ctx

[prop, next_ctx]

Expand All @@ -155,16 +159,15 @@ body_or_end_elem = fn ctx, name:
[true, [], advance_expected ctx, '/>']



jsx_elem = fn ctx:
{start} = curr_loc ctx

elem_ctx = advance ctx
name = curr_value elem_ctx
[name, elem_ctx] = jsx_ident ctx

[props, body_ctx] = jsx_props elem_ctx

[self_closing, children, next_ctx] = body_or_end_elem body_ctx, name

{end} = curr_loc next_ctx

list:
Expand Down

0 comments on commit e666274

Please sign in to comment.