Skip to content

fix(runtime-core): improve consistency of PublicInstanceProxyHandlers.has #13507

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/runtime-core/__tests__/componentPublicInstance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,55 @@ describe('component: proxy', () => {
data() {
return {
foo: 0,
$foo: 0,
}
},
computed: {
cmp: () => {
throw new Error('value of cmp should not be accessed')
},
$cmp: () => {
throw new Error('value of $cmp should not be read')
},
},
setup() {
return {
bar: 1,
}
},
__cssModules: {
$style: {},
cssStyles: {},
},
mounted() {
instanceProxy = this
},
}

const app = createApp(Comp, { msg: 'hello' })
app.config.globalProperties.global = 1
app.config.globalProperties.$global = 1

app.mount(nodeOps.createElement('div'))

// props
expect('msg' in instanceProxy).toBe(true)
// data
expect('foo' in instanceProxy).toBe(true)
// ctx
expect('$foo' in instanceProxy).toBe(false)
// setupState
expect('bar' in instanceProxy).toBe(true)
// ctx
expect('cmp' in instanceProxy).toBe(true)
expect('$cmp' in instanceProxy).toBe(true)
// public properties
expect('$el' in instanceProxy).toBe(true)
// CSS modules
expect('$style' in instanceProxy).toBe(true)
expect('cssStyles' in instanceProxy).toBe(true)
// global properties
expect('global' in instanceProxy).toBe(true)
expect('$global' in instanceProxy).toBe(true)

// non-existent
expect('$foobar' in instanceProxy).toBe(false)
Expand All @@ -202,19 +224,26 @@ describe('component: proxy', () => {
// #4962 triggering getter should not cause non-existent property to
// pass the has check
instanceProxy.baz
instanceProxy.$baz
expect('baz' in instanceProxy).toBe(false)
expect('$baz' in instanceProxy).toBe(false)

// set non-existent (goes into proxyTarget sink)
instanceProxy.baz = 1
expect('baz' in instanceProxy).toBe(true)
instanceProxy.$baz = 1
expect('$baz' in instanceProxy).toBe(true)

// dev mode ownKeys check for console inspection
// should only expose own keys
expect(Object.keys(instanceProxy)).toMatchObject([
'msg',
'bar',
'foo',
'cmp',
'$cmp',
'baz',
'$baz',
])
})

Expand Down
13 changes: 7 additions & 6 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,19 +575,20 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {

has(
{
_: { data, setupState, accessCache, ctx, appContext, propsOptions },
_: { data, setupState, accessCache, ctx, appContext, propsOptions, type },
}: ComponentRenderContext,
key: string,
) {
let normalizedProps
return (
!!accessCache![key] ||
(data !== EMPTY_OBJ && hasOwn(data, key)) ||
let normalizedProps, cssModules
return !!(
accessCache![key] ||
(data !== EMPTY_OBJ && key[0] !== '$' && hasOwn(data, key)) ||
hasSetupBinding(setupState, key) ||
((normalizedProps = propsOptions[0]) && hasOwn(normalizedProps, key)) ||
hasOwn(ctx, key) ||
hasOwn(publicPropertiesMap, key) ||
hasOwn(appContext.config.globalProperties, key)
hasOwn(appContext.config.globalProperties, key) ||
((cssModules = type.__cssModules) && cssModules[key])
)
},

Expand Down