/**
 * 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 fs = require("fs");
var through = require("through");
var transform = require("./lib/visit").transform;
var utils = require("./lib/util");
var genOrAsyncFunExp = /\bfunction\s*\*|\basync\b/;

function exports(file, options) {
  var data = [];
  return through(write, end);

  function write(buf) {
    data.push(buf);
  }

  function end() {
    try {
      this.queue(compile(data.join(""), options).code);
      this.queue(null);
    } catch (e) { this.emit('error', e); }
  }
}

// To get a writable stream for use as a browserify transform, call
// require("regenerator")().
module.exports = exports;

// To include the runtime globally in the current node process, call
// require("regenerator").runtime().
function runtime() {
  regeneratorRuntime = require("regenerator-runtime");
}
exports.runtime = runtime;
runtime.path = require("regenerator-runtime/path.js").path;

var cachedRuntimeCode;
function getRuntimeCode() {
  return cachedRuntimeCode ||
    (cachedRuntimeCode = fs.readFileSync(runtime.path, "utf8"));
}

var transformOptions = {
  presets: [require("regenerator-preset")],
  parserOpts: {
    sourceType: "module",
    allowImportExportEverywhere: true,
    allowReturnOutsideFunction: true,
    allowSuperOutsideMethod: true,
    strictMode: false,
    plugins: ["*", "jsx", "flow"]
  }
};

function compile(source, options) {
  var result;

  options = utils.defaults(options || {}, {
    includeRuntime: false
  });

  // Shortcut: Transform only if generators or async functions present.
  if (genOrAsyncFunExp.test(source)) {
    result = require("@babel/core").transformSync(source, transformOptions);
  } else {
    result = { code: source };
  }

  if (options.includeRuntime === true) {
    result.code = getRuntimeCode() + "\n" + result.code;
  }

  return result;
}

// Allow packages that depend on Regenerator to use the same copy of
// ast-types, in case multiple versions are installed by NPM.
exports.types = require("recast").types;

// Transforms a string of source code, returning a { code, map? } result.
exports.compile = compile;

// To modify an AST directly, call require("regenerator").transform(ast).
exports.transform = transform;

@@ -323,60 +323,8 @@
 l%22);
-%0Avar genOrAsyncFunExp = /%5Cbfunction%5Cs*%5C*%7C%5Cbasync%5Cb/;
 %0A%0Afu
@@ -1578,120 +1578,11 @@
 %0A%0A  
-// Shortcut: Transform only if generators or async functions present.%0A  if (genOrAsyncFunExp.test(source)) %7B%0A   
+var
  res
@@ -1624,23 +1624,32 @@
 ormSync(
+%0A    
 source,
+%0A   
  transfo
@@ -1661,56 +1661,13 @@
 ions
-);
 %0A  
-%7D else %7B%0A    result = %7B code: source %7D;%0A  %7D
+);
 %0A%0A  

Stop using RegExp to detect possible generator/async syntax.