Skip to content

Commit

Permalink
fix(compiler-sfc): skip empty defineOptions and support TypeScript …
Browse files Browse the repository at this point in the history
…type assertions (#8028)
  • Loading branch information
sxzz authored Apr 5, 2023
1 parent 91a931a commit 9557529
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,25 @@ exports[`SFC compile <script setup> > defineOptions() > basic usage 1`] = `
setup(__props, { expose: __expose }) {
__expose();



return { }
}

})"
`;

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



return { }
}

})"
}"
`;

exports[`SFC compile <script setup> > defineProps w/ external definition 1`] = `
Expand Down
54 changes: 49 additions & 5 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const myEmit = defineEmits(['foo', 'bar'])
expect(bindings).toStrictEqual({
myEmit: BindingTypes.SETUP_CONST
})
// should remove defineOptions import and call
// should remove defineEmits import and call
expect(content).not.toMatch('defineEmits')
// should generate correct setup signature
expect(content).toMatch(
Expand Down Expand Up @@ -205,10 +205,10 @@ const myEmit = defineEmits(['foo', 'bar'])
describe('defineOptions()', () => {
test('basic usage', () => {
const { content } = compile(`
<script setup>
defineOptions({ name: 'FooApp' })
</script>
`)
<script setup>
defineOptions({ name: 'FooApp' })
</script>
`)
assertCode(content)
// should remove defineOptions import and call
expect(content).not.toMatch('defineOptions')
Expand All @@ -218,6 +218,18 @@ defineOptions({ name: 'FooApp' })
)
})

test('empty argument', () => {
const { content } = compile(`
<script setup>
defineOptions()
</script>
`)
assertCode(content)
expect(content).toMatch(`export default {`)
// should remove defineOptions import and call
expect(content).not.toMatch('defineOptions')
})

it('should emit an error with two defineProps', () => {
expect(() =>
compile(`
Expand Down Expand Up @@ -249,6 +261,26 @@ defineOptions({ name: 'FooApp' })
).toThrowError(
'[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead.'
)

expect(() =>
compile(`
<script setup>
defineOptions({ expose: ['foo'] })
</script>
`)
).toThrowError(
'[@vue/compiler-sfc] defineOptions() cannot be used to declare expose. Use defineExpose() instead.'
)

expect(() =>
compile(`
<script setup>
defineOptions({ slots: ['foo'] })
</script>
`)
).toThrowError(
'[@vue/compiler-sfc] defineOptions() cannot be used to declare slots. Use defineSlots() instead.'
)
})

it('should emit an error with type generic', () => {
Expand All @@ -262,6 +294,18 @@ defineOptions({ name: 'FooApp' })
'[@vue/compiler-sfc] defineOptions() cannot accept type arguments'
)
})

it('should emit an error with type assertion', () => {
expect(() =>
compile(`
<script setup lang="ts">
defineOptions({ props: [] } as any)
</script>
`)
).toThrowError(
'[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.'
)
})
})

test('defineExpose()', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,10 @@ export function compileScript(
if (node.typeParameters) {
error(`${DEFINE_OPTIONS}() cannot accept type arguments`, node)
}
if (!node.arguments[0]) return true

hasDefineOptionsCall = true
optionsRuntimeDecl = node.arguments[0]
optionsRuntimeDecl = unwrapTSNode(node.arguments[0])

let propsOption = undefined
let emitsOption = undefined
Expand Down

0 comments on commit 9557529

Please sign in to comment.