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('deduplicates hoisted expressions', () => {
38 const code = `
39 import { match } from 'reghex/macro';
40
41 const re = /1/;
42 const str = '1';
43
44 const a = match('a')\`
45 \${re}
46 \${str}
47 \`;
48
49 const b = match('b')\`
50 \${re}
51 \${'2'}
52 \`;
53 `;
54
55 expect(
56 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
57 .code
58 ).toMatchSnapshot();
59});
60
61it('works with local recursion', () => {
62 // NOTE: A different default name is allowed
63 const code = `
64 import { match as m, tag } from 'reghex';
65
66 const inner = m('inner')\`
67 \${/inner/}
68 \`;
69
70 const node = m('node')\`
71 \${inner}
72 \`;
73 `;
74
75 expect(
76 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
77 .code
78 ).toMatchSnapshot();
79});
80
81it('works with self-referential thunks', () => {
82 const code = `
83 import { match, tag } from 'reghex';
84
85 const inner = match('inner')\`
86 \${() => node}
87 \`;
88
89 const node = match('node')\`
90 \${inner}
91 \`;
92 `;
93
94 expect(
95 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
96 .code
97 ).toMatchSnapshot();
98});
99
100it('works with transform functions', () => {
101 const code = `
102 import { match } from 'reghex';
103
104 const first = match('inner', x => x)\`\`;
105
106 const transform = x => x;
107 const second = match('node', transform)\`\`;
108 `;
109
110 expect(
111 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
112 .code
113 ).toMatchSnapshot();
114});
115
116it('works with non-capturing groups', () => {
117 const code = `
118 import { match } from 'reghex';
119
120 const node = match('node')\`
121 \${1} (\${2} | (?: \${3})+)
122 \`;
123 `;
124
125 expect(
126 transform(code, { babelrc: false, presets: [], plugins: [reghexPlugin] })
127 .code
128 ).toMatchSnapshot();
129});
130
131it('works together with @babel/plugin-transform-modules-commonjs', () => {
132 const code = `
133 import { match } from 'reghex';
134
135 const node = match('node')\`
136 \${1} \${2}
137 \`;
138 `;
139
140 expect(
141 transform(code, {
142 babelrc: false,
143 presets: [],
144 plugins: [
145 reghexPlugin,
146 [
147 '@babel/plugin-transform-modules-commonjs',
148 {
149 noInterop: true,
150 loose: true,
151 },
152 ],
153 ],
154 }).code
155 ).toMatchSnapshot();
156});