Skip to content

Commit

Permalink
feat(transformer): 百度小程序某些属性需要用 = = 包裹住
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Dec 7, 2018
1 parent 3c3d57f commit 32627dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/taro-transformer-wx/src/constant.ts
Expand Up @@ -87,3 +87,11 @@ export const IMAGE_COMPONENTS = new Set<string>([
'Image',
'CoverImage'
])

export const swanSpecialAttrs = {
'ScrollView': ['scrollTop', 'scrollLeft', 'scrollIntoView'],
'Input': ['value'],
'Textarea': ['value'],
'MovableView': ['x', 'y'],
'Slider': ['value']
}
13 changes: 11 additions & 2 deletions packages/taro-transformer-wx/src/jsx.ts
Expand Up @@ -2,7 +2,7 @@ import generate from 'babel-generator'
import { NodePath } from 'babel-traverse'
import * as t from 'babel-types'
import { kebabCase } from 'lodash'
import { DEFAULT_Component_SET, SPECIAL_COMPONENT_PROPS } from './constant'
import { DEFAULT_Component_SET, SPECIAL_COMPONENT_PROPS, swanSpecialAttrs } from './constant'
import { createHTMLElement } from './create-html-element'
import { codeFrameError, decodeUnicode } from './utils'
import { Adapter, Adapters } from './adapter'
Expand Down Expand Up @@ -165,13 +165,22 @@ export function parseJSXElement (element: t.JSXElement): string {
} else if (t.isJSXExpressionContainer(attrValue)) {
let isBindEvent =
(name.startsWith('bind') && name !== 'bind') || (name.startsWith('catch') && name !== 'catch')
const code = decodeUnicode(generate(attrValue.expression, {
let code = decodeUnicode(generate(attrValue.expression, {
quotes: 'single',
concise: true
}).code)
.replace(/"/g, "'")
.replace(/(this\.props\.)|(this\.state\.)/g, '')
.replace(/this\./g, '')
if (
Adapters.swan === Adapter.type &&
code !== 'true' &&
code !== 'false' &&
swanSpecialAttrs[componentName] &&
swanSpecialAttrs[componentName].includes(name)
) {
code = `= ${code} =`
}
value = isBindEvent || isAlipayEvent ? code : `{{${code}}}`
if (Adapter.type === Adapters.swan && name === Adapter.for) {
value = code
Expand Down

0 comments on commit 32627dc

Please sign in to comment.