'use babel';

/**
 * Dependencies
 */

import path from 'path';
import module from 'module';
import uniq from 'lodash.uniq';
import { allowUnsafeNewFunction } from 'loophole';
import CLIEngine from 'eslint/lib/cli-engine.js';

/**
 * Private
 */

const Module = module.Module;
const previousResolveLookupPaths = Module._resolveLookupPaths;
const engine = new CLIEngine();

/**
 * Interface
 */
export default function exec(filePath, fileText) {
  const nodeModulePaths = Module._nodeModulePaths(path.dirname(filePath));

  Module._resolveLookupPaths = (request, parent) => {
    if (parent.filename.match(/fast-eslint\/node_modules\/eslint/)) {
      parent.paths = uniq((parent.paths || []).concat(nodeModulePaths));
    }
    return previousResolveLookupPaths.call(this, request, parent);
  };

  return new Promise((resolve, reject) => {
    try {
      allowUnsafeNewFunction(() => {
        resolve(engine.executeOnText(fileText, filePath));
      });
    } catch (error) {
      reject(error);
    } finally {
      Module._resolveLookupPaths = previousResolveLookupPaths;
    }
  });
}

'use babel';

/**
 * Dependencies
 */

import path from 'path';
import module from 'module';
import uniq from 'lodash.uniq';
import { allowUnsafeNewFunction } from 'loophole';
import resolveModule from 'eslint/node_modules/resolve';
import CLIEngine from 'eslint/lib/cli-engine.js';

/**
 * Private
 */

let projectPaths = [];

const resolveModuleSync = resolveModule.sync;
resolveModule.sync = (x, opts) => {
  opts.paths = projectPaths;
  return resolveModuleSync(x, opts);
}

const engine = new CLIEngine();

/**
 * Interface
 */
export default function exec(filePath, fileText) {
  projectPaths = module.Module._nodeModulePaths(path.dirname(filePath)));

  return new Promise((resolve, reject) => {
    try {
      allowUnsafeNewFunction(() => {
        resolve(engine.executeOnText(fileText, filePath));
      });
    } catch (error) {
      reject(error);
    }
  });
}

fix unbundled config import with monkey patch