Skip to content

Commit a9582e6

Browse files
fix: root node injection w/ attrs
1 parent 68393af commit a9582e6

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,11 @@ function injectProviderComponent(
109109
)
110110
return { code, map: null }
111111
}
112-
const before = code.substring(0, template.loc.start.offset + 10)
113-
const content = code.substring(
114-
template.loc.start.offset + 10,
115-
template.loc.end.offset - 11,
116-
)
112+
const startInsertAt = template.children[0].loc.start.offset
113+
const before = code.substring(0, startInsertAt)
114+
const content = code.substring(startInsertAt, template.loc.end.offset - 11)
117115
const after = code.substring(template.loc.end.offset - 11)
118-
code = `${before}\n${open}${content}${close}\n${after}`
116+
code = `${before}\n${open}\n${content}\n${close}\n${after}`
119117

120118
return { code, map: null }
121119
}

test/__snapshots__/index.test.ts.snap

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ import { FormKit } from '@formkit/vue'
77
</script>
88
99
<template>
10+
1011
<FormKitLazyProvider>
11-
<FormKit
12+
<FormKit
1213
type=\\"text\\"
1314
label=\\"Your name\\"
1415
help=\\"Enter your name\\"
1516
/>
17+
1618
</FormKitLazyProvider>
1719
</template>"
1820
`;
1921

2022
exports[`index > injects inside root node if there is one 1`] = `
2123
"<script setup>import { FormKitLazyProvider } from '@formkit/vue'</script>
2224
<template>
23-
<div>
24-
25-
<FormKitLazyProvider> <FormKit /></FormKitLazyProvider>
25+
<div class=\\"fizzbuzz\\">
26+
27+
<FormKitLazyProvider>
28+
<FormKit />
29+
</FormKitLazyProvider>
2630
2731
</div>
2832
</template>"
@@ -43,8 +47,10 @@ export default {
4347
<template>
4448
<div>
4549
46-
<FormKitLazyProvider><h1>Nothing to see here</h1>
47-
<FormKit type=\\"text\\" label=\\"Check me out\\" </FormKitLazyProvider>
50+
<FormKitLazyProvider>
51+
<h1>Nothing to see here</h1>
52+
<FormKit type=\\"text\\" label=\\"Check me out\\"
53+
</FormKitLazyProvider>
4854
/>
4955
</div>
5056
</template>"
@@ -53,8 +59,10 @@ export default {
5359
exports[`index > injects the template block into an normally structured sfc 1`] = `
5460
"<script setup>import { FormKitLazyProvider } from '@formkit/vue'</script>
5561
<template>
62+
5663
<FormKitLazyProvider>
57-
<FormKit />
64+
<FormKit />
65+
5866
</FormKitLazyProvider>
5967
</template>"
6068
`;

test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('index', () => {
3232
(
3333
await plugin.transform(
3434
`<template>
35-
<div>
35+
<div class="fizzbuzz">
3636
<FormKit />
3737
</div>
3838
</template>`,

0 commit comments

Comments
 (0)