Skip to content

Commit 4857148

Browse files
committed
Add support for @shopify/prettier-plugin-liquid
1 parent 47a2cab commit 4857148

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

package-lock.json

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"devDependencies": {
2929
"@prettier/plugin-php": "^0.19.2",
3030
"@prettier/plugin-pug": "^2.3.0",
31+
"@shopify/prettier-plugin-liquid": "^0.4.0",
3132
"@shufo/prettier-plugin-blade": "^1.8.0",
3233
"@tailwindcss/line-clamp": "^0.3.0",
3334
"@trivago/prettier-plugin-sort-imports": "^3.3.0",

src/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,63 @@ function transformGlimmer(ast, { env }) {
345345
})
346346
}
347347

348+
function transformLiquid(ast, { env }) {
349+
visit(ast, {
350+
HtmlElement(node) {
351+
node.source = ''
352+
console.log({node})
353+
},
354+
355+
AttrSingleQuoted(node, _parent, _key, _index, meta) {
356+
if (node.name !== "class") {
357+
return;
358+
}
359+
360+
meta.sortTextNodes = true;
361+
meta.sourceNode = node;
362+
},
363+
364+
AttrDoubleQuoted(node, _parent, _key, _index, meta) {
365+
if (node.name !== "class") {
366+
return;
367+
}
368+
369+
meta.sortTextNodes = true;
370+
371+
// With Liquid Script it uses the "source" of certain nodes as the "source of truth"
372+
// We must modify that node's source to get the desired output
373+
// Even if we modify the AST it will be ignored
374+
meta.sourceNode = node;
375+
},
376+
377+
TextNode(node, _parent, _key, _index, meta) {
378+
if (!meta.sortTextNodes) {
379+
return;
380+
}
381+
382+
node.value = sortClasses(node.value, { env });
383+
384+
// This feels hacky but it's necessary
385+
node.source = node.source.slice(0, node.position.start) + node.value + node.source.slice(node.position.end);
386+
meta.sourceNode.source = node.source;
387+
},
388+
389+
String(node, _parent, _key, _index, meta) {
390+
if (!meta.sortTextNodes) {
391+
return;
392+
}
393+
394+
node.value = sortClasses(node.value, { env });
395+
396+
// This feels hacky but it's necessary
397+
// String position includes the quotes even if the value doesn't
398+
// Hence the +1 and -1 when slicing
399+
node.source = node.source.slice(0, node.position.start+1) + node.value + node.source.slice(node.position.end-1);
400+
meta.sourceNode.source = node.source;
401+
},
402+
});
403+
}
404+
348405
function sortStringLiteral(node, { env }) {
349406
let result = sortClasses(node.value, { env })
350407
let didChange = result !== node.value
@@ -500,6 +557,9 @@ export const parsers = {
500557
...base.parsers.php ? { php: createParser('php', transformPHP) } : {},
501558
...base.parsers.melody ? { melody: createParser('melody', transformMelody) } : {},
502559
...base.parsers.pug ? { pug: createParser('pug', transformPug) } : {},
560+
...(base.parsers['liquid-html']
561+
? { 'liquid-html': createParser("liquid-html", transformLiquid) }
562+
: {}),
503563
// ...base.parsers.blade ? { blade: createParser('blade', transformBlade) } : {},
504564
}
505565

@@ -733,6 +793,7 @@ function getBasePlugins() {
733793
let php = loadIfExists('@prettier/plugin-php')
734794
let melody = loadIfExists('prettier-plugin-twig-melody')
735795
let pug = loadIfExists('@prettier/plugin-pug')
796+
let liquid = loadIfExists('@shopify/prettier-plugin-liquid')
736797
// let blade = loadIfExists('@shufo/prettier-plugin-blade')
737798

738799
return {
@@ -759,6 +820,7 @@ function getBasePlugins() {
759820
...(php?.parsers ?? {}),
760821
...(melody?.parsers ?? {}),
761822
...(pug?.parsers ?? {}),
823+
...(liquid?.parsers ?? {}),
762824
// ...(blade?.parsers ?? {}),
763825
},
764826
printers: {
@@ -782,6 +844,7 @@ function getCompatibleParser(parserFormat, options) {
782844
'prettier-plugin-organize-imports',
783845
'@prettier/plugin-php',
784846
'@prettier/plugin-pug',
847+
'@shopify/prettier-plugin-liquid',
785848
'@shufo/prettier-plugin-blade',
786849
'prettier-plugin-css-order',
787850
'prettier-plugin-import-sort',

tests/plugins.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ let tests = [
208208
],
209209
}
210210
},
211+
{
212+
plugins: [
213+
'@shopify/prettier-plugin-liquid',
214+
],
215+
tests: {
216+
'liquid-html': [
217+
[
218+
`<a class="sm:p-0 p-4" href="https://www.example.com">Example</a>`,
219+
`<a class='p-4 sm:p-0' href='https://www.example.com'>Example</a>`,
220+
],
221+
[
222+
`{% if state == true %}\n <a class="{{ "sm:p-0 p-4" | escape }}" href="https://www.example.com">Example</a>\n{% endif %}`,
223+
`{% if state == true -%}\n <a class='{{ "p-4 sm:p-0" | escape }}' href='https://www.example.com'>Example</a>\n{%- endif %}`,
224+
],
225+
],
226+
}
227+
},
211228
]
212229

213230
for (const group of tests) {

0 commit comments

Comments
 (0)