Skip to content

Commit 3db29eb

Browse files
committed
wip: support per-component compatConfig
1 parent f8e2361 commit 3db29eb

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { extend } from '@vue/shared'
2+
import { ComponentOptions, getCurrentInstance } from '../component'
23
import { DeprecationTypes, warnDeprecation } from './deprecations'
34

45
export type CompatConfig = Partial<
5-
Record<DeprecationTypes, DeprecationConfigItem>
6-
>
7-
8-
export interface DeprecationConfigItem {
9-
warning?: boolean // default: true
10-
enabled?: boolean // default: true
6+
Record<DeprecationTypes, boolean | 'suppress-warning'>
7+
> & {
8+
MODE?: 2 | 3
119
}
1210

1311
const globalCompatConfig: CompatConfig = {}
@@ -16,15 +14,24 @@ export function configureCompat(config: CompatConfig) {
1614
extend(globalCompatConfig, config)
1715
}
1816

19-
export function getCompatConfig(
20-
key: DeprecationTypes
21-
): DeprecationConfigItem | undefined {
17+
export function getCompatConfigForKey(key: DeprecationTypes | 'MODE') {
18+
const instance = getCurrentInstance()
19+
const instanceConfig =
20+
instance && (instance.type as ComponentOptions).compatConfig
21+
if (instanceConfig && key in instanceConfig) {
22+
return instanceConfig[key]
23+
}
2224
return globalCompatConfig[key]
2325
}
2426

2527
export function isCompatEnabled(key: DeprecationTypes): boolean {
26-
const config = getCompatConfig(key)
27-
return !config || config.enabled !== false
28+
const mode = getCompatConfigForKey('MODE') || 2
29+
const val = getCompatConfigForKey(key)
30+
if (mode === 2) {
31+
return val !== false
32+
} else {
33+
return val === true || val === 'suppress-warning'
34+
}
2835
}
2936

3037
export function assertCompatEnabled(key: DeprecationTypes, ...args: any[]) {
@@ -45,9 +52,9 @@ export function softAssertCompatEnabled(key: DeprecationTypes, ...args: any[]) {
4552
// disable features that conflict with v3 behavior
4653
if (__TEST__) {
4754
configureCompat({
48-
COMPONENT_ASYNC: { enabled: false },
49-
COMPONENT_FUNCTIONAL: { enabled: false },
50-
WATCH_ARRAY: { enabled: false },
51-
INSTANCE_ATTRS_CLASS_STYLE: { enabled: false }
55+
COMPONENT_ASYNC: false,
56+
COMPONENT_FUNCTIONAL: false,
57+
WATCH_ARRAY: false,
58+
INSTANCE_ATTRS_CLASS_STYLE: false
5259
})
5360
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
isRuntimeOnly
66
} from '../component'
77
import { warn } from '../warning'
8-
import { getCompatConfig } from './compatConfig'
8+
import { getCompatConfigForKey, isCompatEnabled } from './compatConfig'
99

1010
export const enum DeprecationTypes {
1111
GLOBAL_MOUNT = 'GLOBAL_MOUNT',
@@ -203,10 +203,10 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
203203
`Components with inheritAttrs: false will no longer auto-inherit ` +
204204
`class/style on its root element. If your code relies on this behavior, ` +
205205
`you may see broken styling and need to adjust your CSS. Otherwise, ` +
206-
`you can suppress this warning with:` +
206+
`you can disable the compat behavior and suppress this warning with:` +
207207
`\n\n configureCompat({ ${
208208
DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE
209-
}: { warning: false }})\n`,
209+
}: false )\n`,
210210
link: `https://v3.vuejs.org/guide/migration/attrs-includes-class-style.html`
211211
},
212212

@@ -238,9 +238,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
238238
`trigger on array mutation unless the "deep" option is specified. ` +
239239
`If current usage is intended, you can disable the compat behavior and ` +
240240
`suppress this warning with:` +
241-
`\n\n configureCompat({ ${
242-
DeprecationTypes.WATCH_ARRAY
243-
}: { enabled: false }})\n`,
241+
`\n\n configureCompat({ ${DeprecationTypes.WATCH_ARRAY}: false })\n`,
244242
link: `https://v3.vuejs.org/guide/migration/watch.html`
245243
},
246244

@@ -273,7 +271,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
273271
`you can disable the compat behavior and suppress this warning with:` +
274272
`\n\n configureCompat({ ${
275273
DeprecationTypes.ATTR_FALSE_VALUE
276-
}: { enabled: false }})\n`,
274+
}: false })\n`,
277275
link: `https://v3.vuejs.org/guide/migration/attribute-coercion.html`
278276
},
279277

@@ -288,7 +286,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
288286
`you can disable the compat behavior and suppress this warning with:` +
289287
`\n\n configureCompat({ ${
290288
DeprecationTypes.ATTR_ENUMERATED_COERSION
291-
}: { enabled: false }})\n`,
289+
}: false })\n`,
292290
link: `https://v3.vuejs.org/guide/migration/attribute-coercion.html`
293291
},
294292

@@ -304,7 +302,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
304302
`warning with:` +
305303
`\n\n configureCompat({ ${
306304
DeprecationTypes.TRANSITION_GROUP_ROOT
307-
}: { enabled: false }})\n`,
305+
}: false })\n`,
308306
link: `https://v3.vuejs.org/guide/migration/transition-group.html`
309307
},
310308

@@ -335,7 +333,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
335333
`then disable compat for legacy async components with:` +
336334
`\n\n configureCompat({ ${
337335
DeprecationTypes.COMPONENT_ASYNC
338-
}: { enabled: false }})\n`
336+
}: false })\n`
339337
)
340338
},
341339
link: `https://v3.vuejs.org/guide/migration/functional-components.html`
@@ -354,12 +352,8 @@ export function warnDeprecation(key: DeprecationTypes, ...args: any[]) {
354352
}
355353

356354
// check user config
357-
const config = getCompatConfig(key)
358-
if (
359-
config &&
360-
(config.warning === false ||
361-
(config.enabled === false && config.warning !== true))
362-
) {
355+
const config = getCompatConfigForKey(key)
356+
if (config === 'suppress-warning') {
363357
return
364358
}
365359

@@ -391,4 +385,10 @@ export function warnDeprecation(key: DeprecationTypes, ...args: any[]) {
391385
typeof message === 'function' ? message(...args) : message
392386
}${link ? `\n Details: ${link}` : ``}`
393387
)
388+
if (!isCompatEnabled(key)) {
389+
console.error(
390+
`^ The above deprecation's compat behavior is disabled and will likely ` +
391+
`lead to runtime errors.`
392+
)
393+
}
394394
}

packages/runtime-core/src/componentOptions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ import { callWithAsyncErrorHandling } from './errorHandling'
6767
import { UnionToIntersection } from './helpers/typeUtils'
6868
import { deepMergeData } from './compat/data'
6969
import { DeprecationTypes } from './compat/deprecations'
70-
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
70+
import {
71+
CompatConfig,
72+
isCompatEnabled,
73+
softAssertCompatEnabled
74+
} from './compat/compatConfig'
7175

7276
/**
7377
* Interface for declaring custom options.
@@ -374,6 +378,8 @@ interface LegacyOptions<
374378
Mixin extends ComponentOptionsMixin,
375379
Extends extends ComponentOptionsMixin
376380
> {
381+
compatConfig?: CompatConfig
382+
377383
// allow any custom options
378384
[key: string]: any
379385

packages/runtime-core/src/renderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ import { isAsyncWrapper } from './apiAsyncComponent'
8888
import { isCompatEnabled } from './compat/compatConfig'
8989
import { DeprecationTypes } from './compat/deprecations'
9090

91-
const isHookEventCompatEnabled =
92-
__COMPAT__ && isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS)
93-
9491
export interface Renderer<HostElement = RendererElement> {
9592
render: RootRenderFunction<HostElement>
9693
createApp: CreateAppFunction<HostElement>
@@ -445,6 +442,9 @@ function baseCreateRenderer(
445442
options: RendererOptions,
446443
createHydrationFns?: typeof createHydrationFunctions
447444
): any {
445+
const isHookEventCompatEnabled =
446+
__COMPAT__ && isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS)
447+
448448
// compile-time feature flags check
449449
if (__ESM_BUNDLER__ && !__TEST__) {
450450
initFeatureFlags()

0 commit comments

Comments
 (0)