Skip to content

Commit 64160e8

Browse files
committed
wip: ignore non-ref const mutation cases in codegen
1 parent 8567feb commit 64160e8

File tree

5 files changed

+51
-61
lines changed

5 files changed

+51
-61
lines changed

packages/compiler-core/src/codegen.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export interface CodegenResult {
6666
}
6767

6868
export interface CodegenContext
69-
extends Omit<Required<CodegenOptions>, 'bindingMetadata' | 'inline'> {
69+
extends Omit<
70+
Required<CodegenOptions>,
71+
'bindingMetadata' | 'inline' | 'isTS'
72+
> {
7073
source: string
7174
code: string
7275
line: number
@@ -214,29 +217,33 @@ export function generate(
214217
genFunctionPreamble(ast, preambleContext)
215218
}
216219

217-
// binding optimizations
218-
const optimizeSources =
219-
options.bindingMetadata && !options.inline
220-
? `, $props, $setup, $data, $options`
221-
: ``
220+
const args = ssr ? ['_ctx', '_push', '_parent', '_attrs'] : ['_ctx', '_cache']
221+
if (!__BROWSER__ && options.bindingMetadata && !options.inline) {
222+
// binding optimization args
223+
args.push('$props', '$setup', '$data', '$options')
224+
}
225+
const signature =
226+
!__BROWSER__ && options.isTS
227+
? args.map(arg => `${arg}: any`).join(',')
228+
: args.join(',')
222229
// enter render function
223230
if (!ssr) {
224231
if (isSetupInlined) {
225232
if (genScopeId) {
226233
push(`${PURE_ANNOTATION}_withId(`)
227234
}
228-
push(`(_ctx, _cache${optimizeSources}) => {`)
235+
push(`(${signature}) => {`)
229236
} else {
230237
if (genScopeId) {
231238
push(`const render = ${PURE_ANNOTATION}_withId(`)
232239
}
233-
push(`function render(_ctx, _cache${optimizeSources}) {`)
240+
push(`function render(${signature}) {`)
234241
}
235242
} else {
236243
if (genScopeId) {
237244
push(`const ssrRender = ${PURE_ANNOTATION}_withId(`)
238245
}
239-
push(`function ssrRender(_ctx, _push, _parent, _attrs${optimizeSources}) {`)
246+
push(`function ssrRender(${signature}) {`)
240247
}
241248
indent()
242249

packages/compiler-core/src/options.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ interface SharedTransformCodegenOptions {
124124
* This allows the function to directly access setup() local bindings.
125125
*/
126126
inline?: boolean
127+
/**
128+
* Indicates that transforms and codegen should try to output valid TS code
129+
*/
130+
isTS?: boolean
127131
}
128132

129133
export interface TransformOptions extends SharedTransformCodegenOptions {
@@ -195,10 +199,6 @@ export interface TransformOptions extends SharedTransformCodegenOptions {
195199
* needed to render inline CSS variables on component root
196200
*/
197201
ssrCssVars?: string
198-
/**
199-
* Indicates that transforms should try to output valid TS code
200-
*/
201-
isTS?: boolean
202202
onError?: (error: CompilerError) => void
203203
}
204204

packages/compiler-core/src/transforms/transformExpression.ts

+27-43
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,32 @@ export function processExpression(
118118
// setup inline mode
119119
if (type === BindingTypes.SETUP_CONST) {
120120
return raw
121-
} else if (type === BindingTypes.SETUP_REF) {
121+
} else if (
122+
type === BindingTypes.SETUP_REF ||
123+
type === BindingTypes.SETUP_MAYBE_REF
124+
) {
125+
// const binding that may or may not be ref
126+
// if it's not a ref, then assignments don't make sense -
127+
// so we ignore the non-ref assignment case and generate code
128+
// that assumes the value to be a ref for more efficiency
122129
return isAssignmentLVal || isUpdateArg
123130
? `${raw}.value`
124131
: `${context.helperString(UNREF)}(${raw})`
125-
} else if (
126-
type === BindingTypes.SETUP_MAYBE_REF ||
127-
type === BindingTypes.SETUP_LET
128-
) {
132+
} else if (type === BindingTypes.SETUP_LET) {
129133
if (isAssignmentLVal) {
130-
if (type === BindingTypes.SETUP_MAYBE_REF) {
131-
// const binding that may or may not be ref
132-
// if it's not a ref, then the assignment doesn't make sense so
133-
// just no-op it
134-
// x = y ---> !isRef(x) ? null : x.value = y
135-
return `!${context.helperString(
136-
IS_REF
137-
)}(${raw}) ? null : ${raw}.value`
138-
} else {
139-
// let binding.
140-
// this is a bit more tricky as we need to cover the case where
141-
// let is a local non-ref value, and we need to replicate the
142-
// right hand side value.
143-
// x = y --> isRef(x) ? x.value = y : x = y
144-
const rVal = (parent as AssignmentExpression).right
145-
const rExp = rawExp.slice(rVal.start! - 1, rVal.end! - 1)
146-
const rExpString = stringifyExpression(
147-
processExpression(createSimpleExpression(rExp, false), context)
148-
)
149-
return `${context.helperString(IS_REF)}(${raw})${
150-
context.isTS ? ` //@ts-ignore\n` : ``
151-
} ? ${raw}.value = ${rExpString} : ${raw}`
152-
}
134+
// let binding.
135+
// this is a bit more tricky as we need to cover the case where
136+
// let is a local non-ref value, and we need to replicate the
137+
// right hand side value.
138+
// x = y --> isRef(x) ? x.value = y : x = y
139+
const rVal = (parent as AssignmentExpression).right
140+
const rExp = rawExp.slice(rVal.start! - 1, rVal.end! - 1)
141+
const rExpString = stringifyExpression(
142+
processExpression(createSimpleExpression(rExp, false), context)
143+
)
144+
return `${context.helperString(IS_REF)}(${raw})${
145+
context.isTS ? ` //@ts-ignore\n` : ``
146+
} ? ${raw}.value = ${rExpString} : ${raw}`
153147
} else if (isUpdateArg) {
154148
// make id replace parent in the code range so the raw update operator
155149
// is removed
@@ -158,21 +152,11 @@ export function processExpression(
158152
const { prefix: isPrefix, operator } = parent as UpdateExpression
159153
const prefix = isPrefix ? operator : ``
160154
const postfix = isPrefix ? `` : operator
161-
if (type === BindingTypes.SETUP_MAYBE_REF) {
162-
// const binding that may or may not be ref
163-
// if it's not a ref, then the assignment doesn't make sense so
164-
// just no-op it
165-
// x++ ---> !isRef(x) ? null : x.value++
166-
return `!${context.helperString(
167-
IS_REF
168-
)}(${raw}) ? null : ${prefix}${raw}.value${postfix}`
169-
} else {
170-
// let binding.
171-
// x++ --> isRef(a) ? a.value++ : a++
172-
return `${context.helperString(IS_REF)}(${raw})${
173-
context.isTS ? ` //@ts-ignore\n` : ``
174-
} ? ${prefix}${raw}.value${postfix} : ${prefix}${raw}${postfix}`
175-
}
155+
// let binding.
156+
// x++ --> isRef(a) ? a.value++ : a++
157+
return `${context.helperString(IS_REF)}(${raw})${
158+
context.isTS ? ` //@ts-ignore\n` : ``
159+
} ? ${prefix}${raw}.value${postfix} : ${prefix}${raw}${postfix}`
176160
} else {
177161
return `${context.helperString(UNREF)}(${raw})`
178162
}

packages/compiler-core/src/transforms/vOn.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ export const transformOn: DirectiveTransform = (
124124
exp = createCompoundExpression([
125125
`${
126126
isInlineStatement
127-
? `$event`
127+
? !__BROWSER__ && context.isTS
128+
? `($event: any)`
129+
: `$event`
128130
: `${
129131
!__BROWSER__ && context.isTS ? `\n//@ts-ignore\n` : ``
130132
}(...args)`

packages/runtime-dom/src/helpers/useCssVars.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
ComponentPublicInstance,
32
getCurrentInstance,
43
onMounted,
54
warn,
@@ -14,9 +13,7 @@ import { ShapeFlags } from '@vue/shared'
1413
* Runtime helper for SFC's CSS variable injection feature.
1514
* @private
1615
*/
17-
export function useCssVars(
18-
getter: (ctx: ComponentPublicInstance) => Record<string, string>
19-
) {
16+
export function useCssVars(getter: (ctx: any) => Record<string, string>) {
2017
const instance = getCurrentInstance()
2118
/* istanbul ignore next */
2219
if (!instance) {

0 commit comments

Comments
 (0)