Skip to content

perf(liquid-html-parser): drop redundant endPos store when skipping LiquidDoc newlines - #1410

Draft
EvilGenius13 wants to merge 1 commit into
mainfrom
hydra/issue-17-20261120-162919
Draft

EvilGenius13 wants to merge 1 commit into
mainfrom
hydra/issue-17-20261120-162919

Conversation

@EvilGenius13

Copy link
Copy Markdown
Contributor

What are you adding in this PR?

When collectContentLines in the LiquidDoc parser reaches a run of blank lines that ends in text, it consumes the run in one loop. That loop stored endPos = this.peek().end on every newline. The store does nothing: the loop only runs when text follows the run, and that next text line always sets endPos = current.end again before anything reads it. This PR removes the store, which also saves one token read per newline:

while (this.p < lookAhead) {
  if (this.check(LiquidDocTokenType.Newline)) value += '\n';
  this.advance();
}

@EvilGenius13 suggested this in a review comment on #1387: #1387 (comment)

The parsed output doesn't change. A patch changeset for @shopify/liquid-html-parser is included.

Regression test for the blank-line skip

Right after this loop, the parser skips whitespace-only text lines. That skip is still needed, and no existing test failed when it was removed. This PR adds a test under @prompt (skips the blank line left by a non-breaking space after @prompt). It parses {% doc %}\n@prompt\u00a0\n\nBuild it\n{% enddoc %} and checks that:

  • content.value is "\n\nBuild it\n"
  • content.position.start is 17

Both values were recorded on main before the change. If the blank-line skip is deleted, the test fails with expected 18 to be 17, because the non-breaking space line becomes the first content line and its leading whitespace moves the start position.

What's next? Any followup issues?

None.

Tophatting

These all passed on this branch:

pnpm vitest run packages/liquid-html-parser/src/liquid-doc/parser.test.ts   # 55 passed
pnpm --filter @shopify/liquid-html-parser test                              # 36 files, 988 tests passed
pnpm --filter @shopify/liquid-html-parser type-check                        # clean
pnpm format:check                                                           # clean

I also deleted the blank-line skip temporarily: only the new test failed (expected 18 to be 17). Then I restored the skip.

Before you deploy

  • I included a patch bump changeset

Refs https://github.com/shopify-playground/eg-hydra/issues/17

eg-hydra-issue: https://eg-hydra.quick.shopify.io/#issue/865f8849-492c-4f1a-a27b-bd943bc29ac8

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant