Blame view

node_modules/regenerator/lib/visit.js 1.19 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /**
   * Copyright (c) 2014-present, Facebook, Inc.
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE file in the root directory of this source tree.
   */
  
  var recast = require("recast");
  var types = recast.types;
  var n = types.namedTypes;
  var util = require("./util.js");
  
  exports.transform = function transform(node, options) {
    options = util.defaults(options || {}, {
      includeRuntime: false
    });
  
    var result = require("@babel/core").transformFromAstSync(node, null, {
      presets: [require("regenerator-preset")],
      code: false,
      ast: true
    });
  
    node = result.ast;
  
    if (options.includeRuntime === true) {
      injectRuntime(n.File.check(node) ? node.program : node);
    }
  
    return node;
  };
  
  function injectRuntime(program) {
    n.Program.assert(program);
  
    // Include the runtime by modifying the AST rather than by concatenating
    // strings. This technique will allow for more accurate source mapping.
    var runtimePath = require("..").runtime.path;
    var runtime = fs.readFileSync(runtimePath, "utf8");
    var runtimeBody = recast.parse(runtime, {
      sourceFileName: runtimePath
    }).program.body;
  
    var body = program.body;
    body.unshift.apply(body, runtimeBody);
  }