Skip to content

Commit 560af55

Browse files
committed
fix: omit generic script block attribute
1 parent 1d06eca commit 560af55

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/mkdist.ts

+13-24
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,18 @@ function defineDefaultBlockLoader(options: DefaultBlockLoaderOptions): VueBlockL
167167
return
168168
}
169169

170-
const lang
171-
= typeof block.attrs.lang === 'string'
172-
? block.attrs.lang
173-
: options.defaultLang
170+
const lang = typeof block.attrs.lang === 'string' ? block.attrs.lang : options.defaultLang
174171
const extension = `.${lang}`
175172

176-
const files
177-
= (await loadFile({
178-
getContents: () => block.content,
179-
path: `${rawInput.path}${extension}`,
180-
srcPath: `${rawInput.srcPath}${extension}`,
181-
extension,
182-
})) || []
183-
184-
const blockOutputFile = files.find(
185-
f =>
186-
f.extension === `.${options.defaultLang}`
187-
|| options.validExtensions?.includes(f.extension as string),
173+
const files = await loadFile({
174+
getContents: () => block.content,
175+
path: `${rawInput.path}${extension}`,
176+
srcPath: `${rawInput.srcPath}${extension}`,
177+
extension,
178+
}) || []
179+
180+
const blockOutputFile = files.find(f =>
181+
f.extension === `.${options.defaultLang}` || options.validExtensions?.includes(f.extension as string),
188182
)
189183
if (!blockOutputFile) {
190184
return
@@ -193,7 +187,7 @@ function defineDefaultBlockLoader(options: DefaultBlockLoaderOptions): VueBlockL
193187

194188
return {
195189
type: block.type,
196-
attrs: toOmit(block.attrs, 'lang'),
190+
attrs: toOmit(block.attrs, ['lang', 'generic']),
197191
content: blockOutputFile.contents!,
198192
}
199193
}
@@ -259,11 +253,6 @@ export const vueLoader = defineVueLoader({
259253
function cleanupBreakLine(str: string): string {
260254
return str.replaceAll(/(\n\n)\n+/g, '\n\n').replace(/^\s*\n|\n\s*$/g, '')
261255
}
262-
function toOmit<R extends Record<keyof object, unknown>, K extends keyof R>(
263-
record: R,
264-
toRemove: K,
265-
): Omit<R, K> {
266-
return Object.fromEntries(
267-
Object.entries(record).filter(([key]) => key !== toRemove),
268-
) as Omit<R, K>
256+
function toOmit<R extends Record<keyof object, unknown>, K extends keyof R>(record: R, toRemove: K[]): Omit<R, K> {
257+
return Object.fromEntries(Object.entries(record).filter(([key]) => !toRemove.includes(key as K))) as Omit<R, K>
269258
}

test/setup.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ describe('transform typescript script setup', () => {
6262
`)
6363
})
6464

65+
it('strips generic from script setup blocks', async () => {
66+
expect(
67+
await fixture(
68+
`
69+
<script setup lang="ts" generic="T extends Messages = Messages">
70+
interface AppProps<T extends string = string> {
71+
locale?: Array<T>
72+
}
73+
const props = defineProps<AppProps<T>>()
74+
</script>`,
75+
),
76+
).toMatchInlineSnapshot(`
77+
"<script setup>
78+
79+
interface AppProps<T extends string = string> {
80+
locale?: Array<T>
81+
}
82+
const props = defineProps({
83+
locale: {
84+
type: Array,
85+
required: false
86+
}
87+
})
88+
89+
</script>"
90+
`)
91+
})
92+
6593
it('withDefaults', async () => {
6694
expect(
6795
await fixture(

0 commit comments

Comments
 (0)