Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): skip empty defineOptions and support TypeScript type assertions #8028

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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