提交 7459c1f1 编写于 作者: Q qiang

fix: 解决部分组件写法导致编译报错的问题 question/101132

上级 a890781d
......@@ -187,6 +187,68 @@ global['__wxVueOptions'] = {
}
)
assertCodegenOptions(
`const options = {
name: 'test'
}
export default options`,
{
name: '"test"',
inheritAttrs: null,
props: null
}
)
assertCodegenOptions(
`let options
options = {
name: 'test'
}
export default options`,
{
name: '"test"',
inheritAttrs: null,
props: null
}
)
assertCodegenOptions(
`const options = Vue.extend({
name: 'test'
})
export default options`,
{
name: '"test"',
inheritAttrs: null,
props: null
}
)
assertCodegenOptions(
`let options
options = Vue.extend({
name: 'test'
})
export default options`,
{
name: '"test"',
inheritAttrs: null,
props: null
}
)
assertCodegenOptions(
`const options = {
name: 'test'
}
export default Vue.extend(options)`,
{
name: '"test"',
inheritAttrs: null,
props: null
}
)
assertCodegenOptions(
`export default {
props: ['id', 'test']
......
......@@ -60,6 +60,41 @@ function handleComponentsObjectExpression (componentsObjExpr, path, state, prepe
state.components = prepend ? components.concat(state.components) : components
}
function handleIdentifier ({ name }, path, state) {
// 仅做有限查找
for (let i = path.container.length; i > 0; i--) {
const node = path.container[i - 1]
let declarations = []
if (t.isExpressionStatement(node)) {
declarations = [node]
} else if (t.isVariableDeclaration(node)) {
declarations = node.declarations
}
for (let i = declarations.length; i > 0; i--) {
let declaration = declarations[i - 1]
let identifier
if (t.isVariableDeclarator(declaration)) {
identifier = declaration.id
declaration = declaration.init
} else if (t.isExpressionStatement(declaration)) {
identifier = declaration.expression.left
declaration = declaration.expression.right
}
if (identifier.name === name) {
if (t.isCallExpression(declaration) &&
t.isMemberExpression(declaration.callee) &&
declaration.arguments.length === 1) {
declaration = declaration.arguments[0]
}
if (t.isObjectExpression(declaration)) {
handleObjectExpression(declaration, path, state)
}
return
}
}
}
}
module.exports = function (ast, state = {
type: 'Component',
components: [],
......@@ -96,12 +131,19 @@ module.exports = function (ast, state = {
const declaration = path.node.declaration
if (t.isObjectExpression(declaration)) { // export default {components:{}}
handleObjectExpression(declaration, path, state)
} else if (t.isIdentifier(declaration)) {
handleIdentifier(declaration, path, state)
} else if (t.isCallExpression(declaration) &&
t.isMemberExpression(declaration.callee) &&
declaration.arguments.length === 1) { // export default Vue.extend({components:{}})
if (declaration.callee.object.name === 'Vue' && declaration.callee.property.name ===
'extend') {
handleObjectExpression(declaration.arguments[0], path, state)
const argument = declaration.arguments[0]
if (t.isObjectExpression(argument)) {
handleObjectExpression(argument, path, state)
} else if (t.isIdentifier(argument)) {
handleIdentifier(argument, path, state)
}
}
} else if (t.isClassDeclaration(declaration) &&
declaration.decorators &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册