Skip to content

Commit 5566fc0

Browse files
authored
Merge branch 'main' into fix-jsx-dom-augmentation
2 parents 241d222 + ff60b93 commit 5566fc0

File tree

10 files changed

+81
-18
lines changed

10 files changed

+81
-18
lines changed

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '../../src'
1414
import { transformIf } from '../../src/transforms/vIf'
1515
import { transformExpression } from '../../src/transforms/transformExpression'
16+
import { PatchFlagNames, PatchFlags } from '../../../shared/src'
1617

1718
function parseWithExpressionTransform(
1819
template: string,
@@ -494,7 +495,9 @@ describe('compiler: expression transform', () => {
494495
setup: BindingTypes.SETUP_MAYBE_REF,
495496
setupConst: BindingTypes.SETUP_CONST,
496497
data: BindingTypes.DATA,
497-
options: BindingTypes.OPTIONS
498+
options: BindingTypes.OPTIONS,
499+
reactive: BindingTypes.SETUP_REACTIVE_CONST,
500+
literal: BindingTypes.LITERAL_CONST
498501
}
499502

500503
function compileWithBindingMetadata(
@@ -532,5 +535,25 @@ describe('compiler: expression transform', () => {
532535
expect(code).toMatch(`_ctx.options`)
533536
expect(code).toMatchSnapshot()
534537
})
538+
539+
test('literal const handling', () => {
540+
const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`, {
541+
inline: true
542+
})
543+
// #7973 should skip patch for literal const
544+
expect(code).not.toMatch(
545+
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
546+
)
547+
})
548+
549+
test('reactive const handling', () => {
550+
const { code } = compileWithBindingMetadata(`<div>{{ reactive }}</div>`, {
551+
inline: true
552+
})
553+
// #7973 should not skip patch for reactive const
554+
expect(code).toMatch(
555+
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
556+
)
557+
})
535558
})
536559
})

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ export function processExpression(
128128
const isDestructureAssignment =
129129
parent && isInDestructureAssignment(parent, parentStack)
130130

131-
if (isConst(type) || localVars[raw]) {
131+
if (
132+
isConst(type) ||
133+
type === BindingTypes.SETUP_REACTIVE_CONST ||
134+
localVars[raw]
135+
) {
132136
return raw
133137
} else if (type === BindingTypes.SETUP_REF) {
134138
return `${raw}.value`
@@ -371,8 +375,6 @@ export function stringifyExpression(exp: ExpressionNode | string): string {
371375

372376
function isConst(type: unknown) {
373377
return (
374-
type === BindingTypes.SETUP_CONST ||
375-
type === BindingTypes.LITERAL_CONST ||
376-
type === BindingTypes.SETUP_REACTIVE_CONST
378+
type === BindingTypes.SETUP_CONST || type === BindingTypes.LITERAL_CONST
377379
)
378380
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,9 @@ export default /*#__PURE__*/_defineComponent({
17151715
foo: { type: [Function, null], required: true },
17161716
unknown: { type: null, required: true },
17171717
unknownUnion: { type: null, required: true },
1718-
unknownIntersection: { type: Object, required: true }
1718+
unknownIntersection: { type: Object, required: true },
1719+
unknownUnionWithBoolean: { type: Boolean, required: true, skipCheck: true },
1720+
unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }
17191721
},
17201722
setup(__props: any, { expose: __expose }) {
17211723
__expose();

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ const emit = defineEmits(['a', 'b'])
10421042
unknown: UnknownType
10431043
unknownUnion: UnknownType | string
10441044
unknownIntersection: UnknownType & Object
1045+
unknownUnionWithBoolean: UnknownType | boolean
1046+
unknownUnionWithFunction: UnknownType | (() => any)
10451047
}>()
10461048
</script>`)
10471049
assertCode(content)
@@ -1093,7 +1095,13 @@ const emit = defineEmits(['a', 'b'])
10931095
expect(content).toMatch(`unknownUnion: { type: null, required: true }`)
10941096
// intersection containing unknown type: narrow to the known types
10951097
expect(content).toMatch(
1096-
`unknownIntersection: { type: Object, required: true }`
1098+
`unknownIntersection: { type: Object, required: true },`
1099+
)
1100+
expect(content).toMatch(
1101+
`unknownUnionWithBoolean: { type: Boolean, required: true, skipCheck: true },`
1102+
)
1103+
expect(content).toMatch(
1104+
`unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }`
10971105
)
10981106
expect(bindings).toStrictEqual({
10991107
string: BindingTypes.PROPS,
@@ -1131,7 +1139,9 @@ const emit = defineEmits(['a', 'b'])
11311139
nonNull: BindingTypes.PROPS,
11321140
unknown: BindingTypes.PROPS,
11331141
unknownUnion: BindingTypes.PROPS,
1134-
unknownIntersection: BindingTypes.PROPS
1142+
unknownIntersection: BindingTypes.PROPS,
1143+
unknownUnionWithBoolean: BindingTypes.PROPS,
1144+
unknownUnionWithFunction: BindingTypes.PROPS
11351145
})
11361146
})
11371147

packages/compiler-sfc/src/compileScript.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,11 @@ export function compileScript(
888888
}
889889
}
890890
891-
const { type, required } = props[key]
891+
const { type, required, skipCheck } = props[key]
892892
if (!isProd) {
893893
return `${key}: { type: ${toRuntimeTypeString(
894894
type
895-
)}, required: ${required}${
895+
)}, required: ${required}${skipCheck ? ', skipCheck: true' : ''}${
896896
defaultString ? `, ${defaultString}` : ``
897897
} }`
898898
} else if (
@@ -1964,6 +1964,7 @@ interface PropTypeData {
19641964
key: string
19651965
type: string[]
19661966
required: boolean
1967+
skipCheck: boolean
19671968
}
19681969

19691970
function recordType(node: Node, declaredTypes: Record<string, string[]>) {
@@ -1993,19 +1994,26 @@ function extractRuntimeProps(
19931994
m.key.type === 'Identifier'
19941995
) {
19951996
let type: string[] | undefined
1997+
let skipCheck = false
19961998
if (m.type === 'TSMethodSignature') {
19971999
type = ['Function']
19982000
} else if (m.typeAnnotation) {
19992001
type = inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
20002002
// skip check for result containing unknown types
20012003
if (type.includes(UNKNOWN_TYPE)) {
2002-
type = [`null`]
2004+
if (type.includes('Boolean') || type.includes('Function')) {
2005+
type = type.filter(t => t !== UNKNOWN_TYPE)
2006+
skipCheck = true
2007+
} else {
2008+
type = ['null']
2009+
}
20032010
}
20042011
}
20052012
props[m.key.name] = {
20062013
key: m.key.name,
20072014
required: !m.optional,
2008-
type: type || [`null`]
2015+
type: type || [`null`],
2016+
skipCheck
20092017
}
20102018
}
20112019
}

packages/runtime-core/__tests__/componentProps.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ describe('component props', () => {
335335
arr: { type: Array },
336336
obj: { type: Object },
337337
cls: { type: MyClass },
338-
fn: { type: Function }
338+
fn: { type: Function },
339+
skipCheck: { type: [Boolean, Function], skipCheck: true }
339340
},
340341
setup() {
341342
return () => null
@@ -349,7 +350,8 @@ describe('component props', () => {
349350
arr: {},
350351
obj: 'false',
351352
cls: {},
352-
fn: true
353+
fn: true,
354+
skipCheck: 'foo'
353355
}),
354356
nodeOps.createElement('div')
355357
)
@@ -374,6 +376,9 @@ describe('component props', () => {
374376
expect(
375377
`Invalid prop: type check failed for prop "cls". Expected MyClass, got Object`
376378
).toHaveBeenWarned()
379+
expect(
380+
`Invalid prop: type check failed for prop "skipCheck". Expected Boolean | Function, got String with value "foo".`
381+
).not.toHaveBeenWarned()
377382
})
378383

379384
// #3495

packages/runtime-core/src/componentProps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface PropOptions<T = any, D = T> {
5858
required?: boolean
5959
default?: D | DefaultFactory<D> | null | undefined | object
6060
validator?(value: unknown): boolean
61+
skipCheck?: boolean
6162
}
6263

6364
export type PropType<T> = PropConstructor<T> | PropConstructor<T>[]
@@ -608,7 +609,7 @@ function validateProp(
608609
prop: PropOptions,
609610
isAbsent: boolean
610611
) {
611-
const { type, required, validator } = prop
612+
const { type, required, validator, skipCheck } = prop
612613
// required!
613614
if (required && isAbsent) {
614615
warn('Missing required prop: "' + name + '"')
@@ -619,7 +620,7 @@ function validateProp(
619620
return
620621
}
621622
// type check
622-
if (type != null && type !== true) {
623+
if (type != null && type !== true && !skipCheck) {
623624
let isValid = false
624625
const types = isArray(type) ? type : [type]
625626
const expectedTypes = []

packages/vue/jsx-runtime/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const { h, Fragment } = require('vue')
22

3-
function jsx(type, { children, ...props }) {
3+
function jsx(type, props, key) {
4+
const { children } = props
5+
delete props.children
6+
if (arguments.length > 2) {
7+
props.key = key
8+
}
49
return h(type, props, children)
510
}
611

packages/vue/jsx-runtime/index.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { h, Fragment } from 'vue'
22

3-
function jsx(type, { children, ...props }) {
3+
function jsx(type, props, key) {
4+
const { children } = props
5+
delete props.children
6+
if (arguments.length > 2) {
7+
props.key = key
8+
}
49
return h(type, props, children)
510
}
611

packages/vue/types/jsx-register.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
// imports the global JSX namespace registration for compat.
33
// TODO: remove in 3.4
44
import '../jsx'
5+
6+
export * from '../jsx-runtime/dom'

0 commit comments

Comments
 (0)