Skip to content

Commit 3ebd6d0

Browse files
Fix Liquid capture sorting (#135)
* Collect all changes to Liquid sources before applying them * Add tests
1 parent fa6efdb commit 3ebd6d0

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

src/index.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -358,55 +358,75 @@ function transformLiquid(ast, { env }) {
358358
: node.name === 'class'
359359
}
360360

361-
function sortAttribute(attr, path) {
361+
/** @type {{type: string, source: string}[]} */
362+
let sources = []
363+
364+
/** @type {{pos: {start: number, end: number}, value: string}[]} */
365+
let changes = []
366+
367+
function sortAttribute(attr) {
362368
visit(attr.value, {
363369
TextNode(node) {
364370
node.value = sortClasses(node.value, { env });
365-
366-
let source = node.source.slice(0, node.position.start) + node.value + node.source.slice(node.position.end)
367-
path.forEach(node => (node.source = source))
371+
changes.push({
372+
pos: node.position,
373+
value: node.value,
374+
})
368375
},
369376

370377
String(node) {
371378
node.value = sortClasses(node.value, { env });
372-
373-
// String position includes the quotes even if the value doesn't
374-
// Hence the +1 and -1 when slicing
375-
let source = node.source.slice(0, node.position.start+1) + node.value + node.source.slice(node.position.end-1)
376-
path.forEach(node => (node.source = source))
379+
changes.push({
380+
pos: {
381+
// String position includes the quotes even if the value doesn't
382+
// Hence the +1 and -1 when slicing
383+
start: node.position.start+1,
384+
end: node.position.end-1,
385+
},
386+
value: node.value,
387+
})
377388
},
378389
})
379390
}
380391

381392
visit(ast, {
382-
LiquidTag(node, _parent, _key, _index, meta) {
383-
meta.path = [...meta.path ?? [], node];
393+
LiquidTag(node) {
394+
sources.push(node)
384395
},
385396

386-
HtmlElement(node, _parent, _key, _index, meta) {
387-
meta.path = [...meta.path ?? [], node];
397+
HtmlElement(node) {
398+
sources.push(node)
388399
},
389400

390-
AttrSingleQuoted(node, _parent, _key, _index, meta) {
391-
if (!isClassAttr(node)) {
392-
return;
401+
AttrSingleQuoted(node) {
402+
if (isClassAttr(node)) {
403+
sources.push(node)
404+
sortAttribute(node)
393405
}
394-
395-
meta.path = [...meta.path ?? [], node];
396-
397-
sortAttribute(node, meta.path)
398406
},
399407

400-
AttrDoubleQuoted(node, _parent, _key, _index, meta) {
401-
if (!isClassAttr(node)) {
402-
return;
408+
AttrDoubleQuoted(node) {
409+
if (isClassAttr(node)) {
410+
sources.push(node)
411+
sortAttribute(node)
403412
}
404-
405-
meta.path = [...meta.path ?? [], node];
406-
407-
sortAttribute(node, meta.path)
408413
},
409414
});
415+
416+
// Sort so all changes occur in order
417+
changes = changes.sort((a, b) => {
418+
return a.start - b.start
419+
|| a.end - b.end
420+
})
421+
422+
for (let change of changes) {
423+
for (let node of sources) {
424+
node.source =
425+
node.source.slice(0, change.pos.start) +
426+
change.value +
427+
node.source.slice(change.pos.end)
428+
}
429+
}
410430
}
411431

412432
function sortStringLiteral(node, { env }) {

tests/plugins.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ let tests = [
257257
`{%- capture class_ordering -%}<div class="sm:p-0 p-4"></div>{%- endcapture -%}`,
258258
`{%- capture class_ordering -%}<div class="p-4 sm:p-0"></div>{%- endcapture -%}`,
259259
],
260+
[
261+
`{%- capture class_ordering -%}<div class="foo1 sm:p-0 p-4"></div><div class="foo2 sm:p-0 p-4"></div>{%- endcapture -%}`,
262+
`{%- capture class_ordering -%}<div class="foo1 p-4 sm:p-0"></div><div class="foo2 p-4 sm:p-0"></div>{%- endcapture -%}`,
263+
],
264+
[
265+
`{%- capture class_ordering -%}<div class="foo1 sm:p-0 p-4"><div class="foo2 sm:p-0 p-4"></div></div>{%- endcapture -%}`,
266+
`{%- capture class_ordering -%}<div class="foo1 p-4 sm:p-0"><div class="foo2 p-4 sm:p-0"></div></div>{%- endcapture -%}`,
267+
],
260268
],
261269
}
262270
},

0 commit comments

Comments
 (0)