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


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

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


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

示例1: handleConditions

function handleConditions (conditions: Condition[]) {
  if (conditions.length === 1) {
    const ct = conditions[0]
    ct.path.replaceWith(
      t.jSXExpressionContainer(
        t.logicalExpression('&&', ct.tester.expression, cloneDeep(ct.path.node))
      )
    )
  }
  if (conditions.length > 1) {
    const lastLength = conditions.length - 1
    const lastCon = conditions[lastLength]
    let lastAlternate: t.Expression = cloneDeep(lastCon.path.node)
    if (lastCon.condition === WX_ELSE_IF) {
      lastAlternate = t.logicalExpression(
        '&&',
        lastCon.tester.expression,
        lastAlternate
      )
    }
    const node = conditions
      .slice(0, lastLength)
      .reduceRight((acc: t.Expression, condition) => {
        return t.conditionalExpression(
          condition.tester.expression,
          cloneDeep(condition.path.node),
          acc
        )
      }, lastAlternate)
    conditions[0].path.replaceWith(t.jSXExpressionContainer(node))
    conditions.slice(1).forEach(c => c.path.remove())
  }
}
开发者ID:topud,项目名称:taro,代码行数:33,代码来源:wxml.ts

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

示例3: codeFrameError


//.........这里部分代码省略.........
                        })
                      }
                      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)
                }
              }
            })
        }
        currentStateKeys.forEach(s => {
          if (propsKeys.includes(s)) {
            throw new Error(`当前 Component 定义了重复的 data 和 properites: ${s}`)
          }
        })
        stateKeys.push(...currentStateKeys)
        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)!
        if (name === 'onLoad' && t.isIdentifier(params[0])) {
          params = [t.assignmentPattern(params[0] as t.Identifier, t.logicalExpression('||', t.memberExpression(
            t.memberExpression(
              t.thisExpression(),
              t.identifier('$router')
            ),
            t.identifier('params')
          ), t.objectExpression([])))]
        }
        if (prop.isObjectMethod()) {
          const body = prop.get('body')
          return t.classMethod('method', t.identifier(lifecycle), params, body.node)
        }
        const node = value.node
        const method = t.isFunctionExpression(node) || t.isArrowFunctionExpression(node)
          ? t.classProperty(t.identifier(lifecycle), t.arrowFunctionExpression(params, node.body, isAsync))
          : t.classProperty(t.identifier(lifecycle), node)
        return method
      }
      let hasArguments = false
      prop.traverse({
        Identifier (path) {
          if (path.node.name === 'arguments') {
            hasArguments = true
            path.stop()
          }
        }
      })

      if (prop.isObjectMethod()) {
        const body = prop.get('body')
        if (hasArguments) {
          return t.classMethod('method', t.identifier(name), params, body.node)
        }
        return t.classProperty(
          t.identifier(name),
          t.arrowFunctionExpression(params, body.node, isAsync)
        )
      }

      if (hasArguments && (value.isFunctionExpression() || value.isArrowFunctionExpression())) {
        const method = t.classMethod('method', t.identifier(name), params, value.node.body as any)
        method.async = isAsync
        return method
      }

      const classProp = t.classProperty(
        t.identifier(name),
        value.isFunctionExpression() || value.isArrowFunctionExpression()
          ? t.arrowFunctionExpression(value.node.params, value.node.body, isAsync)
          : value.node
      ) as any

      if (staticProps.includes(name)) {
        classProp.static = true
      }

      return classProp
    })
开发者ID:YangShaoQun,项目名称:taro,代码行数:101,代码来源:script.ts

示例4: _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(bundle => bundle && bundle.$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.identifier('bundle')],
           babel.logicalExpression(
               '||',
               babel.logicalExpression(
                   '&&',
                   babel.identifier('bundle'),
                   babel.memberExpression(
                       babel.identifier('bundle'),
                       babel.identifier(exportName))),
               babel.objectExpression([])))]);
   rewriteObject(importCallExpression, thenifiedCallExpression);
 }
开发者ID:MehdiRaash,项目名称:tools,代码行数:79,代码来源:es6-rewriter.ts


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