Skip to content

Commit

Permalink
fix(transformer): 自定义组件 else 需要加上 block 包裹住,close #1468
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Dec 11, 2018
1 parent 9eeedf7 commit 7e3a0b0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/taro-transformer-wx/src/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import generate from 'babel-generator'
import { NodePath } from 'babel-traverse'
import * as t from 'babel-types'
import { kebabCase } from 'lodash'
import { kebabCase, cloneDeep } from 'lodash'
import { DEFAULT_Component_SET, SPECIAL_COMPONENT_PROPS, swanSpecialAttrs } from './constant'
import { createHTMLElement } from './create-html-element'
import { codeFrameError, decodeUnicode } from './utils'
Expand Down Expand Up @@ -144,6 +144,22 @@ export function parseJSXElement (element: t.JSXElement): string {
const componentName = name.name
const isDefaultComponent = DEFAULT_Component_SET.has(componentName)
const componentSpecialProps = SPECIAL_COMPONENT_PROPS.get(componentName)
let hasElseAttr = false
attributes.forEach((a, index) => {
if (a.name.name === Adapter.else && !['block', 'Block'].includes(componentName) && !isDefaultComponent) {
hasElseAttr = true
attributes.splice(index, 1)
}
})
if (hasElseAttr) {
return createHTMLElement({
name: 'block',
attributes: {
[Adapter.else]: true
},
value: parseJSXChildren([element])
})
}
let attributesTrans = {}
if (attributes.length) {
attributesTrans = attributes.reduce((obj, attr) => {
Expand Down Expand Up @@ -212,6 +228,7 @@ export function parseJSXElement (element: t.JSXElement): string {
} else if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
attributesTrans[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
}

return createHTMLElement({
name: kebabCase(componentName),
attributes: attributesTrans,
Expand Down

0 comments on commit 7e3a0b0

Please sign in to comment.