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


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

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


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

示例1: _deduplicateImportStatements

 /**
  * Attempts to reduce the number of distinct import declarations by combining
  * those referencing the same source into the same declaration. Results in
  * deduplication of imports of the same item as well.
  *
  * Before:
  *     import {a} from './module-1.js';
  *     import {b} from './module-1.js';
  *     import {c} from './module-2.js';
  * After:
  *     import {a,b} from './module-1.js';
  *     import {c} from './module-2.js';
  */
 private _deduplicateImportStatements(node: babel.Node) {
   const importDeclarations = new Map<string, babel.ImportDeclaration>();
   traverse(node, {
     noScope: true,
     ImportDeclaration: {
       enter(path: NodePath) {
         const importDeclaration = path.node;
         if (!babel.isImportDeclaration(importDeclaration)) {
           return;
         }
         const source = babel.isStringLiteral(importDeclaration.source) &&
             importDeclaration.source.value;
         if (!source) {
           return;
         }
         const hasNamespaceSpecifier = importDeclaration.specifiers.some(
             (s) => babel.isImportNamespaceSpecifier(s));
         const hasDefaultSpecifier = importDeclaration.specifiers.some(
             (s) => babel.isImportDefaultSpecifier(s));
         if (!importDeclarations.has(source) && !hasNamespaceSpecifier &&
             !hasDefaultSpecifier) {
           importDeclarations.set(source, importDeclaration);
         } else if (importDeclarations.has(source)) {
           const existingDeclaration = importDeclarations.get(source)!;
           for (const specifier of importDeclaration.specifiers) {
             existingDeclaration.specifiers.push(specifier);
           }
           path.remove();
         }
       }
     }
   });
 }
開發者ID:Polymer,項目名稱:vulcanize,代碼行數:46,代碼來源:es6-rewriter.ts

示例2: it

it('should move static apis under "Taro"', function () {
  const code = `
    import { noop } from '@tarojs/taro-h5';
    noop;
    noop();
  `

  const result = babel.transform(code, { plugins: [pluginOptions] })
  expect(result.code).toMatchSnapshot();

  const ast = result.ast as t.File
  const body = ast.program.body as [t.ImportDeclaration, t.ExpressionStatement]
  expect(t.isImportDeclaration(body[0])).toBeTruthy()
  expect(t.isExpressionStatement(body[1])).toBeTruthy()
  const defaultImport = body[0].specifiers.find(v => t.isImportDefaultSpecifier(v))
  expect(defaultImport).toBeTruthy()

  const taroName = defaultImport!.local.name
  let memberExpression = body[1].expression
  if (t.isCallExpression(body[1])) {
    memberExpression = (body[1].expression as t.CallExpression).callee
  }
  expect(memberExpression).toMatchObject(t.memberExpression(
    t.identifier(taroName),
    t.identifier('noop')
  ))
})
開發者ID:YangShaoQun,項目名稱:taro,代碼行數:27,代碼來源:index-test.ts

示例3:

 const isAsyncImported = ast.program.body.some(statement => {
   return t.isImportDeclaration(statement) && statement.source.value === ASYNC_PACKAGE_NAME
 })
開發者ID:teachat8,項目名稱:taro,代碼行數:3,代碼來源:index.ts


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