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