Skip to content

Commit 3602d4d

Browse files
authored
fix(vue-jsx): handle type asserted export default defineComponent (#615)
1 parent e34ce4c commit 3602d4d

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

packages/plugin-vue-jsx/src/index.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,17 @@ function vueJsxPlugin(options: Options = {}): Plugin {
158158
enter(
159159
_path: babel.NodePath<types.ExportDefaultDeclaration>,
160160
) {
161+
const unwrappedDeclaration = unwrapTypeAssertion(
162+
_path.node.declaration,
163+
)
161164
if (
162165
isDefineComponentCall(
163-
_path.node.declaration,
166+
unwrappedDeclaration,
164167
defineComponentName,
165168
)
166169
) {
167-
const declaration = _path.node
168-
.declaration as types.CallExpression
170+
const declaration =
171+
unwrappedDeclaration as types.CallExpression
169172
const nodesPath = _path.replaceWithMultiple([
170173
// const __default__ = defineComponent(...)
171174
types.variableDeclaration('const', [
@@ -276,7 +279,10 @@ function vueJsxPlugin(options: Options = {}): Plugin {
276279
})
277280
}
278281
} else if (
279-
isDefineComponentCall(node.declaration, defineComponentName)
282+
isDefineComponentCall(
283+
unwrapTypeAssertion(node.declaration),
284+
defineComponentName,
285+
)
280286
) {
281287
hotComponents.push({
282288
local: '__default__',
@@ -337,7 +343,7 @@ function parseComponentDecls(
337343
for (const decl of node.declarations) {
338344
if (
339345
decl.id.type === 'Identifier' &&
340-
isDefineComponentCall(decl.init, fnNames)
346+
isDefineComponentCall(unwrapTypeAssertion(decl.init), fnNames)
341347
) {
342348
names.push(decl.id.name)
343349
}
@@ -357,6 +363,25 @@ function isDefineComponentCall(
357363
)
358364
}
359365

366+
function unwrapTypeAssertion(node: types.Node): types.Node
367+
function unwrapTypeAssertion(
368+
node: types.Node | null | undefined,
369+
): types.Node | null | undefined
370+
function unwrapTypeAssertion(
371+
node: types.Node | null | undefined,
372+
): types.Node | null | undefined {
373+
if (!node) return node
374+
let current = node
375+
while (
376+
current.type === 'TSAsExpression' ||
377+
current.type === 'TSSatisfiesExpression' ||
378+
current.type === 'TSTypeAssertion'
379+
) {
380+
current = current.expression
381+
}
382+
return current
383+
}
384+
360385
function getHash(text: string) {
361386
return crypto.hash('sha256', text, 'hex').substring(0, 8)
362387
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { type DefineComponent, defineComponent } from 'vue'
2+
3+
export default defineComponent({
4+
render() {
5+
return (
6+
<span class="export-default-as">export default defineComponent as</span>
7+
)
8+
},
9+
}) as DefineComponent<any>

playground/vue-jsx/__tests__/vue-jsx.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ test('should render', async () => {
1414
expect(await page.textContent('.export-default')).toMatch(
1515
'export default defineComponent',
1616
)
17+
expect(await page.textContent('.export-default-as')).toMatch(
18+
'export default defineComponent as',
19+
)
1720
})
1821

1922
test('should update', async () => {

playground/vue-jsx/main.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import JsxSetupSyntax from './setup-syntax-jsx.vue'
99
import JsxWithQuery from './Query.jsx?query=true'
1010
import TsImport from './TsImport.vue'
1111
import ExportDefault from './ExportDefault'
12+
import ExportDefaultAs from './ExportDefaultAs'
1213

1314
function App() {
1415
return (
@@ -24,6 +25,7 @@ function App() {
2425
<JsxWithQuery />
2526
<TsImport />
2627
<ExportDefault />
28+
<ExportDefaultAs />
2729
</>
2830
)
2931
}

0 commit comments

Comments
 (0)