From b1e68a300e75aeb538b1045dfd20b451abd74d09 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Mon, 18 Jul 2022 20:17:19 -0700 Subject: [PATCH] fix: regex continuation issue (#121) --- .changeset/gorgeous-laws-grin.md | 5 +++++ .../__snapshots__/attr-regexp.expected.txt | 10 ++++++++++ src/__tests__/fixtures/attr-regexp/input.marko | 1 + .../comments-within-open-tag.expected.txt | 16 ++++++++-------- src/states/EXPRESSION.ts | 8 +++++--- 5 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 .changeset/gorgeous-laws-grin.md create mode 100644 src/__tests__/fixtures/attr-regexp/__snapshots__/attr-regexp.expected.txt create mode 100644 src/__tests__/fixtures/attr-regexp/input.marko diff --git a/.changeset/gorgeous-laws-grin.md b/.changeset/gorgeous-laws-grin.md new file mode 100644 index 00000000..af8281ff --- /dev/null +++ b/.changeset/gorgeous-laws-grin.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Fix regression that causes incorrect expression continuations after regexps. diff --git a/src/__tests__/fixtures/attr-regexp/__snapshots__/attr-regexp.expected.txt b/src/__tests__/fixtures/attr-regexp/__snapshots__/attr-regexp.expected.txt new file mode 100644 index 00000000..ccbdc2c3 --- /dev/null +++ b/src/__tests__/fixtures/attr-regexp/__snapshots__/attr-regexp.expected.txt @@ -0,0 +1,10 @@ +1╭─ + │ ││ │ ││ │ ││ ╰─ openTagEnd + │ ││ │ ││ │ │╰─ attrValue.value "\"test\"" + │ ││ │ ││ │ ╰─ attrValue "=\"test\"" + │ ││ │ ││ ╰─ attrName "name" + │ ││ │ │╰─ attrValue.value "/\\s*?\\S+\\s*?/" + │ ││ │ ╰─ attrValue "=/\\s*?\\S+\\s*?/" + │ ││ ╰─ attrName "pattern" + │ │╰─ tagName "input" + ╰─ ╰─ openTagStart \ No newline at end of file diff --git a/src/__tests__/fixtures/attr-regexp/input.marko b/src/__tests__/fixtures/attr-regexp/input.marko new file mode 100644 index 00000000..5eabfe83 --- /dev/null +++ b/src/__tests__/fixtures/attr-regexp/input.marko @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/__tests__/fixtures/comments-within-open-tag/__snapshots__/comments-within-open-tag.expected.txt b/src/__tests__/fixtures/comments-within-open-tag/__snapshots__/comments-within-open-tag.expected.txt index 0c89d8d0..5649bbd5 100644 --- a/src/__tests__/fixtures/comments-within-open-tag/__snapshots__/comments-within-open-tag.expected.txt +++ b/src/__tests__/fixtures/comments-within-open-tag/__snapshots__/comments-within-open-tag.expected.txt @@ -3,23 +3,23 @@ │ ││ │ ││ │ ││ ││ ╰─ closeTagName "div" │ ││ │ ││ │ ││ │╰─ closeTagStart " │ ││ │ ╰─ closeTagEnd(div) diff --git a/src/states/EXPRESSION.ts b/src/states/EXPRESSION.ts index d8819e17..5fa20467 100644 --- a/src/states/EXPRESSION.ts +++ b/src/states/EXPRESSION.ts @@ -87,10 +87,13 @@ export const EXPRESSION: StateDefinition = { break; default: { if ( - !canCharCodeBeFollowedByDivision( + canCharCodeBeFollowedByDivision( this.getPreviousNonWhitespaceCharCode() ) ) { + this.pos++; + this.consumeWhitespace(); + } else { this.enterState(STATE.REGULAR_EXPRESSION); } break; @@ -219,7 +222,6 @@ function buildPattern(type: PatternType) { `|(?" : ""}])` + // We only continue after a forward slash if it isn't //, /* (or /> in html mode) `|(?${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 @@ -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"); }