Mirror: The magical sticky regex-based parser generator 馃
at v1.0.2 2.0 kB view raw
1import { transform } from '@babel/core'; 2import reghexPlugin from './plugin'; 3 4it('works with standard features', () => { 5 const code = ` 6 import match from 'reghex/macro'; 7 8 const node = match('node')\` 9 \${1}+ | \${2}+ (\${3} ( \${4}? \${5} ) )* 10 \`; 11 `; 12 13 expect( 14 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 15 .code 16 ).toMatchSnapshot(); 17}); 18 19it('works with local recursion', () => { 20 // NOTE: A different default name is allowed 21 const code = ` 22 import match_rec, { tag } from 'reghex'; 23 24 const inner = match_rec('inner')\` 25 \${/inner/} 26 \`; 27 28 const node = match_rec('node')\` 29 \${inner} 30 \`; 31 `; 32 33 expect( 34 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 35 .code 36 ).toMatchSnapshot(); 37}); 38 39it('works with transform functions', () => { 40 const code = ` 41 import match from 'reghex'; 42 43 const first = match('inner', x => x)\`\`; 44 45 const transform = x => x; 46 const second = match('node', transform)\`\`; 47 `; 48 49 expect( 50 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 51 .code 52 ).toMatchSnapshot(); 53}); 54 55it('works with non-capturing groups', () => { 56 const code = ` 57 import match from 'reghex'; 58 59 const node = match('node')\` 60 \${1} (\${2} | (?: \${3})+) 61 \`; 62 `; 63 64 expect( 65 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 66 .code 67 ).toMatchSnapshot(); 68}); 69 70it('works together with @babel/plugin-transform-modules-commonjs', () => { 71 const code = ` 72 import match from 'reghex'; 73 74 const node = match('node')\` 75 \${1} \${2} 76 \`; 77 `; 78 79 expect( 80 transform(code, { 81 babelrc: false, 82 presets: [], 83 plugins: [ 84 reghexPlugin, 85 [ 86 '@babel/plugin-transform-modules-commonjs', 87 { 88 noInterop: true, 89 loose: true, 90 }, 91 ], 92 ], 93 }).code 94 ).toMatchSnapshot(); 95});