]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): do not resolve assets from setup bindings
authorEvan You <yyx990803@gmail.com>
Mon, 29 Mar 2021 20:11:31 +0000 (16:11 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 29 Mar 2021 20:11:38 +0000 (16:11 -0400)
when not using script setup

fix #3270, fix #3275

packages/compiler-core/__tests__/transforms/transformElement.spec.ts
packages/compiler-core/src/options.ts
packages/compiler-core/src/transforms/transformElement.ts
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index bc2c9686a6c193ef4bd0a724eaf1b5caaa9aac66..1258398d4656147d3f743305300892eb3c7d802e 100644 (file)
@@ -2,7 +2,8 @@ import {
   CompilerOptions,
   baseParse as parse,
   transform,
-  ErrorCodes
+  ErrorCodes,
+  BindingTypes
 } from '../../src'
 import {
   RESOLVE_COMPONENT,
@@ -78,6 +79,28 @@ describe('compiler: element transform', () => {
     expect(root.components).toContain(`Example__self`)
   })
 
+  test('resolve component from setup bindings', () => {
+    const { root, node } = parseWithElementTransform(`<Example/>`, {
+      bindingMetadata: {
+        Example: BindingTypes.SETUP_MAYBE_REF
+      }
+    })
+    expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
+    expect(node.tag).toBe(`$setup["Example"]`)
+  })
+
+  test('do not resolve component from non-script-setup bindings', () => {
+    const bindingMetadata = {
+      Example: BindingTypes.SETUP_MAYBE_REF
+    }
+    Object.defineProperty(bindingMetadata, '__isScriptSetup', { value: false })
+    const { root } = parseWithElementTransform(`<Example/>`, {
+      bindingMetadata
+    })
+    expect(root.helpers).toContain(RESOLVE_COMPONENT)
+    expect(root.components).toContain(`Example`)
+  })
+
   test('static props', () => {
     const { node } = parseWithElementTransform(`<div id="foo" class="bar" />`)
     expect(node).toMatchObject({
index d8d2573a6e8ea90855deef73c62f4d23fab02af7..1b5450de29d65900107086161fe3b5018beb90de 100644 (file)
@@ -94,8 +94,10 @@ export const enum BindingTypes {
   OPTIONS = 'options'
 }
 
-export interface BindingMetadata {
+export type BindingMetadata = {
   [key: string]: BindingTypes | undefined
+} & {
+  __isScriptSetup?: boolean
 }
 
 interface SharedTransformCodegenOptions {
index 398758f42342464e0d548fd47a1ebb3927172e2e..9f4f338956499d814f7f3452049a2d6dde183bd4 100644 (file)
@@ -286,7 +286,7 @@ export function resolveComponentType(
 
 function resolveSetupReference(name: string, context: TransformContext) {
   const bindings = context.bindingMetadata
-  if (!bindings) {
+  if (!bindings || bindings.__isScriptSetup === false) {
     return
   }
 
index 30cef0fbcd4e956497743fa1404b8c960da33fb0..f083a0b6731e357c0cdb03575d7967f85628a9f8 100644 (file)
@@ -962,6 +962,7 @@ describe('SFC analyze <script> bindings', () => {
 
     expect(scriptAst).toBeDefined()
   })
+
   it('recognizes props array declaration', () => {
     const { bindings } = compile(`
       <script>
@@ -974,6 +975,7 @@ describe('SFC analyze <script> bindings', () => {
       foo: BindingTypes.PROPS,
       bar: BindingTypes.PROPS
     })
+    expect(bindings!.__isScriptSetup).toBe(false)
   })
 
   it('recognizes props object declaration', () => {
@@ -997,6 +999,7 @@ describe('SFC analyze <script> bindings', () => {
       baz: BindingTypes.PROPS,
       qux: BindingTypes.PROPS
     })
+    expect(bindings!.__isScriptSetup).toBe(false)
   })
 
   it('recognizes setup return', () => {
@@ -1017,6 +1020,7 @@ describe('SFC analyze <script> bindings', () => {
       foo: BindingTypes.SETUP_MAYBE_REF,
       bar: BindingTypes.SETUP_MAYBE_REF
     })
+    expect(bindings!.__isScriptSetup).toBe(false)
   })
 
   it('recognizes async setup return', () => {
@@ -1037,6 +1041,7 @@ describe('SFC analyze <script> bindings', () => {
       foo: BindingTypes.SETUP_MAYBE_REF,
       bar: BindingTypes.SETUP_MAYBE_REF
     })
+    expect(bindings!.__isScriptSetup).toBe(false)
   })
 
   it('recognizes data return', () => {
index 29cf4b2dc33a01ecfe2c75c187e5942afcc0e92c..6d7d84ccadbf36d6bce79133dca80335c3ed68de 100644 (file)
@@ -1586,6 +1586,12 @@ function analyzeScriptBindings(ast: Statement[]): BindingMetadata {
 
 function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
   const bindings: BindingMetadata = {}
+  // #3270, #3275
+  // mark non-script-setup so we don't resolve components/directives from these
+  Object.defineProperty(bindings, '__isScriptSetup', {
+    enumerable: false,
+    value: false
+  })
   for (const property of node.properties) {
     if (
       property.type === 'ObjectProperty' &&