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 transform functions', () => {
58 const code = `
59 import { match } from 'reghex';
60
61 const first = match('inner', x => x)\`\`;
62
63 const transform = x => x;
64 const second = match('node', transform)\`\`;
65 `;
66
67 expect(
68 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
69 .code
70 ).toMatchSnapshot();
71});
72
73it('works with non-capturing groups', () => {
74 const code = `
75 import { match } from 'reghex';
76
77 const node = match('node')\`
78 \${1} (\${2} | (?: \${3})+)
79 \`;
80 `;
81
82 expect(
83 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
84 .code
85 ).toMatchSnapshot();
86});
87
88it('works together with @babel/plugin-transform-modules-commonjs', () => {
89 const code = `
90 import { match } from 'reghex';
91
92 const node = match('node')\`
93 \${1} \${2}
94 \`;
95 `;
96
97 expect(
98 transform(code, {
99 babelrc: false,
100 presets: [],
101 plugins: [
102 reghexPlugin,
103 [
104 '@babel/plugin-transform-modules-commonjs',
105 {
106 noInterop: true,
107 loose: true,
108 },
109 ],
110 ],
111 }).code
112 ).toMatchSnapshot();
113});