]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: support per-component compatConfig
authorEvan You <yyx990803@gmail.com>
Thu, 8 Apr 2021 21:11:05 +0000 (17:11 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 8 Apr 2021 21:11:05 +0000 (17:11 -0400)
packages/runtime-core/src/compat/compatConfig.ts
packages/runtime-core/src/compat/deprecations.ts
packages/runtime-core/src/componentOptions.ts
packages/runtime-core/src/renderer.ts

index d8508853db55a937ec6273310dc54ddb90551266..3eae262ecb894f5948af94a4467c1d37f216a558 100644 (file)
@@ -1,13 +1,11 @@
 import { extend } from '@vue/shared'
+import { ComponentOptions, getCurrentInstance } from '../component'
 import { DeprecationTypes, warnDeprecation } from './deprecations'
 
 export type CompatConfig = Partial<
-  Record<DeprecationTypes, DeprecationConfigItem>
->
-
-export interface DeprecationConfigItem {
-  warning?: boolean // default: true
-  enabled?: boolean // default: true
+  Record<DeprecationTypes, boolean | 'suppress-warning'>
+> & {
+  MODE?: 2 | 3
 }
 
 const globalCompatConfig: CompatConfig = {}
@@ -16,15 +14,24 @@ export function configureCompat(config: CompatConfig) {
   extend(globalCompatConfig, config)
 }
 
-export function getCompatConfig(
-  key: DeprecationTypes
-): DeprecationConfigItem | undefined {
+export function getCompatConfigForKey(key: DeprecationTypes | 'MODE') {
+  const instance = getCurrentInstance()
+  const instanceConfig =
+    instance && (instance.type as ComponentOptions).compatConfig
+  if (instanceConfig && key in instanceConfig) {
+    return instanceConfig[key]
+  }
   return globalCompatConfig[key]
 }
 
 export function isCompatEnabled(key: DeprecationTypes): boolean {
-  const config = getCompatConfig(key)
-  return !config || config.enabled !== false
+  const mode = getCompatConfigForKey('MODE') || 2
+  const val = getCompatConfigForKey(key)
+  if (mode === 2) {
+    return val !== false
+  } else {
+    return val === true || val === 'suppress-warning'
+  }
 }
 
 export function assertCompatEnabled(key: DeprecationTypes, ...args: any[]) {
@@ -45,9 +52,9 @@ export function softAssertCompatEnabled(key: DeprecationTypes, ...args: any[]) {
 // disable features that conflict with v3 behavior
 if (__TEST__) {
   configureCompat({
-    COMPONENT_ASYNC: { enabled: false },
-    COMPONENT_FUNCTIONAL: { enabled: false },
-    WATCH_ARRAY: { enabled: false },
-    INSTANCE_ATTRS_CLASS_STYLE: { enabled: false }
+    COMPONENT_ASYNC: false,
+    COMPONENT_FUNCTIONAL: false,
+    WATCH_ARRAY: false,
+    INSTANCE_ATTRS_CLASS_STYLE: false
   })
 }
index 5f6e7b28f0bbeee494f33b30bc35d2215c30b211..4783b390b8983ad71aeaf4146ecc29e48e78a277 100644 (file)
@@ -5,7 +5,7 @@ import {
   isRuntimeOnly
 } from '../component'
 import { warn } from '../warning'
-import { getCompatConfig } from './compatConfig'
+import { getCompatConfigForKey, isCompatEnabled } from './compatConfig'
 
 export const enum DeprecationTypes {
   GLOBAL_MOUNT = 'GLOBAL_MOUNT',
@@ -203,10 +203,10 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
       `Components with inheritAttrs: false will no longer auto-inherit ` +
       `class/style on its root element. If your code relies on this behavior, ` +
       `you may see broken styling and need to adjust your CSS. Otherwise, ` +
-      `you can suppress this warning with:` +
+      `you can disable the compat behavior and suppress this warning with:` +
       `\n\n  configureCompat({ ${
         DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE
-      }: { warning: false }})\n`,
+      }: false )\n`,
     link: `https://v3.vuejs.org/guide/migration/attrs-includes-class-style.html`
   },
 
@@ -238,9 +238,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
       `trigger on array mutation unless the "deep" option is specified. ` +
       `If current usage is intended, you can disable the compat behavior and ` +
       `suppress this warning with:` +
-      `\n\n  configureCompat({ ${
-        DeprecationTypes.WATCH_ARRAY
-      }: { enabled: false }})\n`,
+      `\n\n  configureCompat({ ${DeprecationTypes.WATCH_ARRAY}: false })\n`,
     link: `https://v3.vuejs.org/guide/migration/watch.html`
   },
 
@@ -273,7 +271,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
       `you can disable the compat behavior and suppress this warning with:` +
       `\n\n  configureCompat({ ${
         DeprecationTypes.ATTR_FALSE_VALUE
-      }: { enabled: false }})\n`,
+      }: false })\n`,
     link: `https://v3.vuejs.org/guide/migration/attribute-coercion.html`
   },
 
@@ -288,7 +286,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
       `you can disable the compat behavior and suppress this warning with:` +
       `\n\n  configureCompat({ ${
         DeprecationTypes.ATTR_ENUMERATED_COERSION
-      }: { enabled: false }})\n`,
+      }: false })\n`,
     link: `https://v3.vuejs.org/guide/migration/attribute-coercion.html`
   },
 
@@ -304,7 +302,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
       `warning with:` +
       `\n\n  configureCompat({ ${
         DeprecationTypes.TRANSITION_GROUP_ROOT
-      }: { enabled: false }})\n`,
+      }: false })\n`,
     link: `https://v3.vuejs.org/guide/migration/transition-group.html`
   },
 
@@ -335,7 +333,7 @@ const deprecationData: Record<DeprecationTypes, DeprecationData> = {
         `then disable compat for legacy async components with:` +
         `\n\n  configureCompat({ ${
           DeprecationTypes.COMPONENT_ASYNC
-        }: { enabled: false }})\n`
+        }: false })\n`
       )
     },
     link: `https://v3.vuejs.org/guide/migration/functional-components.html`
@@ -354,12 +352,8 @@ export function warnDeprecation(key: DeprecationTypes, ...args: any[]) {
   }
 
   // check user config
-  const config = getCompatConfig(key)
-  if (
-    config &&
-    (config.warning === false ||
-      (config.enabled === false && config.warning !== true))
-  ) {
+  const config = getCompatConfigForKey(key)
+  if (config === 'suppress-warning') {
     return
   }
 
@@ -391,4 +385,10 @@ export function warnDeprecation(key: DeprecationTypes, ...args: any[]) {
       typeof message === 'function' ? message(...args) : message
     }${link ? `\n  Details: ${link}` : ``}`
   )
+  if (!isCompatEnabled(key)) {
+    console.error(
+      `^ The above deprecation's compat behavior is disabled and will likely ` +
+        `lead to runtime errors.`
+    )
+  }
 }
index b92b5dcd3ee03c2c9975039d3bbd327b81d3d616..4a93c697c344aa59d35e4666b88911d28212f296 100644 (file)
@@ -67,7 +67,11 @@ import { callWithAsyncErrorHandling } from './errorHandling'
 import { UnionToIntersection } from './helpers/typeUtils'
 import { deepMergeData } from './compat/data'
 import { DeprecationTypes } from './compat/deprecations'
-import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
+import {
+  CompatConfig,
+  isCompatEnabled,
+  softAssertCompatEnabled
+} from './compat/compatConfig'
 
 /**
  * Interface for declaring custom options.
@@ -374,6 +378,8 @@ interface LegacyOptions<
   Mixin extends ComponentOptionsMixin,
   Extends extends ComponentOptionsMixin
 > {
+  compatConfig?: CompatConfig
+
   // allow any custom options
   [key: string]: any
 
index ce72d6da7ba9617b91579d7e12c5a8058804b8c2..a6ad23d0ed357fcdb9a0665b7d4991d3c34f4fe4 100644 (file)
@@ -88,9 +88,6 @@ import { isAsyncWrapper } from './apiAsyncComponent'
 import { isCompatEnabled } from './compat/compatConfig'
 import { DeprecationTypes } from './compat/deprecations'
 
-const isHookEventCompatEnabled =
-  __COMPAT__ && isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS)
-
 export interface Renderer<HostElement = RendererElement> {
   render: RootRenderFunction<HostElement>
   createApp: CreateAppFunction<HostElement>
@@ -445,6 +442,9 @@ function baseCreateRenderer(
   options: RendererOptions,
   createHydrationFns?: typeof createHydrationFunctions
 ): any {
+  const isHookEventCompatEnabled =
+    __COMPAT__ && isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS)
+
   // compile-time feature flags check
   if (__ESM_BUNDLER__ && !__TEST__) {
     initFeatureFlags()