@@ -17,6 +17,8 @@ interface CodeMapping {
1717 lengths : number [ ] ;
1818 /** The generated offsets of the tokens in the *.d.ts file. */
1919 generatedOffsets : number [ ] ;
20+ /** The lengths of the tokens in the *.d.ts file. */
21+ generatedLengths ?: number [ ] ;
2022}
2123
2224/** The map linking the two codes in *.d.ts */
@@ -43,7 +45,7 @@ interface GenerateDtsResult {
4345 */
4446export function generateDts ( cssModule : CSSModule , options : GenerateDtsOptions ) : GenerateDtsResult {
4547 // Exclude invalid tokens
46- const localTokens = cssModule . localTokens . filter ( ( token ) => isValidName ( token . name , options ) ) ;
48+ const localTokens = cssModule . localTokens . filter ( ( token ) => isValidName ( token . name , options , false ) ) ;
4749 const tokenImporters = cssModule . tokenImporters
4850 // Exclude invalid imported tokens
4951 . map ( ( tokenImporter ) => {
@@ -52,8 +54,8 @@ export function generateDts(cssModule: CSSModule, options: GenerateDtsOptions):
5254 ...tokenImporter ,
5355 values : tokenImporter . values . filter (
5456 ( value ) =>
55- isValidName ( value . name , options ) &&
56- ( value . localName === undefined || isValidName ( value . localName , options ) ) ,
57+ isValidName ( value . name , options , true ) &&
58+ ( value . localName === undefined || isValidName ( value . localName , options , true ) ) ,
5759 ) ,
5860 } ;
5961 } else {
@@ -265,7 +267,7 @@ function generateDefaultExportDts(
265267 localTokens : Token [ ] ,
266268 tokenImporters : TokenImporter [ ] ,
267269) : { text : string ; mapping : CodeMapping ; linkedCodeMapping : LinkedCodeMapping } {
268- const mapping : CodeMapping = { sourceOffsets : [ ] , lengths : [ ] , generatedOffsets : [ ] } ;
270+ const mapping : CodeMapping = { sourceOffsets : [ ] , lengths : [ ] , generatedOffsets : [ ] , generatedLengths : [ ] } ;
269271 const linkedCodeMapping : LinkedCodeMapping = {
270272 sourceOffsets : [ ] ,
271273 lengths : [ ] ,
@@ -310,13 +312,31 @@ function generateDefaultExportDts(
310312 * | ^ mapping.generatedOffsets[1]
311313 * |
312314 * 4 | };
315+ *
316+ * invalid as js identifier:
317+ * a.module.css:
318+ * 1 | .a-123 { color: red; }
319+ * | ^ mapping.sourceOffsets[0]
320+ *
321+ * a.module.css.d.ts:
322+ * 1 | declare const styles = {
323+ * 2 | 'a-123': '' as readonly string,
324+ * | ^ mapping.generatedOffsets[0]
325+ * 3 | };
313326 */
314327
315328 text += ` ` ;
316329 mapping . sourceOffsets . push ( token . loc . start . offset ) ;
317330 mapping . generatedOffsets . push ( text . length ) ;
318331 mapping . lengths . push ( token . name . length ) ;
319- text += `${ token . name } : '' as readonly string,\n` ;
332+ if ( isValidAsJSIdentifier ( token . name ) ) {
333+ mapping . generatedLengths ! . push ( token . name . length ) ;
334+ text += `${ token . name } : '' as readonly string,\n` ;
335+ } else {
336+ // Include quotes in the mapping for invalid JS identifiers
337+ mapping . generatedLengths ! . push ( token . name . length + 2 ) ;
338+ text += `'${ token . name } ': '' as readonly string,\n` ;
339+ }
320340 }
321341 for ( const tokenImporter of tokenImporters ) {
322342 if ( tokenImporter . type === 'import' ) {
@@ -347,6 +367,7 @@ function generateDefaultExportDts(
347367 mapping . sourceOffsets . push ( tokenImporter . fromLoc . start . offset - 1 ) ;
348368 mapping . lengths . push ( tokenImporter . from . length + 2 ) ;
349369 mapping . generatedOffsets . push ( text . length ) ;
370+ mapping . generatedLengths ! . push ( tokenImporter . from . length + 2 ) ;
350371 text += `'${ tokenImporter . from } ')).default),\n` ;
351372 } else {
352373 /**
@@ -393,19 +414,22 @@ function generateDefaultExportDts(
393414 mapping . sourceOffsets . push ( localLoc . start . offset ) ;
394415 mapping . lengths . push ( localName . length ) ;
395416 mapping . generatedOffsets . push ( text . length ) ;
417+ mapping . generatedLengths ! . push ( localName . length ) ;
396418 linkedCodeMapping . sourceOffsets . push ( text . length ) ;
397419 linkedCodeMapping . lengths . push ( localName . length ) ;
398420 text += `${ localName } : (await import(` ;
399421 if ( i === 0 ) {
400422 mapping . sourceOffsets . push ( tokenImporter . fromLoc . start . offset - 1 ) ;
401423 mapping . lengths . push ( tokenImporter . from . length + 2 ) ;
402424 mapping . generatedOffsets . push ( text . length ) ;
425+ mapping . generatedLengths ! . push ( tokenImporter . from . length + 2 ) ;
403426 }
404427 text += `'${ tokenImporter . from } ')).default.` ;
405428 if ( 'localName' in value ) {
406429 mapping . sourceOffsets . push ( value . loc . start . offset ) ;
407430 mapping . lengths . push ( value . name . length ) ;
408431 mapping . generatedOffsets . push ( text . length ) ;
432+ mapping . generatedLengths ! . push ( value . name . length ) ;
409433 }
410434 linkedCodeMapping . generatedOffsets . push ( text . length ) ;
411435 linkedCodeMapping . generatedLengths . push ( value . name . length ) ;
@@ -417,8 +441,8 @@ function generateDefaultExportDts(
417441 return { text, mapping, linkedCodeMapping } ;
418442}
419443
420- function isValidName ( name : string , options : GenerateDtsOptions ) : boolean {
421- if ( ! isValidAsJSIdentifier ( name ) ) return false ;
444+ function isValidName ( name : string , options : GenerateDtsOptions , isTokenImport : boolean ) : boolean {
445+ if ( ( options . namedExports || isTokenImport ) && ! isValidAsJSIdentifier ( name ) ) return false ;
422446 if ( name === '__proto__' ) return false ;
423447 if ( options . namedExports && name === 'default' ) return false ;
424448 return true ;
0 commit comments