Skip to content

Commit ad81574

Browse files
mizdraclaude
andauthored
refactor(core): drop inline mapping diagrams from dts-generator (#400)
The diagrams duplicated information that is now better served by the snapshot tests in dts-generator.test.ts (for the concrete generated shape) and docs/ts-plugin-internals.md (for the design rationale). Keeping them in sync with every change was extra maintenance cost. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f758c23 commit ad81574

1 file changed

Lines changed: 1 addition & 180 deletions

File tree

packages/core/src/dts-generator.ts

Lines changed: 1 addition & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -139,29 +139,6 @@ function generateNamedExportsDts(
139139
// Therefore, `@ts-nocheck` is added to the generated type definition file.
140140
let text = `// @ts-nocheck\n`;
141141

142-
/**
143-
* The mapping is created as follows:
144-
* a.module.css:
145-
* 1 | .a_1 { color: red; }
146-
* | ^ mapping.sourceOffsets[0]
147-
* |
148-
* 2 | .a_1 { color: blue; }
149-
* | ^ mapping.sourceOffsets[1]
150-
* |
151-
*
152-
* a.module.css.d.ts:
153-
* 1 | // @ts-nocheck
154-
* 2 | var _token_0: string;
155-
* | ^ mapping.generatedOffsets[0]
156-
* |
157-
* 3 | var _token_0: string;
158-
* | ^ mapping.generatedOffsets[1]
159-
* |
160-
* 4 | export { _token_0 as 'a_1' };
161-
* | ^ ^ linkedCodeMapping.generatedOffsets[0]
162-
* | ^ linkedCodeMapping.sourceOffsets[0]
163-
*/
164-
165142
const groupedLocalTokens = Object.groupBy(localTokens, (token) => token.name);
166143
for (const [index, [name, tokens]] of Object.entries(groupedLocalTokens).entries()) {
167144
if (tokens === undefined || tokens.length === 0) continue;
@@ -184,64 +161,13 @@ function generateNamedExportsDts(
184161
}
185162
for (const tokenImporter of tokenImporters) {
186163
if (tokenImporter.type === 'all') {
187-
/**
188-
* The mapping is created as follows:
189-
* a.module.css:
190-
* 1 | @import './b.module.css';
191-
* | ^ mapping.sourceOffsets[0]
192-
* |
193-
* 2 | @import './c.module.css';
194-
* | ^ mapping.sourceOffsets[1]
195-
* |
196-
*
197-
* a.module.css.d.ts:
198-
* 1 | // @ts-nocheck
199-
* 2 | export * from './b.module.css';
200-
* | ^ mapping.generatedOffsets[0]
201-
* |
202-
* 3 | export * from './c.module.css';
203-
* | ^ mapping.generatedOffsets[1]
204-
*
205-
* NOTE: Not only the specifier but also the surrounding quotes are included in the mapping.
206-
*/
207-
208164
text += `export * from `;
209165
mapping.sourceOffsets.push(tokenImporter.fromLoc.start.offset - 1);
210166
mapping.lengths.push(tokenImporter.from.length + 2);
211167
mapping.generatedOffsets.push(text.length);
212168
mapping.generatedLengths.push(tokenImporter.from.length + 2);
213169
text += `'${tokenImporter.from}';\n`;
214170
} else {
215-
/**
216-
* The mapping is created as follows:
217-
* a.module.css:
218-
* 1 | @value b_1, b_2 as aliased_b_2 from './b.module.css';
219-
* | ^ ^ ^ ^ mapping.sourceOffsets[3]
220-
* | ^ ^ ^ mapping.sourceOffsets[2]
221-
* | ^ ^ mapping.sourceOffsets[1]
222-
* | ^ mapping.sourceOffsets[0]
223-
* |
224-
*
225-
* a.module.css.d.ts:
226-
* 1 | // @ts-nocheck
227-
* 2 | export {
228-
* 3 | 'b_1' as 'b_1',
229-
* | ^ ^^ mapping.generatedOffsets[0]
230-
* | ^ ^ linkedCodeMapping.generatedOffsets[0]
231-
* | ^ linkedCodeMapping.sourceOffsets[0]
232-
* |
233-
* 4 | 'b_2' as 'aliased_b_2',
234-
* | ^^ ^^ mapping.generatedOffsets[2]
235-
* | ^^ ^ linkedCodeMapping.generatedOffsets[1]
236-
* | ^^ mapping.generatedOffsets[1]
237-
* | ^ linkedCodeMapping.sourceOffsets[1]
238-
* |
239-
* 5 | } from './b.module.css';
240-
* | ^ mapping.generatedOffsets[3]
241-
*
242-
* NOTE: Not only the specifier but also the surrounding quotes are included in the mapping.
243-
*/
244-
245171
text += `export {\n`;
246172
for (const entry of tokenImporter.entries) {
247173
const localName = entry.localName ?? entry.name;
@@ -274,25 +200,14 @@ function generateNamedExportsDts(
274200
text += `'${tokenImporter.from}';\n`;
275201
}
276202
}
203+
277204
// Ensure the generated file is treated as a module even when no other
278205
// top-level export/import is emitted (e.g. an empty CSS Module file).
279206
const noModuleSyntax = localTokens.length === 0 && tokenImporters.length === 0;
280207
if (noModuleSyntax) {
281208
text += 'export {};\n';
282209
}
283210

284-
/**
285-
* The mapping is created as follows:
286-
* a.module.css:
287-
* 1 | @keyframes a_1 { ... }
288-
* 2 | .a_2 { animation-name: a_1; }
289-
* | ^ mapping.sourceOffsets[0]
290-
*
291-
* a.module.css.d.ts:
292-
* 1 | declare const __self: typeof import('./<self-filename>');
293-
* 2 | __self['a_1'];
294-
* | ^ mapping.generatedOffsets[0]
295-
*/
296211
if (options.forTsPlugin && tokenReferences.length > 0) {
297212
text += `declare const __self: typeof import('./${basename(fileName)}');\n`;
298213
for (const ref of tokenReferences) {
@@ -348,27 +263,6 @@ function generateDefaultExportDts(
348263

349264
text += `declare const ${STYLES_EXPORT_NAME} = {\n`;
350265
for (const token of localTokens) {
351-
/**
352-
* The mapping is created as follows:
353-
* a.module.css:
354-
* 1 | .a_1 { color: red; }
355-
* | ^ mapping.sourceOffsets[0]
356-
* |
357-
* 2 | .a_2 { color: blue; }
358-
* | ^ mapping.sourceOffsets[1]
359-
* |
360-
*
361-
* a.module.css.d.ts:
362-
* 1 | declare const styles = {
363-
* 2 | 'a_1': '' as string,
364-
* | ^ mapping.generatedOffsets[0]
365-
* |
366-
* 3 | 'a_2': '' as string,
367-
* | ^ mapping.generatedOffsets[1]
368-
* |
369-
* 4 | } as const;
370-
*/
371-
372266
text += ` '`;
373267
mapping.sourceOffsets.push(token.loc.start.offset);
374268
mapping.lengths.push(token.name.length);
@@ -377,74 +271,12 @@ function generateDefaultExportDts(
377271
}
378272
for (const tokenImporter of tokenImporters) {
379273
if (tokenImporter.type === 'all') {
380-
/**
381-
* The mapping is created as follows:
382-
* a.module.css:
383-
* 1 | @import './b.module.css';
384-
* | ^ mapping.sourceOffsets[0]
385-
* |
386-
* 2 | @import './c.module.css';
387-
* | ^ mapping.sourceOffsets[1]
388-
* |
389-
*
390-
* a.module.css.d.ts:
391-
* 1 | declare const styles = {
392-
* 2 | ...blockErrorType((await import('./b.module.css')).default),
393-
* | ^ mapping.generatedOffsets[0]
394-
* |
395-
* 3 | ...blockErrorType((await import('./c.module.css')).default),
396-
* | ^ mapping.generatedOffsets[1]
397-
* |
398-
* 4 | } as const;
399-
*
400-
* NOTE: Not only the specifier but also the surrounding quotes are included in the mapping.
401-
*/
402-
403274
text += ` ...blockErrorType((await import(`;
404275
mapping.sourceOffsets.push(tokenImporter.fromLoc.start.offset - 1);
405276
mapping.lengths.push(tokenImporter.from.length + 2);
406277
mapping.generatedOffsets.push(text.length);
407278
text += `'${tokenImporter.from}')).default),\n`;
408279
} else {
409-
/**
410-
* The mapping is created as follows:
411-
* a.module.css:
412-
* 1 | @value b_1, b_2 from './b.module.css';
413-
* | ^ ^ ^ mapping.sourceOffsets[1]
414-
* | ^ ^ mapping.sourceOffsets[2]
415-
* | ^ mapping.sourceOffsets[0]
416-
* |
417-
* 2 | @value c_1 as aliased_c_1 from './c.module.css';
418-
* | ^ ^ ^ mapping.sourceOffsets[4]
419-
* | ^ ^ mapping.sourceOffsets[3]
420-
* | ^ mapping.sourceOffsets[5]
421-
* |
422-
*
423-
* a.module.css.d.ts:
424-
* 1 | declare const styles = {
425-
* 2 | 'b_1': (await import('./b.module.css')).default['b_1'],
426-
* | ^^ ^ ^ linkedCodeMapping.generatedOffsets[0]
427-
* | ^^ ^ mapping.generatedOffsets[1]
428-
* | ^^ mapping.generatedOffsets[0]
429-
* | ^ linkedCodeMapping.sourceOffsets[0]
430-
* |
431-
* 3 | 'b_2': (await import('./b.module.css')).default['b_2'],
432-
* | ^^ ^ linkedCodeMapping.generatedOffsets[1]
433-
* | ^^ mapping.generatedOffsets[2]
434-
* | ^ linkedCodeMapping.sourceOffsets[1]
435-
* |
436-
* 4 | 'aliased_c_1': (await import('./c.module.css')).default['c_1'],
437-
* | ^^ ^ ^^ mapping.generatedOffsets[5]
438-
* | ^^ ^ ^ linkedCodeMapping.generatedOffsets[2]
439-
* | ^^ ^ mapping.generatedOffsets[4]
440-
* | ^^ mapping.generatedOffsets[3]
441-
* | ^ linkedCodeMapping.sourceOffsets[2]
442-
* |
443-
* 5 | } as const;
444-
*
445-
* NOTE: Not only the specifier but also the surrounding quotes are included in the mapping.
446-
*/
447-
448280
// oxlint-disable-next-line no-loop-func
449281
tokenImporter.entries.forEach((entry, i) => {
450282
const localName = entry.localName ?? entry.name;
@@ -476,17 +308,6 @@ function generateDefaultExportDts(
476308
}
477309
text += `} as const;\n`;
478310

479-
/**
480-
* The mapping is created as follows:
481-
* a.module.css:
482-
* 1 | @keyframes a_1 { ... }
483-
* 2 | .a_2 { animation-name: a_1; }
484-
* | ^ mapping.sourceOffsets[0]
485-
*
486-
* a.module.css.d.ts:
487-
* 1 | styles['a_1'];
488-
* | ^ mapping.generatedOffsets[0]
489-
*/
490311
if (options.forTsPlugin) {
491312
for (const ref of tokenReferences) {
492313
text += `${STYLES_EXPORT_NAME}['`;

0 commit comments

Comments
 (0)