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


TypeScript parser.parse函数代码示例

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


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

示例1: rewriteBareModuleSpecifiers

export function rewriteBareModuleSpecifiers(
    code: string, packageVersions: PackageVersionMap, rootPackage: string):
    string {
  const jsAST = babelParser.parse(
      code, {sourceType: 'module', plugins: ['dynamicImport']});
  for (const node of jsAST.program.body) {
    if ((node.type === 'ImportDeclaration' ||
         node.type === 'ExportNamedDeclaration' ||
         node.type === 'ExportAllDeclaration') &&
        node.source) {
      if (isBareModuleSpecifier(node.source.value)) {
        const parsedPackage = parsePackageName(node.source.value);
        const version = packageVersions[parsedPackage.package];
        const versionString = version ? '@' + version : '';
        const queryString = rootPackage ? '?' + rootPackage : '';
        node.source.value = `/${parsedPackage.package}${versionString}${
            parsedPackage.path}${queryString}`;
      } else {
        // Append rootPackage to relative URLs.
        const parsedUrl = url.parse(node.source.value);
        if (!parsedUrl.protocol) {
          parsedUrl.search = rootPackage || '';
          node.source.value = url.format(parsedUrl);
        }
      }
    }
  }

  const outputJs = babelGenerate(jsAST, {retainLines: true}, code);
  return outputJs.code;
}
开发者ID:customelements,项目名称:v2,代码行数:31,代码来源:html-rewriter.ts

示例2: function

    exit: function(path) {
      const babelPluginOptions = plugin["babelPluginOptions"];
      let usableHelperCode;

      if (babelPluginOptions) {
        const { accessToken, backendPort } = babelPluginOptions;
        usableHelperCode = helperCode;
        usableHelperCode = usableHelperCode.replace(
          /ACCESS_TOKEN_PLACEHOLDER/g,
          accessToken
        );
        usableHelperCode = usableHelperCode.replace(
          /BACKEND_PORT_PLACEHOLDER/g,
          backendPort
        );
      } else {
        usableHelperCode = helperCode;
      }

      // console.log({ enter, enterNotIgnored });

      var initCodeAstNodes = babylon
        .parse(usableHelperCode)
        .program.body.reverse();
      initCodeAstNodes.forEach(node => {
        path.node.body.unshift(node);
      });
    }
开发者ID:mattzeunert,项目名称:fromjs,代码行数:28,代码来源:babelPlugin.ts

示例3: tryParse

  function tryParse(code: any, fullPath: any) {
    var parseOptions = getOptions(fullPath);

    try {
      return babelParse(code, parseOptions).program;

    } catch (error) {
      // If parsing fails, check options.json to see if the failure was
      // expected.
      try {
        var options = JSON.parse(fs.readFileSync(
          path.join(path.dirname(fullPath), "options.json")).toString());
      } catch (optionsError) {
        console.error(optionsError.message);
      }

      if (options &&
          options.throws === error.message) {
        return null;
      }

      throw error;
    }
  }
开发者ID:benjamn,项目名称:ast-types,代码行数:24,代码来源:typescript.ts

示例4: check

 function check(source: string, expected: Array<Insertion>) {
   const ast = parse(source, { sourceType: 'module', tokens: true });
   const { insertions } = process(source, ast);
   expect(expected).toEqual(insertions);
 }
开发者ID:eventualbuddha,项目名称:automatic-semicolon-insertion,代码行数:5,代码来源:insertions.test.ts

示例5: Identifier

        }
    }
};

const MyVisitor2: Visitor = {
    Identifier(path) {
        console.log("Visiting: " + path.node.name);
    }
};

// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-traverse
const code = `function square(n) {
    return n * n;
}`;

const ast = parse(code);

traverse(ast, {
    enter(path) {
        const node = path.node;
        if (t.isIdentifier(node) && node.name === "n") {
            node.name = "x";
        }
    }
});

// Examples from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#writing-your-first-babel-plugin

const v1: Visitor = {
    BinaryExpression(path) {
        if (t.isIdentifier(path.node.left)) {
开发者ID:AlexGalays,项目名称:DefinitelyTyped,代码行数:31,代码来源:babel__traverse-tests.ts

示例6: assert

import { parse, parseExpression } from "@babel/parser";

declare function assert(expr: boolean): void;

const code = `function square(n) {
  return n * n;
}`;

const node = parse(code);
assert(node.type === "File");
assert(node.start === 0);
assert(node.end === 38);
assert(!!node.loc && node.loc.start > node.loc.end);

parse(code, {
  sourceType: "module", // default: "script"
  plugins: ["jsx"] // default: []
});
开发者ID:AlexGalays,项目名称:DefinitelyTyped,代码行数:18,代码来源:babel__parser-tests.ts

示例7: function

        fs.readFile(fullPath, "utf8", function (error, code) {
          if (error) {
            throw error;
          }

          var program = babelParse(code, {
            sourceType: "module",
            plugins: [
              "typescript",
              "objectRestSpread",
              "classProperties",
              "optionalCatchBinding",
              "numericSeparator",
            ]
          }).program;

          tsTypes.namedTypes.Program.assert(program, true);

          done();
        });
开发者ID:benjamn,项目名称:ast-types,代码行数:20,代码来源:typescript.ts

示例8: describe

 describe('scope', () => {
   const scope = [
     "type Foo = {}",
     "interface Bar {}"
   ];
 
   const ast = babelParse(scope.join("\n"), {
     plugins: ['typescript']
   });
 
   it("should register typescript types with the scope", function() {  
     visit(ast, {
       visitProgram(path) {
         assert(path.scope.declaresType('Foo'));
         assert(path.scope.declaresType('Bar'));
         assert.equal(path.scope.lookupType('Foo').getTypes()['Foo'][0].parent.node.type, 'TSTypeAliasDeclaration');
         assert.equal(path.scope.lookupType('Bar').getTypes()['Bar'][0].parent.node.type, 'TSInterfaceDeclaration');
         return false;
       }
     });
   });
 });
开发者ID:benjamn,项目名称:ast-types,代码行数:22,代码来源:typescript.ts


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