Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/major-experts-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Improve expression parsing for typescript, especially around multi line generics.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,18 @@ const parser = createParser({
// TagType.void makes this a void element (cannot have children).
return TagType.void;
case "html-comment":
case "html-script":
case "html-style":
case "script":
case "style":
case "textarea":
// TagType.text makes the child content text only (with placeholders).
return TagType.text;
case "class":
case "export":
case "import":
case "static":
case "client":
case "server":
// 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"`).
return TagType.statement;
}
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@
"bench": "tsx bench.mts",
"build": "tsc -b && tsx build.mts",
"change": "changeset add",
"ci:test": "nyc npm run mocha -- --forbid-only",
"ci:test": "nyc npm test -- --forbid-only",
"format": "npm run lint:eslint -- --fix && npm run lint:prettier -- --write && (fixpack || true)",
"lint": "tsc -b && npm run lint:eslint && npm run lint:prettier -- -l && fixpack",
"lint:eslint": "eslint -f visualstudio .",
"lint:prettier": "prettier \"./**/*{.ts,.js,.json,.md,.yml,rc}\"",
"mocha": "cross-env NODE_ENV=test mocha \"./src/**/__tests__/*.test.ts\"",
"prepare": "husky",
"release": "npm run build && changeset publish",
"report": "open ./coverage/lcov-report/index.html",
"test": "npm run mocha -- --watch",
"test:inspect": "npm test -- --inspect",
"test:update": "npm run mocha -- --update",
"test": "cross-env NODE_ENV=test mocha \"./src/**/__tests__/*.test.ts\"",
"test:update": "npm test -- --update",
"version": "changeset version && npm i --package-lock-only"
},
"types": "dist/index.d.ts"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1╭─ <div value=fn as (x: number) => string />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "fn as (x: number) => string"
│ ││ │ ╰─ attrValue "=fn as (x: number) => string"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
1 change: 1 addition & 0 deletions src/__tests__/fixtures/ts-function-type/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div value=fn as (x: number) => string />
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1╭─ <div value=x as Map<string, Array<Record<K, V>>> />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as Map<string, Array<Record<K, V>>>"
│ ││ │ ╰─ attrValue "=x as Map<string, Array<Record<K, V>>>"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div value=x satisfies Map<string, Array<Record<K, V>>> />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x satisfies Map<string, Array<Record<K, V>>>"
│ ││ │ ╰─ attrValue "=x satisfies Map<string, Array<Record<K, V>>>"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
3╰─
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/ts-generic-complex/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div value=x as Map<string, Array<Record<K, V>>> />
<div value=x satisfies Map<string, Array<Record<K, V>>> />
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1╭─ <div value=fn as <T>(x: T) => T />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "fn as <T>(x: T) => T"
│ ││ │ ╰─ attrValue "=fn as <T>(x: T) => T"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div value=fn as < T >(x: T) => T />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "fn as < T >(x: T) => T"
│ ││ │ ╰─ attrValue "=fn as < T >(x: T) => T"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
3╰─
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/ts-generic-function-type/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div value=fn as <T>(x: T) => T />
<div value=fn as < T >(x: T) => T />
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1╭─ <div value=x as Array<T> />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as Array<T>"
│ ││ │ ╰─ attrValue "=x as Array<T>"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div value=x as Array< T > />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as Array< T >"
│ ││ │ ╰─ attrValue "=x as Array< T >"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
3╰─
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/ts-generic-simple/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div value=x as Array<T> />
<div value=x as Array< T > />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1╭─ <div value=x as A & B<T> />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as A & B<T>"
│ ││ │ ╰─ attrValue "=x as A & B<T>"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div value=x as A & B< T > />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as A & B< T >"
│ ││ │ ╰─ attrValue "=x as A & B< T >"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/ts-intersection-type/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div value=x as A & B<T> />
<div value=x as A & B< T > />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1╭─ <div value=x as keyof typeof T />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as keyof typeof T"
│ ││ │ ╰─ attrValue "=x as keyof typeof T"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╰─
1 change: 1 addition & 0 deletions src/__tests__/fixtures/ts-keyof-typeof/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div value=x as keyof typeof T />
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1╭─ <div value=x as Promise<Array<Record<string, number>>> />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as Promise<Array<Record<string, number>>>"
│ ││ │ ╰─ attrValue "=x as Promise<Array<Record<string, number>>>"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div value=x as Promise< Array< Record< string, number > > > />
│ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │╰─ attrValue.value "x as Promise< Array< Record< string, number > > >"
│ ││ │ ╰─ attrValue "=x as Promise< Array< Record< string, number > > >"
│ ││ ╰─ attrName "value"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
3╰─
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/ts-nested-generics/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div value=x as Promise<Array<Record<string, number>>> />
<div value=x as Promise< Array< Record< string, number > > > />
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1╭─ static type Foo = Array<
╰─ ╰─ tagName "static"
2├─ Bar
3├─ >
4╭─
╰─ ╰─ openTagEnd
5╭─ div
╰─ ╰─ tagName "div"
6╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(div)
5 changes: 5 additions & 0 deletions src/__tests__/fixtures/ts-static-type/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
static type Foo = Array<
Bar
>

div
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1╭─ const/foo:Bar<
│ │ │╰─ tagVar.value "foo:Bar<\n A,\n B\n>"
│ │ ╰─ tagVar "/foo:Bar<\n A,\n B\n>"
╰─ ╰─ tagName "const"
2├─ A,
3├─ B
4╭─ > = baz
│ │ ╰─ attrValue.value "baz"
│ ├─ attrValue "= baz"
╰─ ╰─ attrName
5╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(const)
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/ts-tag-var-type-generic/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const/foo:Bar<
A,
B
> = baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
1╭─ static interface Foo<
╰─ ╰─ tagName "static"
2├─ A,
3├─ B,
4├─ > {
5├─ x: 1
6├─ }
7╭─
╰─ ╰─ openTagEnd
8╭─ static type Bar<
╰─ ╰─ tagName "static"
9├─ A,
10├─ B,
11├─ > = A &
12├─ B
13╭─
╰─ ╰─ openTagEnd
14╭─ static declare const x: Foo<
╰─ ╰─ tagName "static"
15├─ 1,
16├─ 2
17├─ >;
18╭─
╰─ ╰─ openTagEnd
19╭─ static const x: Foo<
╰─ ╰─ tagName "static"
20├─ 1,
21├─ 2
22├─ > = baz;
23╭─
╰─ ╰─ openTagEnd
22 changes: 22 additions & 0 deletions src/__tests__/fixtures/ts-type-statement/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
static interface Foo<
A,
B,
> {
x: 1
}

static type Bar<
A,
B,
> = A &
B

static declare const x: Foo<
1,
2
>;

static const x: Foo<
1,
2
> = baz;
Loading
Loading