Skip to content

Commit

Permalink
perf(compiler-sfc): infer ref binding type for more built-in methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 25, 2023
1 parent 433a58c commit a370e80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1520,4 +1520,19 @@ describe('SFC genDefaultAs', () => {
)
assertCode(content)
})

test('binding type for edge cases', () => {
const { bindings } = compile(
`<script setup lang="ts">
import { toRef } from 'vue'
const props = defineProps<{foo: string}>()
const foo = toRef(() => props.foo)
</script>`
)
expect(bindings).toStrictEqual({
toRef: BindingTypes.SETUP_CONST,
props: BindingTypes.SETUP_REACTIVE_CONST,
foo: BindingTypes.SETUP_REF
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ const props = defineProps({ foo: String })
</script>
`)
expect(bindings).toStrictEqual({
bar: BindingTypes.SETUP_MAYBE_REF,
bar: BindingTypes.SETUP_REF,
computed: BindingTypes.SETUP_CONST
})
})
Expand Down
12 changes: 10 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,16 @@ function walkDeclaration(
: BindingTypes.SETUP_CONST
} else if (isConst) {
if (
isCallOf(init, userImportAliases['ref']) ||
isCallOf(init, DEFINE_MODEL)
isCallOf(
init,
m =>
m === userImportAliases['ref'] ||
m === userImportAliases['computed'] ||
m === userImportAliases['shallowRef'] ||
m === userImportAliases['customRef'] ||
m === userImportAliases['toRef'] ||
m === DEFINE_MODEL
)
) {
bindingType = BindingTypes.SETUP_REF
} else {
Expand Down

0 comments on commit a370e80

Please sign in to comment.