|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { runLiquidCheck, applyFix } from '../../../test'; |
| 3 | +import { LiquidHTMLSyntaxError } from '../index'; |
| 4 | + |
| 5 | +describe('Module: InvalidPipeSyntax', () => { |
| 6 | + describe('Double pipe patterns', () => { |
| 7 | + it('should detect and fix double pipes in variable output', async () => { |
| 8 | + const sourceCode = `{{ 'hello' | upcase | | downcase }}`; |
| 9 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 10 | + expect(offenses).toHaveLength(1); |
| 11 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 12 | + expect(offenses[0].fix).toBeDefined(); |
| 13 | + |
| 14 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 15 | + expect(fixedCode).toBe(`{{ 'hello' | upcase | downcase }}`); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should detect and fix double pipes with extra whitespace', async () => { |
| 19 | + const sourceCode = `{{ 'hello' | upcase | | downcase }}`; |
| 20 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 21 | + expect(offenses).toHaveLength(1); |
| 22 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 23 | + expect(offenses[0].fix).toBeDefined(); |
| 24 | + |
| 25 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 26 | + expect(fixedCode).toBe(`{{ 'hello' | upcase | downcase }}`); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should detect and fix double pipes in assign tag', async () => { |
| 30 | + const sourceCode = `{% assign result = 'hello' | upcase | | downcase %}`; |
| 31 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 32 | + expect(offenses).toHaveLength(1); |
| 33 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 34 | + expect(offenses[0].fix).toBeDefined(); |
| 35 | + |
| 36 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 37 | + expect(fixedCode).toBe(`{% assign result = 'hello' | upcase | downcase %}`); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should detect and fix double pipes in echo tag', async () => { |
| 41 | + const sourceCode = `{% echo 'hello' | upcase | | downcase %}`; |
| 42 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 43 | + expect(offenses).toHaveLength(1); |
| 44 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 45 | + expect(offenses[0].fix).toBeDefined(); |
| 46 | + |
| 47 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 48 | + expect(fixedCode).toBe(`{% echo 'hello' | upcase | downcase %}`); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle multiple double pipes in one expression', async () => { |
| 52 | + const sourceCode = `{{ 'hello' | upcase | | downcase | | reverse }}`; |
| 53 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 54 | + expect(offenses).toHaveLength(2); |
| 55 | + |
| 56 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 57 | + expect(offenses[1].message).toContain('Remove extra `|` character(s)'); |
| 58 | + |
| 59 | + expect(offenses[0].fix).toBeDefined(); |
| 60 | + expect(offenses[1].fix).toBeDefined(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('Trailing pipe patterns', () => { |
| 65 | + it('should detect and fix trailing pipe in variable output', async () => { |
| 66 | + const sourceCode = `{{ 'hello' | upcase | }}`; |
| 67 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 68 | + expect(offenses).toHaveLength(1); |
| 69 | + expect(offenses[0].message).toContain('Remove the trailing `|` character'); |
| 70 | + expect(offenses[0].fix).toBeDefined(); |
| 71 | + |
| 72 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 73 | + expect(fixedCode).toBe(`{{ 'hello' | upcase }}`); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should detect and fix trailing pipe in assign tag', async () => { |
| 77 | + const sourceCode = `{% assign result = 'hello' | upcase | %}`; |
| 78 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 79 | + expect(offenses).toHaveLength(1); |
| 80 | + expect(offenses[0].message).toContain('Remove the trailing `|` character'); |
| 81 | + expect(offenses[0].fix).toBeDefined(); |
| 82 | + |
| 83 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 84 | + expect(fixedCode).toBe(`{% assign result = 'hello' | upcase %}`); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should detect and fix trailing pipe in echo tag', async () => { |
| 88 | + const sourceCode = `{% echo 'hello' | upcase | %}`; |
| 89 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 90 | + expect(offenses).toHaveLength(1); |
| 91 | + expect(offenses[0].message).toContain('Remove the trailing `|` character'); |
| 92 | + expect(offenses[0].fix).toBeDefined(); |
| 93 | + |
| 94 | + const fixedCode = applyFix(sourceCode, offenses[0]); |
| 95 | + expect(fixedCode).toBe(`{% echo 'hello' | upcase %}`); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should detect and fix multiple trailing pipes', async () => { |
| 99 | + const sourceCode = `{{ 'hello' | upcase | | }}`; |
| 100 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 101 | + expect(offenses).toHaveLength(2); // Double pipe AND trailing pipe |
| 102 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 103 | + expect(offenses[1].message).toContain('Remove the trailing `|` character'); |
| 104 | + expect(offenses[0].fix).toBeDefined(); |
| 105 | + expect(offenses[1].fix).toBeDefined(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('Complex pipe scenarios', () => { |
| 110 | + it('should handle mixed valid and invalid pipes', async () => { |
| 111 | + const sourceCode = `{{ 'hello' | upcase | | downcase | reverse | }}`; |
| 112 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 113 | + expect(offenses).toHaveLength(2); |
| 114 | + |
| 115 | + // First should be double pipe |
| 116 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 117 | + // Second should be trailing pipe |
| 118 | + expect(offenses[1].message).toContain('Remove the trailing `|` character'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not interfere with valid filter parameter syntax', async () => { |
| 122 | + const sourceCode = `{{ 'hello' | append: 'world' | upcase }}`; |
| 123 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 124 | + expect(offenses).toHaveLength(0); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle liquid tag with mixed pipe issues', async () => { |
| 128 | + const sourceCode = `{% liquid |
| 129 | + assign foo = 'test' | upcase | | downcase | |
| 130 | + echo bar | reverse |
| 131 | + %}`; |
| 132 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 133 | + expect(offenses).toHaveLength(2); |
| 134 | + expect(offenses[0].message).toContain('Remove extra `|` character(s)'); |
| 135 | + expect(offenses[1].message).toContain('Remove the trailing `|` character'); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('Valid syntax should not be flagged', () => { |
| 140 | + it('should not report on valid filter chains', async () => { |
| 141 | + const sourceCode = `{{ 'hello' | upcase | append: 'world' | downcase }}`; |
| 142 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 143 | + expect(offenses).toHaveLength(0); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should not report on simple filters', async () => { |
| 147 | + const sourceCode = `{{ 'hello' | upcase }}`; |
| 148 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 149 | + expect(offenses).toHaveLength(0); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should not report on filters with arguments', async () => { |
| 153 | + const sourceCode = `{{ product.title | append: ' - ' | append: shop.name }}`; |
| 154 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 155 | + expect(offenses).toHaveLength(0); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should not report on valid assign with filters', async () => { |
| 159 | + const sourceCode = `{% assign title = product.title | upcase | truncate: 50 %}`; |
| 160 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 161 | + expect(offenses).toHaveLength(0); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should not report on valid echo with filters', async () => { |
| 165 | + const sourceCode = `{% echo product.title | upcase | truncate: 50 %}`; |
| 166 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 167 | + expect(offenses).toHaveLength(0); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('Edge cases', () => { |
| 172 | + it('should handle pipes in string literals correctly', async () => { |
| 173 | + const sourceCode = `{{ 'hello | world' | upcase }}`; |
| 174 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 175 | + expect(offenses).toHaveLength(0); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should not report on conditional expressions with pipes', async () => { |
| 179 | + const sourceCode = `{{ product.title | default: 'No title' }}`; |
| 180 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 181 | + expect(offenses).toHaveLength(0); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments