|
| 1 | +import { describe, it } from 'vitest'; |
| 2 | +import { AstCoreFactory } from '../lib/AstCoreFactory.js'; |
| 3 | + |
| 4 | +describe('astCoreFactory', () => { |
| 5 | + describe('source location management', () => { |
| 6 | + it('gen() creates autoGenerate location', ({ expect }) => { |
| 7 | + const factory = new AstCoreFactory(); |
| 8 | + const loc = factory.gen(); |
| 9 | + expect(loc.sourceLocationType).toBe('autoGenerate'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('sourceLocation creates source location when tracking enabled', ({ expect }) => { |
| 13 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 14 | + const loc = factory.sourceLocationSource(10, 20); |
| 15 | + expect(factory.isSourceLocationSource(loc)).toBe(true); |
| 16 | + expect(loc).toMatchObject({ start: 10, end: 20 }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('sourceLocationStringReplace creates stringReplace location', ({ expect }) => { |
| 20 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 21 | + const loc = factory.sourceLocationStringReplace('new content', 5, 15); |
| 22 | + expect(factory.isSourceLocationStringReplace(loc)).toBe(true); |
| 23 | + if (factory.isSourceLocationStringReplace(loc)) { |
| 24 | + expect(loc.newSource).toBe('new content'); |
| 25 | + expect(loc.start).toBe(5); |
| 26 | + expect(loc.end).toBe(15); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('sourceLocationStringReplace returns gen() when tracking disabled', ({ expect }) => { |
| 31 | + const factory = new AstCoreFactory({ tracksSourceLocation: false }); |
| 32 | + const loc = factory.sourceLocationStringReplace('new content', 5, 15); |
| 33 | + expect(factory.isSourceLocationNodeAutoGenerate(loc)).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('sourceLocationNodeReplace with number arguments', ({ expect }) => { |
| 37 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 38 | + const loc = factory.sourceLocationNodeReplace(0, 10); |
| 39 | + expect(factory.isSourceLocationNodeReplace(loc)).toBe(true); |
| 40 | + expect(loc.start).toBe(0); |
| 41 | + expect(loc.end).toBe(10); |
| 42 | + }); |
| 43 | + |
| 44 | + it('sourceLocationNodeReplace with SourceLocationSource argument', ({ expect }) => { |
| 45 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 46 | + const sourceLoc = factory.sourceLocationSource(5, 25); |
| 47 | + if (factory.isSourceLocationSource(sourceLoc)) { |
| 48 | + const loc = factory.sourceLocationNodeReplace(sourceLoc); |
| 49 | + expect(factory.isSourceLocationNodeReplace(loc)).toBe(true); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('sourceLocationNodeReplaceUnsafe handles SourceLocationSource', ({ expect }) => { |
| 54 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 55 | + const sourceLoc = factory.sourceLocationSource(0, 10); |
| 56 | + if (factory.isSourceLocationSource(sourceLoc)) { |
| 57 | + const loc = factory.sourceLocationNodeReplaceUnsafe(sourceLoc); |
| 58 | + expect(factory.isSourceLocationNodeReplace(loc)).toBe(true); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('sourceLocationNodeReplaceUnsafe throws for unsupported types', ({ expect }) => { |
| 63 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 64 | + const genLoc = factory.gen(); |
| 65 | + expect(() => factory.sourceLocationNodeReplaceUnsafe(genLoc)) |
| 66 | + .toThrowError('Cannot convert SourceLocation'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('sourceLocationInlinedSource creates inlined source location', ({ expect }) => { |
| 70 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 71 | + const innerLoc = factory.sourceLocationSource(0, 10); |
| 72 | + // SourceLocationInlinedSource(newSource, subLoc, start, end, startOnNew?, endOnNew?) |
| 73 | + const loc = factory.sourceLocationInlinedSource('inline source', innerLoc, 5, 15); |
| 74 | + expect(factory.isSourceLocationInlinedSource(loc)).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it('sourceLocationNoMaterialize creates noMaterialize location', ({ expect }) => { |
| 78 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 79 | + const loc = factory.sourceLocationNoMaterialize(); |
| 80 | + expect(factory.isSourceLocationNoMaterialize(loc)).toBe(true); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('type checking', () => { |
| 85 | + it('isLocalized checks for loc property', ({ expect }) => { |
| 86 | + const factory = new AstCoreFactory(); |
| 87 | + expect(factory.isLocalized({ loc: factory.gen() })).toBe(true); |
| 88 | + expect(factory.isLocalized({})).toBe(false); |
| 89 | + expect(factory.isLocalized({ loc: 'not a location' })).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('isOfType checks type property', ({ expect }) => { |
| 93 | + const factory = new AstCoreFactory(); |
| 94 | + expect(factory.isOfType({ type: 'test' }, 'test')).toBe(true); |
| 95 | + expect(factory.isOfType({ type: 'other' }, 'test')).toBe(false); |
| 96 | + expect(factory.isOfType({}, 'test')).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('isOfSubType checks type and subType properties', ({ expect }) => { |
| 100 | + const factory = new AstCoreFactory(); |
| 101 | + expect(factory.isOfSubType({ type: 'a', subType: 'b' }, 'a', 'b')).toBe(true); |
| 102 | + expect(factory.isOfSubType({ type: 'a', subType: 'c' }, 'a', 'b')).toBe(false); |
| 103 | + expect(factory.isOfSubType({ type: 'x', subType: 'b' }, 'a', 'b')).toBe(false); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('location types', () => { |
| 108 | + it('identifies all location types correctly', ({ expect }) => { |
| 109 | + const factory = new AstCoreFactory({ tracksSourceLocation: true }); |
| 110 | + |
| 111 | + const genLoc = factory.gen(); |
| 112 | + expect(factory.isSourceLocationNodeAutoGenerate(genLoc)).toBe(true); |
| 113 | + expect(factory.isSourceLocationSource(genLoc)).toBe(false); |
| 114 | + |
| 115 | + const sourceLoc = factory.sourceLocationSource(0, 10); |
| 116 | + expect(factory.isSourceLocationSource(sourceLoc)).toBe(true); |
| 117 | + expect(factory.isSourceLocationNodeAutoGenerate(sourceLoc)).toBe(false); |
| 118 | + |
| 119 | + const noMatLoc = factory.sourceLocationNoMaterialize(); |
| 120 | + expect(factory.isSourceLocationNoMaterialize(noMatLoc)).toBe(true); |
| 121 | + |
| 122 | + const strReplaceLoc = factory.sourceLocationStringReplace('x', 0, 1); |
| 123 | + expect(factory.isSourceLocationStringReplace(strReplaceLoc)).toBe(true); |
| 124 | + |
| 125 | + const nodeReplaceLoc = factory.sourceLocationNodeReplace(0, 5); |
| 126 | + expect(factory.isSourceLocationNodeReplace(nodeReplaceLoc)).toBe(true); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments