|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { parseQuery } from '../src'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Tests for error recovery behavior when ignoreParseErrors is true. |
| 6 | + * |
| 7 | + * There is a bug in tryParse (src/parser/parser.ts): when a clause parse fails, |
| 8 | + * the parser restores this.pos to savedPos (the position before the clause keyword |
| 9 | + * was consumed), then calls this.synchronize(). But synchronize() returns immediately |
| 10 | + * when already at a clause boundary — and the clause keyword IS a clause boundary. |
| 11 | + * This means the parser gets stuck at the failed clause's keyword and all subsequent |
| 12 | + * clauses are silently dropped. |
| 13 | + */ |
| 14 | +describe('error recovery with ignoreParseErrors', () => { |
| 15 | + it('should still parse ORDER BY and LIMIT after a malformed GROUP BY clause', () => { |
| 16 | + // GROUP BY has no fields (malformed), but ORDER BY and LIMIT are valid |
| 17 | + const soql = 'SELECT Id FROM Account GROUP BY ORDER BY Id LIMIT 10'; |
| 18 | + const result = parseQuery(soql, { ignoreParseErrors: true }); |
| 19 | + |
| 20 | + // These basic parts should always parse fine |
| 21 | + expect(result.fields).toEqual([{ type: 'Field', field: 'Id' }]); |
| 22 | + expect(result.sObject).toEqual('Account'); |
| 23 | + |
| 24 | + // The malformed GROUP BY should be skipped, but the subsequent valid clauses |
| 25 | + // should still be parsed. This assertion will FAIL due to the bug: |
| 26 | + // the parser gets stuck at GROUP BY and never reaches ORDER BY or LIMIT. |
| 27 | + expect(result.orderBy).toBeDefined(); |
| 28 | + expect(result.orderBy).toEqual([{ field: 'Id' }]); |
| 29 | + expect(result.limit).toBeDefined(); |
| 30 | + expect(result.limit).toEqual(10); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should still parse LIMIT after a malformed WHERE clause', () => { |
| 34 | + // WHERE is incomplete (no condition after it), but LIMIT is valid |
| 35 | + const soql = 'SELECT Id FROM Account WHERE LIMIT 10'; |
| 36 | + const result = parseQuery(soql, { ignoreParseErrors: true }); |
| 37 | + |
| 38 | + expect(result.fields).toEqual([{ type: 'Field', field: 'Id' }]); |
| 39 | + expect(result.sObject).toEqual('Account'); |
| 40 | + |
| 41 | + // LIMIT should still be parsed even though WHERE failed. |
| 42 | + // This will FAIL due to the same bug: the parser gets stuck at WHERE. |
| 43 | + expect(result.limit).toBeDefined(); |
| 44 | + expect(result.limit).toEqual(10); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should still parse LIMIT and OFFSET after a malformed ORDER BY clause', () => { |
| 48 | + // ORDER BY has no fields (malformed), but LIMIT and OFFSET are valid |
| 49 | + const soql = 'SELECT Id FROM Account ORDER BY LIMIT 10 OFFSET 5'; |
| 50 | + const result = parseQuery(soql, { ignoreParseErrors: true }); |
| 51 | + |
| 52 | + expect(result.fields).toEqual([{ type: 'Field', field: 'Id' }]); |
| 53 | + expect(result.sObject).toEqual('Account'); |
| 54 | + |
| 55 | + // These should be parsed despite the malformed ORDER BY. |
| 56 | + // This will FAIL due to the synchronize bug. |
| 57 | + expect(result.limit).toBeDefined(); |
| 58 | + expect(result.limit).toEqual(10); |
| 59 | + expect(result.offset).toBeDefined(); |
| 60 | + expect(result.offset).toEqual(5); |
| 61 | + }); |
| 62 | +}); |
0 commit comments