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


TypeScript babel-types.isBinaryExpression函數代碼示例

本文整理匯總了TypeScript中babel-types.isBinaryExpression函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isBinaryExpression函數的具體用法?TypeScript isBinaryExpression怎麽用?TypeScript isBinaryExpression使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


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

示例1: toConstant

 function toConstant(expression: b.Expression): any {
   if (!constant) return;
   if (b.isArrayExpression(expression)) {
     const result = [];
     for (let i = 0; constant && i < expression.elements.length; i++) {
       const element = expression.elements[i];
       if (b.isSpreadElement(element)) {
         const spread = toConstant(element.argument);
         if (!(isSpreadable(spread) && constant)) {
           constant = false;
         } else {
           result.push(...spread);
         }
       } else {
         result.push(toConstant(element));
       }
     }
     return result;
   }
   if (b.isBinaryExpression(expression)) {
     const left = toConstant(expression.left);
     const right = toConstant(expression.right);
     return constant && binaryOperation(expression.operator, left, right);
   }
   if (b.isBooleanLiteral(expression)) {
     return expression.value;
   }
   if (b.isCallExpression(expression)) {
     const args = [];
     for (let i = 0; constant && i < expression.arguments.length; i++) {
       const arg = expression.arguments[i];
       if (b.isSpreadElement(arg)) {
         const spread = toConstant(arg.argument);
         if (!(isSpreadable(spread) && constant)) {
           constant = false;
         } else {
           args.push(...spread);
         }
       } else {
         args.push(toConstant(arg));
       }
     }
     if (!constant) return;
     if (b.isMemberExpression(expression.callee)) {
       const object = toConstant(expression.callee.object);
       if (!object || !constant) {
         constant = false;
         return;
       }
       const member = expression.callee.computed
         ? toConstant(expression.callee.property)
         : b.isIdentifier(expression.callee.property)
           ? expression.callee.property.name
           : undefined;
       if (member === undefined && !expression.callee.computed) {
         constant = false;
       }
       if (!constant) return;
       if (canCallMethod(object, '' + member)) {
         return object[member].apply(object, args);
       }
     } else {
       const callee = toConstant(expression.callee);
       if (!constant) return;
       return callee.apply(null, args);
     }
   }
   if (b.isConditionalExpression(expression)) {
     const test = toConstant(expression.test);
     return test
       ? toConstant(expression.consequent)
       : toConstant(expression.alternate);
   }
   if (b.isIdentifier(expression)) {
     if (
       options.constants &&
       {}.hasOwnProperty.call(options.constants, expression.name)
     ) {
       return options.constants[expression.name];
     }
   }
   if (b.isLogicalExpression(expression)) {
     const left = toConstant(expression.left);
     const right = toConstant(expression.right);
     if (constant && expression.operator === '&&') {
       return left && right;
     }
     if (constant && expression.operator === '||') {
       return left || right;
     }
   }
   if (b.isMemberExpression(expression)) {
     const object = toConstant(expression.object);
     if (!object || !constant) {
       constant = false;
       return;
     }
     const member = expression.computed
       ? toConstant(expression.property)
       : b.isIdentifier(expression.property)
//.........這裏部分代碼省略.........
開發者ID:Ashwinie,項目名稱:inceptionEngine,代碼行數:101,代碼來源:index.ts

示例2: transform


//.........這裏部分代碼省略.........
          path.node.name = t.jSXIdentifier('View')
          const store = path.node.attributes.find(attr => attr.name.name === 'store')
          if (store && t.isJSXExpressionContainer(store.value) && t.isIdentifier(store.value.expression)) {
            storeName = store.value.expression.name
          }
          path.node.attributes = []
        }
      }

      if (IMAGE_COMPONENTS.has(name)) {
        for (const attr of path.node.attributes) {
          if (
            attr.name.name === 'src'
          ) {
            if (t.isStringLiteral(attr.value)) {
              imageSource.add(attr.value.value)
            } else if (t.isJSXExpressionContainer(attr.value)) {
              if (t.isStringLiteral(attr.value.expression)) {
                imageSource.add(attr.value.expression.value)
              }
            }
          }
        }
      }
    },
    JSXAttribute (path) {
      const { name, value } = path.node
      if (!t.isJSXIdentifier(name) || value === null || t.isStringLiteral(value) || t.isJSXElement(value)) {
        return
      }

      const expr = value.expression as any
      const exprPath = path.get('value.expression')
      if (!t.isBinaryExpression(expr, { operator: '+' }) && !t.isLiteral(expr) && name.name === 'style') {
        const jsxID = path.findParent(p => p.isJSXOpeningElement()).get('name')
        if (jsxID && jsxID.isJSXIdentifier() && DEFAULT_Component_SET.has(jsxID.node.name)) {
          exprPath.replaceWith(
            t.callExpression(t.identifier(INTERNAL_INLINE_STYLE), [expr])
          )
        }
      }

      if (name.name.startsWith('on')) {
        if (exprPath.isReferencedIdentifier()) {
          const ids = [expr.name]
          const fullPath = buildFullPathThisPropsRef(expr, ids, path)
          if (fullPath) {
            exprPath.replaceWith(fullPath)
          }
        }

        if (exprPath.isReferencedMemberExpression()) {
          const id = findFirstIdentifierFromMemberExpression(expr)
          const ids = getIdsFromMemberProps(expr)
          if (t.isIdentifier(id)) {
            const fullPath = buildFullPathThisPropsRef(id, ids, path)
            if (fullPath) {
              exprPath.replaceWith(fullPath)
            }
          }
        }

        // @TODO: bind 的處理待定
      }
    },
    ImportDeclaration (path) {
開發者ID:topud,項目名稱:taro,代碼行數:67,代碼來源:index.ts

示例3: traverse

declare const ast: t.Node;

traverse(ast, {
    enter(path) {
        const node = path.node;
        if (t.isIdentifier(node, { name: "n" })) {
            node.name = "x";
        }
        if (t.isFunctionExpression(node)) {
            node.params = [t.identifier('param')];
        }
    }
});

if (t.isBinaryExpression(ast)) {
    ast.left;
    ast.right;
    ast.operator;
}

t.assertBinaryExpression(ast);
t.assertBinaryExpression(ast, { operator: "*" });

const exp: t.Expression = t.nullLiteral();

// React examples:
// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-plugin-transform-react-inline-elements/src/index.js#L61
traverse(ast, {
    JSXElement(path, file) {
        const { node } = path;
開發者ID:AlexGalays,項目名稱:DefinitelyTyped,代碼行數:30,代碼來源:babel-types-tests.ts

示例4: transform


//.........這裏部分代碼省略.........
          path.node.name = t.jSXIdentifier('View')
          const store = path.node.attributes.find(attr => attr.name.name === 'store')
          if (store && t.isJSXExpressionContainer(store.value) && t.isIdentifier(store.value.expression)) {
            storeName = store.value.expression.name
          }
          path.node.attributes = []
        }
      }

      if (IMAGE_COMPONENTS.has(name)) {
        for (const attr of path.node.attributes) {
          if (
            t.isIdentifier(attr) &&
            attr.name.name === 'src'
          ) {
            if (t.isStringLiteral(attr.value)) {
              imageSource.add(attr.value.value)
            } else if (t.isJSXExpressionContainer(attr.value)) {
              if (t.isStringLiteral(attr.value.expression)) {
                imageSource.add(attr.value.expression.value)
              }
            }
          }
        }
      }
    },
    JSXAttribute (path) {
      const { name, value } = path.node
      if (!t.isJSXIdentifier(name) || value === null || t.isStringLiteral(value) || t.isJSXElement(value)) {
        return
      }

      const expr = value.expression
      if (t.isBinaryExpression(expr, { operator: '+' }) || t.isLiteral(expr) || name.name !== 'style') {
        return
      }

      path.get('value.expression').replaceWith(
        t.callExpression(t.identifier(INTERNAL_INLINE_STYLE), [expr])
      )
    },
    ImportDeclaration (path) {
      const source = path.node.source.value
      const names: string[] = []
      if (source === TARO_PACKAGE_NAME) {
        path.node.specifiers.push(
          t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
          t.importSpecifier(t.identifier(INTERNAL_DYNAMIC), t.identifier(INTERNAL_DYNAMIC)),
          t.importSpecifier(t.identifier(INTERNAL_INLINE_STYLE), t.identifier(INTERNAL_INLINE_STYLE))
        )
      }
      if (
        source === REDUX_PACKAGE_NAME
      ) {
        path.node.specifiers.forEach((s, index, specs) => {
          if (s.local.name === 'Provider') {
            specs.splice(index, 1)
            specs.push(
              t.importSpecifier(t.identifier('setStore'), t.identifier('setStore'))
            )
          }
        })
      }
      path.traverse({
        ImportDefaultSpecifier (path) {
          const name = path.node.local.name
開發者ID:ApolloRobot,項目名稱:taro,代碼行數:67,代碼來源:index.ts


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