Skip to content

Commit

Permalink
fix: regex continuation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 19, 2022
1 parent c91aa55 commit 4e13e74
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-laws-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Fix regression that causes incorrect expression continuations after regexps.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1╭─ <input pattern=/\s*?\S+\s*?/ name="test">
│ ││ │ ││ │ ││ ╰─ openTagEnd
│ ││ │ ││ │ │╰─ attrValue.value "\"test\""
│ ││ │ ││ │ ╰─ attrValue "=\"test\""
│ ││ │ ││ ╰─ attrName "name"
│ ││ │ │╰─ attrValue.value "/\\s*?\\S+\\s*?/"
│ ││ │ ╰─ attrValue "=/\\s*?\\S+\\s*?/"
│ ││ ╰─ attrName "pattern"
│ │╰─ tagName "input"
╰─ ╰─ openTagStart
1 change: 1 addition & 0 deletions src/__tests__/fixtures/attr-regexp/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input pattern=/\s*?\S+\s*?/ name="test">
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
│ ││ │ ││ │ ││ ││ ╰─ closeTagName "div"
│ ││ │ ││ │ ││ │╰─ closeTagStart "</"
│ ││ │ ││ │ ││ ╰─ openTagEnd
│ ││ │ ││ │ │╰─ attrValue.value "\"world\""
│ ││ │ ││ │ ╰─ attrValue "=\"world\""
│ ││ │ ││ │ │╰─ attrValue.value "\"world\" /* this is another comment */"
│ ││ │ ││ │ ╰─ attrValue "=\"world\" /* this is another comment */"
│ ││ │ ││ ╰─ attrName "hello"
│ ││ │ │╰─ attrValue.value "\"bar\""
│ ││ │ ╰─ attrValue "=\"bar\""
│ ││ │ │╰─ attrValue.value "\"bar\" /*this is a comment*/"
│ ││ │ ╰─ attrValue "=\"bar\" /*this is a comment*/"
│ ││ ╰─ attrName "foo"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div foo="bar" // this is a test
│ ││ │ │╰─ attrValue.value "\"bar\""
│ ││ │ ╰─ attrValue "=\"bar\""
│ ││ │ │╰─ attrValue.value "\"bar\" // this is a test"
│ ││ │ ╰─ attrValue "=\"bar\" // this is a test"
│ ││ ╰─ attrName "foo"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
3╭─ hello="world" // this is another test
│ │ │╰─ attrValue.value "\"world\""
│ │ ╰─ attrValue "=\"world\""
│ │ │╰─ attrValue.value "\"world\" // this is another test"
│ │ ╰─ attrValue "=\"world\" // this is another test"
╰─ ╰─ attrName "hello"
4╭─ ></div>
│ ││ │ ╰─ closeTagEnd(div)
Expand Down
8 changes: 5 additions & 3 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
break;
default: {
if (
!canCharCodeBeFollowedByDivision(
canCharCodeBeFollowedByDivision(
this.getPreviousNonWhitespaceCharCode()
)
) {
this.pos++;
this.consumeWhitespace();
} else {
this.enterState(STATE.REGULAR_EXPRESSION);
}
break;
Expand Down Expand Up @@ -219,7 +222,6 @@ function buildPattern(type: PatternType) {
`|(?<!-)-${
type === PatternType.CONCISE_ATTRS ? "" : "(?:\\s*-\\s*-)*\\s*"
}(?!-)` + // In concise mode we can't match multiple hyphens otherwise we can match an even number of hyphens
`|(?<![/*])/(?![/*${type === PatternType.HTML_ATTRS ? ">" : ""}])` + // We only continue after a forward slash if it isn't //, /* (or /> in html mode)
`|(?<!\\.)\\.(?!\\.)` + // We only continue after a dot if it isn't a ...
`|>${type === PatternType.HTML_ATTRS ? "{2,}" : "+"}` + // in html mode only consume closing angle brackets if it is >>
"|[ \\t]+(?:in(?:stanceof)?|as|extends)(?=[ \\t]+[^=/,;:>])"; // We only continue after word operators (instanceof/in) when they are not followed by a terminator
Expand All @@ -233,7 +235,7 @@ function buildPattern(type: PatternType) {
"|typeof" +
"|void" +
")\\b";
const lookAheadPattern = `${space}*(?:${binary})\\s*|${space}+(?=[{(])`; // if we have spaces followed by an opening bracket, we'll consume the spaces and let the expression state handle the brackets
const lookAheadPattern = `${space}*(?:${binary})\\s*|${space}+(?=[{(]|/[^>])`; // if we have spaces followed by an opening bracket, we'll consume the spaces and let the expression state handle the brackets
const lookBehindPattern = `(?<=${unary}|${binary})`;
return new RegExp(`${lookAheadPattern}|${lookBehindPattern}`, "ym");
}
Expand Down

0 comments on commit 4e13e74

Please sign in to comment.