Skip to content

Commit a2f441d

Browse files
committed
wip: refactor compat check utils
1 parent 7a25cbb commit a2f441d

File tree

8 files changed

+53
-22
lines changed

8 files changed

+53
-22
lines changed

packages/runtime-core/src/apiWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
import { queuePostRenderEffect } from './renderer'
3535
import { warn } from './warning'
3636
import { DeprecationTypes } from './compat/deprecations'
37-
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
37+
import { checkCompatEnabled, isCompatEnabled } from './compat/compatConfig'
3838

3939
export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void
4040

@@ -226,7 +226,7 @@ function doWatch(
226226
const val = baseGetter()
227227
if (
228228
isArray(val) &&
229-
softAssertCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
229+
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
230230
) {
231231
traverse(val)
232232
}

packages/runtime-core/src/compat/__tests__/global.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Vue from '@vue/compat'
22

33
describe('compat: global API', () => {
4-
test('should work', async () => {
4+
beforeEach(() => Vue.configureCompat({ MODE: 2 }))
5+
afterEach(() => Vue.configureCompat({ MODE: 3 }))
6+
7+
test('should work', () => {
58
const el = document.createElement('div')
69
el.innerHTML = `{{ msg }}`
710
new Vue({

packages/runtime-core/src/compat/compatConfig.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export function isCompatEnabled(
3939
}
4040
}
4141

42+
/**
43+
* Use this for features that are completely removed in non-compat build.
44+
*/
4245
export function assertCompatEnabled(
4346
key: DeprecationTypes,
4447
instance: ComponentInternalInstance | null,
@@ -51,6 +54,10 @@ export function assertCompatEnabled(
5154
}
5255
}
5356

57+
/**
58+
* Use this for features where legacy usage is still possible, but will likely
59+
* lead to runtime error if compat is disabled. (warn in all cases)
60+
*/
5461
export function softAssertCompatEnabled(
5562
key: DeprecationTypes,
5663
instance: ComponentInternalInstance | null,
@@ -62,12 +69,26 @@ export function softAssertCompatEnabled(
6269
return isCompatEnabled(key, instance)
6370
}
6471

65-
// disable features that conflict with v3 behavior
72+
/**
73+
* Use this for features with the same syntax but with mutually exclusive
74+
* behavior in 2 vs 3. Only warn if compat is enabled.
75+
* e.g. render function
76+
*/
77+
export function checkCompatEnabled(
78+
key: DeprecationTypes,
79+
instance: ComponentInternalInstance | null,
80+
...args: any[]
81+
) {
82+
const enabled = isCompatEnabled(key, instance)
83+
if (__DEV__ && enabled) {
84+
warnDeprecation(key, instance, ...args)
85+
}
86+
return enabled
87+
}
88+
89+
// run tests in v3 mode by default
6690
if (__TEST__) {
6791
configureCompat({
68-
COMPONENT_ASYNC: false,
69-
COMPONENT_FUNCTIONAL: false,
70-
WATCH_ARRAY: false,
71-
INSTANCE_ATTRS_CLASS_STYLE: false
92+
MODE: 3
7293
})
7394
}

packages/runtime-core/src/compat/component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
import { resolveInjections } from '../componentOptions'
1111
import { InternalSlots } from '../componentSlots'
1212
import { isVNode } from '../vnode'
13-
import { isCompatEnabled, softAssertCompatEnabled } from './compatConfig'
14-
import { DeprecationTypes, warnDeprecation } from './deprecations'
13+
import { checkCompatEnabled, softAssertCompatEnabled } from './compatConfig'
14+
import { DeprecationTypes } from './deprecations'
1515
import { getCompatListeners } from './instanceListeners'
1616
import { compatH } from './renderFn'
1717

@@ -24,9 +24,8 @@ export function convertLegacyComponent(
2424
// use softAssert here.
2525
if (
2626
isFunction(comp) &&
27-
isCompatEnabled(DeprecationTypes.COMPONENT_ASYNC, instance)
27+
checkCompatEnabled(DeprecationTypes.COMPONENT_ASYNC, instance, comp)
2828
) {
29-
__DEV__ && warnDeprecation(DeprecationTypes.COMPONENT_ASYNC, instance, comp)
3029
return convertLegacyAsyncComponent(comp)
3130
}
3231

packages/runtime-core/src/compat/instance.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { extend, NOOP } from '@vue/shared'
22
import { PublicPropertiesMap } from '../componentPublicInstance'
33
import { getCompatChildren } from './instanceChildren'
4-
import { assertCompatEnabled, isCompatEnabled } from './compatConfig'
5-
import { DeprecationTypes, warnDeprecation } from './deprecations'
4+
import {
5+
assertCompatEnabled,
6+
checkCompatEnabled,
7+
isCompatEnabled
8+
} from './compatConfig'
9+
import { DeprecationTypes } from './deprecations'
610
import { off, on, once } from './instanceEventEmitter'
711
import { getCompatListeners } from './instanceListeners'
812
import { shallowReadonly } from '@vue/reactivity'
@@ -48,7 +52,7 @@ export function installCompatInstanceProperties(map: PublicPropertiesMap) {
4852
if (isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, i)) {
4953
return new Proxy(i.slots, legacySlotProxyHandlers)
5054
}
51-
return i.slots
55+
return __DEV__ ? shallowReadonly(i.slots) : i.slots
5256
},
5357

5458
$scopedSlots: i => {
@@ -59,7 +63,7 @@ export function installCompatInstanceProperties(map: PublicPropertiesMap) {
5963
// overrides existing accessor
6064
$attrs: i => {
6165
if (__DEV__ && i.type.inheritAttrs === false) {
62-
warnDeprecation(DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE, i)
66+
checkCompatEnabled(DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE, i)
6367
}
6468
return __DEV__ ? shallowReadonly(i.attrs) : i.attrs
6569
},

packages/runtime-core/src/component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ import { CompilerOptions } from '@vue/compiler-core'
5454
import { markAttrsAccessed } from './componentRenderUtils'
5555
import { currentRenderingInstance } from './componentRenderContext'
5656
import { startMeasure, endMeasure } from './profiling'
57-
import { isCompatEnabled } from './compat/compatConfig'
58-
import { DeprecationTypes, warnDeprecation } from './compat/deprecations'
57+
import { checkCompatEnabled } from './compat/compatConfig'
58+
import { DeprecationTypes } from './compat/deprecations'
5959
import { compatH } from './compat/renderFn'
6060

6161
export type Data = Record<string, unknown>
@@ -687,9 +687,8 @@ export function finishComponentSetup(
687687
if (
688688
__COMPAT__ &&
689689
Component.render &&
690-
isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, instance)
690+
checkCompatEnabled(DeprecationTypes.RENDER_FUNCTION, instance)
691691
) {
692-
warnDeprecation(DeprecationTypes.RENDER_FUNCTION, instance)
693692
const originalRender = Component.render
694693
Component.render = function compatRender() {
695694
return originalRender.call(this, compatH)

packages/runtime-core/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,17 @@ export { LegacyConfig } from './compat/globalConfig'
288288

289289
import { warnDeprecation } from './compat/deprecations'
290290
import { createCompatVue } from './compat/global'
291-
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
291+
import {
292+
isCompatEnabled,
293+
checkCompatEnabled,
294+
softAssertCompatEnabled
295+
} from './compat/compatConfig'
292296

293297
const _compatUtils = {
294298
warnDeprecation,
295299
createCompatVue,
296300
isCompatEnabled,
301+
checkCompatEnabled,
297302
softAssertCompatEnabled
298303
}
299304

packages/runtime-dom/src/components/TransitionGroup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const TransitionGroupImpl = {
106106
if (
107107
__COMPAT__ &&
108108
!rawProps.tag &&
109-
compatUtils.softAssertCompatEnabled(
109+
compatUtils.checkCompatEnabled(
110110
DeprecationTypes.TRANSITION_GROUP_ROOT,
111111
instance.parent
112112
)

0 commit comments

Comments
 (0)