Skip to content

Commit f297f19

Browse files
committed
fix: improve typescript generic parsing
1 parent 1110d1d commit f297f19

File tree

26 files changed

+347
-46
lines changed

26 files changed

+347
-46
lines changed

.changeset/major-experts-start.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 expression parsing for typescript, especially around multi line generics.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,18 @@ const parser = createParser({
201201
// TagType.void makes this a void element (cannot have children).
202202
return TagType.void;
203203
case "html-comment":
204+
case "html-script":
205+
case "html-style":
204206
case "script":
205207
case "style":
206208
case "textarea":
207209
// TagType.text makes the child content text only (with placeholders).
208210
return TagType.text;
209-
case "class":
210211
case "export":
211212
case "import":
212213
case "static":
214+
case "client":
215+
case "server":
213216
// TagType.statement makes this a statement tag where the content following the tag name will be parsed as script code until we reach a new line, eg for `import x from "y"`).
214217
return TagType.statement;
215218
}

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,16 @@
6363
"bench": "tsx bench.mts",
6464
"build": "tsc -b && tsx build.mts",
6565
"change": "changeset add",
66-
"ci:test": "nyc npm run mocha -- --forbid-only",
66+
"ci:test": "nyc npm test -- --forbid-only",
6767
"format": "npm run lint:eslint -- --fix && npm run lint:prettier -- --write && (fixpack || true)",
6868
"lint": "tsc -b && npm run lint:eslint && npm run lint:prettier -- -l && fixpack",
6969
"lint:eslint": "eslint -f visualstudio .",
7070
"lint:prettier": "prettier \"./**/*{.ts,.js,.json,.md,.yml,rc}\"",
71-
"mocha": "cross-env NODE_ENV=test mocha \"./src/**/__tests__/*.test.ts\"",
7271
"prepare": "husky",
7372
"release": "npm run build && changeset publish",
7473
"report": "open ./coverage/lcov-report/index.html",
75-
"test": "npm run mocha -- --watch",
76-
"test:inspect": "npm test -- --inspect",
77-
"test:update": "npm run mocha -- --update",
74+
"test": "cross-env NODE_ENV=test mocha \"./src/**/__tests__/*.test.ts\"",
75+
"test:update": "npm test -- --update",
7876
"version": "changeset version && npm i --package-lock-only"
7977
},
8078
"types": "dist/index.d.ts"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1╭─ <div value=fn as (x: number) => string />
2+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
3+
│ ││ │ │╰─ attrValue.value "fn as (x: number) => string"
4+
│ ││ │ ╰─ attrValue "=fn as (x: number) => string"
5+
│ ││ ╰─ attrName "value"
6+
│ │╰─ tagName "div"
7+
╰─ ╰─ openTagStart
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div value=fn as (x: number) => string />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
1╭─ <div value=x as Map<string, Array<Record<K, V>>> />
2+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
3+
│ ││ │ │╰─ attrValue.value "x as Map<string, Array<Record<K, V>>>"
4+
│ ││ │ ╰─ attrValue "=x as Map<string, Array<Record<K, V>>>"
5+
│ ││ ╰─ attrName "value"
6+
│ │╰─ tagName "div"
7+
╰─ ╰─ openTagStart
8+
2╭─ <div value=x satisfies Map<string, Array<Record<K, V>>> />
9+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
10+
│ ││ │ │╰─ attrValue.value "x satisfies Map<string, Array<Record<K, V>>>"
11+
│ ││ │ ╰─ attrValue "=x satisfies Map<string, Array<Record<K, V>>>"
12+
│ ││ ╰─ attrName "value"
13+
│ │╰─ tagName "div"
14+
╰─ ╰─ openTagStart
15+
3╰─
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div value=x as Map<string, Array<Record<K, V>>> />
2+
<div value=x satisfies Map<string, Array<Record<K, V>>> />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
1╭─ <div value=fn as <T>(x: T) => T />
2+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
3+
│ ││ │ │╰─ attrValue.value "fn as <T>(x: T) => T"
4+
│ ││ │ ╰─ attrValue "=fn as <T>(x: T) => T"
5+
│ ││ ╰─ attrName "value"
6+
│ │╰─ tagName "div"
7+
╰─ ╰─ openTagStart
8+
2╭─ <div value=fn as < T >(x: T) => T />
9+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
10+
│ ││ │ │╰─ attrValue.value "fn as < T >(x: T) => T"
11+
│ ││ │ ╰─ attrValue "=fn as < T >(x: T) => T"
12+
│ ││ ╰─ attrName "value"
13+
│ │╰─ tagName "div"
14+
╰─ ╰─ openTagStart
15+
3╰─
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div value=fn as <T>(x: T) => T />
2+
<div value=fn as < T >(x: T) => T />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
1╭─ <div value=x as Array<T> />
2+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
3+
│ ││ │ │╰─ attrValue.value "x as Array<T>"
4+
│ ││ │ ╰─ attrValue "=x as Array<T>"
5+
│ ││ ╰─ attrName "value"
6+
│ │╰─ tagName "div"
7+
╰─ ╰─ openTagStart
8+
2╭─ <div value=x as Array< T > />
9+
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
10+
│ ││ │ │╰─ attrValue.value "x as Array< T >"
11+
│ ││ │ ╰─ attrValue "=x as Array< T >"
12+
│ ││ ╰─ attrName "value"
13+
│ │╰─ tagName "div"
14+
╰─ ╰─ openTagStart
15+
3╰─

0 commit comments

Comments
 (0)