• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript babylon.parse函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: test

  test('returns events from a comment', () => {
    const node = parse(`
        class Foo {
          /**
           * This is an event
           *
           * @event event-name
           * @param {Event} event
           */
           myMethod() { }

           /**
            * @event descriptionless-event
            */
           anotherMethod() { }
        }`);
    const events = [...getEventComments(node).values()];
    const eventMatches = events.map((ev) => ({
                                      description: ev.description,
                                      name: ev.name,
                                      params: ev.params,
                                      warnings: ev.warnings
                                    }));

    assert.deepEqual(eventMatches, [
      {
        description: 'descriptionless-event',
        name: 'descriptionless-event',
        params: [],
        warnings: []
      },
      {
        description: 'This is an event',
        name: 'event-name',
        params: [{desc: '', name: 'event', type: 'Event'}],
        warnings: []
      }
    ]);
  });
开发者ID:MehdiRaash,项目名称:tools,代码行数:39,代码来源:esutil_test.ts


示例2: run

  static run(content: string): StageResult {
    let log = logger(this.name);
    log(content);

    let editor = new MagicString(content);
    let ast = parse(content, {
      sourceType: 'module',
      plugins: BABYLON_PLUGINS,
      allowReturnOutsideFunction: true,
      tokens: true
    } as any); // tslint:disable-line no-any
    let config = buildConfig(content, ast);

    asi(config);

    config.insertions.forEach(({ index, content }: Insertion) => editor.appendLeft(index, content));
    config.removals.forEach(({ start, end }: Removal) => editor.remove(start, end));

    return {
      code: editor.toString(),
      suggestions: []
    };
  }
开发者ID:alangpierce,项目名称:decaffeinate,代码行数:23,代码来源:index.ts


示例3: assert

/// <reference types="babel-types" />
/// <reference types="babel-types" />


// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babylon
import * as babylon from "babylon";
declare function assert(expr: boolean): void;

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

let node = babylon.parse(code);
assert(node.type === "File");
assert(node.start === 0);
assert(node.end === 38);
assert(node.loc.start > node.loc.end);


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


示例4:

const parse = (source: string) =>
  babylon.parse(source, {
    sourceType: 'module',
    plugins: ['jsx', 'typescript', 'classProperties', 'objectRestSpread']
  });
开发者ID:OfficeDev,项目名称:office-ui-fabric-react,代码行数:5,代码来源:codepenTransform.ts


示例5: parse

/// <reference types="babylon" />



// Example from https://github.com/babel/babel/tree/master/packages/babel-generator
import {parse} from 'babylon';
import generate from 'babel-generator';

const code = 'class Example {}';
const ast = parse(code);

ast.type;
ast.loc.start;

const output = generate(ast, { /* options */ }, code);


// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-generator
let result = generate(ast, {
    retainLines: false,
    compact: "auto",
    concise: false,
    quotes: "double",
    // ...
}, code);
result.code;
result.map;
开发者ID:ArtemZag,项目名称:DefinitelyTyped,代码行数:27,代码来源:babel-generator-tests.ts


示例6: 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 = babylon.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:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:31,代码来源:babel-traverse-tests.ts


示例7: jsTransform

export function jsTransform(js: string, options: JsTransformOptions): string {
  // Even with no transform plugins, parsing and serializing with Babel will
  // make some minor formatting changes to the code. Skip Babel altogether
  // if we have no meaningful changes to make.
  let doBabelTransform = false;

  // Note that Babel plugins run in this order:
  // 1) plugins, first to last
  // 2) presets, last to first
  const plugins = [...babelSyntaxPlugins];
  const presets = [];

  if (options.externalHelpers) {
    plugins.push(babelExternalHelpersPlugin);
  }
  if (options.minify) {
    doBabelTransform = true;
    // Minify last, so push first.
    presets.push(babelPresetMinify);
  }
  if (options.compile === true || options.compile === 'es5') {
    doBabelTransform = true;
    plugins.push(...babelTransformEs2015);
    plugins.push(...babelTransformEs2016);
    plugins.push(...babelTransformEs2017);
    plugins.push(...babelTransformEs2018);
  } else if (options.compile === 'es2015') {
    doBabelTransform = true;
    plugins.push(...babelTransformEs2016);
    plugins.push(...babelTransformEs2017);
    plugins.push(...babelTransformEs2018);
  } else if (options.compile === 'es2016') {
    doBabelTransform = true;
    plugins.push(...babelTransformEs2017);
    plugins.push(...babelTransformEs2018);
  } else if (options.compile === 'es2017') {
    doBabelTransform = true;
    plugins.push(...babelTransformEs2018);
  }
  if (options.moduleResolution === 'node') {
    if (!options.filePath) {
      throw new Error(
          'Cannot perform node module resolution without filePath.');
    }
    doBabelTransform = true;
    plugins.push(resolveBareSpecifiers(
        options.filePath,
        !!options.isComponentRequest,
        options.packageName,
        options.componentDir,
        options.rootDir));
  }

  // When the AMD option is "auto", these options will change based on whether
  // we have a module or not (unless they are already definitely true).
  let transformModulesToAmd = options.transformModulesToAmd;
  if (transformModulesToAmd === true) {
    doBabelTransform = true;
  }

  const maybeDoBabelTransform =
      doBabelTransform || transformModulesToAmd === 'auto';

  if (maybeDoBabelTransform) {
    let ast;
    try {
      ast = babylon.parse(js, {
        // TODO(aomarks) Remove any when typings are updated for babylon 7.
        // tslint:disable-next-line: no-any
        sourceType: transformModulesToAmd === 'auto' ? 'unambiguous' as any :
                                                       'module',
        plugins: [
          'asyncGenerators',
          'dynamicImport',
          // tslint:disable-next-line: no-any
          'importMeta' as any,
          'objectRestSpread',
        ],
      });
    } catch (e) {
      if (options.softSyntaxError && e.constructor.name === 'SyntaxError') {
        console.error(
            'ERROR [polymer-build]: failed to parse JavaScript' +
                (options.filePath ? ` (${options.filePath}):` : ':'),
            e);
        return js;
      } else {
        throw e;
      }
    }

    if (transformModulesToAmd === 'auto' &&
        ast.program.sourceType === 'module') {
      transformModulesToAmd = true;
    }

    if (transformModulesToAmd) {
      doBabelTransform = true;
      plugins.push(...babelTransformModulesAmd);
    }
//.........这里部分代码省略.........
开发者ID:poehlmann,项目名称:EvaluacionDiferencialDeLaMemoria,代码行数:101,代码来源:js-transform.ts


示例8: parseNumber

/**
 * Parses JavaScript source representing a number.
 */
export default function parseNumber(string: string): number {
  let expressionStatement = parse(`(${string})`).program.body[0] as ExpressionStatement;
  let literal = expressionStatement.expression as NumericLiteral;
  return literal.value;
}
开发者ID:alangpierce,项目名称:decaffeinate-parser,代码行数:8,代码来源:parseNumber.ts


示例9: parseRegExp

/**
 * Parses JavaScript source representing a regular expression.
 */
export default function parseRegExp(string: string): { pattern: string, flags?: string } {
  let expressionStatement = parse(`(${string})`).program.body[0] as ExpressionStatement;
  let literal = expressionStatement.expression as RegExpLiteral;
  return literal;
}
开发者ID:alangpierce,项目名称:decaffeinate-parser,代码行数:8,代码来源:parseRegExp.ts



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript babylonjs.AnimationGroup类代码示例发布时间:2022-05-25
下一篇:
TypeScript babel-types.variableDeclarator函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap