<commit_before>import cloneDeep from "lodash/lang/cloneDeep";
import has from "lodash/object/has";
import traverse from "babel-traverse";
import * as babylon from "babylon";
import * as t from "babel-types";

let FROM_TEMPLATE = "_fromTemplate"; //Symbol(); // todo: probably wont get copied over
let TEMPLATE_SKIP = Symbol();

export default function (code: string): Function {
  // since we lazy parse the template, we get the current stack so we have the
  // original stack to append if it errors when parsing
  let stack = new Error().stack.split("\n").slice(1).join("\n");

  let getAst = function () {
    let ast;

    try {
      ast = babylon.parse(code, {
        allowReturnOutsideFunction: true,
        allowSuperOutsideMethod: true
      });

      ast = traverse.removeProperties(ast);

      traverse.cheap(ast, function (node) {
        node[FROM_TEMPLATE] = true;
      });
    } catch (err) {
      err.stack = `${err.stack}from\n${stack}`;
      throw err;
    }

    getAst = function () {
      return ast;
    };

    return ast;
  };
<commit_msg>`babel-template` no longer crashes in IE attempting to access unpopulated error stack.<commit_after>import cloneDeep from "lodash/lang/cloneDeep";
import has from "lodash/object/has";
import traverse from "babel-traverse";
import * as babylon from "babylon";
import * as t from "babel-types";

let FROM_TEMPLATE = "_fromTemplate"; //Symbol(); // todo: probably wont get copied over
let TEMPLATE_SKIP = Symbol();

export default function (code: string): Function {
  // since we lazy parse the template, we get the current stack so we have the
  // original stack to append if it errors when parsing
  let stack;
  try {
    // error stack gets populated in IE only on throw (https://msdn.microsoft.com/en-us/library/hh699850(v=vs.94).aspx)
    throw new Error();
  } catch (error) {
    stack = error.stack.split("\n").slice(1).join("\n")
  }

  let getAst = function () {
    let ast;

    try {
      ast = babylon.parse(code, {
        allowReturnOutsideFunction: true,
        allowSuperOutsideMethod: true
      });

      ast = traverse.removeProperties(ast);

      traverse.cheap(ast, function (node) {
        node[FROM_TEMPLATE] = true;
      });
    } catch (err) {
      err.stack = `${err.stack}from\n${stack}`;
      throw err;
    }

    getAst = function () {
      return ast;
    };

    return ast;
  };

{'fraction_non_alphanumeric': 0.12573099415204678, 'fraction_numerical': 0.004177109440267335, 'mean_word_length': 2.9198036006546646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4537156', 'n_tokens_mistral': 764, 'n_tokens_neox': 717, 'n_words': 227}