@@ -4,6 +4,26 @@ import { parse } from 'acorn'
4
4
import { MagicRegExpTransformPlugin } from '../src/transform'
5
5
6
6
describe ( 'transformer' , ( ) => {
7
+ const couldTransform = [
8
+ "import { createRegExp, exactly, anyOf } from 'magic-regexp'" ,
9
+ "const re1 = createRegExp(exactly('bar').notBefore('foo'))" ,
10
+ ]
11
+
12
+ it ( 'ignores non-JS files' , ( ) => {
13
+ expect ( transform ( couldTransform , 'test.css' ) ) . toBeUndefined ( )
14
+ } )
15
+
16
+ it ( 'transforms vue script blocks' , ( ) => {
17
+ expect ( transform ( couldTransform , 'test.vue?type=script' ) ) . toBeDefined ( )
18
+ expect ( transform ( couldTransform , 'test.vue' ) ) . toBeDefined ( )
19
+ expect ( transform ( couldTransform , 'test.vue?type=template' ) ) . toBeUndefined ( )
20
+ } )
21
+
22
+ it ( 'ignores code without imports from magic-regexp' , ( ) => {
23
+ expect ( transform ( couldTransform [ 1 ] ) ) . toBeUndefined ( )
24
+ expect ( transform ( [ '// magic-regexp' , couldTransform [ 1 ] ] ) ) . toBeUndefined ( )
25
+ } )
26
+
7
27
it ( 'preserves context for dynamic regexps' , ( ) => {
8
28
expect (
9
29
transform ( [
@@ -15,6 +35,7 @@ describe('transformer', () => {
15
35
16
36
it ( 'statically replaces regexps where possible' , ( ) => {
17
37
const code = transform ( [
38
+ "import { something } from 'other-module'" ,
18
39
"import { createRegExp, exactly, anyOf } from 'magic-regexp'" ,
19
40
'//' , // this lets us tree-shake the import for use in our test-suite
20
41
"const re1 = createRegExp(exactly('bar').notBefore('foo'))" ,
@@ -24,7 +45,8 @@ describe('transformer', () => {
24
45
"re3.test('/foo/bar')" ,
25
46
] )
26
47
expect ( code ) . toMatchInlineSnapshot ( `
27
- "import { createRegExp, exactly, anyOf } from 'magic-regexp'
48
+ "import { something } from 'other-module'
49
+ import { createRegExp, exactly, anyOf } from 'magic-regexp'
28
50
//
29
51
const re1 = /bar(?!foo)/
30
52
const re2 = /(bar|foo)/
@@ -55,11 +77,11 @@ describe('transformer', () => {
55
77
} )
56
78
} )
57
79
58
- const transform = ( code : string | string [ ] ) => {
80
+ const transform = ( code : string | string [ ] , id = 'some-id.js' ) => {
59
81
const plugin = MagicRegExpTransformPlugin . vite ( )
60
82
return plugin . transform . call (
61
83
{ parse : ( code : string ) => parse ( code , { ecmaVersion : 2022 , sourceType : 'module' } ) } ,
62
84
Array . isArray ( code ) ? code . join ( '\n' ) : code ,
63
- 'some-id.js'
85
+ id
64
86
) ?. code
65
87
}
0 commit comments