Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regex continuation issue #121

Merged
merged 1 commit into from
Jul 19, 2022
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/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