Skip to content

Commit 9557529

Browse files
authored
fix(compiler-sfc): skip empty defineOptions and support TypeScript type assertions (#8028)
1 parent 91a931a commit 9557529

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+14-1
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,25 @@ exports[`SFC compile <script setup> > defineOptions() > basic usage 1`] = `
658658
setup(__props, { expose: __expose }) {
659659
__expose();
660660

661+
662+
663+
return { }
664+
}
665+
666+
})"
667+
`;
661668

669+
exports[`SFC compile <script setup> > defineOptions() > empty argument 1`] = `
670+
"export default {
671+
setup(__props, { expose: __expose }) {
672+
__expose();
662673

674+
675+
663676
return { }
664677
}
665678

666-
})"
679+
}"
667680
`;
668681

669682
exports[`SFC compile <script setup> > defineProps w/ external definition 1`] = `

packages/compiler-sfc/__tests__/compileScript.spec.ts

+49-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const myEmit = defineEmits(['foo', 'bar'])
133133
expect(bindings).toStrictEqual({
134134
myEmit: BindingTypes.SETUP_CONST
135135
})
136-
// should remove defineOptions import and call
136+
// should remove defineEmits import and call
137137
expect(content).not.toMatch('defineEmits')
138138
// should generate correct setup signature
139139
expect(content).toMatch(
@@ -205,10 +205,10 @@ const myEmit = defineEmits(['foo', 'bar'])
205205
describe('defineOptions()', () => {
206206
test('basic usage', () => {
207207
const { content } = compile(`
208-
<script setup>
209-
defineOptions({ name: 'FooApp' })
210-
</script>
211-
`)
208+
<script setup>
209+
defineOptions({ name: 'FooApp' })
210+
</script>
211+
`)
212212
assertCode(content)
213213
// should remove defineOptions import and call
214214
expect(content).not.toMatch('defineOptions')
@@ -218,6 +218,18 @@ defineOptions({ name: 'FooApp' })
218218
)
219219
})
220220

221+
test('empty argument', () => {
222+
const { content } = compile(`
223+
<script setup>
224+
defineOptions()
225+
</script>
226+
`)
227+
assertCode(content)
228+
expect(content).toMatch(`export default {`)
229+
// should remove defineOptions import and call
230+
expect(content).not.toMatch('defineOptions')
231+
})
232+
221233
it('should emit an error with two defineProps', () => {
222234
expect(() =>
223235
compile(`
@@ -249,6 +261,26 @@ defineOptions({ name: 'FooApp' })
249261
).toThrowError(
250262
'[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead.'
251263
)
264+
265+
expect(() =>
266+
compile(`
267+
<script setup>
268+
defineOptions({ expose: ['foo'] })
269+
</script>
270+
`)
271+
).toThrowError(
272+
'[@vue/compiler-sfc] defineOptions() cannot be used to declare expose. Use defineExpose() instead.'
273+
)
274+
275+
expect(() =>
276+
compile(`
277+
<script setup>
278+
defineOptions({ slots: ['foo'] })
279+
</script>
280+
`)
281+
).toThrowError(
282+
'[@vue/compiler-sfc] defineOptions() cannot be used to declare slots. Use defineSlots() instead.'
283+
)
252284
})
253285

254286
it('should emit an error with type generic', () => {
@@ -262,6 +294,18 @@ defineOptions({ name: 'FooApp' })
262294
'[@vue/compiler-sfc] defineOptions() cannot accept type arguments'
263295
)
264296
})
297+
298+
it('should emit an error with type assertion', () => {
299+
expect(() =>
300+
compile(`
301+
<script setup lang="ts">
302+
defineOptions({ props: [] } as any)
303+
</script>
304+
`)
305+
).toThrowError(
306+
'[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.'
307+
)
308+
})
265309
})
266310

267311
test('defineExpose()', () => {

packages/compiler-sfc/src/compileScript.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,10 @@ export function compileScript(
702702
if (node.typeParameters) {
703703
error(`${DEFINE_OPTIONS}() cannot accept type arguments`, node)
704704
}
705+
if (!node.arguments[0]) return true
705706

706707
hasDefineOptionsCall = true
707-
optionsRuntimeDecl = node.arguments[0]
708+
optionsRuntimeDecl = unwrapTSNode(node.arguments[0])
708709

709710
let propsOption = undefined
710711
let emitsOption = undefined

0 commit comments

Comments
 (0)