Skip to content

Commit

Permalink
fix: root node injection w/ attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-schroeder committed Dec 30, 2023
1 parent 68393af commit a9582e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ function injectProviderComponent(
)
return { code, map: null }
}
const before = code.substring(0, template.loc.start.offset + 10)
const content = code.substring(
template.loc.start.offset + 10,
template.loc.end.offset - 11,
)
const startInsertAt = template.children[0].loc.start.offset
const before = code.substring(0, startInsertAt)
const content = code.substring(startInsertAt, template.loc.end.offset - 11)
const after = code.substring(template.loc.end.offset - 11)
code = `${before}\n${open}${content}${close}\n${after}`
code = `${before}\n${open}\n${content}\n${close}\n${after}`

return { code, map: null }
}
Expand Down
22 changes: 15 additions & 7 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ import { FormKit } from '@formkit/vue'
</script>
<template>
<FormKitLazyProvider>
<FormKit
<FormKit
type=\\"text\\"
label=\\"Your name\\"
help=\\"Enter your name\\"
/>
</FormKitLazyProvider>
</template>"
`;

exports[`index > injects inside root node if there is one 1`] = `
"<script setup>import { FormKitLazyProvider } from '@formkit/vue'</script>
<template>
<div>
<FormKitLazyProvider> <FormKit /></FormKitLazyProvider>
<div class=\\"fizzbuzz\\">
<FormKitLazyProvider>
<FormKit />
</FormKitLazyProvider>
</div>
</template>"
Expand All @@ -43,8 +47,10 @@ export default {
<template>
<div>
<FormKitLazyProvider><h1>Nothing to see here</h1>
<FormKit type=\\"text\\" label=\\"Check me out\\" </FormKitLazyProvider>
<FormKitLazyProvider>
<h1>Nothing to see here</h1>
<FormKit type=\\"text\\" label=\\"Check me out\\"
</FormKitLazyProvider>
/>
</div>
</template>"
Expand All @@ -53,8 +59,10 @@ export default {
exports[`index > injects the template block into an normally structured sfc 1`] = `
"<script setup>import { FormKitLazyProvider } from '@formkit/vue'</script>
<template>
<FormKitLazyProvider>
<FormKit />
<FormKit />
</FormKitLazyProvider>
</template>"
`;
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('index', () => {
(
await plugin.transform(
`<template>
<div>
<div class="fizzbuzz">
<FormKit />
</div>
</template>`,
Expand Down

0 comments on commit a9582e6

Please sign in to comment.