Skip to content

Commit

Permalink
fix(transformer): 单独使用的自定义组件也当做循环自定义组件处理
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jul 3, 2018
1 parent c88033e commit 47bdc55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/taro-transformer-wx/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class Transformer {
const callee = rootCallExpression.node.callee
const calleeCode = generate(callee).code
let nodes: t.ObjectExpression[] = []
const uuid = createUUID()
loopComponents.forEach((loopComponent) => {
const { name, element: component, parent } = loopComponent
let subscript = ''
Expand All @@ -291,7 +292,7 @@ class Transformer {
const calleeCode = generate(callee).code
subscript = calleeCode.split('.').slice(1, calleeCode.split('.').length).join('')
}
const node = this.generateTopLoopNodes(name, component, anonymousPropsFunctionId, subscript.slice(0, subscript.length - 3), parent, iterator.name, index)
const node = this.generateTopLoopNodes(name, component, uuid, subscript.slice(0, subscript.length - 3), parent, iterator.name, index)
nodes.push(node)
})
let stateName = ''
Expand All @@ -304,7 +305,6 @@ class Transformer {
stateName = ary[1] === 'state' || ary[1] === 'props' || ary[1] === '__state' || ary[1] === '__props'
? ary[2] : ary[1]
}
const uuid = createUUID()
const stateNameDecl = buildConstVariableDeclaration('stateName', t.stringLiteral(stateName))
const returnStatement = t.returnStatement(
t.objectExpression([
Expand Down Expand Up @@ -394,7 +394,7 @@ class Transformer {
])
blockStatement.push(nodeDeclare)
blockStatement.push(returnStatement)
const node = this.generateTopLoopNodes(name, component, anonymousPropsFunctionId, '', null, undefined, undefined, false)
const node = this.generateTopLoopNodes(name, component, uuid, '', null, undefined, undefined, false)
nodes.push(node)
properties.push(
t.objectProperty(t.identifier(uuid), t.arrowFunctionExpression([], t.blockStatement(blockStatement)))
Expand All @@ -413,7 +413,7 @@ class Transformer {
generateTopLoopNodes (
name: string,
component: NodePath<t.JSXElement>,
uid: () => number,
uuid: string,
subscript: string,
parent: NodePath<t.CallExpression> | null,
iterator?: string,
Expand Down Expand Up @@ -460,7 +460,7 @@ class Transformer {
}
expresionPath.replaceWith(replacement)
} else if (isNeedClassMethodWrapper(name.name, expresion)) {
const id = `$$anonymousPropsFunction_${uid()}`
const id = `$$anonymousPropsFunction_${anonymousPropsFunctionId()}`
path.get('value').get('expression').replaceWith(
t.memberExpression(
t.thisExpression(),
Expand Down Expand Up @@ -525,7 +525,7 @@ class Transformer {
const argsFunction = t.objectMethod('method', t.identifier('args'), isLoop ? [t.identifier(MAP_CALL_ITERATOR), t.identifier('index')] : [],
t.blockStatement(
[
t.returnStatement(t.objectExpression(properties))
t.returnStatement(t.objectExpression(properties.concat([t.objectProperty(t.identifier('$path'), t.stringLiteral(uuid))])))
]
)
)
Expand Down Expand Up @@ -614,6 +614,20 @@ class Transformer {
'wx:for-item',
t.stringLiteral('item')
)
setJSXAttr(
jsx,
'data',
t.jSXExpressionContainer(
t.identifier('...item')
)
)
setJSXAttr(
jsx,
'wx:for',
t.jSXExpressionContainer(
t.identifier('$$' + name)
)
)
path.replaceWith(jsx)
this.customComponentNames.add('$$' + name)
})
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ export class RenderParser {
.filter(i => !this.templates.has(i))
.filter(i => !i.includes('.'))
.filter(i => i !== MAP_CALL_ITERATOR && !this.reserveStateWords.has(i))
.filter(i => !i.startsWith('.'))
.filter(i => !i.startsWith('$$'))
.map(i => t.objectProperty(t.identifier(i), t.identifier(i)))
if (this.customComponentData.length > 0) {
properties = properties.concat(this.customComponentData)
Expand Down

0 comments on commit 47bdc55

Please sign in to comment.