+import { BindingTypes } from '@vue/compiler-dom/src'
import { compileSFCScript as compile, assertCode } from './utils'
describe('SFC compile <script setup>', () => {
assertCode(content)
// should anayze bindings
expect(bindings).toStrictEqual({
- foo: 'props',
- bar: 'const',
- props: 'const',
- emit: 'const'
+ foo: BindingTypes.PROPS,
+ bar: BindingTypes.SETUP_CONST,
+ props: BindingTypes.SETUP_CONST,
+ emit: BindingTypes.SETUP_CONST
})
// should remove defineOptions import and call
)
expect(content).toMatch(`intersection: { type: Object, required: true }`)
expect(bindings).toStrictEqual({
- string: 'props',
- number: 'props',
- boolean: 'props',
- object: 'props',
- objectLiteral: 'props',
- fn: 'props',
- functionRef: 'props',
- objectRef: 'props',
- array: 'props',
- arrayRef: 'props',
- tuple: 'props',
- set: 'props',
- literal: 'props',
- optional: 'props',
- recordRef: 'props',
- interface: 'props',
- alias: 'props',
- union: 'props',
- literalUnion: 'props',
- literalUnionMixed: 'props',
- intersection: 'props'
+ string: BindingTypes.PROPS,
+ number: BindingTypes.PROPS,
+ boolean: BindingTypes.PROPS,
+ object: BindingTypes.PROPS,
+ objectLiteral: BindingTypes.PROPS,
+ fn: BindingTypes.PROPS,
+ functionRef: BindingTypes.PROPS,
+ objectRef: BindingTypes.PROPS,
+ array: BindingTypes.PROPS,
+ arrayRef: BindingTypes.PROPS,
+ tuple: BindingTypes.PROPS,
+ set: BindingTypes.PROPS,
+ literal: BindingTypes.PROPS,
+ optional: BindingTypes.PROPS,
+ recordRef: BindingTypes.PROPS,
+ interface: BindingTypes.PROPS,
+ alias: BindingTypes.PROPS,
+ union: BindingTypes.PROPS,
+ literalUnion: BindingTypes.PROPS,
+ literalUnionMixed: BindingTypes.PROPS,
+ intersection: BindingTypes.PROPS
})
})
expect(content).toMatch(`let d`)
assertCode(content)
expect(bindings).toStrictEqual({
- foo: 'setup',
- a: 'setup',
- b: 'setup',
- c: 'setup',
- d: 'setup'
+ foo: BindingTypes.SETUP_CONST_REF,
+ a: BindingTypes.SETUP_CONST_REF,
+ b: BindingTypes.SETUP_CONST_REF,
+ c: BindingTypes.SETUP_LET,
+ d: BindingTypes.SETUP_LET
})
})
expect(content).toMatch(`return { a, b, c }`)
assertCode(content)
expect(bindings).toStrictEqual({
- a: 'setup',
- b: 'setup',
- c: 'setup'
+ a: BindingTypes.SETUP_CONST_REF,
+ b: BindingTypes.SETUP_CONST_REF,
+ c: BindingTypes.SETUP_CONST_REF
})
})
)
expect(content).toMatch(`return { n, a, c, d, f, g }`)
expect(bindings).toStrictEqual({
- n: 'setup',
- a: 'setup',
- c: 'setup',
- d: 'setup',
- f: 'setup',
- g: 'setup'
+ n: BindingTypes.SETUP_CONST_REF,
+ a: BindingTypes.SETUP_CONST_REF,
+ c: BindingTypes.SETUP_CONST_REF,
+ d: BindingTypes.SETUP_CONST_REF,
+ f: BindingTypes.SETUP_CONST_REF,
+ g: BindingTypes.SETUP_CONST_REF
})
assertCode(content)
})
expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
expect(content).toMatch(`return { n, a, b, c }`)
expect(bindings).toStrictEqual({
- n: 'setup',
- a: 'setup',
- b: 'setup',
- c: 'setup'
+ n: BindingTypes.SETUP_CONST_REF,
+ a: BindingTypes.SETUP_CONST_REF,
+ b: BindingTypes.SETUP_CONST_REF,
+ c: BindingTypes.SETUP_CONST_REF
})
assertCode(content)
})
expect(content).toMatch(`\nconst e = _ref(__e);`)
expect(content).toMatch(`return { b, d, e }`)
expect(bindings).toStrictEqual({
- b: 'setup',
- d: 'setup',
- e: 'setup'
+ b: BindingTypes.SETUP_CONST_REF,
+ d: BindingTypes.SETUP_CONST_REF,
+ e: BindingTypes.SETUP_CONST_REF
})
assertCode(content)
})
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'props', bar: 'props' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.PROPS,
+ bar: BindingTypes.PROPS
+ })
})
it('recognizes props object declaration', () => {
</script>
`)
expect(bindings).toStrictEqual({
- foo: 'props',
- bar: 'props',
- baz: 'props',
- qux: 'props'
+ foo: BindingTypes.PROPS,
+ bar: BindingTypes.PROPS,
+ baz: BindingTypes.PROPS,
+ qux: BindingTypes.PROPS
})
})
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'setup', bar: 'setup' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.SETUP_CONST_REF,
+ bar: BindingTypes.SETUP_CONST_REF
+ })
})
it('recognizes async setup return', () => {
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'setup', bar: 'setup' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.SETUP_CONST_REF,
+ bar: BindingTypes.SETUP_CONST_REF
+ })
})
it('recognizes data return', () => {
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'data', bar: 'data' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.DATA,
+ bar: BindingTypes.DATA
+ })
})
it('recognizes methods', () => {
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'options' })
+ expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
})
it('recognizes computeds', () => {
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.OPTIONS,
+ bar: BindingTypes.OPTIONS
+ })
})
it('recognizes injections array declaration', () => {
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.OPTIONS,
+ bar: BindingTypes.OPTIONS
+ })
})
it('recognizes injections object declaration', () => {
}
</script>
`)
- expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' })
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.OPTIONS,
+ bar: BindingTypes.OPTIONS
+ })
})
it('works for mixed bindings', () => {
</script>
`)
expect(bindings).toStrictEqual({
- foo: 'options',
- bar: 'props',
- baz: 'setup',
- qux: 'data',
- quux: 'options',
- quuz: 'options'
+ foo: BindingTypes.OPTIONS,
+ bar: BindingTypes.PROPS,
+ baz: BindingTypes.SETUP_CONST_REF,
+ qux: BindingTypes.DATA,
+ quux: BindingTypes.OPTIONS,
+ quuz: BindingTypes.OPTIONS
})
})
</script>
`)
expect(bindings).toStrictEqual({
- foo: 'props'
+ foo: BindingTypes.PROPS
})
})
})
source: string
}
> = Object.create(null)
- const setupBindings: Record<
- string,
- BindingTypes.SETUP | BindingTypes.CONST
- > = Object.create(null)
- const refBindings: Record<string, BindingTypes.SETUP> = Object.create(null)
+ const setupBindings: Record<string, BindingTypes> = Object.create(null)
+ const refBindings: Record<string, BindingTypes> = Object.create(null)
const refIdentifiers: Set<Identifier> = new Set()
const enableRefSugar = options.refSugar !== false
let defaultExport: Node | undefined
if (id.name[0] === '$') {
error(`ref variable identifiers cannot start with $.`, id)
}
- refBindings[id.name] = setupBindings[id.name] = BindingTypes.SETUP
+ refBindings[id.name] = setupBindings[id.name] = BindingTypes.SETUP_CONST_REF
refIdentifiers.add(id)
}
}
for (const [key, { source }] of Object.entries(userImports)) {
bindingMetadata[key] = source.endsWith('.vue')
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : BindingTypes.SETUP_CONST_REF
}
for (const key in setupBindings) {
bindingMetadata[key] = setupBindings[key]
init!.type !== 'Identifier' && // const a = b
init!.type !== 'CallExpression' && // const a = ref()
init!.type !== 'MemberExpression') // const a = b.c
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : isConst
+ ? BindingTypes.SETUP_CONST_REF
+ : BindingTypes.SETUP_LET
} else if (id.type === 'ObjectPattern') {
walkObjectPattern(id, bindings, isConst, isUseOptionsCall)
} else if (id.type === 'ArrayPattern') {
) {
// export function foo() {} / export class Foo {}
// export declarations must be named.
- bindings[node.id!.name] = BindingTypes.CONST
+ bindings[node.id!.name] = BindingTypes.SETUP_CONST
}
}
if (p.key === p.value) {
// const { x } = ...
bindings[p.key.name] = isUseOptionsCall
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : isConst
+ ? BindingTypes.SETUP_CONST_REF
+ : BindingTypes.SETUP_LET
} else {
walkPattern(p.value, bindings, isConst, isUseOptionsCall)
}
// ...rest
// argument can only be identifer when destructuring
bindings[(p.argument as Identifier).name] = isConst
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : BindingTypes.SETUP_LET
}
}
}
) {
if (node.type === 'Identifier') {
bindings[node.name] = isUseOptionsCall
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : isConst
+ ? BindingTypes.SETUP_CONST_REF
+ : BindingTypes.SETUP_LET
} else if (node.type === 'RestElement') {
// argument can only be identifer when destructuring
bindings[(node.argument as Identifier).name] = isConst
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : BindingTypes.SETUP_LET
} else if (node.type === 'ObjectPattern') {
walkObjectPattern(node, bindings, isConst)
} else if (node.type === 'ArrayPattern') {
} else if (node.type === 'AssignmentPattern') {
if (node.left.type === 'Identifier') {
bindings[node.left.name] = isUseOptionsCall
- ? BindingTypes.CONST
- : BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST
+ : isConst
+ ? BindingTypes.SETUP_CONST_REF
+ : BindingTypes.SETUP_LET
} else {
walkPattern(node.left, bindings, isConst)
}
for (const key of getObjectExpressionKeys(bodyItem.argument)) {
bindings[key] =
property.key.name === 'setup'
- ? BindingTypes.SETUP
+ ? BindingTypes.SETUP_CONST_REF
: BindingTypes.DATA
}
}