Skip to content

Commit

Permalink
refactor(jsx-directive): use null instead of empty string (#441)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
  • Loading branch information
zhiyuanzmj and sxzz committed Aug 3, 2023
1 parent 4710c81 commit 9867173
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-eyes-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@vue-macros/jsx-directive": patch
"@vue-macros/volar": patch
---

refactor(jsx-directive): use null instead of empty string.
4 changes: 2 additions & 2 deletions packages/jsx-directive/src/core/v-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export function vForTransform(ast: Program, s: MagicString, offset = 0) {
const hasScope = ['JSXElement', 'JSXFragment'].includes(`${parent?.type}`)
s.appendLeft(
node.start! + offset,
`${hasScope ? ' { ' : ''}${list}.map(${i}=> `
`${hasScope ? '{' : ''}${list}.map(${i} => `
)

s.appendRight(node.end! + offset, `)${hasScope ? ' }' : ''}`)
s.appendRight(node.end! + offset, `)${hasScope ? '}' : ''}`)
s.remove(attribute.start! + offset - 1, attribute.end! + offset)
}
})
Expand Down
4 changes: 2 additions & 2 deletions packages/jsx-directive/src/core/v-if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function vIfTransform(ast: Program, s: MagicString, offset = 0) {
if (attribute.value)
s.appendLeft(
node.start! + offset,
`${attribute.name.name === 'v-if' && hasScope ? '{ ' : ''}${s.slice(
`${attribute.name.name === 'v-if' && hasScope ? '{' : ''}${s.slice(
attribute.value.start! + offset + 1,
attribute.value.end! + offset - 1
)} ? `
Expand All @@ -57,7 +57,7 @@ export function vIfTransform(ast: Program, s: MagicString, offset = 0) {
node.end! + offset,
`${nodes[index + 1]?.attribute.name.name}`.startsWith('v-else')
? ' :'
: ` : ''${hasScope ? ' }' : ''}`
: ` : null${hasScope ? '}' : ''}`
)
s.remove(attribute.start! + offset - 1, attribute.end! + offset)
} else if (attribute.name.name === 'v-else') {
Expand Down
8 changes: 4 additions & 4 deletions packages/jsx-directive/tests/__snapshots__/v-for.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const list = [1, 2, 3]
export default () => (
<>
{ list.map((i, index)=> <div key={index}>
{list.map((i, index) => <div key={index}>
<div>{i}</div>
</div>) }
</div>)}
</>
)
"
Expand All @@ -20,9 +20,9 @@ const list = [1, 2, 3]
defineRender(() => (
<>
{ list.map(i=> <div key={i}>
{list.map(i => <div key={i}>
<div>{i}</div>
</div>) }
</div>)}
</>
))
</script>
Expand Down
32 changes: 16 additions & 16 deletions packages/jsx-directive/tests/__snapshots__/v-if.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports[`jsx-vue-directive > v-if > ./fixtures/v-if/v-else.tsx 1`] = `
export default () => (
<>
{ foo === 0 ? <div>0</div> :
{foo === 0 ? <div>0</div> :
foo === 1 ? <div>1</div> :
<div v-else={foo === 2}>2</div> }
</>
Expand All @@ -23,7 +23,7 @@ const { foo = 2 } = defineProps<{
defineRender(() => (
<>
{ foo === 0 ? <div>0</div> :
{foo === 0 ? <div>0</div> :
foo === 1 ? <div>1</div> :
<div v-else={foo === 2}>2</div> }
</>
Expand All @@ -39,9 +39,9 @@ exports[`jsx-vue-directive > v-if > ./fixtures/v-if/v-else-if.tsx 1`] = `
export default () => (
<>
{ foo === 0 ? <div>0</div> :
{foo === 0 ? <div>0</div> :
foo === 1 ? <div>1</div> :
foo === 2 ? <div>2</div> : '' }
foo === 2 ? <div>2</div> : null}
</>
)
"
Expand All @@ -55,8 +55,8 @@ const { foo = 0 } = defineProps<{
defineRender(() => (
<>
{ foo === 0 ? <div>0</div> :
foo === 1 ? <div>1</div> : '' }
{foo === 0 ? <div>0</div> :
foo === 1 ? <div>1</div> : null}
</>
))
</script>
Expand All @@ -71,8 +71,8 @@ exports[`jsx-vue-directive > v-if > ./fixtures/v-if/v-if.tsx 1`] = `
export default () => {
return (
<>
{ foo === 0 ? <div>0</div> : '' }
{ foo === 1 ? <div>1</div> : '' }
{foo === 0 ? <div>0</div> : null}
{foo === 1 ? <div>1</div> : null}
</>
)
}
Expand All @@ -87,8 +87,8 @@ const { foo } = defineProps<{
defineRender(() => (
<>
{ foo === 0 ? <div>0</div> : '' }
{ foo === 1 ? <div>1</div> : '' }
{foo === 0 ? <div>0</div> : null}
{foo === 1 ? <div>1</div> : null}
</>
))
</script>
Expand All @@ -102,11 +102,11 @@ exports[`jsx-vue-directive > v-if > ./fixtures/v-if/v-if-nested.tsx 1`] = `
export default () => (
<>
{ foo === 0 ? <div>
{ foo === 0 ? <div>0-0</div> :
{foo === 0 ? <div>
{foo === 0 ? <div>0-0</div> :
foo === 1 ? <div>0-1</div> :
<div v-else>0-2</div> }
</div> : '' }
</div> : null}
</>
)
"
Expand All @@ -120,11 +120,11 @@ const { foo = 0 } = defineProps<{
defineRender(() => (
<>
{ foo === 0 ? <div>
{ foo === 0 ? <div>0-0</div> :
{foo === 0 ? <div>
{foo === 0 ? <div>0-0</div> :
foo === 1 ? <div>0-1</div> :
<div v-else>0-2</div> }
</div> : '' }
</div> : null}
</>
))
</script>
Expand Down
14 changes: 4 additions & 10 deletions packages/volar/src/jsx-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function transformJsxDirective({
],
'.map(',
[itemText, source, attribute.pos + 8, FileRangeCapabilities.full],
'=>'
' => '
)

replaceSourceRange(
Expand Down Expand Up @@ -130,7 +130,7 @@ function transformJsxDirective({
attribute.end - expressionText.length - 1,
FileRangeCapabilities.full,
],
'?'
' ? '
)

const nextNode = nodes
Expand All @@ -155,17 +155,11 @@ function transformJsxDirective({
source,
node.end,
node.end,
`${nextNodeHasElse ? ':' : `:''${parent ? '}' : ''}`}`
nextNodeHasElse ? ' : ' : ` : null${parent ? '}' : ''}`
)
replaceSourceRange(codes, source, attribute.pos, attribute.end)
} else if (attribute.name.escapedText === 'v-else') {
replaceSourceRange(
codes,
source,
node.end,
node.end,
`${parent ? `}` : ''}`
)
replaceSourceRange(codes, source, node.end, node.end, parent ? '}' : '')
replaceSourceRange(codes, source, attribute.pos, attribute.end)
}
}
Expand Down

0 comments on commit 9867173

Please sign in to comment.