Skip to content

Commit 1bddeea

Browse files
committed
feat(runtime-core): warn against user properties with reserved prefixes
1 parent 20bc7ba commit 1bddeea

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/runtime-core/src/componentOptions.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,14 @@ export function applyOptions(
417417
for (const key in rawData) {
418418
checkDuplicateProperties!(OptionTypes.DATA, key)
419419
// expose data on ctx during dev
420-
Object.defineProperty(ctx, key, {
421-
configurable: true,
422-
enumerable: true,
423-
get: () => rawData[key],
424-
set: NOOP
425-
})
420+
if (key[0] !== '$' && key[0] !== '_') {
421+
Object.defineProperty(ctx, key, {
422+
configurable: true,
423+
enumerable: true,
424+
get: () => rawData[key],
425+
set: NOOP
426+
})
427+
}
426428
}
427429
}
428430
}

packages/runtime-core/src/componentProxy.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,19 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
195195
) {
196196
return globalProperties[key]
197197
} else if (__DEV__ && currentRenderingInstance) {
198-
warn(
199-
`Property ${JSON.stringify(key)} was accessed during render ` +
200-
`but is not defined on instance.`
201-
)
198+
if (data !== EMPTY_OBJ && key[0] === '$' && hasOwn(data, key)) {
199+
warn(
200+
`Property ${JSON.stringify(
201+
key
202+
)} must be accessed via $data because it starts with a reserved ` +
203+
`character and is not proxied on the render context.`
204+
)
205+
} else {
206+
warn(
207+
`Property ${JSON.stringify(key)} was accessed during render ` +
208+
`but is not defined on instance.`
209+
)
210+
}
202211
}
203212
},
204213

@@ -280,7 +289,15 @@ export const RuntimeCompiledPublicInstanceProxyHandlers = {
280289
return PublicInstanceProxyHandlers.get!(target, key, target)
281290
},
282291
has(_: ComponentRenderContext, key: string) {
283-
return key[0] !== '_' && !isGloballyWhitelisted(key)
292+
const has = key[0] !== '_' && !isGloballyWhitelisted(key)
293+
if (__DEV__ && !has && PublicInstanceProxyHandlers.has!(_, key)) {
294+
warn(
295+
`Property ${JSON.stringify(
296+
key
297+
)} should not start with _ which is a reserved prefix for Vue internals.`
298+
)
299+
}
300+
return has
284301
}
285302
}
286303

0 commit comments

Comments
 (0)