11import dedent from 'dedent' ;
22import { describe , expect , test } from 'vitest' ;
3- import { generateDts , type GenerateDtsHost , type GenerateDtsOptions } from './dts-generator.js' ;
3+ import { generateDts , type GenerateDtsOptions } from './dts-generator.js' ;
44import { readAndParseCSSModule } from './test/css-module.js' ;
5- import { fakeMatchesPattern , fakeResolver } from './test/faker.js' ;
65import { createIFF } from './test/fixture.js' ;
76
8- const host : GenerateDtsHost = {
9- resolver : fakeResolver ( ) ,
10- matchesPattern : fakeMatchesPattern ( ) ,
11- } ;
127const options : GenerateDtsOptions = {
138 namedExports : false ,
149 prioritizeNamedImports : false ,
@@ -20,8 +15,7 @@ describe('generateDts', () => {
2015 const iff = await createIFF ( {
2116 'test.module.css' : '' ,
2217 } ) ;
23- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , options ) . text )
24- . toMatchInlineSnapshot ( `
18+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , options ) . text ) . toMatchInlineSnapshot ( `
2519 "// @ts-nocheck
2620 declare const styles = {
2721 };
@@ -36,8 +30,7 @@ describe('generateDts', () => {
3630 .local2 { color: red; }
3731 ` ,
3832 } ) ;
39- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , options ) . text )
40- . toMatchInlineSnapshot ( `
33+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , options ) . text ) . toMatchInlineSnapshot ( `
4134 "// @ts-nocheck
4235 declare const styles = {
4336 local1: '' as readonly string,
@@ -47,107 +40,48 @@ describe('generateDts', () => {
4740 "
4841 ` ) ;
4942 } ) ;
50- test ( 'generates d.ts file with token importers' , async ( ) => {
43+ test ( 'generates types for token importers' , async ( ) => {
5144 const iff = await createIFF ( {
5245 'test.module.css' : dedent `
5346 @import './a.module.css';
54- @value imported1 from './b.module.css';
55- @value imported2 as aliasedImported2 from './c.module.css';
47+ @value imported1, imported2 as aliasedImported2 from './b.module.css';
5648 ` ,
57- 'a.module.css' : '' ,
58- 'b.module.css' : '' ,
59- 'c.module.css' : '' ,
6049 } ) ;
61- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , options ) . text )
62- . toMatchInlineSnapshot ( `
50+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , options ) . text ) . toMatchInlineSnapshot ( `
6351 "// @ts-nocheck
6452 declare const styles = {
6553 ...(await import('./a.module.css')).default,
6654 imported1: (await import('./b.module.css')).default.imported1,
67- aliasedImported2: (await import('./c .module.css')).default.imported2,
55+ aliasedImported2: (await import('./b .module.css')).default.imported2,
6856 };
6957 export default styles;
7058 "
7159 ` ) ;
7260 } ) ;
73- test ( 'resolves specifiers ' , async ( ) => {
61+ test ( 'does not generate types for URL token importers ' , async ( ) => {
7462 const iff = await createIFF ( {
7563 'test.module.css' : dedent `
76- @import '@/a.module.css';
77- @value imported1 from '@/b.module.css';
78- @value imported2 as aliasedImported2 from '@/c.module.css';
64+ @import 'https://example.com/a.module.css';
65+ @value imported1 from 'https://example.com/b.module.css';
7966 ` ,
80- 'a.module.css' : '' ,
81- 'b.module.css' : '' ,
82- 'c.module.css' : '' ,
8367 } ) ;
84- const resolver = ( specifier : string ) => specifier . replace ( '@' , '/src' ) ;
85- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , { ...host , resolver } , options ) . text )
86- . toMatchInlineSnapshot ( `
87- "// @ts-nocheck
88- declare const styles = {
89- ...(await import('@/a.module.css')).default,
90- imported1: (await import('@/b.module.css')).default.imported1,
91- aliasedImported2: (await import('@/c.module.css')).default.imported2,
92- };
93- export default styles;
94- "
95- ` ) ;
96- } ) ;
97- test ( 'does not generate types for unmatched modules' , async ( ) => {
98- const iff = await createIFF ( {
99- 'test.module.css' : dedent `
100- @import './unmatched.module.css';
101- @value unmatched_1 from './unmatched.module.css';
102- ` ,
103- 'unmatched.module.css' : '.unmatched_1 { color: red; }' ,
104- } ) ;
105- expect (
106- generateDts (
107- readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! ,
108- { ...host , matchesPattern : ( path ) => path . endsWith ( '.module.css' ) && ! path . endsWith ( 'unmatched.module.css' ) } ,
109- options ,
110- ) . text ,
111- ) . toMatchInlineSnapshot ( `
68+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , options ) . text ) . toMatchInlineSnapshot ( `
11269 "// @ts-nocheck
11370 declare const styles = {
11471 };
11572 export default styles;
11673 "
11774 ` ) ;
11875 } ) ;
119- test ( 'generates types for unresolvable modules' , async ( ) => {
120- const iff = await createIFF ( {
121- 'test.module.css' : dedent `
122- @import './unresolvable.module.css';
123- @value unresolvable_1 from './unresolvable.module.css';
124- ` ,
125- } ) ;
126- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , options ) . text )
127- . toMatchInlineSnapshot ( `
128- "// @ts-nocheck
129- declare const styles = {
130- ...(await import('./unresolvable.module.css')).default,
131- unresolvable_1: (await import('./unresolvable.module.css')).default.unresolvable_1,
132- };
133- export default styles;
134- "
135- ` ) ;
136- } ) ;
13776 test ( 'does not generate types for invalid name as JS identifier' , async ( ) => {
13877 const iff = await createIFF ( {
13978 'test.module.css' : dedent `
14079 .a-1 { color: red; }
14180 @value b-1 from './b.module.css';
14281 @value b_2 as a-2 from './b.module.css';
14382 ` ,
144- 'b.module.css' : dedent `
145- @value b-1: red;
146- @value b_2: red;
147- ` ,
14883 } ) ;
149- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , options ) . text )
150- . toMatchInlineSnapshot ( `
84+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , options ) . text ) . toMatchInlineSnapshot ( `
15185 "// @ts-nocheck
15286 declare const styles = {
15387 };
@@ -159,8 +93,7 @@ describe('generateDts', () => {
15993 const iff = await createIFF ( {
16094 'test.module.css' : '.__proto__ { color: red; }' ,
16195 } ) ;
162- expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , options ) . text )
163- . toMatchInlineSnapshot ( `
96+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , options ) . text ) . toMatchInlineSnapshot ( `
16497 "// @ts-nocheck
16598 declare const styles = {
16699 };
@@ -172,15 +105,13 @@ describe('generateDts', () => {
172105 const iff = await createIFF ( {
173106 'test.module.css' : '.default { color: red; }' ,
174107 } ) ;
175- expect (
176- generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , { ...options , namedExports : true } ) . text ,
177- ) . toMatchInlineSnapshot ( `
108+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , { ...options , namedExports : true } ) . text )
109+ . toMatchInlineSnapshot ( `
178110 "// @ts-nocheck
179111 "
180112 ` ) ;
181- expect (
182- generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , { ...options , namedExports : false } ) . text ,
183- ) . toMatchInlineSnapshot ( `
113+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , { ...options , namedExports : false } ) . text )
114+ . toMatchInlineSnapshot ( `
184115 "// @ts-nocheck
185116 declare const styles = {
186117 default: '' as readonly string,
@@ -197,15 +128,9 @@ describe('generateDts', () => {
197128 @import './a.module.css';
198129 @value imported1, imported2 as aliasedImported2 from './b.module.css';
199130 ` ,
200- 'a.module.css' : '' ,
201- 'b.module.css' : dedent `
202- @value imported1: red;
203- @value imported2: red;
204- ` ,
205131 } ) ;
206- expect (
207- generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , { ...options , namedExports : true } ) . text ,
208- ) . toMatchInlineSnapshot ( `
132+ expect ( generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , { ...options , namedExports : true } ) . text )
133+ . toMatchInlineSnapshot ( `
209134 "// @ts-nocheck
210135 export var local1: string;
211136 export var local2: string;
@@ -222,7 +147,7 @@ describe('generateDts', () => {
222147 'test.module.css' : '.local1 { color: red; }' ,
223148 } ) ;
224149 expect (
225- generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , host , {
150+ generateDts ( readAndParseCSSModule ( iff . paths [ 'test.module.css' ] ) ! , {
226151 ...options ,
227152 namedExports : true ,
228153 forTsPlugin : true ,
0 commit comments