Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't move partial classes inside Liquid script attributes #164

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Don't move partial classes inside Liquid script attributes ([#164](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/164))

## [0.3.0] - 2023-05-15

Expand Down
59 changes: 39 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,33 +296,52 @@ function transformLiquid(ast, { env }) {
/** @type {{pos: {start: number, end: number}, value: string}[]} */
let changes = []

/** @typedef {import('@shopify/prettier-plugin-liquid/dist/types.js').AttrSingleQuoted} AttrSingleQuoted */
/** @typedef {import('@shopify/prettier-plugin-liquid/dist/types.js').AttrDoubleQuoted} AttrDoubleQuoted */

/**
* @param {AttrSingleQuoted | AttrDoubleQuoted} attr
*/
function sortAttribute(attr) {
visit(attr.value, {
TextNode(node) {
node.value = sortClasses(node.value, { env })
for (let i = 0; i < attr.value.length; i++) {
let node = attr.value[i]
if (node.type === 'TextNode') {
node.value = sortClasses(node.value, {
env,
ignoreFirst: i > 0 && !/^\s/.test(node.value),
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(node.value),
})

changes.push({
pos: node.position,
value: node.value,
})
},

String(node) {
let pos = { ...node.position }
} else if (
node.type === 'LiquidDrop' &&
typeof node.markup === 'object' &&
node.markup.type === 'LiquidVariable'
) {
visit(node.markup.expression, {
String(node) {
let pos = { ...node.position }

// We have to offset the position ONLY when quotes are part of the String node
// This is because `value` does NOT include quotes
if (hasSurroundingQuotes(node.source.slice(pos.start, pos.end))) {
pos.start += 1
pos.end -= 1
}

// We have to offset the position ONLY when quotes are part of the String node
// This is because `value` does NOT include quotes
if (hasSurroundingQuotes(node.source.slice(pos.start, pos.end))) {
pos.start += 1
pos.end -= 1
}
node.value = sortClasses(node.value, { env })

node.value = sortClasses(node.value, { env })
changes.push({
pos,
value: node.value,
changes.push({
pos,
value: node.value,
})
},
})
},
})
}
}
}

visit(ast, {
Expand Down Expand Up @@ -351,7 +370,7 @@ function transformLiquid(ast, { env }) {

// Sort so all changes occur in order
changes = changes.sort((a, b) => {
return a.start - b.start || a.end - b.end
return a.pos.start - b.pos.start || a.pos.end - b.pos.end
})

for (let change of changes) {
Expand Down
2 changes: 2 additions & 0 deletions tests/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ let tests = [
t`<div class='${yes} {% render 'foo', bar: true %}'></div>`,
t`<div class='${yes} {% include 'foo' %}'></div>`,
t`<div class='${yes} {% include 'foo', bar: true %}'></div>`,
t`<div class='${yes} foo--{{ id }}'></div>`,
t`<div class='${yes} {{ id }}'></div>`,
],
},
},
Expand Down