diff --git a/.changeset/chubby-glasses-read.md b/.changeset/chubby-glasses-read.md new file mode 100644 index 00000000..58201398 --- /dev/null +++ b/.changeset/chubby-glasses-read.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Fix issue where arrow functions content was being parsed as types. diff --git a/src/__tests__/fixtures/ts-static-const-arrow-function-return-type/__snapshots__/ts-static-const-arrow-function-return-type.expected.txt b/src/__tests__/fixtures/ts-static-const-arrow-function-return-type/__snapshots__/ts-static-const-arrow-function-return-type.expected.txt new file mode 100644 index 00000000..49a17920 --- /dev/null +++ b/src/__tests__/fixtures/ts-static-const-arrow-function-return-type/__snapshots__/ts-static-const-arrow-function-return-type.expected.txt @@ -0,0 +1,28 @@ +1╭─ static const a = (a: number, b: number): string => { + ╰─ ╰─ tagName "static" +2├─ if (a < b) { +3├─ return "foo"; +4├─ } else { +5├─ return "bar"; +6├─ } +7├─ }; +8╭─ + ╰─ ╰─ openTagEnd +9╭─ static const b = (a: number, b: number): () => string => () => { + ╰─ ╰─ tagName "static" +10├─ if (a < b) { +11├─ return "foo"; +12├─ } else { +13├─ return "bar"; +14├─ } +15├─ }; +16╭─ + ╰─ ╰─ openTagEnd +17╭─ static const c = (a: number, b: number): string => a < b ? "foo" : "bar"; + ╰─ ╰─ tagName "static" +18╭─ + ╰─ ╰─ openTagEnd +19╭─ static const d = (a: number, b: number): () => string => () => a < b ? "foo" : "bar"; + ╰─ ╰─ tagName "static" +20╭─ + ╰─ ╰─ openTagEnd \ No newline at end of file diff --git a/src/__tests__/fixtures/ts-static-const-arrow-function-return-type/input.marko b/src/__tests__/fixtures/ts-static-const-arrow-function-return-type/input.marko new file mode 100644 index 00000000..eef8b1a2 --- /dev/null +++ b/src/__tests__/fixtures/ts-static-const-arrow-function-return-type/input.marko @@ -0,0 +1,19 @@ +static const a = (a: number, b: number): string => { + if (a < b) { + return "foo"; + } else { + return "bar"; + } +}; + +static const b = (a: number, b: number): () => string => () => { + if (a < b) { + return "foo"; + } else { + return "bar"; + } +}; + +static const c = (a: number, b: number): string => a < b ? "foo" : "bar"; + +static const d = (a: number, b: number): () => string => () => a < b ? "foo" : "bar"; diff --git a/src/states/EXPRESSION.ts b/src/states/EXPRESSION.ts index 05274824..08bfc43f 100644 --- a/src/states/EXPRESSION.ts +++ b/src/states/EXPRESSION.ts @@ -150,6 +150,13 @@ export const EXPRESSION: StateDefinition = { case CODE.EQUAL: if (expression.operators) { if (this.lookAtCharCodeAhead(1) === CODE.CLOSE_ANGLE_BRACKET) { + if ( + expression.inType && + !expression.forceType && + this.getPreviousNonWhitespaceCharCode() !== CODE.CLOSE_PAREN + ) { + expression.inType = false; + } this.pos++; } else if (!(expression.forceType || expression.groupStack.length)) { expression.inType = false;