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


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

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


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

示例1: transformLoop

function transformLoop (
  name: string,
  attr: NodePath<t.JSXAttribute>,
  jsx: NodePath<t.JSXElement>,
  value: AttrValue
) {
  if (name !== WX_FOR) {
    return
  }
  if (!value || !t.isJSXExpressionContainer(value)) {
    throw new Error('wx:for 的值必须使用 "{{}}"  包裹')
  }
  attr.remove()
  let item = t.stringLiteral('item')
  let index = t.stringLiteral('index')
  jsx
    .get('openingElement')
    .get('attributes')
    .forEach(p => {
      const node = p.node
      if (node.name.name === WX_FOR_ITEM) {
        if (!node.value || !t.isStringLiteral(node.value)) {
          throw new Error(WX_FOR_ITEM + ' 的值必须是一个字符串')
        }
        item = node.value
        p.remove()
      }
      if (node.name.name === WX_FOR_INDEX) {
        if (!node.value || !t.isStringLiteral(node.value)) {
          throw new Error(WX_FOR_INDEX + ' 的值必须是一个字符串')
        }
        index = node.value
        p.remove()
      }
      if (node.name.name === WX_KEY) {
        p.get('name').replaceWith(t.jSXIdentifier('key'))
      }
    })

  const replacement = t.jSXExpressionContainer(
    t.callExpression(
      t.memberExpression(value.expression, t.identifier('map')),
      [
        t.arrowFunctionExpression(
          [t.identifier(item.value), t.identifier(index.value)],
          t.blockStatement([t.returnStatement(jsx.node)])
        )
      ]
    )
  )

  const block = buildBlockElement()
  block.children = [replacement]
  jsx.replaceWith(block)
}
开发者ID:topud,项目名称:taro,代码行数:55,代码来源:wxml.ts

示例2: if

 .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,代码行数:48,代码来源:script.ts

示例3: _rewriteDynamicImport

 /**
  * Extends dynamic import statements to extract the explicitly namespace
  * export for the imported module.
  *
  * Before:
  *     import('./module-a.js')
  *         .then((moduleA) => moduleA.doSomething());
  *
  * After:
  *     import('./bundle_1.js')
  *         .then(({$moduleA}) => $moduleA)
  *         .then((moduleA) => moduleA.doSomething());
  */
 private _rewriteDynamicImport(
     baseUrl: ResolvedUrl,
     root: babel.Node,
     importNodePath: NodePath) {
   if (!importNodePath) {
     return;
   }
   const importCallExpression = importNodePath.parent;
   if (!importCallExpression ||
       !babel.isCallExpression(importCallExpression)) {
     return;
   }
   const importCallArgument = importCallExpression.arguments[0];
   if (!babel.isStringLiteral(importCallArgument)) {
     return;
   }
   const sourceUrl = importCallArgument.value;
   const resolvedSourceUrl = this.bundler.analyzer.urlResolver.resolve(
       baseUrl, sourceUrl as FileRelativeUrl);
   if (!resolvedSourceUrl) {
     return;
   }
   const sourceBundle = this.manifest.getBundleForFile(resolvedSourceUrl);
   // TODO(usergenic): To support *skipping* the rewrite, we need a way to
   // identify whether a bundle contains a single top-level module or is a
   // merged bundle with multiple top-level modules.
   let exportName;
   if (sourceBundle) {
     exportName =
         getOrSetBundleModuleExportName(sourceBundle, resolvedSourceUrl, '*');
   }
   // If there's no source bundle or the namespace export name of the bundle
   // is just '*', then we don't need to append a .then() to transform the
   // return value of the import().  Lets just rewrite the URL to be a relative
   // path and exit.
   if (!sourceBundle || exportName === '*') {
     const relativeSourceUrl =
         ensureLeadingDot(this.bundler.analyzer.urlResolver.relative(
             baseUrl, resolvedSourceUrl));
     importCallArgument.value = relativeSourceUrl;
     return;
   }
   // Rewrite the URL to be a relative path to the bundle.
   const relativeSourceUrl = ensureLeadingDot(
       this.bundler.analyzer.urlResolver.relative(baseUrl, sourceBundle.url));
   importCallArgument.value = relativeSourceUrl;
   const importCallExpressionParent = importNodePath.parentPath.parent!;
   if (!importCallExpressionParent) {
     return;
   }
   const thenifiedCallExpression = babel.callExpression(
       babel.memberExpression(
           clone(importCallExpression), babel.identifier('then')),
       [babel.arrowFunctionExpression(
           [
             babel.objectPattern(
                 [babel.objectProperty(
                      babel.identifier(exportName),
                      babel.identifier(exportName),
                      undefined,
                      true) as any]),
           ],
           babel.identifier(exportName))]);
   rewriteObject(importCallExpression, thenifiedCallExpression);
 }
开发者ID:Polymer,项目名称:vulcanize,代码行数:78,代码来源:es6-rewriter.ts

示例4: transformLoop

function transformLoop (
  name: string,
  attr: NodePath<t.JSXAttribute>,
  jsx: NodePath<t.JSXElement>,
  value: AttrValue
) {
  const jsxElement = jsx.get('openingElement')
  if (!jsxElement.node) {
    return
  }
  const attrs = jsxElement.get('attributes').map(a => a.node)
  const wxForItem = attrs.find(a => a.name.name === WX_FOR_ITEM)
  const hasSinglewxForItem = wxForItem && wxForItem.value && t.isJSXExpressionContainer(wxForItem.value)
  if (hasSinglewxForItem || name === WX_FOR || name === 'wx:for-items') {
    if (!value || !t.isJSXExpressionContainer(value)) {
      throw new Error('wx:for 的值必须使用 "{{}}"  包裹')
    }
    attr.remove()
    let item = t.stringLiteral('item')
    let index = t.stringLiteral('index')
    jsx
      .get('openingElement')
      .get('attributes')
      .forEach(p => {
        const node = p.node
        if (node.name.name === WX_FOR_ITEM) {
          if (!node.value || !t.isStringLiteral(node.value)) {
            throw new Error(WX_FOR_ITEM + ' 的值必须是一个字符串')
          }
          item = node.value
          p.remove()
        }
        if (node.name.name === WX_FOR_INDEX) {
          if (!node.value || !t.isStringLiteral(node.value)) {
            throw new Error(WX_FOR_INDEX + ' 的值必须是一个字符串')
          }
          index = node.value
          p.remove()
        }
      })

    const replacement = t.jSXExpressionContainer(
      t.callExpression(
        t.memberExpression(value.expression, t.identifier('map')),
        [
          t.arrowFunctionExpression(
            [t.identifier(item.value), t.identifier(index.value)],
            t.blockStatement([t.returnStatement(jsx.node)])
          )
        ]
      )
    )

    const block = buildBlockElement()
    block.children = [replacement]
    try {
      jsx.replaceWith(block)
    } catch (error) {
      //
    }

    return {
      item: item.value,
      index: index.value
    }
  }
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:67,代码来源:wxml.ts

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

示例6: codeFrameError


//.........这里部分代码省略.........
         .forEach(prop => {
           if (t.isObjectProperty(prop)) {
             if (t.isStringLiteral(prop.key)) {
               stateKeys.push(prop.key.value)
             }
             if (t.isIdentifier(prop.key)) {
               stateKeys.push(prop.key.name)
             }
           }
         })
     }
     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 (propKey) {
               propsKeys.push(propKey)
             }
           }
         })
     }
     return t.classProperty(t.identifier('_observeProps'), t.arrayExpression(
       observeProps.map(p => t.objectExpression([
         t.objectProperty(
           t.identifier('name'),
           t.stringLiteral(p.name)
         ),
         t.objectProperty(
           t.identifier('observer'),
           p.observer
         )
       ]))
     ))
   }
   if (PageLifecycle.has(name)) {
     const lifecycle = PageLifecycle.get(name)!
     const node = value.node as
       | t.FunctionExpression
       | t.ArrowFunctionExpression
     const method = t.classMethod(
       'method',
       t.identifier(lifecycle),
       params,
       node ? node.body as t.BlockStatement : (prop.get('body') as any).node
     )
     method.async = isAsync
     return method
   }
   if (prop.isObjectMethod()) {
     const body = prop.get('body')
     return t.classProperty(
       t.identifier(name),
       t.arrowFunctionExpression(params, body.node, isAsync)
     )
   }
   return t.classProperty(
     t.identifier(name),
     value.isFunctionExpression() || value.isArrowFunctionExpression()
       ? t.arrowFunctionExpression(value.node.params, value.node.body, isAsync)
       : value.node
   )
 })
开发者ID:topud,项目名称:taro,代码行数:101,代码来源:script.ts

示例7: JSXElement

} = () => {
  return {
    visitor: {
      JSXElement (path) {
        const arrowFuncExpr = path.findParent(p => p.isArrowFunctionExpression())
        if (arrowFuncExpr && arrowFuncExpr.isArrowFunctionExpression() && arrowFuncExpr.parentPath.isVariableDeclarator()) {
          const valDecl = arrowFuncExpr.parentPath.parentPath
          if (!valDecl.isVariableDeclaration()) {
            throw codeFrameError(valDecl.node, '函数式组件不能同时定义多个值')
          }
          const id = arrowFuncExpr.parentPath.node.id
          if (!t.isIdentifier(id)) {
            throw codeFrameError(id, '函数式组件只能使用普通标识符定义')
          }
          if (!initialIsCapital(id.name)) {
            return
          }
          const hasClassDecl = arrowFuncExpr.findParent(p => p.isClassDeclaration())
          if (hasClassDecl) {
            // @TODO: 加上链接
            return
          }
          const { body } = arrowFuncExpr.node
          if (t.isBlockStatement(body)) {
            valDecl.replaceWith(t.functionDeclaration(id, arrowFuncExpr.node.params, body))
          } else {
            valDecl.replaceWith(t.functionDeclaration(id, arrowFuncExpr.node.params, t.blockStatement([
              t.returnStatement(body)
            ])))
          }
          return
        }

        const functionDecl = path.findParent(p => p.isFunctionDeclaration())
        if (functionDecl && functionDecl.isFunctionDeclaration()) {
          const hasClassDecl = functionDecl.findParent(p => p.isClassDeclaration())
          if (hasClassDecl) {
            // @TODO: 加上链接
            return
          }
          const { id, body, params } = functionDecl.node
          let arg: null | t.LVal = null
          if (params.length > 1) {
            throw codeFrameError(id, '函数式组件的参数最多只能传入一个')
          } else if (params.length === 1) {
            arg = params[0]
          }
          const cloneBody = cloneDeep(body)
          if (!initialIsCapital(id.name)) {
            throw codeFrameError(id, `普通函数式组件命名规则请遵守帕斯卡命名法(Pascal Case), 如果是在函数内声明闭包组件,则需要使用函数表达式的写法。
形如:
const ${id.name} = ${generate(t.arrowFunctionExpression(params, body)).code}
            `)
          }
          if (arg) {
            if (t.isIdentifier(arg)) {
              cloneBody.body.unshift(buildConstVariableDeclaration(arg.name, t.memberExpression(t.thisExpression(), t.identifier('props'))))
            } else if (t.isObjectPattern(arg)) {
              cloneBody.body.unshift(
                t.variableDeclaration('const', [
                  t.variableDeclarator(arg, t.memberExpression(t.thisExpression(), t.identifier('props')))
                ])
              )
            } else {
              throw codeFrameError(arg, '函数式组件只支持传入一个简单标识符或使用对象结构')
            }
          }
          const classDecl = t.classDeclaration(id, t.memberExpression(t.identifier('Taro'), t.identifier('Component')), t.classBody([
            t.classMethod('method', t.identifier('render'), [], cloneBody)
          ]), [])
          functionDecl.replaceWith(classDecl)
        }
      }
    }
  }
}
开发者ID:YangShaoQun,项目名称:taro,代码行数:76,代码来源:functional.ts


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