From bcfd809fd376527e5fe624aa8418dd62fb2bf4fa Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Tue, 24 Jan 2023 13:55:29 -0700 Subject: [PATCH] fix: ensure keyword operators are always followed by a space (#146) --- .changeset/beige-seahorses-talk.md | 5 +++ .../attr-complex-instanceof.expected.txt | 39 +++++++++++++------ .../attr-complex-instanceof/input.marko | 3 ++ src/states/EXPRESSION.ts | 4 +- 4 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 .changeset/beige-seahorses-talk.md diff --git a/.changeset/beige-seahorses-talk.md b/.changeset/beige-seahorses-talk.md new file mode 100644 index 00000000..3f288f22 --- /dev/null +++ b/.changeset/beige-seahorses-talk.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Fixes an issue where attribute names that started with a keyword (eg: `as-thing` or `instanceof-thing`) were incorrectly treated as an expression continuation. diff --git a/src/__tests__/fixtures/attr-complex-instanceof/__snapshots__/attr-complex-instanceof.expected.txt b/src/__tests__/fixtures/attr-complex-instanceof/__snapshots__/attr-complex-instanceof.expected.txt index dc74e8d0..cce3e5d0 100644 --- a/src/__tests__/fixtures/attr-complex-instanceof/__snapshots__/attr-complex-instanceof.expected.txt +++ b/src/__tests__/fixtures/attr-complex-instanceof/__snapshots__/attr-complex-instanceof.expected.txt @@ -44,17 +44,34 @@ ╰─ ╰─ tagName "tag" 7╭─ ╰─ ╰─ openTagEnd -8╭─ tag a = 'foo' instanceof, b - │ │ │ │ │ │ ╰─ attrName - │ │ │ │ │ ╰─ attrName "instanceof" +8╭─ tag a = 'foo' instanceof-test; + │ │ │ │ │ ╰─ attrName "instanceof-test" │ │ │ │ ╰─ attrValue.value "'foo'" │ │ │ ╰─ attrValue "= 'foo'" │ │ ╰─ attrName │ ├─ closeTagEnd(tag) ╰─ ╰─ tagName "tag" -9╭─ - ╰─ ╰─ openTagEnd -10╭─ +9╭─ tag a = 'foo' instanceof_test; + │ │ │ │ │ ╰─ attrName "instanceof_test" + │ │ │ │ ╰─ attrValue.value "'foo'" + │ │ │ ╰─ attrValue "= 'foo'" + │ │ ╰─ attrName + │ ├─ closeTagEnd(tag) + │ ├─ openTagEnd + ╰─ ╰─ tagName "tag" +10╭─ + ╰─ ╰─ openTagEnd +11╭─ tag a = 'foo' instanceof, b + │ │ │ │ │ │ ╰─ attrName + │ │ │ │ │ ╰─ attrName "instanceof" + │ │ │ │ ╰─ attrValue.value "'foo'" + │ │ │ ╰─ attrValue "= 'foo'" + │ │ ╰─ attrName + │ ├─ closeTagEnd(tag) + ╰─ ╰─ tagName "tag" +12╭─ + ╰─ ╰─ openTagEnd +13╭─ │ ││ │ │ │ │ ││ │ ╰─ closeTagEnd(tag) │ ││ │ │ │ │ ││ ╰─ closeTagName "tag" │ ││ │ │ │ │ │╰─ closeTagStart " +14╭─ │ ││ │ │ │ │ ╰─ openTagEnd:selfClosed "/>" │ ││ │ │ │ ╰─ attrName "instanceof" │ ││ │ │ ╰─ attrValue.value "'foo'" @@ -74,17 +91,17 @@ │ ││ ╰─ attrName │ │╰─ tagName "tag" ╰─ ╰─ openTagStart -12├─ -13╭─ tag a = 'foo' instanceofthing String +15├─ +16╭─ tag a = 'foo' instanceofthing String │ │ │ │ │ │ ╰─ attrName "String" │ │ │ │ │ ╰─ attrName "instanceofthing" │ │ │ │ ╰─ attrValue.value "'foo'" │ │ │ ╰─ attrValue "= 'foo'" │ │ ╰─ attrName ╰─ ╰─ tagName "tag" -14╭─ +17╭─ ╰─ ╰─ openTagEnd -15╭─ tag a = 'foo' instanceof +18╭─ tag a = 'foo' instanceof │ │ │ │ │ │ ├─ closeTagEnd(tag) │ │ │ │ │ │ ╰─ openTagEnd │ │ │ │ │ ╰─ attrName "instanceof" diff --git a/src/__tests__/fixtures/attr-complex-instanceof/input.marko b/src/__tests__/fixtures/attr-complex-instanceof/input.marko index 12dc9920..1f68b80b 100644 --- a/src/__tests__/fixtures/attr-complex-instanceof/input.marko +++ b/src/__tests__/fixtures/attr-complex-instanceof/input.marko @@ -5,6 +5,9 @@ tag a = 'foo' instanceof := String tag a = 'foo' instanceof= String tag a = 'foo' instanceof; +tag a = 'foo' instanceof-test; +tag a = 'foo' instanceof_test; + tag a = 'foo' instanceof, b diff --git a/src/states/EXPRESSION.ts b/src/states/EXPRESSION.ts index da05cc4b..c1ca28c1 100644 --- a/src/states/EXPRESSION.ts +++ b/src/states/EXPRESSION.ts @@ -342,8 +342,8 @@ function lookAheadForOperator(data: string, pos: number): number { nextPos = lookAheadWhile(isWhitespaceCode, data, nextPos + 2); if (nextPos === max) return -1; nextCode = data.charCodeAt(nextPos); - } else if (isWordCode(nextCode)) { - // bail if we are continuing a word, eg "**in**teger" + } else { + // bail if we didn't match a space keyword. return -1; }