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
13 changes: 13 additions & 0 deletions .changeset/statement-fn-return-type-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"htmljs-parser": patch
---

Fix a spurious "Mismatched group" error when a `>` (or other comparison) appears in the body of a statement function that declares a return type, e.g.:

```marko
export function a(): b {
return c > d;
}
```

Previously the type-parsing state from the return type annotation leaked into the function body, so a `>` was treated as a closing generic bracket. A `{` that follows a completed type now correctly ends the annotation and parses the block as a value.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
1╭─ export function a(): b {
╰─ ╰─ tagName "export"
2├─ return c > d;
3├─ }
4╭─
╰─ ╰─ openTagEnd
5╭─ static function e<T>(x: T): Array<T> {
╰─ ╰─ tagName "static"
6├─ return x > 0 ? f : g;
7├─ }
8╭─
╰─ ╰─ openTagEnd
9╭─ export function h(): { i: 1 } {
╰─ ╰─ tagName "export"
10├─ return j > k;
11├─ }
12╭─
╰─ ╰─ openTagEnd
13╭─ div
╰─ ╰─ tagName "div"
14╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(div)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function a(): b {
return c > d;
}

static function e<T>(x: T): Array<T> {
return x > 0 ? f : g;
}

export function h(): { i: 1 } {
return j > k;
}

div
10 changes: 10 additions & 0 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
this.pos++;
break;
case CODE.OPEN_CURLY_BRACE:
if (expression.inType && !expression.forceType) {
const prevPos = lookBehindWhile(
isWhitespaceCode,
data,
this.pos - 1,
);
if (lookBehindForOperator(expression, data, prevPos) === -1) {
expression.inType = false;
}
}
expression.groupStack.push(CODE.CLOSE_CURLY_BRACE);
this.pos++;
break;
Expand Down
Loading