<commit_before>import fs from 'fs'

export default function (path, context) {

  if (context == null) throw new Error("need context to parse properly")

  let { parserOptions, parserPath } = context

  if (!parserPath) throw new Error("parserPath is required!")

  // hack: espree blows up with frozen options
  parserOptions = Object.assign({}, parserOptions)
  parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)

  // require the parser relative to the main module (i.e., ESLint)
  const parser = require.main.require(parserPath)

  return parser.parse(
    fs.readFileSync(path, {encoding: 'utf8'}),
    parserOptions)
}<commit_msg>find Espree by hunting down ESLint first<commit_after>import fs from 'fs'

export default function (p, context) {

  if (context == null) throw new Error("need context to parse properly")

  let { parserOptions, parserPath } = context

  if (!parserPath) throw new Error("parserPath is required!")

  // hack: espree blows up with frozen options
  parserOptions = Object.assign({}, parserOptions)
  parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)

  // require the parser relative to the main module (i.e., ESLint)
  const parser = requireParser(parserPath)

  return parser.parse(
    fs.readFileSync(p, {encoding: 'utf8'}),
    parserOptions)
}

import Module from 'module'
import * as path from 'path'

// borrowed from babel-eslint
function createModule(filename) {
  var mod = new Module(filename)
  mod.filename = filename
  mod.paths = Module._nodeModulePaths(path.dirname(filename))
  return mod
}

function requireParser(p) {
  try {
    // attempt to get espree relative to eslint
    const eslintPath = require.resolve('eslint')
    const eslintModule = createModule(eslintPath)
    return require(Module._resolveFilename('espree', eslintModule))
  } catch(err) { /* ignore */ }

  try {
    // try relative to entry point
    return require.main.require(p)
  } catch(err) { /* ignore */ }

  // finally, try from here
  return require(p)
}
{'fraction_non_alphanumeric': 0.11045655375552282, 'fraction_numerical': 0.0009818360333824251, 'mean_word_length': 4.095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7202980', 'n_tokens_mistral': 602, 'n_tokens_neox': 583, 'n_words': 191}