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


TypeScript NodePath.isJSXElement方法代碼示例

本文整理匯總了TypeScript中babel-traverse.NodePath.isJSXElement方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript NodePath.isJSXElement方法的具體用法?TypeScript NodePath.isJSXElement怎麽用?TypeScript NodePath.isJSXElement使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在babel-traverse.NodePath的用法示例。


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

示例1: setAncestorCondition

export function setAncestorCondition (jsx: NodePath<t.Node>, expr: t.Expression): t.Expression {
  const ifAttrSet = new Set<string>([
    Adapter.if,
    Adapter.else
  ])
  const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => ifAttrSet.has(a.name.name as string))) as NodePath<t.JSXElement>
  if (logicalJSX) {
    const attr = logicalJSX.node.openingElement.attributes.find(a => ifAttrSet.has(a.name.name as string))
    if (attr) {
      if (attr.name.name === Adapter.else) {
        const prevElement: NodePath<t.JSXElement | null> = (logicalJSX as any).getPrevSibling()
        if (prevElement && prevElement.isJSXElement()) {
          const attr = prevElement.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
          if (attr && t.isJSXExpressionContainer(attr.value)) {
            const condition = reverseBoolean(cloneDeep(attr.value.expression))
            expr = t.logicalExpression('&&', setAncestorCondition(logicalJSX, condition), expr)
          }
        }
      } else if (t.isJSXExpressionContainer(attr.value)) {
        const condition = cloneDeep(attr.value.expression)
        expr = t.logicalExpression('&&', setAncestorCondition(logicalJSX, condition), expr)
      }
    }
  }

  return expr
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:27,代碼來源:utils.ts

示例2: findWXIfProps

function findWXIfProps (
  jsx: NodePath<t.Node>
): { reg: RegExpMatchArray; tester: AttrValue } | null {
  let matches: { reg: RegExpMatchArray; tester: AttrValue } | null = null
  jsx &&
    jsx.isJSXElement() &&
    jsx
      .get('openingElement')
      .get('attributes')
      .some(path => {
        const attr = path.node
        if (t.isJSXIdentifier(attr.name)) {
          const name = attr.name.name
          if (name === WX_IF) {
            return true
          }
          const match = name.match(/wx:else|wx:elif/)
          if (match) {
            path.remove()
            matches = {
              reg: match,
              tester: attr.value
            }
            return true
          }
        }
        return false
      })

  return matches
}
開發者ID:topud,項目名稱:taro,代碼行數:31,代碼來源:wxml.ts

示例3: setParentCondition

export function setParentCondition (jsx: NodePath<t.Node>, expr: t.Expression, array = false) {
  const conditionExpr = jsx.findParent(p => p.isConditionalExpression())
  const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
  if (array) {
    const ifAttrSet = new Set<string>([
      Adapter.if,
      Adapter.else
    ])
    const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => ifAttrSet.has(a.name.name as string))) as NodePath<t.JSXElement>
    if (logicalJSX) {
      const attr = logicalJSX.node.openingElement.attributes.find(a => ifAttrSet.has(a.name.name as string))
      if (attr) {
        if (attr.name.name === Adapter.else) {
          const prevElement: NodePath<t.JSXElement | null> = (logicalJSX as any).getPrevSibling()
          if (prevElement && prevElement.isJSXElement()) {
            const attr = prevElement.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
            if (attr && t.isJSXExpressionContainer(attr.value)) {
              expr = t.conditionalExpression(reverseBoolean(cloneDeep(attr.value.expression)), expr, t.arrayExpression())
              return expr
            }
          }
        } else if (t.isJSXExpressionContainer(attr.value)) {
          expr = t.conditionalExpression(cloneDeep(attr.value.expression), expr, t.arrayExpression())
          return expr
        }
      }
    }
  }
  if (conditionExpr && conditionExpr.isConditionalExpression()) {
    const consequent = conditionExpr.get('consequent')
    if (consequent === jsx || jsx.findParent(p => p === consequent)) {
      expr = t.conditionalExpression(cloneDeep(conditionExpr.get('test').node) as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
    }
  }
  if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
    const consequent = logicExpr.get('right')
    if (consequent === jsx || jsx.findParent(p => p === consequent)) {
      expr = t.conditionalExpression(cloneDeep(logicExpr.get('left').node) as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
    }
  }
  return expr
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:42,代碼來源:utils.ts


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