From 41167d5853dbae36f93a466bc242bd9199709eca Mon Sep 17 00:00:00 2001 From: qmhc Date: Thu, 27 Feb 2025 16:44:26 +0800 Subject: [PATCH] fix: should process alias for module declaration fix #409 --- examples/ts/src/index.ts | 7 +++++++ src/transform.ts | 27 ++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/examples/ts/src/index.ts b/examples/ts/src/index.ts index a9fa206..b890063 100644 --- a/examples/ts/src/index.ts +++ b/examples/ts/src/index.ts @@ -25,3 +25,10 @@ export type { AliasType } from '@alias/type' export type * from './namespace' export type * from './modules' + +declare module '@/test' { + interface TestBase { + name: string, + mail?: string + } +} diff --git a/src/transform.ts b/src/transform.ts index b1e007e..854bf9a 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -53,7 +53,7 @@ function transformAlias( aliasesExclude: (string | RegExp)[] ) { if ( - aliases.length && + aliases?.length && !aliasesExclude.some(e => (isRegExp(e) ? e.test(importer) : String(e) === importer)) ) { const matchedAlias = aliases.find(alias => isAliasMatch(alias, importer)) @@ -245,14 +245,23 @@ export function transformCode(options: { node.body.statements.some(isVLSNode) ) { s.remove(node.pos, node.end) - } else if ( - node.modifiers?.[0] && - node.modifiers[0].kind === ts.SyntaxKind.DeclareKeyword && - !node.body.statements.some( - s => ts.isExportAssignment(s) || ts.isExportDeclaration(s) || ts.isImportDeclaration(s) - ) - ) { - declareModules.push(s.slice(node.pos, node.end + 1)) + } else if (ts.isStringLiteral(node.name)) { + const libName = toLibName(node.name.text) + + if (libName !== node.name.text) { + s.update(node.name.pos, node.name.end, ` '${libName}'`) + } + + if ( + !libName.startsWith('.') && + node.modifiers?.[0] && + node.modifiers[0].kind === ts.SyntaxKind.DeclareKeyword && + !node.body.statements.some( + s => ts.isExportAssignment(s) || ts.isExportDeclaration(s) || ts.isImportDeclaration(s) + ) + ) { + declareModules.push(s.slice(node.pos, node.end + 1)) + } } return false