當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript babel-types.isJSXMemberExpression函數代碼示例

本文整理匯總了TypeScript中babel-types.isJSXMemberExpression函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isJSXMemberExpression函數的具體用法?TypeScript isJSXMemberExpression怎麽用?TypeScript isJSXMemberExpression使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了isJSXMemberExpression函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: parseJSXElement

export function parseJSXElement (element: t.JSXElement): string {
  const children = element.children
  const { attributes, name } = element.openingElement
  if (t.isJSXMemberExpression(name)) {
    throw codeFrameError(name.loc, '暫不支持 JSX 成員表達式')
  }
  const isDefaultComponent = DEFAULT_Component_SET.has(name.name)
  const componentSpecialProps = SPECIAL_COMPONENT_PROPS.get(name.name)
  return createHTMLElement({
    name: kebabCase(name.name),
    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\.)/g, '')
            .replace(/this\./g, '')
          value = isBindEvent ? code : `{{${code}}}`
          if (t.isStringLiteral(attrValue.expression)) {
            value = attrValue.expression.value
          }
        } else if (attrValue === null && name !== 'wx:else') {
          value = `{{true}}`
        }
        if (
          componentSpecialProps &&
          componentSpecialProps.has(name)
        ) {
          obj[name] = value
        } else {
          obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
        }
      }
      return obj
    }, {}),
    value: parseJSXChildren(children)
  })
}
開發者ID:ApolloRobot,項目名稱:taro,代碼行數:48,代碼來源:jsx.ts

示例2: parseJSXElement

export function parseJSXElement (element: t.JSXElement): string {
  const children = element.children
  const { attributes, name } = element.openingElement
  //@fix
  //const TRIGGER_OBSERER = Adapter.type === Adapters.swan ? 'privateTriggerObserer' : '__triggerObserer'
  if (t.isJSXMemberExpression(name)) {
    throw codeFrameError(name.loc, '暫不支持 JSX 成員表達式')
  }
  const componentName = name.name
  const isDefaultComponent = DEFAULT_Component_SET.has(componentName)
  const componentSpecialProps = SPECIAL_COMPONENT_PROPS.get(componentName)
  const componentTransfromProps = TRANSFORM_COMPONENT_PROPS.get(Adapter.type)
  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) => {
      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) ||
//.........這裏部分代碼省略.........
開發者ID:AlloyTeam,項目名稱:Nuclear,代碼行數:101,代碼來源:jsx.ts

示例3: parseJSXElement

export function parseJSXElement (element: t.JSXElement): string {
  const children = element.children
  const { attributes, name } = element.openingElement
  const TRIGGER_OBSERER = Adapter.type === Adapters.swan ? 'privateTriggerObserer' : '__triggerObserer'
  if (t.isJSXMemberExpression(name)) {
    throw codeFrameError(name.loc, '暫不支持 JSX 成員表達式')
  }
  const componentName = name.name
  const isDefaultComponent = DEFAULT_Component_SET.has(componentName)
  const componentSpecialProps = SPECIAL_COMPONENT_PROPS.get(componentName)
  let attributesTrans = {}
  if (attributes.length) {
    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
    }, {})
  } else if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
    attributesTrans[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
  }
  return createHTMLElement({
    name: kebabCase(componentName),
    attributes: attributesTrans,
    value: parseJSXChildren(children)
  })
}
開發者ID:topud,項目名稱:taro,代碼行數:74,代碼來源:jsx.ts


注:本文中的babel-types.isJSXMemberExpression函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。