Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): fix warning for absent props #3363

Merged
merged 2 commits into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Expand Up @@ -301,6 +301,23 @@ describe('component props', () => {
}).not.toThrow(TypeError)
})

test('warn absent required props', () => {
const Comp = {
props: {
bool: { type: Boolean, required: true },
str: { type: String, required: true },
num: { type: Number, required: true }
},
setup() {
return () => null
}
}
render(h(Comp), nodeOps.createElement('div'))
expect(`Missing required prop: "bool"`).toHaveBeenWarned()
expect(`Missing required prop: "str"`).toHaveBeenWarned()
expect(`Missing required prop: "num"`).toHaveBeenWarned()
})

test('merging props from mixins and extends', () => {
let setupProps: any
let renderProxy: any
Expand Down
16 changes: 10 additions & 6 deletions packages/runtime-core/src/componentProps.ts
Expand Up @@ -140,7 +140,7 @@ export function initProps(
setFullProps(instance, rawProps, props, attrs)
// validation
if (__DEV__) {
validateProps(props, instance)
validateProps(rawProps || {}, props, instance)
}

if (isStateful) {
Expand Down Expand Up @@ -262,8 +262,8 @@ export function updateProps(
// trigger updates for $attrs in case it's used in component slots
trigger(instance, TriggerOpTypes.SET, '$attrs')

if (__DEV__ && rawProps) {
validateProps(props, instance)
if (__DEV__) {
validateProps(rawProps || {}, props, instance)
}
}

Expand Down Expand Up @@ -460,13 +460,17 @@ function getTypeIndex(
/**
* dev only
*/
function validateProps(props: Data, instance: ComponentInternalInstance) {
const rawValues = toRaw(props)
function validateProps(
rawProps: Data,
props: Data,
instance: ComponentInternalInstance
) {
const resolvedValues = toRaw(props)
const options = instance.propsOptions[0]
for (const key in options) {
let opt = options[key]
if (opt == null) continue
validateProp(key, rawValues[key], opt, !hasOwn(rawValues, key))
validateProp(key, resolvedValues[key], opt, !hasOwn(rawProps, key))
}
}

Expand Down