Skip to content

Commit

Permalink
fix(alita-core): 修复 isChildComp 方法只通过基本组件名称来判断是否需要处理成抽象节点导致的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
canfoo committed Apr 3, 2020
1 parent 575386f commit c6748b8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/alita-core/src/tran/addTempName.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import * as t from "@babel/types"
import {InnerTemplateNamePrefix} from "../constants";
import {isJSXChild, isChildCompChild} from "../util/uast";
import {geneOrder} from '../util/util'
export default function addTempName (ast) {
export default function addTempName (ast, info) {
const go = geneOrder()
errorLogTraverse(ast, {
exit: path => {
if (path.type === 'JSXElement'
&& (!isJSXChild(path) || isChildCompChild(path))
&& (!isJSXChild(path) || isChildCompChild(path, info.filepath))
) {
const jsxOp = path.node.openingElement

Expand Down
2 changes: 1 addition & 1 deletion packages/alita-core/src/tran/childrenToTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default function childrenToTemplate(ast, info) {
exit: path => {

if (path.type === 'JSXElement'
&& !isChildComp(path.node.openingElement.name.name)
&& !isChildComp(path.node.openingElement.name.name, info.filepath)
) {
const children = path.node.children
let tempName = null//`${ChildTemplateNamePrefix}${goForCTNP.next}`
Expand Down
2 changes: 1 addition & 1 deletion packages/alita-core/src/tran/cptCompHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export default function cptCompHandler (ast, info) {
}

if (path.type === 'JSXElement'
&& isChildComp(path.node.openingElement.name.name)
&& isChildComp(path.node.openingElement.name.name, info.filepath)
&& path.node.children.length > 0
) {
const pe = path.node.openingElement
Expand Down
4 changes: 2 additions & 2 deletions packages/alita-core/src/tran/geneAllTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function(ast, info) {


if (path.type === 'JSXElement'
&& isChildComp(path.node.openingElement.name.name)
&& isChildComp(path.node.openingElement.name.name, info.filepath)
) {
path.node.children = []
}
Expand All @@ -156,7 +156,7 @@ export default function(ast, info) {
* 若A是自定义组件,需要将其children转化为 generic:抽象节点的形式,传递出去,也需要提取为 tempName
*/
if (path.type === 'JSXElement'
&& (!isJSXChild(path) || isChildCompChild(path))
&& (!isJSXChild(path) || isChildCompChild(path, info.filepath))
) {
const jsxOp = path.node.openingElement
let tempName = ''
Expand Down
12 changes: 8 additions & 4 deletions packages/alita-core/src/util/uast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as t from "@babel/types"

import {allBaseComp, extChildComp} from './getAndStorecompInfos'
import {wxBaseComp} from '../constants'
import {getModuleInfo} from './cacheModuleInfos'
import {judgeLibPath} from './util'

import configure from '../configure'

Expand Down Expand Up @@ -154,16 +156,18 @@ export function isJSXChild(path) {
* @param name
* @returns {boolean}
*/
export function isChildComp(name) {
export function isChildComp(name, filepath) {
if (wxBaseComp.has(name) || configure.configObj.miniprogramComponents[name]) return false

// 基本组件children 需要转化为childrencpt的组件
if (extChildComp.has(name)) {
return true
}

const {im} = getModuleInfo(filepath)
// 基本组件children 不需要转化为childrencpt的组件
if (allBaseComp.has(name)) {
// 通过组件名称判断还不够,还需要判断来源是否是组件库里
if (allBaseComp.has(name) && judgeLibPath(im[name].source)) {
return false
}

Expand All @@ -176,15 +180,15 @@ export function isChildComp(name) {
* @param path
* @returns {any}
*/
export function isChildCompChild(path) {
export function isChildCompChild(path, filepath) {
const jc = isJSXChild(path)
if (!jc) return false

const parentElement = path.parentPath
const name = parentElement.node.openingElement.name.name


return isChildComp(name)
return isChildComp(name, filepath)
}

/**
Expand Down

0 comments on commit c6748b8

Please sign in to comment.