Skip to content
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

fix(runtime-core): should not set the access cache in the data-fn #3299

Merged
merged 1 commit into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Expand Up @@ -275,4 +275,25 @@ describe('renderer: component', () => {
await nextTick()
expect(App.updated).toHaveBeenCalledTimes(0)
})

describe('render with access caches', () => {
// #3297
test('should not set the access cache in the data() function (production mode)', () => {
const Comp = {
data() {
;(this as any).foo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the ; needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need, just my habit

return { foo: 1 }
},
render() {
return h('h1', (this as any).foo)
}
}
const root = nodeOps.createElement('div')

__DEV__ = false
render(h(Comp), root)
__DEV__ = true
expect(serializeInner(root)).toBe(`<h1>1</h1>`)
})
})
})
8 changes: 5 additions & 3 deletions packages/runtime-core/src/componentOptions.ts
Expand Up @@ -465,7 +465,7 @@ function createDuplicateChecker() {

type DataFn = (vm: ComponentPublicInstance) => any

export let isInBeforeCreate = false
export let shouldCacheAccess = true

export function applyOptions(
instance: ComponentInternalInstance,
Expand Down Expand Up @@ -518,15 +518,15 @@ export function applyOptions(

// applyOptions is called non-as-mixin once per instance
if (!asMixin) {
isInBeforeCreate = true
shouldCacheAccess = false
callSyncHook(
'beforeCreate',
LifecycleHooks.BEFORE_CREATE,
options,
instance,
globalMixins
)
isInBeforeCreate = false
shouldCacheAccess = true
// global mixins are applied first
applyMixins(
instance,
Expand Down Expand Up @@ -892,7 +892,9 @@ function resolveData(
`Plain object usage is no longer supported.`
)
}
shouldCacheAccess = false
const data = dataFn.call(publicThis, publicThis)
shouldCacheAccess = true
if (__DEV__ && isPromise(data)) {
warn(
`data() returned a Promise - note data() cannot be async; If you ` +
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentPublicInstance.ts
Expand Up @@ -31,7 +31,7 @@ import {
OptionTypesType,
OptionTypesKeys,
resolveMergedOptions,
isInBeforeCreate
shouldCacheAccess
} from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits'
import { Slots } from './componentSlots'
Expand Down Expand Up @@ -307,7 +307,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
} else if (ctx !== EMPTY_OBJ && hasOwn(ctx, key)) {
accessCache![key] = AccessTypes.CONTEXT
return ctx[key]
} else if (!__FEATURE_OPTIONS_API__ || !isInBeforeCreate) {
} else if (!__FEATURE_OPTIONS_API__ || shouldCacheAccess) {
accessCache![key] = AccessTypes.OTHER
}
}
Expand Down