Skip to content

Commit 2a9e9a4

Browse files
refactor(runtime-core): extract function isReservedPrefix (#3265)
* chore(runtime-core): extract function isReservedKey * chore: improve code Co-authored-by: Evan You <[email protected]>
1 parent 40794c8 commit 2a9e9a4

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

packages/runtime-core/src/componentOptions.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ import { EmitsOptions, EmitsToProps } from './componentEmits'
5858
import { Directive } from './directives'
5959
import {
6060
CreateComponentPublicInstance,
61-
ComponentPublicInstance
61+
ComponentPublicInstance,
62+
isReservedPrefix
6263
} from './componentPublicInstance'
6364
import { warn } from './warning'
6465
import { VNodeChild } from './vnode'
@@ -681,7 +682,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
681682
for (const key in data) {
682683
checkDuplicateProperties!(OptionTypes.DATA, key)
683684
// expose data on ctx during dev
684-
if (key[0] !== '$' && key[0] !== '_') {
685+
if (!isReservedPrefix(key[0])) {
685686
Object.defineProperty(ctx, key, {
686687
configurable: true,
687688
enumerable: true,

packages/runtime-core/src/componentPublicInstance.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ export interface ComponentRenderContext {
274274
_: ComponentInternalInstance
275275
}
276276

277+
export const isReservedPrefix = (key: string) => key === '_' || key === '$'
278+
277279
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
278280
get({ _: instance }: ComponentRenderContext, key: string) {
279281
const { ctx, setupState, data, props, accessCache, type, appContext } =
@@ -385,11 +387,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
385387
// to infinite warning loop
386388
key.indexOf('__v') !== 0)
387389
) {
388-
if (
389-
data !== EMPTY_OBJ &&
390-
(key[0] === '$' || key[0] === '_') &&
391-
hasOwn(data, key)
392-
) {
390+
if (data !== EMPTY_OBJ && isReservedPrefix(key[0]) && hasOwn(data, key)) {
393391
warn(
394392
`Property ${JSON.stringify(
395393
key
@@ -571,7 +569,7 @@ export function exposeSetupStateOnRenderContext(
571569
const { ctx, setupState } = instance
572570
Object.keys(toRaw(setupState)).forEach(key => {
573571
if (!setupState.__isScriptSetup) {
574-
if (key[0] === '$' || key[0] === '_') {
572+
if (isReservedPrefix(key[0])) {
575573
warn(
576574
`setup() return property ${JSON.stringify(
577575
key

0 commit comments

Comments
 (0)