Skip to content

Commit

Permalink
fix: patch for volar with pnp mode
Browse files Browse the repository at this point in the history
fix #385
  • Loading branch information
qmhc committed Sep 30, 2024
1 parent d5cd854 commit 3bc780a
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ function transformAlias(
return importer
}

const vlsRE = /^_?__VLS_/

function isVLSNode(node: ts.Node) {
if (ts.isVariableStatement(node)) {
return node.declarationList.declarations.some(
d => ts.isIdentifier(d.name) && vlsRE.test(`${d.name.escapedText}`)
)
}

if (ts.isTypeAliasDeclaration(node)) {
return vlsRE.test(`${node.name.escapedText}`)
}

if (ts.isFunctionDeclaration(node)) {
return !!node.name && vlsRE.test(`${node.name.escapedText}`)
}

return false
}

export function transformCode(options: {
filePath: string,
content: string,
Expand Down Expand Up @@ -214,12 +234,16 @@ export function transformCode(options: {
return false
}

if (ts.isModuleDeclaration(node)) {
if (ts.isModuleDeclaration(node) && node.body && ts.isModuleBlock(node.body)) {
if (
ts.isIdentifier(node.name) &&
node.name.escapedText === 'global' &&
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 &&
ts.isModuleBlock(node.body) &&
!node.body.statements.some(
s => ts.isExportAssignment(s) || ts.isExportDeclaration(s) || ts.isImportDeclaration(s)
)
Expand Down

0 comments on commit 3bc780a

Please sign in to comment.