|
| 1 | +import { expect, describe, it, vi, beforeEach } from 'vitest'; |
| 2 | +import { applyFix, runLiquidCheck } from '../../../test'; |
| 3 | +import { LiquidHTMLSyntaxError } from '../index'; |
| 4 | +import { toLiquidAST } from '@shopify/liquid-html-parser'; |
| 5 | +import { INVALID_LOOP_RANGE_MESSAGE } from './InvalidLoopRange'; |
| 6 | + |
| 7 | +vi.mock('@shopify/liquid-html-parser', async (importOriginal) => { |
| 8 | + const original: any = await importOriginal(); |
| 9 | + |
| 10 | + return { |
| 11 | + ...original, |
| 12 | + |
| 13 | + // we will be mocked later on |
| 14 | + toLiquidAST: vi.fn().mockImplementation((source, options) => { |
| 15 | + return original.toLiquidAST(source, options); |
| 16 | + }), |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +describe('detectInvalidLoopRange', async () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not report when range is valid', async () => { |
| 26 | + const testCases = [ |
| 27 | + `{% for i in (1..10) %}{% endfor %}`, |
| 28 | + `{% tablerow x in (a..b) %}{% endtablerow %}`, |
| 29 | + ]; |
| 30 | + |
| 31 | + for (const sourceCode of testCases) { |
| 32 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 33 | + expect(offenses).to.have.length(0); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + it('should report when there is an extra `.` character in range', async () => { |
| 38 | + const testCases = [ |
| 39 | + [`{% for i in (1...10) %}{% endfor %}`, '{% for i in (1..10) %}{% endfor %}'], |
| 40 | + [ |
| 41 | + `{% tablerow x in (a...b) %}{% endtablerow %}`, |
| 42 | + '{% tablerow x in (a..b) %}{% endtablerow %}', |
| 43 | + ], |
| 44 | + [ |
| 45 | + `{% tablerow x in (a .. b) %}{% endtablerow %}`, |
| 46 | + '{% tablerow x in (a..b) %}{% endtablerow %}', |
| 47 | + ], |
| 48 | + [ |
| 49 | + `{% tablerow x in ( a..b ) %}{% endtablerow %}`, |
| 50 | + '{% tablerow x in (a..b) %}{% endtablerow %}', |
| 51 | + ], |
| 52 | + ]; |
| 53 | + |
| 54 | + for (const [sourceCode, expected] of testCases) { |
| 55 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 56 | + expect(offenses).to.have.length(1); |
| 57 | + expect(offenses[0].message).to.equal(INVALID_LOOP_RANGE_MESSAGE); |
| 58 | + |
| 59 | + const fixed = applyFix(sourceCode, offenses[0]); |
| 60 | + expect(fixed).to.equal(expected); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it('should report when there are multiple instances of the error', async () => { |
| 65 | + const sourceCode = `{% for i in (1...10) %}{% endfor %} {% tablerow x in (a...b) %}{% endtablerow %}`; |
| 66 | + |
| 67 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 68 | + expect(offenses).to.have.length(2); |
| 69 | + expect(offenses[0].message).to.equal(INVALID_LOOP_RANGE_MESSAGE); |
| 70 | + expect(offenses[1].message).to.equal(INVALID_LOOP_RANGE_MESSAGE); |
| 71 | + |
| 72 | + expect(toLiquidAST).toHaveBeenCalledTimes(2); |
| 73 | + expect(toLiquidAST).toHaveBeenCalledWith(`{% for i in (1..10) %}{% endfor %}`, { |
| 74 | + allowUnclosedDocumentNode: false, |
| 75 | + mode: 'strict', |
| 76 | + }); |
| 77 | + expect(toLiquidAST).toHaveBeenCalledWith(`{% tablerow x in (a..b) %}{% endtablerow %}`, { |
| 78 | + allowUnclosedDocumentNode: false, |
| 79 | + mode: 'strict', |
| 80 | + }); |
| 81 | + |
| 82 | + const fixed = applyFix(sourceCode, offenses[0]); |
| 83 | + expect(fixed).to.equal( |
| 84 | + `{% for i in (1..10) %}{% endfor %} {% tablerow x in (a...b) %}{% endtablerow %}`, |
| 85 | + ); |
| 86 | + |
| 87 | + const fixed2 = applyFix(sourceCode, offenses[1]); |
| 88 | + expect(fixed2).to.equal( |
| 89 | + `{% for i in (1...10) %}{% endfor %} {% tablerow x in (a..b) %}{% endtablerow %}`, |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should not report when the fixed code produces an invalid AST', async () => { |
| 94 | + const sourceCode = `{% for i in (1..10) %}{% endfor %}`; |
| 95 | + |
| 96 | + vi.mocked(toLiquidAST).mockImplementation((_source, _options) => { |
| 97 | + throw new SyntaxError('Invalid AST'); |
| 98 | + }); |
| 99 | + |
| 100 | + const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode); |
| 101 | + expect(offenses).to.have.length(0); |
| 102 | + }); |
| 103 | +}); |
0 commit comments