Skip to content

Commit c8b0171

Browse files
committed
fix(babel): prevent slot errors by excluding conditional expressions
1 parent 6fd7f73 commit c8b0171

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

.changeset/chilly-dots-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vue-jsx-vapor/babel': patch
3+
---
4+
5+
prevent slot errors by excluding conditional expressions

packages/babel/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import SyntaxJSX from '@babel/plugin-syntax-jsx'
33
import { parse } from '@babel/parser'
44
import { isJSXElement, transformJSX } from './transform'
5-
import type { VisitNodeFunction } from '@babel/traverse'
5+
import type { NodePath, VisitNodeFunction } from '@babel/traverse'
66
import type { JSXElement, JSXFragment, Node } from '@babel/types'
77
import type { CompilerOptions } from '@vue-jsx-vapor/compiler'
88
import type { Visitor } from '@babel/core'
@@ -41,7 +41,8 @@ export default (): {
4141
> = (path) => {
4242
if (
4343
(path.parent?.type !== 'JSXExpressionContainer' &&
44-
!isJSXElement(path.parent)) ||
44+
!isJSXElement(path.parent) &&
45+
!isConditionalExpression(path.parentPath)) ||
4546
path.parentPath.parent?.type === 'JSXAttribute'
4647
) {
4748
state.rootCodes.push(path.getSource())
@@ -96,3 +97,14 @@ export default (): {
9697
},
9798
}
9899
}
100+
101+
export function isConditionalExpression(path: NodePath<Node> | null): boolean {
102+
return !!(
103+
path &&
104+
(path?.type === 'LogicalExpression' ||
105+
path.type === 'ConditionalExpression') &&
106+
(path.parent.type === 'JSXExpressionContainer' ||
107+
(path.parent.type === 'ConditionalExpression' &&
108+
isConditionalExpression(path.parentPath)))
109+
)
110+
}

playground/vue-macros.config.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)