refactor(runtime-core): extract function isReservedPrefix
(#3265)
* chore(runtime-core): extract function isReservedKey * chore: improve code Co-authored-by: Evan You <yyx990803@gmail.com>
This commit is contained in:
parent
40794c80ca
commit
2a9e9a4096
@ -58,7 +58,8 @@ import { EmitsOptions, EmitsToProps } from './componentEmits'
|
|||||||
import { Directive } from './directives'
|
import { Directive } from './directives'
|
||||||
import {
|
import {
|
||||||
CreateComponentPublicInstance,
|
CreateComponentPublicInstance,
|
||||||
ComponentPublicInstance
|
ComponentPublicInstance,
|
||||||
|
isReservedPrefix
|
||||||
} from './componentPublicInstance'
|
} from './componentPublicInstance'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
import { VNodeChild } from './vnode'
|
import { VNodeChild } from './vnode'
|
||||||
@ -681,7 +682,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
|
|||||||
for (const key in data) {
|
for (const key in data) {
|
||||||
checkDuplicateProperties!(OptionTypes.DATA, key)
|
checkDuplicateProperties!(OptionTypes.DATA, key)
|
||||||
// expose data on ctx during dev
|
// expose data on ctx during dev
|
||||||
if (key[0] !== '$' && key[0] !== '_') {
|
if (!isReservedPrefix(key[0])) {
|
||||||
Object.defineProperty(ctx, key, {
|
Object.defineProperty(ctx, key, {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
|
@ -274,6 +274,8 @@ export interface ComponentRenderContext {
|
|||||||
_: ComponentInternalInstance
|
_: ComponentInternalInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isReservedPrefix = (key: string) => key === '_' || key === '$'
|
||||||
|
|
||||||
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
||||||
get({ _: instance }: ComponentRenderContext, key: string) {
|
get({ _: instance }: ComponentRenderContext, key: string) {
|
||||||
const { ctx, setupState, data, props, accessCache, type, appContext } =
|
const { ctx, setupState, data, props, accessCache, type, appContext } =
|
||||||
@ -385,11 +387,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
|||||||
// to infinite warning loop
|
// to infinite warning loop
|
||||||
key.indexOf('__v') !== 0)
|
key.indexOf('__v') !== 0)
|
||||||
) {
|
) {
|
||||||
if (
|
if (data !== EMPTY_OBJ && isReservedPrefix(key[0]) && hasOwn(data, key)) {
|
||||||
data !== EMPTY_OBJ &&
|
|
||||||
(key[0] === '$' || key[0] === '_') &&
|
|
||||||
hasOwn(data, key)
|
|
||||||
) {
|
|
||||||
warn(
|
warn(
|
||||||
`Property ${JSON.stringify(
|
`Property ${JSON.stringify(
|
||||||
key
|
key
|
||||||
@ -571,7 +569,7 @@ export function exposeSetupStateOnRenderContext(
|
|||||||
const { ctx, setupState } = instance
|
const { ctx, setupState } = instance
|
||||||
Object.keys(toRaw(setupState)).forEach(key => {
|
Object.keys(toRaw(setupState)).forEach(key => {
|
||||||
if (!setupState.__isScriptSetup) {
|
if (!setupState.__isScriptSetup) {
|
||||||
if (key[0] === '$' || key[0] === '_') {
|
if (isReservedPrefix(key[0])) {
|
||||||
warn(
|
warn(
|
||||||
`setup() return property ${JSON.stringify(
|
`setup() return property ${JSON.stringify(
|
||||||
key
|
key
|
||||||
|
Loading…
Reference in New Issue
Block a user