Skip to content

Commit 83d9535

Browse files
authored
fix(compiler-sfc): check template ref usage, (#12985)
close #12984
1 parent 24fcf69 commit 83d9535

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

packages/compiler-sfc/src/compileScript.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,8 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor, isTS: boolean) {
18181818
if (value) {
18191819
code += `,${processExp(value, isTS, baseName)}`
18201820
}
1821+
} else if (name === 'ref') {
1822+
code += `,${value}`
18211823
}
18221824
}
18231825
},

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

+14
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,20 @@ return { vMyDir }
364364
})"
365365
`;
366366
367+
exports[`SFC compile <script setup> > dev mode import usage check > imported ref as template ref 1`] = `
368+
"import { defineComponent as _defineComponent } from 'vue'
369+
import { aref } from './x'
370+
371+
export default /*#__PURE__*/_defineComponent({
372+
setup(__props) {
373+
374+
375+
return { aref }
376+
}
377+
378+
})"
379+
`;
380+
367381
exports[`SFC compile <script setup> > dev mode import usage check > js template string interpolations 1`] = `
368382
"import { defineComponent as _defineComponent } from 'vue'
369383
import { VAR, VAR2, VAR3 } from './x'

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

+13
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,19 @@ defineExpose({ foo: 123 })
398398
assertCode(content)
399399
})
400400

401+
test('imported ref as template ref', () => {
402+
const { content } = compile(`
403+
<script setup lang="ts">
404+
import { aref } from './x'
405+
</script>
406+
<template>
407+
<div ref="aref"></div>
408+
</template>
409+
`)
410+
expect(content).toMatch(`return { aref }`)
411+
assertCode(content)
412+
})
413+
401414
test('vue interpolations', () => {
402415
const { content } = compile(`
403416
<script setup lang="ts">

0 commit comments

Comments
 (0)