/* @flow */

import {sync as resolve} from 'resolve';
import {dirname} from 'path';

import type {AssertionLocation} from './assertions/assertionTypes';


/* We need to use source-map-support in order to extract the right locations
 * from a stack trace. Unfortunately, we need to use the exact same version
 * that babel-register uses. We can use resolve() to find it.
 */
let wrapCallSite = null;
function getWrapCallSite() {
  if (wrapCallSite == null) {
    const babelRegisterPath = resolve('babel-register');
    const sourceMapSupportPath = resolve(
      'source-map-support',
      {basedir: dirname(babelRegisterPath)},
    );
    // $FlowFixMe - This is necessary :(
    wrapCallSite = require(sourceMapSupportPath).wrapCallSite;
  }
  return wrapCallSite;
}

export default function(): ?AssertionLocation {
  const oldPrepareStackTrace = Error.prepareStackTrace;

  const wrapCallSite = getWrapCallSite();
  Error.prepareStackTrace = (_, stack) => stack.map(wrapCallSite);
  const stack: Array<Object> = ((new Error()).stack: any);
  Error.prepareStackTrace = oldPrepareStackTrace;

  for (const callSite of stack) {
    const filename = callSite.getFileName();
    if (filename.match(/test.js$/)) {
      return {
        filename,
        line: callSite.getLineNumber(),
        column: callSite.getColumnNumber(),
      };
    }
  }
  return null;
}

/* @flow */

import {sync as resolve} from 'resolve';
import {dirname} from 'path';

import type {AssertionLocation} from './assertions/assertionTypes';


/* We need to use source-map-support in order to extract the right locations
 * from a stack trace. Unfortunately, we need to use the exact same version
 * that babel-register uses. We can use resolve() to find it.
 */
let wrapCallSite = null;
function getWrapCallSite() {
  if (wrapCallSite == null) {
    const babelRegisterPath = resolve('babel-register');
    const sourceMapSupportPath = resolve(
      'source-map-support',
      {basedir: dirname(babelRegisterPath)},
    );
    // $FlowFixMe - This is necessary :(
    wrapCallSite = require(sourceMapSupportPath).wrapCallSite;
  }
  return wrapCallSite;
}

export default function(): ?AssertionLocation {
  const oldPrepareStackTrace = Error.prepareStackTrace;

  const wrapCallSite = getWrapCallSite();
  Error.prepareStackTrace = (_, stack) => stack.map(wrapCallSite);
  const stack: Array<Object> = ((new Error()).stack: any);
  Error.prepareStackTrace = oldPrepareStackTrace;

  for (const callSite of stack) {
    const filename = callSite.getFileName();
    if (filename != null && filename.match(/test.js$/)) {
      return {
        filename,
        line: callSite.getLineNumber(),
        column: callSite.getColumnNumber(),
      };
    }
  }
  return null;
}

Fix the tool tests when run with buck
Fix the tool tests when run with buck

Summary:
Not sure why source-map-support is starting to return `null` as the
filename for some stack frames. Also not sure why this only happens when run
with buck. This does fix all the tests, though.

Reviewed By: samwgoldman

Differential Revision: D5427555

fbshipit-source-id: 2d30606452a11e2c2d744cf07c04af2f35deae38
