Shopify / Shopify/theme-tools

Adding quotes around output markup with `| json` is not a safe formatting operation

Open
#377 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

area:prettier-plugin Bug Severity: 4
Dominant language
TypeScript
Stars
234
Forks
92
Avg merge
1d 3h
Merged PRs (30d)
6

Description

Describe the bug

Formatting the document causes quotation marks to be added around output markup that contains | json, which is not a safe formatting operation if the value being piped to the json filter is a string.

Unformatted source
No quotes around the tag:

<div
  data-something={{ someStringVar | json }}
></div>

Expected output
(no change)

<div
  data-something={{ someStringVar | json }}
></div>

Actual output
Adds quotes around tag:

<div
  data-something='{{ someStringVar | json }}'
></div>

Which gets evaluated as:

<div
  data-something=""someValue""
></div>

(Or maybe that's just the way Chrome renders it on the 'Elements' tab. When I copy the outerHTML to a clipboard it looks like "&quot;someValue&quot;")

And then the element's dataset gets changed from:

{
  something: 'someValue'
}

to:

{
  something: '\"someValue\"'
}

Debugging information

  • Ubuntu 20.04
  • Prettier version 3.0.0
  • @shopify/prettier-plugin-liquid version 1.2.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the formatting change with Prettier 3.0.0 and @shopify/prettier-plugin-liquid 1.2.1 using the Liquid and HTML example in the report. Trace the formatter behavior for output markup containing the json filter, then verify that formatting preserves the unquoted form and does not add escaped quotes to the resulting dataset value.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.