Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx-directive): add v-slot directive (#463)
* feat: add v-slot directive * feat: add volar plugin * test: add * docs: add v-slot * chore: typo * docs: typo * chore: add changeset
- Loading branch information
1 parent
eaf04d5
commit 8ad8454
Showing
16 changed files
with
477 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@vue-macros/jsx-directive': minor | ||
'@vue-macros/volar': patch | ||
--- | ||
|
||
Add v-slot directive. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { type MagicString } from '@vue-macros/common' | ||
import { type JSXElement } from '@babel/types' | ||
|
||
export function transformVSlot( | ||
nodes: JSXElement[], | ||
s: MagicString, | ||
offset = 0, | ||
version: number | ||
) { | ||
nodes.reverse().forEach((node) => { | ||
const attribute = node.openingElement.attributes.find( | ||
(attribute) => | ||
attribute.type === 'JSXAttribute' && | ||
(attribute.name.type === 'JSXNamespacedName' | ||
? attribute.name.namespace | ||
: attribute.name | ||
).name === 'v-slot' | ||
) | ||
|
||
const slots = | ||
attribute?.type === 'JSXAttribute' | ||
? { | ||
[`${ | ||
attribute.name.type === 'JSXNamespacedName' | ||
? attribute.name.name.name | ||
: 'default' | ||
}`]: { | ||
isTemplateTag: false, | ||
expressionContainer: attribute.value, | ||
children: node.children, | ||
}, | ||
} | ||
: {} | ||
if (!attribute) { | ||
for (const child of node.children) { | ||
let name = 'default' | ||
let expressionContainer | ||
const isTemplateTag = | ||
child.type === 'JSXElement' && | ||
child.openingElement.name.type === 'JSXIdentifier' && | ||
child.openingElement.name.name === 'template' | ||
|
||
if (child.type === 'JSXElement') { | ||
for (const attr of child.openingElement.attributes) { | ||
if (attr.type !== 'JSXAttribute') continue | ||
if (isTemplateTag) { | ||
name = | ||
attr.name.type === 'JSXNamespacedName' | ||
? attr.name.name.name | ||
: 'default' | ||
} | ||
|
||
if ( | ||
(attr.name.type === 'JSXNamespacedName' | ||
? attr.name.namespace | ||
: attr.name | ||
).name === 'v-slot' | ||
) | ||
expressionContainer = attr.value | ||
} | ||
} | ||
|
||
slots[name] ??= { | ||
isTemplateTag, | ||
expressionContainer, | ||
children: [child], | ||
} | ||
if (!slots[name].isTemplateTag) { | ||
slots[name].expressionContainer = expressionContainer | ||
slots[name].isTemplateTag = isTemplateTag | ||
if (isTemplateTag) { | ||
slots[name].children = [child] | ||
} else { | ||
slots[name].children.push(child) | ||
} | ||
} | ||
} | ||
} | ||
|
||
const result = `${ | ||
version < 3 ? 'scopedSlots' : 'v-slots' | ||
}={{${Object.entries(slots) | ||
.map( | ||
([name, { expressionContainer, children }]) => | ||
`'${name}': (${ | ||
expressionContainer?.type === 'JSXExpressionContainer' | ||
? s.sliceNode(expressionContainer.expression, { offset }) | ||
: '' | ||
}) => ${version < 3 ? '<span>' : '<>'}${children | ||
.map((child) => { | ||
const result = s.sliceNode( | ||
child.type === 'JSXElement' && | ||
child.openingElement.name.type === 'JSXIdentifier' && | ||
child.openingElement.name.name === 'template' | ||
? child.children | ||
: child, | ||
{ offset } | ||
) | ||
s.removeNode(child, { offset }) | ||
return result | ||
}) | ||
.join('')}${version < 3 ? '</span>' : '</>'}` | ||
) | ||
.join(',')}}}` | ||
|
||
if (attribute) { | ||
s.overwriteNode(attribute, result, { offset }) | ||
} else { | ||
s.appendLeft(node.openingElement.end! + offset - 1, ` ${result}`) | ||
} | ||
}) | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/jsx-directive/tests/__snapshots__/v-slot.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`jsx-vue-directive > vue 2.7 v-slot > ./fixtures/v-slot/index.vue 1`] = ` | ||
"<script setup lang=\\"tsx\\"> | ||
import Child from './child.vue' | ||
defineRender(() => ( | ||
<div> | ||
<Child scopedSlots={{'bottom': ({ foo }) => <span> | ||
{foo} | ||
<Child scopedSlots={{'default': () => <span>default</span>}}></Child> | ||
</span>}}></Child> | ||
</div> | ||
)) | ||
</script> | ||
" | ||
`; | ||
|
||
exports[`jsx-vue-directive > vue 3 v-slot > ./fixtures/v-slot/index.vue 1`] = ` | ||
"<script setup lang=\\"tsx\\"> | ||
import Child from './child.vue' | ||
defineRender(() => ( | ||
<div> | ||
<Child v-slots={{'bottom': ({ foo }) => <> | ||
{foo} | ||
<Child v-slots={{'default': () => <>default</>}}></Child> | ||
</>}}></Child> | ||
</div> | ||
)) | ||
</script> | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script setup lang="tsx"> | ||
defineSlots<{ | ||
default: () => any | ||
bottom: (props: { foo: 1 }) => any | ||
}>() | ||
</script> | ||
|
||
<template> | ||
<span> | ||
<slot /> | ||
<slot name="bottom" v-bind="{ foo: 1 }" /> | ||
</span> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script setup lang="tsx"> | ||
import Child from './child.vue' | ||
defineRender(() => ( | ||
<div> | ||
<Child v-slot:bottom={{ foo }}> | ||
{foo} | ||
<Child v-slot>default</Child> | ||
</Child> | ||
</div> | ||
)) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe } from 'vitest' | ||
import { testFixtures } from '@vue-macros/test-utils' | ||
import { transformJsxDirective } from '../src/api' | ||
|
||
describe('jsx-vue-directive', () => { | ||
describe('vue 3 v-slot', async () => { | ||
await testFixtures( | ||
import.meta.glob('./fixtures/v-slot/index.{vue,jsx,tsx}', { | ||
eager: true, | ||
as: 'raw', | ||
}), | ||
(_, id, code) => transformJsxDirective(code, id, 3)?.code | ||
) | ||
}) | ||
|
||
describe('vue 2.7 v-slot', async () => { | ||
await testFixtures( | ||
import.meta.glob('./fixtures/v-slot/index.{vue,jsx,tsx}', { | ||
eager: true, | ||
as: 'raw', | ||
}), | ||
(_, id, code) => transformJsxDirective(code, id, 2.7)?.code | ||
) | ||
}) | ||
}) |
Oops, something went wrong.