Skip to content

Commit 2aabc3c

Browse files
authored
Revert "Fix Liquid capture sorting (#135)" (#140)
This reverts commit 3ebd6d0.
1 parent 41c7738 commit 2aabc3c

File tree

2 files changed

+27
-55
lines changed

2 files changed

+27
-55
lines changed

src/index.js

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

382-
/** @type {{type: string, source: string}[]} */
383-
let sources = []
384-
385-
/** @type {{pos: {start: number, end: number}, value: string}[]} */
386-
let changes = []
387-
388-
function sortAttribute(attr) {
382+
function sortAttribute(attr, path) {
389383
visit(attr.value, {
390384
TextNode(node) {
391385
node.value = sortClasses(node.value, { env });
392-
changes.push({
393-
pos: node.position,
394-
value: node.value,
395-
})
386+
387+
let source = node.source.slice(0, node.position.start) + node.value + node.source.slice(node.position.end)
388+
path.forEach(node => (node.source = source))
396389
},
397390

398391
String(node) {
399392
node.value = sortClasses(node.value, { env });
400-
changes.push({
401-
pos: {
402-
// String position includes the quotes even if the value doesn't
403-
// Hence the +1 and -1 when slicing
404-
start: node.position.start+1,
405-
end: node.position.end-1,
406-
},
407-
value: node.value,
408-
})
393+
394+
// String position includes the quotes even if the value doesn't
395+
// Hence the +1 and -1 when slicing
396+
let source = node.source.slice(0, node.position.start+1) + node.value + node.source.slice(node.position.end-1)
397+
path.forEach(node => (node.source = source))
409398
},
410399
})
411400
}
412401

413402
visit(ast, {
414-
LiquidTag(node) {
415-
sources.push(node)
403+
LiquidTag(node, _parent, _key, _index, meta) {
404+
meta.path = [...meta.path ?? [], node];
416405
},
417406

418-
HtmlElement(node) {
419-
sources.push(node)
407+
HtmlElement(node, _parent, _key, _index, meta) {
408+
meta.path = [...meta.path ?? [], node];
420409
},
421410

422-
AttrSingleQuoted(node) {
423-
if (isClassAttr(node)) {
424-
sources.push(node)
425-
sortAttribute(node)
411+
AttrSingleQuoted(node, _parent, _key, _index, meta) {
412+
if (!isClassAttr(node)) {
413+
return;
426414
}
415+
416+
meta.path = [...meta.path ?? [], node];
417+
418+
sortAttribute(node, meta.path)
427419
},
428420

429-
AttrDoubleQuoted(node) {
430-
if (isClassAttr(node)) {
431-
sources.push(node)
432-
sortAttribute(node)
421+
AttrDoubleQuoted(node, _parent, _key, _index, meta) {
422+
if (!isClassAttr(node)) {
423+
return;
433424
}
434-
},
435-
});
436425

437-
// Sort so all changes occur in order
438-
changes = changes.sort((a, b) => {
439-
return a.start - b.start
440-
|| a.end - b.end
441-
})
426+
meta.path = [...meta.path ?? [], node];
442427

443-
for (let change of changes) {
444-
for (let node of sources) {
445-
node.source =
446-
node.source.slice(0, change.pos.start) +
447-
change.value +
448-
node.source.slice(change.pos.end)
449-
}
450-
}
428+
sortAttribute(node, meta.path)
429+
},
430+
});
451431
}
452432

453433
function sortStringLiteral(node, { env }) {

tests/plugins.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,6 @@ 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-
],
268260
],
269261
}
270262
},

0 commit comments

Comments
 (0)