提交 9d30f60b 编写于 作者: fxy060608's avatar fxy060608

fix(mp): defineComponent #2166

上级 7c70802a
......@@ -46,6 +46,24 @@ function assertCodegenOptions (content, expectedOptions, isScoped = true) {
describe('mp:loader', () => {
it('parse scoped component', () => {
assertCodegen(
`import { uniBadge,uniCard} from '@dcloudio/uni-ui';
export default defineComponent({
components: {
'uni-badge':uniBadge,
'uni-card':uniCard
}
})`,
[{
name: 'uni-badge',
value: 'uniBadge',
source: '@dcloudio/uni-ui/lib/uni-badge/uni-badge'
}, {
name: 'uni-card',
value: 'uniCard',
source: '@dcloudio/uni-ui/lib/uni-card/uni-card'
}])
assertCodegen(
`
import mediaList from '@/components/tab-nvue/mediaList.vue';
......@@ -158,29 +176,27 @@ global['__wxVueOptions'] = {
'van-button': VanButton,
'van-search': VanSearch,
},exports.default.components || {})`,
[
{
name: 'van-button',
value: 'VanButton',
source: '../button/index.vue'
},
{
name: 'van-search',
value: 'VanSearch',
source: '../search/index.vue'
},
{
name: 'myButton',
value: 'myButton',
source: '@/components/my-button/my-button.vue'
}
[{
name: 'van-button',
value: 'VanButton',
source: '../button/index.vue'
},
{
name: 'van-search',
value: 'VanSearch',
source: '../search/index.vue'
},
{
name: 'myButton',
value: 'myButton',
source: '@/components/my-button/my-button.vue'
}
])
assertCodegenOptions(
`export default {
name: 'test'
}`,
{
}`, {
name: '"test"',
inheritAttrs: null,
props: null
......@@ -191,8 +207,7 @@ global['__wxVueOptions'] = {
`const options = {
name: 'test'
}
export default options`,
{
export default options`, {
name: '"test"',
inheritAttrs: null,
props: null
......@@ -204,8 +219,7 @@ global['__wxVueOptions'] = {
options = {
name: 'test'
}
export default options`,
{
export default options`, {
name: '"test"',
inheritAttrs: null,
props: null
......@@ -216,8 +230,7 @@ global['__wxVueOptions'] = {
`const options = Vue.extend({
name: 'test'
})
export default options`,
{
export default options`, {
name: '"test"',
inheritAttrs: null,
props: null
......@@ -229,8 +242,7 @@ global['__wxVueOptions'] = {
options = Vue.extend({
name: 'test'
})
export default options`,
{
export default options`, {
name: '"test"',
inheritAttrs: null,
props: null
......@@ -241,8 +253,7 @@ global['__wxVueOptions'] = {
`const options = {
name: 'test'
}
export default Vue.extend(options)`,
{
export default Vue.extend(options)`, {
name: '"test"',
inheritAttrs: null,
props: null
......@@ -252,8 +263,7 @@ global['__wxVueOptions'] = {
assertCodegenOptions(
`export default {
props: ['id', 'test']
}`,
{
}`, {
name: null,
inheritAttrs: null,
props: '["id","test"]'
......@@ -270,8 +280,7 @@ global['__wxVueOptions'] = {
type: String
}
}
}`,
{
}`, {
name: null,
inheritAttrs: null,
props: '["id","test"]'
......@@ -302,4 +311,4 @@ global['__wxVueOptions'] = {
source: '@/components/tab-nvue/mediaList.vue'
}], false)
})
})
})
......@@ -16,10 +16,15 @@ function handleObjectExpression (declaration, path, state) {
if (optionProperty) {
if (name === 'props') {
if (t.isArrayExpression(optionProperty.value)) {
state.options[name] = JSON.stringify(optionProperty.value.elements.filter(element => t.isStringLiteral(element)).map(({ value }) => value))
state.options[name] = JSON.stringify(optionProperty.value.elements.filter(element => t.isStringLiteral(
element)).map(({
value
}) => value))
} else if (t.isObjectExpression(optionProperty.value)) {
const props = []
optionProperty.value.properties.forEach(({ key }) => {
optionProperty.value.properties.forEach(({
key
}) => {
if (t.isIdentifier(key)) {
props.push(key.name)
} else if (t.isStringLiteral(key)) {
......@@ -60,7 +65,9 @@ function handleComponentsObjectExpression (componentsObjExpr, path, state, prepe
state.components = prepend ? components.concat(state.components) : components
}
function handleIdentifier ({ name }, path, state) {
function handleIdentifier ({
name
}, path, state) {
// 仅做有限查找
for (let i = path.container.length; i > 0; i--) {
const node = path.container[i - 1]
......@@ -101,6 +108,19 @@ module.exports = function (ast, state = {
options: {}
}) {
babelTraverse(ast, {
CallExpression (path) {
const callee = path.node.callee
const args = path.node.arguments
const objExpr = args[0]
if (
t.isIdentifier(callee) &&
callee.name === 'defineComponent' &&
args.length === 1 &&
t.isObjectExpression(objExpr)
) {
handleObjectExpression(objExpr, path, state)
}
},
AssignmentExpression (path) {
const leftExpression = path.node.left
const rightExpression = path.node.right
......@@ -162,4 +182,4 @@ module.exports = function (ast, state = {
ast,
state
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册