Skip to content

Commit

Permalink
fix(cli): 支持 convert 组件引用自身
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 6, 2018
1 parent 9fca026 commit 42f2a03
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions packages/taro-cli/src/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,86 @@ class Convertor {
parseAst ({ ast, sourceFilePath, outputFilePath, importStylePath, depComponents, imports = [], isApp = false }) {
const scriptFiles = new Set()
const self = this
let componentClassName = null
traverse(ast, {
Program: {
enter (astPath) {
astPath.traverse({
ClassDeclaration (astPath) {
const node = astPath.node
let isTaroComponent = false
if (node.superClass) {
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
if (isTaroComponent) {
componentClassName = node.id.name
}
}
},

ClassExpression (astPath) {
const node = astPath.node
if (node.superClass) {
let isTaroComponent = false
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
if (isTaroComponent) {
if (node.id === null) {
const parentNode = astPath.parentPath.node
if (t.isVariableDeclarator(astPath.parentPath)) {
componentClassName = parentNode.id.name
}
} else {
componentClassName = node.id.name
}
}
}
},
ExportDefaultDeclaration (astPath) {
const node = astPath.node
const declaration = node.declaration
if (
declaration &&
(declaration.type === 'ClassDeclaration' || declaration.type === 'ClassExpression')
) {
const superClass = declaration.superClass
if (superClass) {
let isTaroComponent = false
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
if (isTaroComponent) {
componentClassName = declaration.id.name
}
}
}
},
ImportDeclaration (astPath) {
const node = astPath.node
const source = node.source
Expand Down Expand Up @@ -175,6 +251,9 @@ class Convertor {
if (imports && imports.length) {
imports.forEach(({ name, ast }) => {
const importName = pascalCase(name)
if (componentClassName === importName) {
return
}
const importPath = path.join(self.importsDir, importName + '.js')
if (!self.hadBeenBuiltImports.has(importPath)) {
self.hadBeenBuiltImports.add(importPath)
Expand Down

0 comments on commit 42f2a03

Please sign in to comment.