Skip to content

Commit

Permalink
fix: ensure type args directly follow tag name (#152)
Browse files Browse the repository at this point in the history
* fix: ensure type args directly follow tag name
  • Loading branch information
DylanPiercey authored Feb 25, 2023
1 parent 210427c commit ea65c9f
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 211 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-pants-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Improve handling ambiguity with tag type args vs type params. Type args must now always be directly adjacent the tag name, otherwise it will become type params.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
1╭─ <foo<A>(event: A){
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
│ ││ ││ │╰─ attrMethod.params.value "event: A"
│ ││ ││ ─ attrMethod.params "(event: A)"
│ ││ │─ attrMethod.typeParams.value
│ ││ ├─ attrMethod.typeParams "<A>"
│ ││ ├─ attrMethod "<A>(event: A){\n console.log(event.type)\n}"
│ ││ ╰─ attrName
│ ││ ││ ─ attrMethod.params "(event: A)"
│ ││ ││ ├─ attrMethod "(event: A){\n console.log(event.type)\n}"
│ ││ ││ ╰─ attrName
│ ││ │╰─ tagTypeArgs.value
│ ││ ╰─ tagTypeArgs "<A>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
2╭─ console.log(event.type)
Expand All @@ -16,11 +16,11 @@
5╭─ <foo<A> (event: A){
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
│ ││ ││ │╰─ attrMethod.params.value "event: A"
│ ││ ││ ─ attrMethod.params "(event: A)"
│ ││ │─ attrMethod.typeParams.value
│ ││ ├─ attrMethod.typeParams "<A>"
│ ││ ├─ attrMethod "<A> (event: A){\n console.log(event.type)\n}"
│ ││ ╰─ attrName
│ ││ ││ ─ attrMethod.params "(event: A)"
│ ││ ││ ├─ attrMethod "(event: A){\n console.log(event.type)\n}"
│ ││ ││ ╰─ attrName
│ ││ │╰─ tagTypeArgs.value
│ ││ ╰─ tagTypeArgs "<A>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
6╭─ console.log(event.type)
Expand Down Expand Up @@ -61,11 +61,11 @@
17╭─ <foo<A, B = string>(event: A & B){
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
│ ││ ││ │╰─ attrMethod.params.value "event: A & B"
│ ││ ││ ─ attrMethod.params "(event: A & B)"
│ ││ │─ attrMethod.typeParams.value "A, B = string"
│ ││ ├─ attrMethod.typeParams "<A, B = string>"
│ ││ ├─ attrMethod "<A, B = string>(event: A & B){\n console.log(event.type)\n}"
│ ││ ╰─ attrName
│ ││ ││ ─ attrMethod.params "(event: A & B)"
│ ││ ││ ├─ attrMethod "(event: A & B){\n console.log(event.type)\n}"
│ ││ ││ ╰─ attrName
│ ││ │╰─ tagTypeArgs.value "A, B = string"
│ ││ ╰─ tagTypeArgs "<A, B = string>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
18╭─ console.log(event.type)
Expand All @@ -76,11 +76,11 @@
21╭─ <foo<A, B = string> (event: A & B){
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
│ ││ ││ │╰─ attrMethod.params.value "event: A & B"
│ ││ ││ ─ attrMethod.params "(event: A & B)"
│ ││ │─ attrMethod.typeParams.value "A, B = string"
│ ││ ├─ attrMethod.typeParams "<A, B = string>"
│ ││ ├─ attrMethod "<A, B = string> (event: A & B){\n console.log(event.type)\n}"
│ ││ ╰─ attrName
│ ││ ││ ─ attrMethod.params "(event: A & B)"
│ ││ ││ ├─ attrMethod "(event: A & B){\n console.log(event.type)\n}"
│ ││ ││ ╰─ attrName
│ ││ │╰─ tagTypeArgs.value "A, B = string"
│ ││ ╰─ tagTypeArgs "<A, B = string>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
22╭─ console.log(event.type)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
1╭─ tagname <span>hello frank</span>
│ │ ││ │ │ ╰─ error(INVALID_ATTR_TYPE_PARAMS:Attribute cannot contain type parameters unless it is a shorthand method) "/span"
│ │ ││ │ ╰─ attrName "frank"
│ │ ││ ╰─ attrName "hello"
│ │ │╰─ tagTypeArgs.value "span"
│ │ ╰─ tagTypeArgs "<span>"
│ │ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.) "span"
╰─ ╰─ tagName "tagname"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1╭─ <foo<A><B>/>
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must follow a tag name and type paremeters must precede a method or tag parameters.)
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
│ ││ │╰─ tagTypeArgs.value
│ ││ ╰─ tagTypeArgs "<A>"
│ │╰─ tagName "foo"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1╭─ <foo <A>>
│ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
2├─ hi
3├─ </>
4╰─
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<foo <A>>
hi
</>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1╭─ <foo(a)<A>/>
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must follow a tag name and type paremeters must precede a method or tag parameters.)
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
│ ││ │╰─ tagArgs.value
│ ││ ╰─ tagArgs "(a)"
│ │╰─ tagName "foo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1╭─ <foo|a|<A>/>
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must follow a tag name and type paremeters must precede a method or tag parameters.)
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
│ ││ │╰─ tagParams.value
│ ││ ╰─ tagParams "|a|"
│ │╰─ tagName "foo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
│ ││ ││ ││ ╰─ openTagEnd
│ ││ ││ │╰─ tagParams.value "data: A"
│ ││ ││ ╰─ tagParams "|data: A|"
│ ││ │╰─ tagTypeParams.value
│ ││ ╰─ tagTypeParams "<A>"
│ ││ │╰─ tagTypeArgs.value
│ ││ ╰─ tagTypeArgs "<A>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
2╭─ hi
Expand All @@ -17,8 +17,8 @@
│ ││ ││ ││ ╰─ openTagEnd
│ ││ ││ │╰─ tagParams.value "data: A"
│ ││ ││ ╰─ tagParams "|data: A|"
│ ││ │╰─ tagTypeParams.value
│ ││ ╰─ tagTypeParams "<A>"
│ ││ │╰─ tagTypeArgs.value
│ ││ ╰─ tagTypeArgs "<A>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
6╭─ hi
Expand Down Expand Up @@ -62,8 +62,8 @@
│ ││ ││ ││ ╰─ openTagEnd
│ ││ ││ │╰─ tagParams.value "data: A & B"
│ ││ ││ ╰─ tagParams "|data: A & B|"
│ ││ │╰─ tagTypeParams.value "A, B = string"
│ ││ ╰─ tagTypeParams "<A, B = string>"
│ ││ │╰─ tagTypeArgs.value "A, B = string"
│ ││ ╰─ tagTypeArgs "<A, B = string>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
18╭─ hi
Expand All @@ -77,8 +77,8 @@
│ ││ ││ ││ ╰─ openTagEnd
│ ││ ││ │╰─ tagParams.value "data: A & B"
│ ││ ││ ╰─ tagParams "|data: A & B|"
│ ││ │╰─ tagTypeParams.value "A, B = string"
│ ││ ╰─ tagTypeParams "<A, B = string>"
│ ││ │╰─ tagTypeArgs.value "A, B = string"
│ ││ ╰─ tagTypeArgs "<A, B = string>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
22╭─ hi
Expand Down Expand Up @@ -117,4 +117,54 @@
│ │ ├─ closeTagEnd(foo)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
32╰─
32├─
33╭─ <foo()<A> |data: A|>
│ ││ ││││ ││ ╰─ openTagEnd
│ ││ ││││ │╰─ tagParams.value "data: A"
│ ││ ││││ ╰─ tagParams "|data: A|"
│ ││ │││╰─ tagTypeParams.value
│ ││ ││╰─ tagTypeParams "<A>"
│ ││ │╰─ tagArgs.value
│ ││ ╰─ tagArgs "()"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
34╭─ hi
╰─ ╰─ text "\n hi\n"
35╭─ </>
│ │ ├─ closeTagEnd(foo)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
36├─
37╭─ <foo/x<A> |data: A|>
│ ││ ││││ ││ ╰─ openTagEnd
│ ││ ││││ │╰─ tagParams.value "data: A"
│ ││ ││││ ╰─ tagParams "|data: A|"
│ ││ │││╰─ tagTypeParams.value
│ ││ ││╰─ tagTypeParams "<A>"
│ ││ │╰─ tagVar.value
│ ││ ╰─ tagVar "/x"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
38╭─ hi
╰─ ╰─ text "\n hi\n"
39╭─ </>
│ │ ├─ closeTagEnd(foo)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
40├─
41╭─ <foo<string><A> |data: A|>
│ ││ ││ ││ ││ ╰─ openTagEnd
│ ││ ││ ││ │╰─ tagParams.value "data: A"
│ ││ ││ ││ ╰─ tagParams "|data: A|"
│ ││ ││ │╰─ tagTypeParams.value
│ ││ ││ ╰─ tagTypeParams "<A>"
│ ││ │╰─ tagTypeArgs.value "string"
│ ││ ╰─ tagTypeArgs "<string>"
│ │╰─ tagName "foo"
╰─ ╰─ openTagStart
42╭─ hi
╰─ ╰─ text "\n hi\n"
43╭─ </>
│ │ ├─ closeTagEnd(foo)
│ │ ╰─ closeTagName
╰─ ╰─ closeTagStart "</"
12 changes: 12 additions & 0 deletions src/__tests__/fixtures/tag-params-with-type-parameters/input.marko
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@
<foo <A, B = string> |data: A & B|>
hi
</>

<foo()<A> |data: A|>
hi
</>

<foo/x<A> |data: A|>
hi
</>

<foo<string><A> |data: A|>
hi
</>
Loading

0 comments on commit ea65c9f

Please sign in to comment.