Skip to content

Commit

Permalink
fix(compiler-sfc): fix sfc template unref rewrite for class instantia…
Browse files Browse the repository at this point in the history
…tion

close #6483
close #6491
  • Loading branch information
yyx990803 committed Jan 4, 2024
1 parent fda5192 commit ae60a91
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ export function isInDestructureAssignment(
return false
}

export function isInNewExpression(parentStack: Node[]): boolean {
let i = parentStack.length
while (i--) {
const p = parentStack[i]
if (p.type === 'NewExpression') {
return true
} else if (p.type !== 'MemberExpression') {
break
}
}
return false
}

export function walkFunctionParams(
node: Function,
onIdent: (id: Identifier) => void,
Expand Down
10 changes: 8 additions & 2 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../ast'
import {
isInDestructureAssignment,
isInNewExpression,
isStaticProperty,
isStaticPropertyKey,
walkIdentifiers,
Expand Down Expand Up @@ -131,6 +132,11 @@ export function processExpression(
// ({ x } = y)
const isDestructureAssignment =
parent && isInDestructureAssignment(parent, parentStack)
const isNewExpression = parent && isInNewExpression(parentStack)
const wrapWithUnref = (raw: string) => {
const wrapped = `${context.helperString(UNREF)}(${raw})`
return isNewExpression ? `(${wrapped})` : wrapped
}

if (
isConst(type) ||
Expand All @@ -147,7 +153,7 @@ export function processExpression(
// that assumes the value to be a ref for more efficiency
return isAssignmentLVal || isUpdateArg || isDestructureAssignment
? `${raw}.value`
: `${context.helperString(UNREF)}(${raw})`
: wrapWithUnref(raw)
} else if (type === BindingTypes.SETUP_LET) {
if (isAssignmentLVal) {
// let binding.
Expand Down Expand Up @@ -190,7 +196,7 @@ export function processExpression(
// for now
return raw
} else {
return `${context.helperString(UNREF)}(${raw})`
return wrapWithUnref(raw)
}
} else if (type === BindingTypes.PROPS) {
// use __props which is generated by compileScript so in ts mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,26 @@ return (_ctx, _cache) => {
}"
`;
exports[`SFC compile <script setup> > inlineTemplate mode > unref + new expression 1`] = `
"import { unref as _unref, toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
import Foo from './foo'
export default {
setup(__props) {
return (_ctx, _cache) => {
return (_openBlock(), _createElementBlock(_Fragment, null, [
_createElementVNode("div", null, _toDisplayString(new (_unref(Foo))()), 1 /* TEXT */),
_createElementVNode("div", null, _toDisplayString(new (_unref(Foo)).Bar()), 1 /* TEXT */)
], 64 /* STABLE_FRAGMENT */))
}
}
}"
`;
exports[`SFC compile <script setup> > inlineTemplate mode > v-model codegen 1`] = `
"import { vModelText as _vModelText, createElementVNode as _createElementVNode, withDirectives as _withDirectives, unref as _unref, isRef as _isRef, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
Expand Down
18 changes: 18 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,24 @@ describe('SFC compile <script setup>', () => {
),
).not.toThrowError()
})

test('unref + new expression', () => {
const { content } = compile(
`
<script setup>
import Foo from './foo'
</script>
<template>
<div>{{ new Foo() }}</div>
<div>{{ new Foo.Bar() }}</div>
</template>
`,
{ inlineTemplate: true },
)
expect(content).toMatch(`new (_unref(Foo))()`)
expect(content).toMatch(`new (_unref(Foo)).Bar()`)
assertCode(content)
})
})

describe('with TypeScript', () => {
Expand Down

0 comments on commit ae60a91

Please sign in to comment.