From 3a696d018d063665c36296565279881d6b05d8c6 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Fri, 10 Nov 2023 16:52:27 -0700 Subject: [PATCH] fix: improve detection of regexp vs division --- .changeset/heavy-stingrays-invite.md | 5 ++++ .../__snapshots__/css-grid.expected.txt | 12 ++++++++++ src/__tests__/fixtures/css-grid/input.marko | 5 ++++ src/states/EXPRESSION.ts | 24 ++++++++++++------- 4 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 .changeset/heavy-stingrays-invite.md create mode 100644 src/__tests__/fixtures/css-grid/__snapshots__/css-grid.expected.txt create mode 100644 src/__tests__/fixtures/css-grid/input.marko diff --git a/.changeset/heavy-stingrays-invite.md b/.changeset/heavy-stingrays-invite.md new file mode 100644 index 00000000..6eb56b91 --- /dev/null +++ b/.changeset/heavy-stingrays-invite.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +When the preceding character of an expression is a quote, prefer division over regexp state. This improves parsing for inline css grid properties. diff --git a/src/__tests__/fixtures/css-grid/__snapshots__/css-grid.expected.txt b/src/__tests__/fixtures/css-grid/__snapshots__/css-grid.expected.txt new file mode 100644 index 00000000..616140b1 --- /dev/null +++ b/src/__tests__/fixtures/css-grid/__snapshots__/css-grid.expected.txt @@ -0,0 +1,12 @@ +1╭─ style.scss { + │ │ ││ ╰─ attrName "{\n .foo {\n grid: \"left right\" / 3fr 7fr;\n }\n}" + │ │ │╰─ tagShorthandClass.quasis[0] "scss" + │ │ ╰─ tagShorthandClass ".scss" + ╰─ ╰─ tagName "style" +2├─ .foo { +3├─ grid: "left right" / 3fr 7fr; +4├─ } +5├─ } +6╭─ + │ ├─ openTagEnd + ╰─ ╰─ closeTagEnd(style) \ No newline at end of file diff --git a/src/__tests__/fixtures/css-grid/input.marko b/src/__tests__/fixtures/css-grid/input.marko new file mode 100644 index 00000000..2a62c9c6 --- /dev/null +++ b/src/__tests__/fixtures/css-grid/input.marko @@ -0,0 +1,5 @@ +style.scss { + .foo { + grid: "left right" / 3fr 7fr; + } +} diff --git a/src/states/EXPRESSION.ts b/src/states/EXPRESSION.ts index cd866195..6e20b30a 100644 --- a/src/states/EXPRESSION.ts +++ b/src/states/EXPRESSION.ts @@ -390,15 +390,21 @@ function lookAheadForOperator(data: string, pos: number): number { } function canFollowDivision(code: number) { - return ( - isWordCode(code) || - code === CODE.PERCENT || - code === CODE.CLOSE_PAREN || - code === CODE.PERIOD || - code === CODE.OPEN_ANGLE_BRACKET || - code === CODE.CLOSE_SQUARE_BRACKET || - code === CODE.CLOSE_CURLY_BRACE - ); + if (isWordCode(code)) return true; + switch (code) { + case CODE.BACKTICK: + case CODE.SINGLE_QUOTE: + case CODE.DOUBLE_QUOTE: + case CODE.PERCENT: + case CODE.CLOSE_PAREN: + case CODE.PERIOD: + case CODE.OPEN_ANGLE_BRACKET: + case CODE.CLOSE_SQUARE_BRACKET: + case CODE.CLOSE_CURLY_BRACE: + return true; + default: + return false; + } } function isWordCode(code: number) {