Skip to content

Commit ea65c9f

Browse files
authored
fix: ensure type args directly follow tag name (#152)
* fix: ensure type args directly follow tag name
1 parent 210427c commit ea65c9f

File tree

13 files changed

+226
-211
lines changed

13 files changed

+226
-211
lines changed

.changeset/stale-pants-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": minor
3+
---
4+
5+
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.

src/__tests__/fixtures/attr-method-shorthand-with-type-parameters/__snapshots__/attr-method-shorthand-with-type-parameters.expected.txt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
1╭─ <foo<A>(event: A){
22
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
33
│ ││ ││ │╰─ attrMethod.params.value "event: A"
4-
│ ││ ││ ─ attrMethod.params "(event: A)"
5-
│ ││ │─ attrMethod.typeParams.value
6-
│ ││ ├─ attrMethod.typeParams "<A>"
7-
│ ││ ├─ attrMethod "<A>(event: A){\n console.log(event.type)\n}"
8-
│ ││ ╰─ attrName
4+
│ ││ ││ ─ attrMethod.params "(event: A)"
5+
│ ││ ││ ├─ attrMethod "(event: A){\n console.log(event.type)\n}"
6+
│ ││ ││ ╰─ attrName
7+
│ ││ │╰─ tagTypeArgs.value
8+
│ ││ ╰─ tagTypeArgs "<A>"
99
│ │╰─ tagName "foo"
1010
╰─ ╰─ openTagStart
1111
2╭─ console.log(event.type)
@@ -16,11 +16,11 @@
1616
5╭─ <foo<A> (event: A){
1717
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
1818
│ ││ ││ │╰─ attrMethod.params.value "event: A"
19-
│ ││ ││ ─ attrMethod.params "(event: A)"
20-
│ ││ │─ attrMethod.typeParams.value
21-
│ ││ ├─ attrMethod.typeParams "<A>"
22-
│ ││ ├─ attrMethod "<A> (event: A){\n console.log(event.type)\n}"
23-
│ ││ ╰─ attrName
19+
│ ││ ││ ─ attrMethod.params "(event: A)"
20+
│ ││ ││ ├─ attrMethod "(event: A){\n console.log(event.type)\n}"
21+
│ ││ ││ ╰─ attrName
22+
│ ││ │╰─ tagTypeArgs.value
23+
│ ││ ╰─ tagTypeArgs "<A>"
2424
│ │╰─ tagName "foo"
2525
╰─ ╰─ openTagStart
2626
6╭─ console.log(event.type)
@@ -61,11 +61,11 @@
6161
17╭─ <foo<A, B = string>(event: A & B){
6262
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
6363
│ ││ ││ │╰─ attrMethod.params.value "event: A & B"
64-
│ ││ ││ ─ attrMethod.params "(event: A & B)"
65-
│ ││ │─ attrMethod.typeParams.value "A, B = string"
66-
│ ││ ├─ attrMethod.typeParams "<A, B = string>"
67-
│ ││ ├─ attrMethod "<A, B = string>(event: A & B){\n console.log(event.type)\n}"
68-
│ ││ ╰─ attrName
64+
│ ││ ││ ─ attrMethod.params "(event: A & B)"
65+
│ ││ ││ ├─ attrMethod "(event: A & B){\n console.log(event.type)\n}"
66+
│ ││ ││ ╰─ attrName
67+
│ ││ │╰─ tagTypeArgs.value "A, B = string"
68+
│ ││ ╰─ tagTypeArgs "<A, B = string>"
6969
│ │╰─ tagName "foo"
7070
╰─ ╰─ openTagStart
7171
18╭─ console.log(event.type)
@@ -76,11 +76,11 @@
7676
21╭─ <foo<A, B = string> (event: A & B){
7777
│ ││ ││ ││ ╰─ attrMethod.body "{\n console.log(event.type)\n}"
7878
│ ││ ││ │╰─ attrMethod.params.value "event: A & B"
79-
│ ││ ││ ─ attrMethod.params "(event: A & B)"
80-
│ ││ │─ attrMethod.typeParams.value "A, B = string"
81-
│ ││ ├─ attrMethod.typeParams "<A, B = string>"
82-
│ ││ ├─ attrMethod "<A, B = string> (event: A & B){\n console.log(event.type)\n}"
83-
│ ││ ╰─ attrName
79+
│ ││ ││ ─ attrMethod.params "(event: A & B)"
80+
│ ││ ││ ├─ attrMethod "(event: A & B){\n console.log(event.type)\n}"
81+
│ ││ ││ ╰─ attrName
82+
│ ││ │╰─ tagTypeArgs.value "A, B = string"
83+
│ ││ ╰─ tagTypeArgs "<A, B = string>"
8484
│ │╰─ tagName "foo"
8585
╰─ ╰─ openTagStart
8686
22╭─ console.log(event.type)
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
1╭─ tagname <span>hello frank</span>
2-
│ │ ││ │ │ ╰─ error(INVALID_ATTR_TYPE_PARAMS:Attribute cannot contain type parameters unless it is a shorthand method) "/span"
3-
│ │ ││ │ ╰─ attrName "frank"
4-
│ │ ││ ╰─ attrName "hello"
5-
│ │ │╰─ tagTypeArgs.value "span"
6-
│ │ ╰─ tagTypeArgs "<span>"
2+
│ │ ╰─ 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"
73
╰─ ╰─ tagName "tagname"

src/__tests__/fixtures/invalid-multiple-tag-type-params/__snapshots__/invalid-multiple-tag-type-params.expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
1╭─ <foo<A><B>/>
2-
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must follow a tag name and type paremeters must precede a method or tag parameters.)
2+
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
33
│ ││ │╰─ tagTypeArgs.value
44
│ ││ ╰─ tagTypeArgs "<A>"
55
│ │╰─ tagName "foo"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1╭─ <foo <A>>
2+
│ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
3+
│ │╰─ tagName "foo"
4+
╰─ ╰─ openTagStart
5+
2├─ hi
6+
3├─ </>
7+
4╰─
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<foo <A>>
2+
hi
3+
</>

src/__tests__/fixtures/invalid-type-params-after-args/__snapshots__/invalid-type-params-after-args.expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
1╭─ <foo(a)<A>/>
2-
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must follow a tag name and type paremeters must precede a method or tag parameters.)
2+
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
33
│ ││ │╰─ tagArgs.value
44
│ ││ ╰─ tagArgs "(a)"
55
│ │╰─ tagName "foo"

src/__tests__/fixtures/invalid-type-params-after-params/__snapshots__/invalid-type-params-after-params.expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
1╭─ <foo|a|<A>/>
2-
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must follow a tag name and type paremeters must precede a method or tag parameters.)
2+
│ ││ ││ ╰─ error(INVALID_TAG_TYPES:Unexpected types. Type arguments must directly follow a tag name and type paremeters must precede a method or tag parameters.)
33
│ ││ │╰─ tagParams.value
44
│ ││ ╰─ tagParams "|a|"
55
│ │╰─ tagName "foo"

src/__tests__/fixtures/tag-params-with-type-parameters/__snapshots__/tag-params-with-type-parameters.expected.txt

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
│ ││ ││ ││ ╰─ openTagEnd
33
│ ││ ││ │╰─ tagParams.value "data: A"
44
│ ││ ││ ╰─ tagParams "|data: A|"
5-
│ ││ │╰─ tagTypeParams.value
6-
│ ││ ╰─ tagTypeParams "<A>"
5+
│ ││ │╰─ tagTypeArgs.value
6+
│ ││ ╰─ tagTypeArgs "<A>"
77
│ │╰─ tagName "foo"
88
╰─ ╰─ openTagStart
99
2╭─ hi
@@ -17,8 +17,8 @@
1717
│ ││ ││ ││ ╰─ openTagEnd
1818
│ ││ ││ │╰─ tagParams.value "data: A"
1919
│ ││ ││ ╰─ tagParams "|data: A|"
20-
│ ││ │╰─ tagTypeParams.value
21-
│ ││ ╰─ tagTypeParams "<A>"
20+
│ ││ │╰─ tagTypeArgs.value
21+
│ ││ ╰─ tagTypeArgs "<A>"
2222
│ │╰─ tagName "foo"
2323
╰─ ╰─ openTagStart
2424
6╭─ hi
@@ -62,8 +62,8 @@
6262
│ ││ ││ ││ ╰─ openTagEnd
6363
│ ││ ││ │╰─ tagParams.value "data: A & B"
6464
│ ││ ││ ╰─ tagParams "|data: A & B|"
65-
│ ││ │╰─ tagTypeParams.value "A, B = string"
66-
│ ││ ╰─ tagTypeParams "<A, B = string>"
65+
│ ││ │╰─ tagTypeArgs.value "A, B = string"
66+
│ ││ ╰─ tagTypeArgs "<A, B = string>"
6767
│ │╰─ tagName "foo"
6868
╰─ ╰─ openTagStart
6969
18╭─ hi
@@ -77,8 +77,8 @@
7777
│ ││ ││ ││ ╰─ openTagEnd
7878
│ ││ ││ │╰─ tagParams.value "data: A & B"
7979
│ ││ ││ ╰─ tagParams "|data: A & B|"
80-
│ ││ │╰─ tagTypeParams.value "A, B = string"
81-
│ ││ ╰─ tagTypeParams "<A, B = string>"
80+
│ ││ │╰─ tagTypeArgs.value "A, B = string"
81+
│ ││ ╰─ tagTypeArgs "<A, B = string>"
8282
│ │╰─ tagName "foo"
8383
╰─ ╰─ openTagStart
8484
22╭─ hi
@@ -117,4 +117,54 @@
117117
│ │ ├─ closeTagEnd(foo)
118118
│ │ ╰─ closeTagName
119119
╰─ ╰─ closeTagStart "</"
120-
32╰─
120+
32├─
121+
33╭─ <foo()<A> |data: A|>
122+
│ ││ ││││ ││ ╰─ openTagEnd
123+
│ ││ ││││ │╰─ tagParams.value "data: A"
124+
│ ││ ││││ ╰─ tagParams "|data: A|"
125+
│ ││ │││╰─ tagTypeParams.value
126+
│ ││ ││╰─ tagTypeParams "<A>"
127+
│ ││ │╰─ tagArgs.value
128+
│ ││ ╰─ tagArgs "()"
129+
│ │╰─ tagName "foo"
130+
╰─ ╰─ openTagStart
131+
34╭─ hi
132+
╰─ ╰─ text "\n hi\n"
133+
35╭─ </>
134+
│ │ ├─ closeTagEnd(foo)
135+
│ │ ╰─ closeTagName
136+
╰─ ╰─ closeTagStart "</"
137+
36├─
138+
37╭─ <foo/x<A> |data: A|>
139+
│ ││ ││││ ││ ╰─ openTagEnd
140+
│ ││ ││││ │╰─ tagParams.value "data: A"
141+
│ ││ ││││ ╰─ tagParams "|data: A|"
142+
│ ││ │││╰─ tagTypeParams.value
143+
│ ││ ││╰─ tagTypeParams "<A>"
144+
│ ││ │╰─ tagVar.value
145+
│ ││ ╰─ tagVar "/x"
146+
│ │╰─ tagName "foo"
147+
╰─ ╰─ openTagStart
148+
38╭─ hi
149+
╰─ ╰─ text "\n hi\n"
150+
39╭─ </>
151+
│ │ ├─ closeTagEnd(foo)
152+
│ │ ╰─ closeTagName
153+
╰─ ╰─ closeTagStart "</"
154+
40├─
155+
41╭─ <foo<string><A> |data: A|>
156+
│ ││ ││ ││ ││ ╰─ openTagEnd
157+
│ ││ ││ ││ │╰─ tagParams.value "data: A"
158+
│ ││ ││ ││ ╰─ tagParams "|data: A|"
159+
│ ││ ││ │╰─ tagTypeParams.value
160+
│ ││ ││ ╰─ tagTypeParams "<A>"
161+
│ ││ │╰─ tagTypeArgs.value "string"
162+
│ ││ ╰─ tagTypeArgs "<string>"
163+
│ │╰─ tagName "foo"
164+
╰─ ╰─ openTagStart
165+
42╭─ hi
166+
╰─ ╰─ text "\n hi\n"
167+
43╭─ </>
168+
│ │ ├─ closeTagEnd(foo)
169+
│ │ ╰─ closeTagName
170+
╰─ ╰─ closeTagStart "</"

src/__tests__/fixtures/tag-params-with-type-parameters/input.marko

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@
2929
<foo <A, B = string> |data: A & B|>
3030
hi
3131
</>
32+
33+
<foo()<A> |data: A|>
34+
hi
35+
</>
36+
37+
<foo/x<A> |data: A|>
38+
hi
39+
</>
40+
41+
<foo<string><A> |data: A|>
42+
hi
43+
</>

0 commit comments

Comments
 (0)