Skip to content

Commit

Permalink
refactor(runtime-core): extract function isReservedPrefix (#3265)
Browse files Browse the repository at this point in the history
* chore(runtime-core): extract function isReservedKey

* chore: improve code

Co-authored-by: Evan You <yyx990803@gmail.com>
  • Loading branch information
edison1105 and yyx990803 authored May 13, 2022
1 parent 40794c8 commit 2a9e9a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ import { EmitsOptions, EmitsToProps } from './componentEmits'
import { Directive } from './directives'
import {
CreateComponentPublicInstance,
ComponentPublicInstance
ComponentPublicInstance,
isReservedPrefix
} from './componentPublicInstance'
import { warn } from './warning'
import { VNodeChild } from './vnode'
Expand Down Expand Up @@ -681,7 +682,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
for (const key in data) {
checkDuplicateProperties!(OptionTypes.DATA, key)
// expose data on ctx during dev
if (key[0] !== '$' && key[0] !== '_') {
if (!isReservedPrefix(key[0])) {
Object.defineProperty(ctx, key, {
configurable: true,
enumerable: true,
Expand Down
10 changes: 4 additions & 6 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ export interface ComponentRenderContext {
_: ComponentInternalInstance
}

export const isReservedPrefix = (key: string) => key === '_' || key === '$'

export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
get({ _: instance }: ComponentRenderContext, key: string) {
const { ctx, setupState, data, props, accessCache, type, appContext } =
Expand Down Expand Up @@ -385,11 +387,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
// to infinite warning loop
key.indexOf('__v') !== 0)
) {
if (
data !== EMPTY_OBJ &&
(key[0] === '$' || key[0] === '_') &&
hasOwn(data, key)
) {
if (data !== EMPTY_OBJ && isReservedPrefix(key[0]) && hasOwn(data, key)) {
warn(
`Property ${JSON.stringify(
key
Expand Down Expand Up @@ -571,7 +569,7 @@ export function exposeSetupStateOnRenderContext(
const { ctx, setupState } = instance
Object.keys(toRaw(setupState)).forEach(key => {
if (!setupState.__isScriptSetup) {
if (key[0] === '$' || key[0] === '_') {
if (isReservedPrefix(key[0])) {
warn(
`setup() return property ${JSON.stringify(
key
Expand Down

0 comments on commit 2a9e9a4

Please sign in to comment.