当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript babel-types.isArrowFunctionExpression函数代码示例

本文整理汇总了TypeScript中babel-types.isArrowFunctionExpression函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isArrowFunctionExpression函数的具体用法?TypeScript isArrowFunctionExpression怎么用?TypeScript isArrowFunctionExpression使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了isArrowFunctionExpression函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

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

示例2: resetTSClassProperty

export function resetTSClassProperty (body) {
  for (const method of body) {
    if (t.isClassMethod(method) && method.kind === 'constructor') {
      for (const statement of cloneDeep(method.body.body)) {
        if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression)) {
          const expr = statement.expression
          const { left, right } = expr
          if (
            t.isMemberExpression(left) &&
              t.isThisExpression(left.object) &&
              t.isIdentifier(left.property)
          ) {
            if (
              (t.isArrowFunctionExpression(right) || t.isFunctionExpression(right)) ||
                (left.property.name === 'config' && t.isObjectExpression(right))
            ) {
              body.push(
                t.classProperty(left.property, right)
              )
              remove(method.body.body, statement)
            }
          }
        }
      }
    }
  }
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:27,代码来源:helper.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 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

示例4: findParentLoops

export function findParentLoops (
  callee: NodePath<t.CallExpression>,
  names: Map<NodePath<t.CallExpression>, string>,
  loops: t.ArrayExpression
) {
  let indexId: t.Identifier | null = null
  let name: string | undefined
  const [ func ] = callee.node.arguments
  if (t.isFunctionExpression(func) || t.isArrowFunctionExpression(func)) {
    const params = func.params as t.Identifier[]
    indexId = params[1]
    name = names.get(callee)
  }

  if (indexId === null || !t.isIdentifier(indexId)) {
    indexId = t.identifier(callee.scope.generateUid('anonIdx'));
    (func as any).params = [(func as any).params[0], indexId]
  }

  if (!name) {
    throw codeFrameError(callee.node, '找不到循环对应的名称')
  }

  loops.elements.unshift(t.objectExpression([
    t.objectProperty(t.identifier('indexId'), indexId),
    t.objectProperty(t.identifier('name'), t.stringLiteral(name))
  ]))

  const parentCallExpr = callee.findParent(p => p.isCallExpression())
  if (parentCallExpr && parentCallExpr.isCallExpression()) {
    const callee = parentCallExpr.node.callee
    if (
      t.isMemberExpression(callee) &&
      t.isIdentifier(callee.property) &&
      callee.property.name === 'map'
    ) {
      findParentLoops(parentCallExpr, names, loops)
    }
  }
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:40,代码来源:utils.ts

示例5: handleThirdPartyComponent

 method.body.body = method.body.body.filter(statement => {
   if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression)) {
     const expr = statement.expression
     const { left, right } = expr
     if (
       t.isMemberExpression(left) &&
       t.isThisExpression(left.object) &&
       t.isIdentifier(left.property)
     ) {
       if (
         (t.isArrowFunctionExpression(right) || t.isFunctionExpression(right))
         ||
         (left.property.name === 'config' && t.isObjectExpression(right))
       ) {
         const classProp = t.classProperty(left.property, right)
         body.push(classProp)
         handleThirdPartyComponent(classProp)
         return false
       }
     }
   }
   return true
 })
开发者ID:topud,项目名称:taro,代码行数:23,代码来源:index.ts

示例6: parseLoopBody


//.........这里部分代码省略.........
              const callee = caller.node.callee
              if (
                t.isMemberExpression(callee) &&
                t.isIdentifier(callee.property) &&
                callee.property.name === 'map'
              ) {
                let ary = callee.object
                const blockStatementPath = parentPath.findParent(p => p.isBlockStatement()) as NodePath<t.BlockStatement>
                const body = blockStatementPath.node.body
                let stateToBeAssign = new Set<string>()
                for (const statement of body) {
                  if (t.isVariableDeclaration(statement)) {
                    for (const dcl of statement.declarations) {
                      if (t.isIdentifier(dcl.id)) {
                        const scope = blockStatementPath.scope
                        const stateName = scope.generateUid(LOOP_STATE)
                        stateToBeAssign.add(stateName)
                        blockStatementPath.scope.rename(dcl.id.name, stateName)
                      }
                    }
                  }
                }
                if (t.isCallExpression(ary) || isContainFunction(caller.get('callee').get('object'))) {
                  const variableName = `anonymousState_${bodyScope.generateUid()}`
                  caller.getStatementParent().insertBefore(
                    buildConstVariableDeclaration(variableName, ary)
                  )
                  ary = t.identifier(variableName)
                }
                setJSXAttr(jsxElementPath.node, Adapter.for, t.jSXExpressionContainer(ary))
                const [func] = caller.node.arguments
                if (
                  t.isFunctionExpression(func) ||
                  t.isArrowFunctionExpression(func)
                ) {
                  const [item, index] = func.params
                  if (t.isIdentifier(item)) {
                    setJSXAttr(
                      jsxElementPath.node,
                      Adapter.forItem,
                      t.stringLiteral(item.name)
                    )
                    loopScopes.add(item.name)
                  } else {
                    setJSXAttr(
                      jsxElementPath.node,
                      Adapter.forItem,
                      t.stringLiteral('__item')
                    )
                  }
                  if (t.isIdentifier(index)) {
                    setJSXAttr(
                      jsxElementPath.node,
                      Adapter.forIndex,
                      t.stringLiteral(index.name)
                    )
                    loopScopes.add(index.name)
                  }
                  caller.replaceWith(jsxElementPath.node)
                  if (statementParent) {
                    const name = findIdentifierFromStatement(
                      statementParent.node as t.VariableDeclaration
                    )
                    // setTemplate(name, path, templates)
                    name && templates.set(name, jsxElementPath.node)
                  }
开发者ID:AlloyTeam,项目名称:Nuclear,代码行数:67,代码来源:loop-component.ts

示例7: codeFrameError

    classBody = properties.map(prop => {
      const key = prop.get('key')
      const value = prop.get('value')
      let params = prop.isObjectMethod()
        ? prop.node.params
        : value.isFunctionExpression() || value.isArrowFunctionExpression()
          ? value.node.params
          : []
      const isAsync = prop.isObjectMethod()
        ? prop.node.async
        : value.isFunctionExpression() || value.isArrowFunctionExpression()
          ? value.node.async
          : false
      if (!key.isIdentifier()) {
        throw codeFrameError(key.node, 'Page 对象的键值只能是字符串')
      }
      const name = key.node.name
      const currentStateKeys: string[] = []
      if (name === 'data') {
        if (value.isObjectExpression()) {
          value
            .get('properties')
            .map(p => p.node)
            .forEach(prop => {
              if (t.isObjectProperty(prop)) {
                let propKey = ''
                if (t.isStringLiteral(prop.key)) {
                  propKey = prop.key.value
                }
                if (t.isIdentifier(prop.key)) {
                  propKey = prop.key.name
                }

                if (!isValidVarName(propKey)) {
                  throw codeFrameError(prop, `${propKey} 不是一个合法的 JavaScript 变量名`)
                }

                if (propKey) {
                  currentStateKeys.push(propKey)
                }
              }
            })
        }
        return t.classProperty(t.identifier('state'), value.node)
      }
      if (name === 'properties') {
        const observeProps: { name: string, observer: any }[] = []
        if (value.isObjectExpression()) {
          value
            .get('properties')
            .map(p => p.node)
            .forEach(prop => {
              if (t.isObjectProperty(prop)) {
                let propKey: string | null = null
                if (t.isStringLiteral(prop.key)) {
                  propKey = prop.key.value
                }
                if (t.isIdentifier(prop.key)) {
                  propKey = prop.key.name
                  // propsKeys.push(prop.key.name)
                }
                if (t.isObjectExpression(prop.value) && propKey) {
                  for (const p of prop.value.properties) {
                    if (t.isObjectProperty(p)) {
                      let key: string | null = null
                      if (t.isStringLiteral(p.key)) {
                        key = p.key.value
                      }
                      if (t.isIdentifier(p.key)) {
                        key = p.key.name
                      }
                      if (key === 'value') {
                        defaultProps.push({
                          name: propKey,
                          value: p.value
                        })
                      } else if (key === 'observer') {
                        observeProps.push({
                          name: propKey,
                          observer: p.value
                        })
                      }
                      if (!isValidVarName(propKey)) {
                        throw codeFrameError(prop, `${propKey} 不是一个合法的 JavaScript 变量名`)
                      }
                    }
                    if (t.isObjectMethod(p) && t.isIdentifier(p.key, { name: 'observer' })) {
                      observeProps.push({
                        name: propKey,
                        observer: t.arrowFunctionExpression(p.params, p.body, p.async)
                      })
                    }
                  }
                }
                if (propKey) {
                  propsKeys.push(propKey)
                }
              }
            })
        }
//.........这里部分代码省略.........
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:script.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 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: (path) => {
          const { id, init } = path.node
          const isArrowFunctionInJSX = path.findParent(p => p.isJSXAttribute() ||
            (
              p.isAssignmentExpression() && t.isMemberExpression(p.node.left) && t.isThisExpression(p.node.left.object)
                && t.isIdentifier(p.node.left.property) && p.node.left.property.name.startsWith('')
            )
          )
          if (isArrowFunctionInJSX) {
            return
          }
          if (t.isIdentifier(id) && !id.name.startsWith(LOOP_STATE)) {
            const newId = scope.generateDeclaredUidIdentifier('$' + id.name)
            refIds.forEach((refId) => {
              if (refId.name === variableName && !variableName.startsWith('_$')) {
                refIds.delete(refId)
              }
            })
            variableName = newId.name
            if (Adapter.type === Adapters.quickapp && variableName.startsWith('_$')) {
              const newVarName = variableName.slice(2)
              scope.rename(variableName, newVarName)
              variableName = newVarName
            }
            refIds.add(t.identifier(variableName))
            blockStatement.scope.rename(id.name, newId.name)
            path.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 {
        if (ifExpr && ifExpr.isIfStatement() && ifExpr.findParent(p => p === callExpr)) {
          const consequent = ifExpr.get('consequent')
          const test = ifExpr.get('test')
          if (consequent.isBlockStatement()) {
            if (jsx === test || jsx.findParent(p => p === test)) {
              func.body.body.unshift(buildConstVariableDeclaration(variableName, expr))
            } else {
              func.body.body.unshift(t.variableDeclaration('let', [t.variableDeclarator(t.identifier(variableName), t.nullLiteral())]))
              consequent.node.body.push(t.expressionStatement(t.assignmentExpression(
                '=',
                t.identifier(variableName),
                expr
              )))
            }
          } else {
            throw codeFrameError(consequent.node, 'if 表达式的结果必须由一个花括号包裹')
          }
        } 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:YangShaoQun,项目名称:taro,代码行数:93,代码来源:utils.ts


注:本文中的babel-types.isArrowFunctionExpression函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。