also looking at this
fix(extractor): keep the heritage clause of an anonymous default class
#3124
2 files
+103
-1
| @@ -0,0 +1,93 @@ | ||
| 1 | /** | |
| 2 | * An anonymous default-exported class with a heritage clause lost it, and its | |
| 3 | * members with it: | |
| 4 | * | |
| 5 | * export default class extends Base { c = 2 } | |
| 6 | * | |
| 7 | * emitted `export default class extends {}`, which does not parse. | |
| 8 | * `extractClass` read `extends` (or `implements`) as the class name. The class | |
| 9 | * now gets a helper name, `__dtsx_default_class__`, as a default-exported | |
| 10 | * expression gets `__dtsx_default_export__`. | |
| 11 | */ | |
| 12 | import { describe, expect, it } from 'bun:test' | |
| 13 | import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' | |
| 14 | import { tmpdir } from 'node:os' | |
| 15 | import { join } from 'node:path' | |
| 16 | import { generate } from '../src/generator' | |
| 17 | import { runTsc } from './helpers/tsc' | |
| 18 | import { processCode } from './test-utils' | |
| 19 | ||
| 20 | /** Type-check `consumer` against the generated declarations as `./index`. */ | |
| 21 | async function checkConsumer(dts: string, consumer: string): Promise<string> { | |
| 22 | const dir = await mkdtemp(join(tmpdir(), 'dtsx-anonymous-class-')) | |
| 23 | try { | |
| 24 | await writeFile(join(dir, 'index.d.ts'), dts) | |
| 25 | await writeFile(join(dir, 'use.ts'), consumer) | |
| 26 | return runTsc(dir, ['index.d.ts', 'use.ts']).output | |
| 27 | } | |
| 28 | finally { | |
| 29 | await rm(dir, { recursive: true, force: true }) | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | describe('anonymous default class with a heritage clause', () => { | |
| 34 | it.each([ | |
| 35 | [ | |
| 36 | 'extends', | |
| 37 | 'class Base { b = 1 }\nexport default class extends Base { c = 2 }\n', | |
| 38 | 'export default class __dtsx_default_class__ extends Base {', | |
| 39 | 'import C from \'./index\'\nconst c = new C()\nconst n: number = c.b + c.c\nexport {}\n', | |
| 40 | ], | |
| 41 | [ | |
| 42 | 'implements', | |
| 43 | 'interface Named { name(): string }\nexport default class implements Named { name(): string { return \'x\' } }\n', | |
| 44 | 'export default class __dtsx_default_class__ implements Named {', | |
| 45 | 'import C from \'./index\'\nconst s: string = new C().name()\nexport {}\n', | |
| 46 | ], | |
| 47 | [ | |
| 48 | 'abstract, implements', | |
| 49 | 'export default abstract class implements Iterable<number> { abstract [Symbol.iterator](): Iterator<number> }\n', | |
| 50 | 'export default abstract class __dtsx_default_class__ implements Iterable<number> {', | |
| 51 | 'import C from \'./index\'\nexport class D extends C { *[Symbol.iterator](): Iterator<number> { yield 1 } }\nconst all: number[] = [...new D()]\n', | |
| 52 | ], | |
| 53 | [ | |
| 54 | 'generic, extends', | |
| 55 | 'class Box<T> { value!: T }\nexport default class<T> extends Box<T> { label = \'\' }\n', | |
| 56 | 'export default class AnonymousClass<T> extends Box<T> {', | |
| 57 | 'import C from \'./index\'\nconst c = new C<number>()\nconst n: number = c.value\nconst s: string = c.label\nexport {}\n', | |
| 58 | ], | |
| 59 | [ | |
| 60 | 'beside a class named AnonymousClass', | |
| 61 | 'interface Marker {}\nexport class AnonymousClass { tag = \'real\' }\nexport default class implements Marker { id = 1 }\n', | |
| 62 | 'export default class __dtsx_default_class__ implements Marker {', | |
| 63 | 'import C, { AnonymousClass } from \'./index\'\nconst n: number = new C().id\nconst s: string = new AnonymousClass().tag\nexport {}\n', | |
| 64 | ], | |
| 65 | ])('keeps the clause and the members: %s', async (_label, code, header, consumer) => { | |
| 66 | const output = processCode(code) | |
| 67 | expect(output).toContain(header) | |
| 68 | expect(await checkConsumer(output, consumer)).toBe('') | |
| 69 | }) | |
| 70 | ||
| 71 | it.each([ | |
| 72 | ['ambient class', 'export declare class implements { c: number }\n', 'export declare class implements {'], | |
| 73 | ['default class', 'export default class implements { c = 1 }\n', 'export default class implements {'], | |
| 74 | ])('keeps a class named implements: %s', (_label, code, header) => { | |
| 75 | expect(processCode(code)).toContain(header) | |
| 76 | }) | |
| 77 | ||
| 78 | it('keeps the clause in a bundle', async () => { | |
| 79 | const cwd = await mkdtemp(join(tmpdir(), 'dtsx-anonymous-class-bundle-')) | |
| 80 | try { | |
| 81 | await mkdir(join(cwd, 'src')) | |
| 82 | await writeFile(join(cwd, 'src', 'index.ts'), 'class Base { b = 1 }\nexport default class extends Base { c = 2 }\nexport * from \'./other\'\n') | |
| 83 | await writeFile(join(cwd, 'src', 'other.ts'), 'export class AnonymousClass { real = \'yes\' }\nexport type Label = `AnonymousClass-${string}`\n') | |
| 84 | await generate({ cwd, root: 'src', outdir: 'dist', entrypoints: ['index.ts'], bundle: true, clean: false, keepComments: false }) | |
| 85 | expect(await readFile(join(cwd, 'dist', 'index.d.ts'), 'utf8')).toContain('class __dtsx_default_class__ extends Base {') | |
| 86 | await writeFile(join(cwd, 'dist', 'use.ts'), 'import C, { AnonymousClass, type Label } from \'./index\'\nconst n: number = new C().b + new C().c\nconst real: string = new AnonymousClass().real\nconst label: Label = \'AnonymousClass-ok\'\nexport {}\n') | |
| 87 | expect(runTsc(join(cwd, 'dist'), ['index.d.ts', 'use.ts']).output).toBe('') | |
| 88 | } | |
| 89 | finally { | |
| 90 | await rm(cwd, { recursive: true, force: true }) | |
| 91 | } | |
| 92 | }) | |
| 93 | }) | |