Mirror: The magical sticky regex-based parser generator 馃
at v2.0.2 2.7 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 while only minifying', () => { 20 const code = ` 21 import { match } from 'reghex/macro'; 22 23 const node = match('node')\` 24 \${1}+ | \${2}+ (\${3} ( \${4}? \${5} ) )* 25 \`; 26 `; 27 28 expect( 29 transform(code, { 30 babelrc: false, 31 presets: [], 32 plugins: [[reghexPlugin, { codegen: false }]], 33 }).code 34 ).toMatchSnapshot(); 35}); 36 37it('works with local recursion', () => { 38 // NOTE: A different default name is allowed 39 const code = ` 40 import { match as m, tag } from 'reghex'; 41 42 const inner = m('inner')\` 43 \${/inner/} 44 \`; 45 46 const node = m('node')\` 47 \${inner} 48 \`; 49 `; 50 51 expect( 52 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 53 .code 54 ).toMatchSnapshot(); 55}); 56 57it('works with self-referential thunks', () => { 58 const code = ` 59 import { match, tag } from 'reghex'; 60 61 const inner = match('inner')\` 62 \${() => node} 63 \`; 64 65 const node = match('node')\` 66 \${inner} 67 \`; 68 `; 69 70 expect( 71 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 72 .code 73 ).toMatchSnapshot(); 74}); 75 76it('works with transform functions', () => { 77 const code = ` 78 import { match } from 'reghex'; 79 80 const first = match('inner', x => x)\`\`; 81 82 const transform = x => x; 83 const second = match('node', transform)\`\`; 84 `; 85 86 expect( 87 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 88 .code 89 ).toMatchSnapshot(); 90}); 91 92it('works with non-capturing groups', () => { 93 const code = ` 94 import { match } from 'reghex'; 95 96 const node = match('node')\` 97 \${1} (\${2} | (?: \${3})+) 98 \`; 99 `; 100 101 expect( 102 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] }) 103 .code 104 ).toMatchSnapshot(); 105}); 106 107it('works together with @babel/plugin-transform-modules-commonjs', () => { 108 const code = ` 109 import { match } from 'reghex'; 110 111 const node = match('node')\` 112 \${1} \${2} 113 \`; 114 `; 115 116 expect( 117 transform(code, { 118 babelrc: false, 119 presets: [], 120 plugins: [ 121 reghexPlugin, 122 [ 123 '@babel/plugin-transform-modules-commonjs', 124 { 125 noInterop: true, 126 loose: true, 127 }, 128 ], 129 ], 130 }).code 131 ).toMatchSnapshot(); 132});