Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: imports and exports follows type-only #333

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/transform/ExportsFixer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ts from "typescript";
import type { TypeHint } from "./TypeOnlyFixer.js";

type NamedExport = {
localName: string;
Expand All @@ -15,7 +16,10 @@ type ExportDeclaration = {

export class ExportsFixer {
private readonly DEBUG = !!process.env.DTS_EXPORTS_FIXER_DEBUG;
constructor(private readonly source: ts.SourceFile) {}
constructor(
private readonly source: ts.SourceFile,
private readonly typeOnlyHints: Map<string, TypeHint[]>
) {}

public fix(): string {
const exports = this.findExports();
Expand All @@ -25,13 +29,17 @@ export class ExportsFixer {

private findExports(): Array<ExportDeclaration> {
const { rawExports, values, types } = this.getExportsAndLocals();
const typeOnlyNames = [...this.typeOnlyHints.values()]
.flatMap((hints) => hints.map((hint) => hint.originalName))

return rawExports.map((rawExport) => {
const elements = rawExport.elements.map((e) => {
const exportedName = e.name.text;
const localName = e.propertyName?.text ?? e.name.text;
const kind =
types.some((node) => node.getText() === localName) && !values.some((node) => node.getText() === localName)
const kind =
(types.some((node) => node.getText() === localName) && !values.some((node) => node.getText() === localName))
|| e.isTypeOnly
|| typeOnlyNames.includes(localName)
? ("type" as const)
: ("value" as const);
this.DEBUG && console.log(`export ${localName} as ${exportedName} is a ${kind}`);
Expand Down
334 changes: 334 additions & 0 deletions src/transform/TypeOnlyFixer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
import MagicString from "magic-string";
import ts from "typescript";

export interface TypeHint {
isTypeOnly: boolean
isTypeOnlyImport: boolean
isTypeOnlyNamedImport: boolean
isTypeOnlyExport: boolean
isTypeOnlyNamedReExport: boolean
isTypeOnlyNamespaceReExport: boolean
hintName: string
originalName: string
used?: boolean
}

interface TypeHintStatement {
statement: ts.TypeAliasDeclaration
alias: string
reference: string
aliasHint: Omit<TypeHint, 'originalName'>
referenceHint: Omit<TypeHint, 'originalName'>
}

interface TypeHintElement {
sourceName: string
importName: string
hint?: TypeHint
}

export class TypeOnlyFixer {
private code: MagicString

constructor(private readonly source: ts.SourceFile) {
this.code = new MagicString(this.source.getFullText());
}

fix() {
const hints = this.findTypeOnlyHints()
if(hints.size) {
this.fixTypeOnlyImports(hints)
this.fixTypeOnlyExports(hints)
}
const code = this.code.toString()
return {
code: code,
typeOnlyHints: hints
}
}

private fixTypeOnlyImports(hints: Map<string, TypeHint[]>) {
for (const statement of this.source.statements) {
if (!ts.isImportDeclaration(statement) || !statement.importClause) {
continue;
}

// Restore type-only imports.
if(statement.importClause.name) {
const hint = hints.get(statement.importClause.name.text)?.[0];

// import A$type_only_import from 'a'
// ↓
// import type A from 'a';
if(hint?.isTypeOnlyImport) {
this.code.overwrite(
statement.importClause.getStart(),
statement.importClause.getEnd(),
`type ${hint.originalName}`,
);
continue;
}
}

// Restore type-only namespace imports/re-exports.
if (
statement.importClause.namedBindings
&& ts.isNamespaceImport(statement.importClause.namedBindings)
) {
const hint = hints.get(statement.importClause.namedBindings.name.text)?.[0];

// import * as A$type_only_namespace_import from 'a'
// ↓
// import type * as A from 'a'
if(hint?.isTypeOnlyImport) {
this.code.overwrite(
statement.importClause.getStart(),
statement.importClause.getEnd(),
`type * as ${hint.originalName}`,
)
continue;
}

// import * as A$type_only_namespace_re_export from 'a'
// ↓
// export type * as A from 'a'
if(hint?.isTypeOnlyNamespaceReExport) {
const specifier = statement.moduleSpecifier.getText()
hint.used = true
hints.get(hint.hintName)!.forEach(hint => hint.used = true)
this.code.overwrite(
statement.getStart(),
statement.getEnd(),
`export type * as ${hint.originalName} from ${specifier};`,
);
continue;
}
}

// Restore type-only named imports/re-exports.
if (
statement.importClause.namedBindings
&& ts.isNamedImports(statement.importClause.namedBindings)
) {
const elements: TypeHintElement[] = [];

for (const element of statement.importClause.namedBindings.elements) {
const importName = element.name.text;
const sourceName = element.propertyName?.text || importName;
const elementHints = hints.get(importName);

if(!elementHints) {
elements.push({ sourceName, importName });
} else {
elements.push(...elementHints.map(hint => ({
sourceName,
importName: hint.originalName || importName,
hint,
})))
}
}

const isNamedReExport = elements.some((element) => element.hint?.isTypeOnlyNamedReExport);
const hasTypeOnly = elements.some((element) => element.hint?.isTypeOnly);
const isTypeOnly = elements.length > 0 && elements.every((element) => element.hint?.isTypeOnly)

const namedBindings = elements
.map((element) => this.createNamedBindings(element, isTypeOnly))
.join(', ');
const typeModifier = isTypeOnly ? 'type ' : '';

if(isNamedReExport) {
const specifier = statement.moduleSpecifier.getText();
elements.forEach((element) => {
if(element.hint) {
element.hint.used = true;
hints.get(element.hint.hintName)!.forEach(hint => hint.used = true);
}
})
// import { A as uniqName } from 'a'
// type A$type_only_named_re_export = uniqName
// ↓
// export type { A } from 'a'
this.code.overwrite(
statement.getStart(),
statement.getEnd(),
`export ${typeModifier}{ ${namedBindings} } from ${specifier};`,
);
} else if(hasTypeOnly) {
// import { A as uniqName } from 'a'
// type A$type_only_named_import = uniqName
// ↓
// import type { A } from 'a'
this.code.overwrite(
statement.importClause.getStart(),
statement.importClause.getEnd(),
`${typeModifier}{ ${namedBindings} }`,
)
}
}
}
}

private fixTypeOnlyExports(hints: Map<string, TypeHint[]>) {
for (const statement of this.source.statements) {
if (
!ts.isExportDeclaration(statement)
|| !statement.exportClause
|| !ts.isNamedExports(statement.exportClause)
) {
continue;
}

let removedCount = 0;
for(const element of statement.exportClause.elements) {
const hint = element.propertyName?.text
? hints.get(element.propertyName.text)?.[0]
: null;
if(
(hint?.isTypeOnlyNamedReExport || hint?.isTypeOnlyNamespaceReExport)
&& hint.used
) {
// Remove re-exported type hints.
// export { A$type_only_named_re_export as A }
// export { A$type_only_namespace_re_export as A }
// ↓
// export { }
this.code.remove(element.getStart(), element.getEnd());
removedCount++;
continue;
}

if(hint?.isTypeOnly && element.propertyName) {
// Restore type-only named exports.
// export { A$type_only_export as A }
// ↓
// export { type A }
this.code.overwrite(
element.propertyName.getStart(),
element.propertyName.getEnd(),
`type ${hint.originalName}`,
);
}
}

if(removedCount && removedCount === statement.exportClause.elements.length) {
// Remove the entire export statement if all elements are re-exported types.
this.code.remove(statement.getStart(), statement.getEnd());
}
}
}

private findTypeOnlyHints(): Map<string, TypeHint[]> {
const hintStatements: TypeHintStatement[] = [];

for (const statement of this.source.statements) {
if (
ts.isTypeAliasDeclaration(statement) &&
ts.isTypeReferenceNode(statement.type) &&
ts.isIdentifier(statement.type.typeName)
) {
const alias = statement.name.text;
const reference = statement.type.typeName.text;
const aliasHint = parseTypeOnlyName(alias);
const referenceHint = parseTypeOnlyName(reference);

if (aliasHint.isTypeOnly || referenceHint.isTypeOnly) {
hintStatements.push({ statement, alias, reference, aliasHint, referenceHint });
}
}
}

const hints = new Map<string, TypeHint[]>();

for (const { statement, alias, aliasHint, reference, referenceHint } of hintStatements) {
if(referenceHint.isTypeOnlyImport) {
pushHint(reference, { ...referenceHint, originalName: alias });
}
if(aliasHint.isTypeOnlyNamedImport) {
const originalName = hintStatements.find(({ reference }) => reference === alias)!.alias;
pushHint(reference, { ...aliasHint, originalName });
}
if(aliasHint.isTypeOnlyExport) {
pushHint(alias, { ...aliasHint, originalName: reference });
}
if(aliasHint.isTypeOnlyNamedReExport || aliasHint.isTypeOnlyNamespaceReExport) {
const originalName = aliasHint.isTypeOnlyNamedReExport
? alias.split(TYPE_ONLY_NAMED_RE_EXPORT)[0]!
: alias.split(TYPE_ONLY_NAMESPACE_RE_EXPORT)[0]!
pushHint(reference, { ...aliasHint, originalName});
pushHint(alias, { ...aliasHint, originalName});
}
if(aliasHint.isTypeOnly || referenceHint.isTypeOnly) {
this.code.remove(statement.getStart(), statement.getEnd());
}
}

return hints

function pushHint(name: string, hint: TypeHint) {
const _hints = hints.get(name)
_hints ? _hints.push(hint) : hints.set(name, [hint])
}
}

private createNamedBindings(element: TypeHintElement, isTypeOnly: boolean) {
const typeModifier = !isTypeOnly
// && (element.hint?.isTypeOnlyImport || element.hint?.isTypeOnlyNamedReExport)
&& element.hint?.isTypeOnly
? 'type '
: ''
return element.sourceName === element.importName
? `${typeModifier}${element.importName}`
: `${typeModifier}${element.sourceName} as ${element.importName}`
}
}

const UNIQ_IMPORT = '$UNIQ_IMPORT'
const TYPE_ONLY_IMPORT = '$TYPE_ONLY_IMPORT'
const TYPE_ONLY_NAMED_IMPORT = '$TYPE_ONLY_NAMED_IMPORT'
const TYPE_ONLY_EXPORT = '$TYPE_ONLY_EXPORT'
const TYPE_ONLY_NAMED_RE_EXPORT = '$TYPE_ONLY_NAMED_RE_EXPORT'
const TYPE_ONLY_NAMESPACE_RE_EXPORT = '$TYPE_ONLY_NAMESPACE_RE_EXPORT'
let typeHintIds = 0

export function createUniqImportTypeName() {
return `${UNIQ_IMPORT}_${typeHintIds++}`
}
export function createTypeOnlyImportName(name: string) {
return `${name}${TYPE_ONLY_IMPORT}_${typeHintIds++}`
}
export function createTypeOnlyNamedImportName(name: string) {
return `${name}${TYPE_ONLY_NAMED_IMPORT}_${typeHintIds++}`
}
export function createTypeOnlyExportName(name: string) {
return `${name}${TYPE_ONLY_EXPORT}_${typeHintIds++}`
}
export function createTypeOnlyNamedReExportName(name: string) {
return `${name}${TYPE_ONLY_NAMED_RE_EXPORT}_${typeHintIds++}`
}
export function createTypeOnlyNamespaceReExportName(name: string) {
return `${name}${TYPE_ONLY_NAMESPACE_RE_EXPORT}_${typeHintIds++}`
}
export function parseTypeOnlyName(name: string): Omit<TypeHint, 'originalName'> {
const isTypeOnlyImport = name.includes(TYPE_ONLY_IMPORT)
const isTypeOnlyNamedImport = name.includes(TYPE_ONLY_NAMED_IMPORT)
const isTypeOnlyExport = name.includes(TYPE_ONLY_EXPORT)
const isTypeOnlyNamedReExport = name.includes(TYPE_ONLY_NAMED_RE_EXPORT)
const isTypeOnlyNamespaceReExport = name.includes(TYPE_ONLY_NAMESPACE_RE_EXPORT)

const isTypeOnly = isTypeOnlyImport
|| isTypeOnlyNamedImport
|| isTypeOnlyExport
|| isTypeOnlyNamedReExport
|| isTypeOnlyNamespaceReExport

return {
isTypeOnly,
isTypeOnlyImport,
isTypeOnlyNamedImport,
isTypeOnlyExport,
isTypeOnlyNamedReExport,
isTypeOnlyNamespaceReExport,
hintName: name,
}
}
6 changes: 5 additions & 1 deletion src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NamespaceFixer } from "./NamespaceFixer.js";
import { preProcess } from "./preprocess.js";
import { convert } from "./Transformer.js";
import { ExportsFixer } from "./ExportsFixer.js";
import { TypeOnlyFixer } from "./TypeOnlyFixer.js";

function parse(fileName: string, code: string): ts.SourceFile {
return ts.createSourceFile(fileName, code, ts.ScriptTarget.Latest, true);
Expand Down Expand Up @@ -129,7 +130,10 @@ export const transform = () => {
code += "\nexport { }";
}

const exportsFixer = new ExportsFixer(parse(chunk.fileName, code));
const typeOnlyFixer = new TypeOnlyFixer(parse(chunk.fileName, code));
const { code: typeOnlyFixedCode, typeOnlyHints } = typeOnlyFixer.fix();

const exportsFixer = new ExportsFixer(parse(chunk.fileName, typeOnlyFixedCode), typeOnlyHints);

return { code: exportsFixer.fix(), map: { mappings: "" } };
},
Expand Down
Loading