<commit_before>const path = require("path");
const fs = require("fs");
const eachSeries = require("async/eachSeries");

class FS {
  constructor(workingDirectory, contractsBuildDirectory) {
    this.workingDirectory = workingDirectory;
    this.contractsBuildDirectory = contractsBuildDirectory;
  }

  require(importPath, searchPath = this.contractsBuildDirectory) {
    const normalizedImportPath = path.normalize(normalizedImportPath);
    const contractName = this.getContractName(normalizedImportPath, searchPath);

    // If we have an absoulte path, only check the file if it's a child of the workingDirectory.
    if (path.isAbsolute(normalizedImportPath)) {
      if (normalizedImportPath.indexOf(this.workingDirectory) !== 0) {
        return null;
      }
    }

    try {
      const result = fs.readFileSync(
        path.join(searchPath, `${contractName}.json`),
        "utf8"
      );
      return JSON.parse(result);
    } catch (e) {
      return null;
    }
  }

  getContractName(sourcePath, searchPath = this.contractsBuildDirectory) {
    const contractsBuildDirFiles = fs.readdirSync(searchPath);
    const filteredBuildArtifacts = contractsBuildDirFiles.filter(
      file => file.match(".json") != null
    );

    for (let i = 0; i < filenames.length; i++) {
      const filename = filenames[i];

      const artifact = JSON.parse(
        fs.readFileSync(path.resolve(searchPath, filename))
      );

      if (artifact.sourcePath === sourcePath) {
        return artifact.contractName;
      }
    }

    // fallback
    return path.basename(sourcePath, ".sol");
  }

  resolve(importPath, importedFrom = "", callback) {
    const possiblePaths = [
      importPath,
      path.join(path.dirname(importedFrom), importPath)
    ];<commit_msg>use forEach instead of for loop in getContractName() (truffle-resolver/fs)<commit_after>const path = require("path");
const fs = require("fs");
const eachSeries = require("async/eachSeries");

class FS {
  constructor(workingDirectory, contractsBuildDirectory) {
    this.workingDirectory = workingDirectory;
    this.contractsBuildDirectory = contractsBuildDirectory;
  }

  require(importPath, searchPath = this.contractsBuildDirectory) {
    const normalizedImportPath = path.normalize(importPath);
    const contractName = this.getContractName(normalizedImportPath, searchPath);

    // If we have an absoulte path, only check the file if it's a child of the workingDirectory.
    if (path.isAbsolute(normalizedImportPath)) {
      if (normalizedImportPath.indexOf(this.workingDirectory) !== 0) {
        return null;
      }
    }

    try {
      const result = fs.readFileSync(
        path.join(searchPath, `${contractName}.json`),
        "utf8"
      );
      return JSON.parse(result);
    } catch (e) {
      return null;
    }
  }

  getContractName(sourcePath, searchPath = this.contractsBuildDirectory) {
    const contractsBuildDirFiles = fs.readdirSync(searchPath);
    const filteredBuildArtifacts = contractsBuildDirFiles.filter(
      file => file.match(".json") != null
    );

    filteredBuildArtifacts.forEach(buildArtifact => {
      const artifact = JSON.parse(
        fs.readFileSync(path.resolve(searchPath, buildArtifact))
      );

      if (artifact.sourcePath === sourcePath) {
        return artifact.contractName;
      }
    });

    // fallback
    return path.basename(sourcePath, ".sol");
  }

  resolve(importPath, importedFrom = "", callback) {
    const possiblePaths = [
      importPath,
      path.join(path.dirname(importedFrom), importPath)
    ];
{'fraction_non_alphanumeric': 0.11142295818130789, 'fraction_numerical': 0.0014033118158854898, 'mean_word_length': 3.341047503045067, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '5263229', 'n_tokens_mistral': 1024, 'n_tokens_neox': 995, 'n_words': 215}