Skip to content

Commit

Permalink
wip: fix component resolution check
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 15, 2022
1 parent aa2b1f4 commit bd8409b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 4 additions & 2 deletions packages/compiler-sfc/src/prefixIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export function prefixIdentifiers(
plugins
})

const isScriptSetup = bindings && bindings.__isScriptSetup !== false

walkIdentifiers(
ast,
ident => {
Expand All @@ -47,7 +49,7 @@ export function prefixIdentifiers(
return
}

if (!bindings) {
if (!isScriptSetup) {
s.prependRight(ident.start!, '_vm.')
return
}
Expand All @@ -62,7 +64,7 @@ export function prefixIdentifiers(
s.prependRight(
node.start!,
`var _vm=this,_c=_vm._self._c${
bindings ? `,_setup=_vm._setupProxy;` : `;`
isScriptSetup ? `,_setup=_vm._setupProxy;` : `;`
}`
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function genElement(el: ASTElement, state: CodegenState): string {
let tag: string | undefined
// check if this is a component in <script setup>
const bindings = state.options.bindings
if (bindings && !bindings.__isScriptSetup) {
if (bindings && bindings.__isScriptSetup !== false) {
tag =
checkBindingType(bindings, el.tag) ||
checkBindingType(bindings, camelize(el.tag)) ||
Expand Down
26 changes: 20 additions & 6 deletions test/unit/modules/compiler/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { generate } from 'compiler/codegen'
import { isObject, isFunction, extend } from 'shared/util'
import { isReservedTag } from 'web/util/index'
import { baseOptions } from 'web/compiler/options'
import { BindingTypes } from '../../../../packages/compiler-sfc/src/types'

function assertCodegen(template, generatedCode, ...args) {
let staticRenderFnCodes = []
let staticRenderFnCodes: string[] = []
let generateOptions = baseOptions
let proc = null
let proc: Function | null = null
let len = args.length
while (len--) {
const arg = args[len]
Expand All @@ -28,7 +29,6 @@ function assertCodegen(template, generatedCode, ...args) {
expect(res.staticRenderFns).toEqual(staticRenderFnCodes)
}

/* eslint-disable quotes */
describe('codegen', () => {
it('generate directive', () => {
assertCodegen(
Expand Down Expand Up @@ -624,7 +624,7 @@ describe('codegen', () => {
expect(
'Inline-template components must have exactly one child element.'
).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', () => {
Expand Down Expand Up @@ -689,7 +689,7 @@ describe('codegen', () => {
})

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.staticRenderFns).toEqual([])
})
Expand All @@ -709,5 +709,19 @@ describe('codegen', () => {
`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 */

0 comments on commit bd8409b

Please sign in to comment.