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


TypeScript NodePath.findParent方法代碼示例

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


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

示例1: 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 logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => a.name.name === Adapter.if)) as NodePath<t.JSXElement>
    if (logicalJSX) {
      const attr = logicalJSX.node.openingElement.attributes.find(a => a.name.name === Adapter.if)
      if (attr && t.isJSXExpressionContainer(attr.value)) {
        expr = t.conditionalExpression(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(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(logicExpr.get('left').node as any, expr, array ? t.arrayExpression([]) : t.nullLiteral())
    }
  }
  return expr
}
開發者ID:topud,項目名稱:taro,代碼行數:27,代碼來源:utils.ts

示例2: findDeclarationScope

function findDeclarationScope (path: NodePath<t.Node>, id: t.Identifier) {
  const scopePath = path.findParent(p => !!p.scope.getOwnBindingIdentifier(id.name))
  if (scopePath) {
    return scopePath
  }
  throw codeFrameError(path.node, '該引用從未被定義')
}
開發者ID:topud,項目名稱:taro,代碼行數:7,代碼來源:index.ts

示例3: generateAnonymousState

export function generateAnonymousState (
  scope: Scope,
  expression: NodePath<t.Expression>,
  refIds: Set<t.Identifier>,
  isLogical?: boolean
) {
  let variableName = `anonymousState_${scope.generateUid()}`
  let statementParent = expression.getStatementParent()
  if (!statementParent) {
    throw codeFrameError(expression.node.loc, '無法生成匿名 State,嘗試先把值賦到一個變量上再把變量調換。')
  }
  const jsx = isLogical ? expression : expression.findParent(p => p.isJSXElement())
  const callExpr = jsx.findParent(p => p.isCallExpression() && isArrayMapCallExpression(p)) as NodePath<t.CallExpression>
  const ifExpr = jsx.findParent(p => p.isIfStatement())
  const blockStatement = jsx.findParent(p => p.isBlockStatement() && p.parentPath === ifExpr) as NodePath<t.BlockStatement>
  const expr = setParentCondition(jsx, cloneDeep(expression.node))
  if (!callExpr) {
    refIds.add(t.identifier(variableName))
    statementParent.insertBefore(
      buildConstVariableDeclaration(variableName, expr)
    )
    if (blockStatement && blockStatement.isBlockStatement()) {
      blockStatement.traverse({
        VariableDeclarator: (p) => {
          const { id, init } = p.node
          if (t.isIdentifier(id)) {
            const newId = scope.generateDeclaredUidIdentifier('$' + id.name)
            refIds.forEach((refId) => {
              if (refId.name === variableName && !variableName.startsWith('_$')) {
                refIds.delete(refId)
              }
            })
            variableName = newId.name
            refIds.add(t.identifier(variableName))
            blockStatement.scope.rename(id.name, newId.name)
            p.parentPath.replaceWith(
              template('ID = INIT;')({ ID: newId, INIT: init })
            )
          }
        }
      })
    }
  } else {
    variableName = `${LOOP_STATE}_${callExpr.scope.generateUid()}`
    const func = callExpr.node.arguments[0]
    if (t.isArrowFunctionExpression(func)) {
      if (!t.isBlockStatement(func.body)) {
        func.body = t.blockStatement([
          buildConstVariableDeclaration(variableName, expr),
          t.returnStatement(func.body)
        ])
      } else {
        func.body.body.splice(func.body.body.length - 1, 0, buildConstVariableDeclaration(variableName, expr))
      }
    }
  }
  const id = t.identifier(variableName)
  expression.replaceWith(id)
  return id
}
開發者ID:topud,項目名稱:taro,代碼行數:60,代碼來源:utils.ts

示例4: 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

示例5: handleClosureJSXFunc

function handleClosureJSXFunc (jsx: NodePath<t.JSXElement>, mainClass: NodePath<t.ClassDeclaration>) {
  // 在 ./functional.ts 會把 FunctionExpression 轉化為 arrowFunctionExpr
  // 所以我們這裏隻處理一種情況
  const arrowFunc = jsx.findParent(p => p.isArrowFunctionExpression())
  if (arrowFunc && arrowFunc.isArrowFunctionExpression()) {
    const parentPath = arrowFunc.parentPath
    if (parentPath.isVariableDeclarator()) {
      const id = parentPath.node.id
      if (t.isIdentifier(id) && id.name.startsWith('render')) {
        const funcName = `renderClosure${id.name.slice(6, id.name.length)}`
        mainClass.node.body.body.push(
          t.classProperty(
            t.identifier(funcName),
            cloneDeep(arrowFunc.node)
          )
        )
        parentPath.scope.rename(id.name, funcName)
        arrowFunc.replaceWith(t.memberExpression(
          t.thisExpression(),
          t.identifier(funcName)
        ))
      }
    }
  }
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:25,代碼來源:index.ts

示例6: replaceJSXTextWithTextComponent

export function replaceJSXTextWithTextComponent (path: NodePath<t.JSXText | t.JSXExpressionContainer>) {
  const parent = path.findParent(p => p.isJSXElement())
  if (parent && parent.isJSXElement() && t.isJSXIdentifier(parent.node.openingElement.name) && parent.node.openingElement.name.name !== 'Text') {
    path.replaceWith(t.jSXElement(
      t.jSXOpeningElement(t.jSXIdentifier('Text'), []),
      t.jSXClosingElement(t.jSXIdentifier('Text')),
      [path.isJSXText() ? t.jSXText(path.node.value) : path.node]
    ))
  }
}
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:10,代碼來源:utils.ts

示例7: 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

示例8: generateAnonymousState

export function generateAnonymousState (
  scope: Scope,
  expression: NodePath<t.Expression>,
  refIds: Set<t.Identifier>,
  isLogical?: boolean
) {
  let variableName = `anonymousState_${scope.generateUid()}`
  let statementParent = expression.getStatementParent()
  if (!statementParent) {
    throw codeFrameError(expression.node.loc, '無法生成匿名 State,嘗試先把值賦到一個變量上再把變量調換。')
  }
  const jsx = isLogical ? expression : expression.findParent(p => p.isJSXElement())
  const callExpr = jsx.findParent(p => p.isCallExpression() && isArrayMapCallExpression(p)) as NodePath<t.CallExpression>
  const conditionExpr = jsx.findParent(p => p.isConditionalExpression())
  const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }))
  let expr = cloneDeep(expression.node)
  if (conditionExpr && conditionExpr.isConditionalExpression()) {
    const consequent = conditionExpr.get('consequent')
    if (consequent === jsx || jsx.findParent(p => p === consequent)) {
      expr = t.conditionalExpression(conditionExpr.get('test').node as any, expr, t.nullLiteral())
    }
  }
  if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
    const consequent = logicExpr.get('right')
    if (consequent === jsx || jsx.findParent(p => p === consequent)) {
      expr = t.conditionalExpression(logicExpr.get('left').node as any, expr, t.nullLiteral())
    }
  }
  if (!callExpr) {
    refIds.add(t.identifier(variableName))
    statementParent.insertBefore(
      buildConstVariableDeclaration(variableName, expr)
    )
  } else {
    variableName = `${LOOP_STATE}_${callExpr.scope.generateUid()}`
    const func = callExpr.node.arguments[0]
    if (t.isArrowFunctionExpression(func)) {
      if (!t.isBlockStatement(func.body)) {
        func.body = t.blockStatement([
          buildConstVariableDeclaration(variableName, expr),
          t.returnStatement(func.body)
        ])
      } else {
        statementParent.insertBefore(
          buildConstVariableDeclaration(variableName, expr)
        )
      }
    }
  }
  expression.replaceWith(
    t.identifier(variableName)
  )
}
開發者ID:ApolloRobot,項目名稱:taro,代碼行數:53,代碼來源:utils.ts

示例9: cloneDeep

 const jsxAttrVisitor = (path: NodePath<t.JSXAttribute>) => {
   const name = path.node.name as t.JSXIdentifier
   const jsx = path.findParent(p => p.isJSXElement()) as NodePath<
     t.JSXElement
   >
   const valueCopy = cloneDeep(path.get('value').node)
   transformIf(name.name, path, jsx, valueCopy)
   const loopItem = transformLoop(name.name, path, jsx, valueCopy)
   if (loopItem) {
     if (loopItem.index) {
       loopIds.add(loopItem.index)
     }
     if (loopItem.item) {
       loopIds.add(loopItem.item)
     }
   }
 }
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:17,代碼來源:wxml.ts

示例10: hasComplexExpression

export function hasComplexExpression (path: NodePath<t.Node>) {
  let matched = false
  if (isContainJSXElement(path)) {
    return false
  }
  if (path.isTemplateLiteral() || path.isCallExpression()) {
    return true
  }
  path.traverse({
    CallExpression: (p) => {
      matched = true
      p.stop()
    },
    TemplateLiteral (p) {
      matched = true
      p.stop()
    },
    TaggedTemplateExpression (p) {
      matched = true
      p.stop()
    },
    MemberExpression (path) {
      const jsxElement = path.findParent(p => p.isJSXExpressionContainer())
      const object = path.get('object')
      const property = path.get('property')
      const parentPath = path.parentPath
      if (
        jsxElement &&
        object.isThisExpression() &&
        property.isIdentifier({ name: 'state' }) &&
        parentPath.isMemberExpression() &&
        parentPath.parentPath.isMemberExpression()
      ) {
        const sourceCode = parentPath.parentPath.getSource()
        if (sourceCode.includes('[') && sourceCode.includes(']')) {
          matched = true
          path.stop()
        }
      }
    }
  })
  return matched
}
開發者ID:ApolloRobot,項目名稱:taro,代碼行數:43,代碼來源:utils.ts


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