Beispiel #1
0
 attributes: attributes.reduce((obj, attr) => {
   if (t.isJSXSpreadAttribute(attr)) {
     throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
   }
   const name = attr.name.name === 'className' ? 'class' : attr.name.name
   let value: string | boolean = true
   let attrValue = attr.value
   if (typeof name === 'string') {
     if (t.isStringLiteral(attrValue)) {
       value = attrValue.value
     } else if (t.isJSXExpressionContainer(attrValue)) {
       const isBindEvent =
         name.startsWith('bind') || name.startsWith('catch')
       let { code } = generate(attrValue.expression)
       code = code
         .replace(/(this\.props\.)|(this\.state\.)/, '')
         .replace(/this\./, '')
       value = isBindEvent ? code : `{{${code}}}`
       if (t.isStringLiteral(attrValue.expression)) {
         value = attrValue.expression.value
       }
     }
     if (
       componentSpecialProps &&
       componentSpecialProps.has(name)
     ) {
       obj[name] = value
     } else {
       obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
     }
   }
   return obj
 }, {}),
Beispiel #2
0
Datei: jsx.ts Projekt: topud/taro
 attributesTrans = attributes.reduce((obj, attr) => {
   if (t.isJSXSpreadAttribute(attr)) {
     throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
   }
   let name = attr.name.name
   if (DEFAULT_Component_SET.has(componentName)) {
     if (name === 'className') {
       name = 'class'
     }
   }
   let value: string | boolean = true
   let attrValue = attr.value
   if (typeof name === 'string') {
     const isAlipayEvent = Adapter.type === Adapters.alipay && /(^on[A-Z_])|(^catch[A-Z_])/.test(name)
     if (t.isStringLiteral(attrValue)) {
       value = attrValue.value
     } else if (t.isJSXExpressionContainer(attrValue)) {
       let isBindEvent =
         (name.startsWith('bind') && name !== 'bind') || (name.startsWith('catch') && name !== 'catch')
       const code = decodeUnicode(generate(attrValue.expression, {
           quotes: 'single',
           concise: true
         }).code)
         .replace(/"/g, "'")
         .replace(/(this\.props\.)|(this\.state\.)/g, '')
         .replace(/this\./g, '')
       value = isBindEvent || isAlipayEvent ? code : `{{${code}}}`
       if (Adapter.type === Adapters.swan && name === Adapter.for) {
         value = code
       }
       if (t.isStringLiteral(attrValue.expression)) {
         value = attrValue.expression.value
       }
     } else if (attrValue === null && name !== Adapter.else) {
       value = `{{true}}`
     }
     if ((componentName === 'Input' || componentName === 'input') && name === 'maxLength') {
       obj['maxlength'] = value
     } else if (
       componentSpecialProps && componentSpecialProps.has(name) ||
       name.startsWith('__fn_') ||
       isAlipayEvent
     ) {
       obj[name] = value
     } else {
       obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
     }
   }
   if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
     obj[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
   }
   return obj
 }, {})
Beispiel #3
0
 attributesTrans = attributes.reduce((obj, attr) => {
   if (t.isJSXSpreadAttribute(attr)) {
     throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
   }
   let name = attr.name.name
   if (DEFAULT_Component_SET.has(componentName)) {
     if (name === 'className') {
       name = 'class'
     }
   }
   let value: string | boolean = true
   let attrValue = attr.value
   if (typeof name === 'string') {
     const isAlipayEvent = Adapter.type === Adapters.alipay && /(^on[A-Z_])|(^catch[A-Z_])/.test(name)
     if (t.isStringLiteral(attrValue)) {
       value = attrValue.value
     } else if (t.isJSXExpressionContainer(attrValue)) {
       let isBindEvent =
         (name.startsWith('bind') && name !== 'bind') || (name.startsWith('catch') && name !== 'catch')
       let code = decodeUnicode(generate(attrValue.expression, {
           quotes: 'single',
           concise: true
         }).code)
         .replace(/"/g, "'")
         .replace(/(this\.props\.)|(this\.data\.)/g, '')
         .replace(/this\./g, '')
       if (
         Adapters.swan === Adapter.type &&
         code !== 'true' &&
         code !== 'false' &&
         swanSpecialAttrs[componentName] &&
         swanSpecialAttrs[componentName].includes(name)
       ) {
         value = `{= ${code} =}`
       } else {
         if (Adapter.key === name) {
           const splitCode = code.split('.')
           if (splitCode.length > 1) {
             value = splitCode.slice(1).join('.')
           } else {
             value = code
           }
         } else {
           value = isBindEvent || isAlipayEvent ? code : `{{${code}}}`
         }
       }
       if (Adapter.type === Adapters.swan && name === Adapter.for) {
         value = code
       }
       if (t.isStringLiteral(attrValue.expression)) {
         value = attrValue.expression.value
       }
     } else if (attrValue === null && name !== Adapter.else) {
       value = `{{true}}`
     }
     if (THIRD_PARTY_COMPONENTS.has(componentName) && /^bind/.test(name) && name.includes('-')) {
       name = name.replace(/^bind/, 'bind:')
     }
     if (componentTransfromProps && componentTransfromProps[componentName]) {
       const transfromProps = componentTransfromProps[componentName]
       Object.keys(transfromProps).forEach(oriName => {
         if (transfromProps.hasOwnProperty(name as string)) {
           name = transfromProps[oriName]
         }
       })
     }
     if ((componentName === 'Input' || componentName === 'input') && name === 'maxLength') {
       obj['maxlength'] = value
     } else if (
       componentSpecialProps && componentSpecialProps.has(name) ||
       name.startsWith('__fn_') ||
       isAlipayEvent
     ) {
       obj[name] = value
     } else {
       obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
     }
   }
   if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
     //@fix
     //obj[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
   }
   return obj
 }, {})