Skip to content

Commit ba34837

Browse files
fix: corrects faulty endblock detection
1 parent e007766 commit ba34837

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,16 @@ function injectProviderComponent(
107107
const template = getRootBlock(root, 'template')
108108
if (!template) {
109109
console.warn(
110-
`To <template> block found in ${id}. Skipping FormKitLazyProvider injection.`,
110+
`No <template> block found in ${id}. Skipping FormKitLazyProvider injection.`,
111111
)
112112
return { code, map: null }
113113
}
114114
const startInsertAt = template.children[0].loc.start.offset
115+
const endInsertAt =
116+
template.children[template.children.length - 1].loc.end.offset
115117
const before = code.substring(0, startInsertAt)
116-
const content = code.substring(startInsertAt, template.loc.end.offset - 11)
117-
const after = code.substring(template.loc.end.offset - 11)
118+
const content = code.substring(startInsertAt, endInsertAt)
119+
const after = code.substring(endInsertAt)
118120
code = `${before}\n${open}\n${content}\n${close}\n${after}`
119121
return { code, map: null }
120122
}

test/__snapshots__/index.test.ts.snap

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { FormKit } from '@formkit/vue'
1414
label=\\"Your name\\"
1515
help=\\"Enter your name\\"
1616
/>
17-
1817
</FormKitLazyProvider>
18+
1919
</template>"
2020
`;
2121

@@ -48,9 +48,38 @@ function handleLoginSubmit(values: any) {
4848
<FormKit type=\\"form\\" submit-label=\\"login\\" @submit=\\"handleLoginSubmit\\">
4949
<FormKit type=\\"email\\" label=\\"Email\\" name=\\"email\\" />
5050
<FormKit type=\\"password\\" label=\\"Password\\" name=\\"password\\" />
51-
</FormKi
51+
</FormKit>
5252
</FormKitLazyProvider>
53-
t>
53+
54+
</div>
55+
</template>
56+
"
57+
`;
58+
59+
exports[`index > injects inside root node with multiple child elements 1`] = `
60+
"<script lang=\\"ts\\" setup>
61+
import { FormKitLazyProvider } from '@formkit/vue'
62+
function handleLoginSubmit(values: any) {
63+
window.alert(\\"You are logged in. Credentials:
64+
\\" + JSON.stringify(values));
65+
}
66+
</script>
67+
68+
<template>
69+
<div>
70+
71+
<FormKitLazyProvider>
72+
<main>
73+
<p>
74+
<FormKit type=\\"form\\" submit-label=\\"login\\" @submit=\\"handleLoginSubmit\\">
75+
<FormKit type=\\"email\\" label=\\"Email\\" name=\\"email\\" />
76+
<FormKit type=\\"password\\" label=\\"Password\\" name=\\"password\\" />
77+
</FormKit>
78+
</p>
79+
</main>
80+
<div class=\\"filler\\">Here we go</div>
81+
</FormKitLazyProvider>
82+
5483
</div>
5584
</template>
5685
"
@@ -73,9 +102,9 @@ export default {
73102
74103
<FormKitLazyProvider>
75104
<h1>Nothing to see here</h1>
76-
<FormKit type=\\"text\\" label=\\"Check me out\\"
105+
<FormKit type=\\"text\\" label=\\"Check me out\\" />
77106
</FormKitLazyProvider>
78-
/>
107+
79108
</div>
80109
</template>"
81110
`;
@@ -86,7 +115,7 @@ exports[`index > injects the template block into an normally structured sfc 1`]
86115
87116
<FormKitLazyProvider>
88117
<FormKit />
89-
90118
</FormKitLazyProvider>
119+
91120
</template>"
92121
`;

test/index.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ function handleLoginSubmit(values: any) {
6767
).toMatchSnapshot()
6868
})
6969

70+
it('injects inside root node with multiple child elements', async () => {
71+
expect(
72+
(
73+
await plugin.transform(
74+
`<script lang="ts" setup>
75+
function handleLoginSubmit(values: any) {
76+
window.alert("You are logged in. Credentials: \n" + JSON.stringify(values));
77+
}
78+
</script>
79+
80+
<template>
81+
<div>
82+
<main>
83+
<p>
84+
<FormKit type="form" submit-label="login" @submit="handleLoginSubmit">
85+
<FormKit type="email" label="Email" name="email" />
86+
<FormKit type="password" label="Password" name="password" />
87+
</FormKit>
88+
</p>
89+
</main>
90+
<div class="filler">Here we go</div>
91+
</div>
92+
</template>
93+
`,
94+
'test.vue',
95+
)
96+
).code,
97+
).toMatchSnapshot()
98+
})
99+
70100
it('injects import into script setup block', async () => {
71101
expect(
72102
(await plugin.transform(aboutSFCFile, 'about.vue')).code,

0 commit comments

Comments
 (0)