Skip to content

Commit

Permalink
fix: always consume terminator if it is preceded by an operator
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 15, 2023
1 parent be73442 commit 085451c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-moons-enjoy.md
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Always consume next character of expression if terminator was preceded by an operator.
Expand Up @@ -5,31 +5,31 @@
╰─ ╰─ tagName "tag"
2╭─
╰─ ╰─ openTagEnd
3╭─ tag a = class, b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "class"
│ │ │ ╰─ attrValue "= class"
3╭─ tag a = class {}, b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "class {}"
│ │ │ ╰─ attrValue "= class {}"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
4╭─
╰─ ╰─ openTagEnd
5╭─ <tag a = class></tag>
│ ││ │ │ │ ││ │ ╰─ closeTagEnd(tag)
│ ││ │ │ │ ││ ╰─ closeTagName "tag"
│ ││ │ │ │ │╰─ closeTagStart "</"
│ ││ │ │ │ ╰─ openTagEnd
│ ││ │ │ ╰─ attrValue.value "class"
│ ││ │ ╰─ attrValue "= class"
5╭─ <tag a = class {}></tag>
│ ││ │ │ │ ││ │ ╰─ closeTagEnd(tag)
│ ││ │ │ │ ││ ╰─ closeTagName "tag"
│ ││ │ │ │ │╰─ closeTagStart "</"
│ ││ │ │ │ ╰─ openTagEnd
│ ││ │ │ ╰─ attrValue.value "class {}"
│ ││ │ ╰─ attrValue "= class {}"
│ ││ ╰─ attrName
│ │╰─ tagName "tag"
│ ├─ closeTagEnd(tag)
╰─ ╰─ openTagStart
6├─
7╭─ <tag a = class/>
│ ││ │ │ │ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │ ╰─ attrValue.value "class"
│ ││ │ ╰─ attrValue "= class"
7╭─ <tag a = class {}/>
│ ││ │ │ │ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │ ╰─ attrValue.value "class {}"
│ ││ │ ╰─ attrValue "= class {}"
│ ││ ╰─ attrName
│ │╰─ tagName "tag"
╰─ ╰─ openTagStart
Expand Down Expand Up @@ -69,9 +69,9 @@
╰─ ╰─ tagName "tag"
16╭─
╰─ ╰─ openTagEnd
17╭─ tag a = test+class b
│ │ │ │ ╰─ attrValue.value "test+class b"
│ │ │ ╰─ attrValue "= test+class b"
17╭─ tag a = test+class b {}
│ │ │ │ ╰─ attrValue.value "test+class b {}"
│ │ │ ╰─ attrValue "= test+class b {}"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/fixtures/attr-complex-unary/input.marko
@@ -1,10 +1,10 @@
tag a = class b {}

tag a = class, b
tag a = class {}, b

<tag a = class></tag>
<tag a = class {}></tag>

<tag a = class/>
<tag a = class {}/>

tag a = classthing b

Expand All @@ -14,4 +14,4 @@ tag a = test_class b

tag a = test$class b

tag a = test+class b
tag a = test+class b {}
@@ -0,0 +1,50 @@
1╭─ let/a:(B | C) = D
│ │ ││ │ ╰─ attrValue.value
│ │ ││ ├─ attrValue "= D"
│ │ ││ ╰─ attrName
│ │ │╰─ tagVar.value "a:(B | C)"
│ │ ╰─ tagVar "/a:(B | C)"
╰─ ╰─ tagName "let"
2╭─
╰─ ╰─ openTagEnd
3╭─ let/a: (B | C) = D
│ │ ││ │ ╰─ attrValue.value
│ │ ││ ├─ attrValue "= D"
│ │ ││ ╰─ attrName
│ │ │╰─ tagVar.value "a: (B | C)"
│ │ ╰─ tagVar "/a: (B | C)"
│ ├─ closeTagEnd(let)
╰─ ╰─ tagName "let"
4╭─
╰─ ╰─ openTagEnd
5╭─ let/a : (B | C) = D
│ │ ││ │ ╰─ attrValue.value
│ │ ││ ├─ attrValue "= D"
│ │ ││ ╰─ attrName
│ │ │╰─ tagVar.value "a : (B | C)"
│ │ ╰─ tagVar "/a : (B | C)"
│ ├─ closeTagEnd(let)
╰─ ╰─ tagName "let"
6╭─
╰─ ╰─ openTagEnd
7╭─ let/a :(B | C) = D
│ │ ││ │ ╰─ attrValue.value
│ │ ││ ├─ attrValue "= D"
│ │ ││ ╰─ attrName
│ │ │╰─ tagVar.value "a :(B | C)"
│ │ ╰─ tagVar "/a :(B | C)"
│ ├─ closeTagEnd(let)
╰─ ╰─ tagName "let"
8╭─
╰─ ╰─ openTagEnd
9╭─ let/a: B & (C | D) = D
│ │ ││ │ ╰─ attrValue.value
│ │ ││ ├─ attrValue "= D"
│ │ ││ ╰─ attrName
│ │ │╰─ tagVar.value "a: B & (C | D)"
│ │ ╰─ tagVar "/a: B & (C | D)"
│ ├─ closeTagEnd(let)
╰─ ╰─ tagName "let"
10╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(let)
9 changes: 9 additions & 0 deletions src/__tests__/fixtures/tag-var-type-with-parens/input.marko
@@ -0,0 +1,9 @@
let/a:(B | C) = D

let/a: (B | C) = D

let/a : (B | C) = D

let/a :(B | C) = D

let/a: B & (C | D) = D
19 changes: 17 additions & 2 deletions src/states/EXPRESSION.ts
Expand Up @@ -61,8 +61,23 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
}

if (expression.shouldTerminate(code, this.data, this.pos)) {
this.exitState();
return;
let wasExpression = false;
if (expression.operators) {
const prevNonWhitespacePos = lookBehindWhile(
isWhitespaceCode,
this.data,
this.pos - 1
);
if (prevNonWhitespacePos > expression.start) {
wasExpression =
lookBehindForOperator(this.data, prevNonWhitespacePos) !== -1;
}
}

if (!wasExpression) {
this.exitState();
return;
}
}
}

Expand Down

0 comments on commit 085451c

Please sign in to comment.