Skip to content

Commit 41167d5

Browse files
committed
fix: should process alias for module declaration
fix #409
1 parent 0da89cc commit 41167d5

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

examples/ts/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ export type { AliasType } from '@alias/type'
2525

2626
export type * from './namespace'
2727
export type * from './modules'
28+
29+
declare module '@/test' {
30+
interface TestBase {
31+
name: string,
32+
mail?: string
33+
}
34+
}

src/transform.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function transformAlias(
5353
aliasesExclude: (string | RegExp)[]
5454
) {
5555
if (
56-
aliases.length &&
56+
aliases?.length &&
5757
!aliasesExclude.some(e => (isRegExp(e) ? e.test(importer) : String(e) === importer))
5858
) {
5959
const matchedAlias = aliases.find(alias => isAliasMatch(alias, importer))
@@ -245,14 +245,23 @@ export function transformCode(options: {
245245
node.body.statements.some(isVLSNode)
246246
) {
247247
s.remove(node.pos, node.end)
248-
} else if (
249-
node.modifiers?.[0] &&
250-
node.modifiers[0].kind === ts.SyntaxKind.DeclareKeyword &&
251-
!node.body.statements.some(
252-
s => ts.isExportAssignment(s) || ts.isExportDeclaration(s) || ts.isImportDeclaration(s)
253-
)
254-
) {
255-
declareModules.push(s.slice(node.pos, node.end + 1))
248+
} else if (ts.isStringLiteral(node.name)) {
249+
const libName = toLibName(node.name.text)
250+
251+
if (libName !== node.name.text) {
252+
s.update(node.name.pos, node.name.end, ` '${libName}'`)
253+
}
254+
255+
if (
256+
!libName.startsWith('.') &&
257+
node.modifiers?.[0] &&
258+
node.modifiers[0].kind === ts.SyntaxKind.DeclareKeyword &&
259+
!node.body.statements.some(
260+
s => ts.isExportAssignment(s) || ts.isExportDeclaration(s) || ts.isImportDeclaration(s)
261+
)
262+
) {
263+
declareModules.push(s.slice(node.pos, node.end + 1))
264+
}
256265
}
257266

258267
return false

0 commit comments

Comments
 (0)