wip: fix component resolution check

This commit is contained in:
Evan You 2022-06-15 21:38:05 +08:00
parent aa2b1f4d93
commit bd8409bc98
3 changed files with 25 additions and 9 deletions

View File

@ -39,6 +39,8 @@ export function prefixIdentifiers(
plugins plugins
}) })
const isScriptSetup = bindings && bindings.__isScriptSetup !== false
walkIdentifiers( walkIdentifiers(
ast, ast,
ident => { ident => {
@ -47,7 +49,7 @@ export function prefixIdentifiers(
return return
} }
if (!bindings) { if (!isScriptSetup) {
s.prependRight(ident.start!, '_vm.') s.prependRight(ident.start!, '_vm.')
return return
} }
@ -62,7 +64,7 @@ export function prefixIdentifiers(
s.prependRight( s.prependRight(
node.start!, node.start!,
`var _vm=this,_c=_vm._self._c${ `var _vm=this,_c=_vm._self._c${
bindings ? `,_setup=_vm._setupProxy;` : `;` isScriptSetup ? `,_setup=_vm._setupProxy;` : `;`
}` }`
) )
} }

View File

@ -102,7 +102,7 @@ export function genElement(el: ASTElement, state: CodegenState): string {
let tag: string | undefined let tag: string | undefined
// check if this is a component in <script setup> // check if this is a component in <script setup>
const bindings = state.options.bindings const bindings = state.options.bindings
if (bindings && !bindings.__isScriptSetup) { if (bindings && bindings.__isScriptSetup !== false) {
tag = tag =
checkBindingType(bindings, el.tag) || checkBindingType(bindings, el.tag) ||
checkBindingType(bindings, camelize(el.tag)) || checkBindingType(bindings, camelize(el.tag)) ||

View File

@ -4,11 +4,12 @@ import { generate } from 'compiler/codegen'
import { isObject, isFunction, extend } from 'shared/util' import { isObject, isFunction, extend } from 'shared/util'
import { isReservedTag } from 'web/util/index' import { isReservedTag } from 'web/util/index'
import { baseOptions } from 'web/compiler/options' import { baseOptions } from 'web/compiler/options'
import { BindingTypes } from '../../../../packages/compiler-sfc/src/types'
function assertCodegen(template, generatedCode, ...args) { function assertCodegen(template, generatedCode, ...args) {
let staticRenderFnCodes = [] let staticRenderFnCodes: string[] = []
let generateOptions = baseOptions let generateOptions = baseOptions
let proc = null let proc: Function | null = null
let len = args.length let len = args.length
while (len--) { while (len--) {
const arg = args[len] const arg = args[len]
@ -28,7 +29,6 @@ function assertCodegen(template, generatedCode, ...args) {
expect(res.staticRenderFns).toEqual(staticRenderFnCodes) expect(res.staticRenderFns).toEqual(staticRenderFnCodes)
} }
/* eslint-disable quotes */
describe('codegen', () => { describe('codegen', () => {
it('generate directive', () => { it('generate directive', () => {
assertCodegen( assertCodegen(
@ -624,7 +624,7 @@ describe('codegen', () => {
expect( expect(
'Inline-template components must have exactly one child element.' 'Inline-template components must have exactly one child element.'
).toHaveBeenWarned() ).toHaveBeenWarned()
expect(console.error.mock.calls.length).toBe(3) expect((console.error as any).mock.calls.length).toBe(3)
}) })
it('generate static trees inside v-for', () => { it('generate static trees inside v-for', () => {
@ -689,7 +689,7 @@ describe('codegen', () => {
}) })
it('not specified ast type', () => { it('not specified ast type', () => {
const res = generate(null, baseOptions) const res = generate(undefined, baseOptions)
expect(res.render).toBe(`with(this){return _c("div")}`) expect(res.render).toBe(`with(this){return _c("div")}`)
expect(res.staticRenderFns).toEqual([]) expect(res.staticRenderFns).toEqual([])
}) })
@ -709,5 +709,19 @@ describe('codegen', () => {
`with(this){return _c('div',[(ok)?_l((1),function(i){return _c('foo',{key:i})}):_e()],2)}` `with(this){return _c('div',[(ok)?_l((1),function(i){return _c('foo',{key:i})}):_e()],2)}`
) )
}) })
it('component with bindings ', () => {
const ast = parse(`<div><Foo/><foo-bar></foo-bar></div>`, baseOptions)
optimize(ast, baseOptions)
const res = generate(ast, {
...baseOptions,
bindings: {
Foo: BindingTypes.SETUP_CONST,
FooBar: BindingTypes.SETUP_CONST
}
})
expect(res.render).toMatchInlineSnapshot(
'"with(this){return _c(\'div\',[_c(Foo),_c(FooBar)],1)}"'
)
})
}) })
/* eslint-enable quotes */