Skip to content

Commit

Permalink
fix(compiler-sfc): do not resolve assets from setup bindings
Browse files Browse the repository at this point in the history
when not using script setup

fix #3270, fix #3275
  • Loading branch information
yyx990803 committed Mar 29, 2021
1 parent 4d9f9fd commit f5827fd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
CompilerOptions,
baseParse as parse,
transform,
ErrorCodes
ErrorCodes,
BindingTypes
} from '../../src'
import {
RESOLVE_COMPONENT,
Expand Down Expand Up @@ -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({
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ export const enum BindingTypes {
OPTIONS = 'options'
}

export interface BindingMetadata {
export type BindingMetadata = {
[key: string]: BindingTypes | undefined
} & {
__isScriptSetup?: boolean
}

interface SharedTransformCodegenOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ describe('SFC analyze <script> bindings', () => {

expect(scriptAst).toBeDefined()
})

it('recognizes props array declaration', () => {
const { bindings } = compile(`
<script>
Expand All @@ -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', () => {
Expand All @@ -997,6 +999,7 @@ describe('SFC analyze <script> bindings', () => {
baz: BindingTypes.PROPS,
qux: BindingTypes.PROPS
})
expect(bindings!.__isScriptSetup).toBe(false)
})

it('recognizes setup return', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down

0 comments on commit f5827fd

Please sign in to comment.