<commit_before>'use strict';

var path = require('path');
var config = require('./config');
var jscs;
var logger;

function registration (mimosaConfig, register) {
  logger = mimosaConfig.log;

  var extensions = getFileExtensions(mimosaConfig);
  if (extensions.length > 0) {
    register(['buildFile', 'add', 'update'],
             'afterCompile',
             onMimosaWorkflowCallback, extensions);
  }
}

/**
 * Based on a Mimosa config, return an array of file name extensions
 * the module should act on.
 */
function getFileExtensions(mimosaConfig) {
  var result = [];

  if (mimosaConfig.jscs.copied || mimosaConfig.jscs.vendor) {
    result.push('js');
  }

  if (mimosaConfig.jscs.compiled) {
    mimosaConfig.extensions.javascript.forEach(function (extension) {
      extension !== 'js' && result.push(extension);
    });
  }

  return result;
}

/**
 * Called by Mimosa when the module should act, see Mimosa doc for
 * details.
 */
function onMimosaWorkflowCallback(mimosaConfig, options, next) {
  if (shouldProcessFilesBasedOnOptions(mimosaConfig.jscs, options)) {
    processFiles(mimosaConfig, options.files);
  } else {
    logSkippedFiles(options);
  }

  next();
}

/**
 * Decides whether a given set of files should be JSCS linted or not
 * based on module configuration and Mimosa options - Mimosa options
 * keeps track of if the files are vendor files, copied or compiled,
 * etc.
 */
function shouldProcessFilesBasedOnOptions(moduleConfig, mimosaOptions) {
  // Special treatment of vendor files - setting vendor to true lints
  // copied vendor files regardless of copy option, compiled vendor
  // files are never linted
  if (mimosaOptions.isVendor) {
    return moduleConfig.vendor && mimosaOptions.isCopy;
  }

  if (mimosaOptions.isCopy && !moduleConfig.copied) {
    return false;
  }

  if (mimosaOptions.isJavascript &&
      !mimosaOptions.isCopy &&
      !moduleConfig.compiled)
  {
    return false;
  }

  return true;
}

/**
 * Debug logs that all files included in a Mimosa options object has
 * been skipped.
 */
function logSkippedFiles(mimosaOptions) {
  mimosaOptions.files.forEach(function (file) {
    var message = 'Not JSCS linting ';
    mimosaOptions.isCopy && (message += 'copied ');
    !mimosaOptions.isCopy &&
      mimosaOptions.isJavascript &&
      (message += 'compiled ');
    mimosaOptions.isVendor && (message += 'vendor ');
    message += 'file [[ ' + file.inputFileName + ' ]]';
    logger.debug(message);
  });
}

/**
 * Lints the files in an array that should be linted based on the
 * module configuration.
 */
function processFiles(mimosaConfig, files) {
  files.forEach(function (file) {
    if (shouldProcessFile(mimosaConfig, file)) {
      processFile(mimosaConfig.jscs, file);
    } else {
      logger.debug('Excluding [[ ' + file.inputFileName +
                   ' ]] from JSCS linting');
    }
  });
}

/**
 * Returns true of a given file should be linted based on the
 * configuration.
 */
function shouldProcessFile(mimosaConfig, file) {
  if (!file.outputFileText) {
    return false;
  }

  var relativePath = path.relative(mimosaConfig.watch.sourceDir,
                                   file.inputFileName);
  if (isFileExcludedBasedOnName(mimosaConfig.jscs,
                                file.inputFileName,
                                relativePath))
  {
    return false;
  }

  return true;
}

/**
 * Returns true if a file should be excluded based on file name
 * according to the module configuration.
 */
function isFileExcludedBasedOnName(moduleConfig,
                                   absolutePath,
                                   relativePath)
{
  // Note that config validation has modified moduleConfig.exlude and
  // create moduleConfig.excludeRegex
  if ((moduleConfig.exclude || []).indexOf(absolutePath) !== -1) {
    return true;
  }

  if (moduleConfig.excludeRegex &&
      relativePath.match(moduleConfig.excludeRegex))
  {<commit_msg>Cosmetic change to remove JSCS warning<commit_after>'use strict';

var path = require('path');
var config = require('./config');
var jscs;
var logger;

function registration (mimosaConfig, register) {
  logger = mimosaConfig.log;

  var extensions = getFileExtensions(mimosaConfig);
  if (extensions.length > 0) {
    register(['buildFile', 'add', 'update'],
             'afterCompile',
             onMimosaWorkflowCallback, extensions);
  }
}

/**
 * Based on a Mimosa config, return an array of file name extensions
 * the module should act on.
 */
function getFileExtensions(mimosaConfig) {
  var result = [];

  if (mimosaConfig.jscs.copied || mimosaConfig.jscs.vendor) {
    result.push('js');
  }

  if (mimosaConfig.jscs.compiled) {
    mimosaConfig.extensions.javascript.forEach(function (extension) {
      extension !== 'js' && result.push(extension);
    });
  }

  return result;
}

/**
 * Called by Mimosa when the module should act, see Mimosa doc for
 * details.
 */
function onMimosaWorkflowCallback(mimosaConfig, options, next) {
  if (shouldProcessFilesBasedOnOptions(mimosaConfig.jscs, options)) {
    processFiles(mimosaConfig, options.files);
  } else {
    logSkippedFiles(options);
  }

  next();
}

/**
 * Decides whether a given set of files should be JSCS linted or not
 * based on module configuration and Mimosa options - Mimosa options
 * keeps track of if the files are vendor files, copied or compiled,
 * etc.
 */
function shouldProcessFilesBasedOnOptions(moduleConfig, mimosaOptions) {
  // Special treatment of vendor files - setting vendor to true lints
  // copied vendor files regardless of copy option, compiled vendor
  // files are never linted
  if (mimosaOptions.isVendor) {
    return moduleConfig.vendor && mimosaOptions.isCopy;
  }

  if (mimosaOptions.isCopy && !moduleConfig.copied) {
    return false;
  }

  if (mimosaOptions.isJavascript &&
      !mimosaOptions.isCopy &&
      !moduleConfig.compiled)
  {
    return false;
  }

  return true;
}

/**
 * Debug logs that all files included in a Mimosa options object has
 * been skipped.
 */
function logSkippedFiles(mimosaOptions) {
  mimosaOptions.files.forEach(function (file) {
    var message = 'Not JSCS linting ';
    mimosaOptions.isCopy && (message += 'copied ');
    !mimosaOptions.isCopy &&
      mimosaOptions.isJavascript &&
      (message += 'compiled ');
    mimosaOptions.isVendor && (message += 'vendor ');
    message += 'file [[ ' + file.inputFileName + ' ]]';
    logger.debug(message);
  });
}

/**
 * Lints the files in an array that should be linted based on the
 * module configuration.
 */
function processFiles(mimosaConfig, files) {
  files.forEach(function (file) {
    if (shouldProcessFile(mimosaConfig, file)) {
      processFile(mimosaConfig.jscs, file);
    } else {
      logger.debug('Excluding [[ ' + file.inputFileName +
                   ' ]] from JSCS linting');
    }
  });
}

/**
 * Returns true of a given file should be linted based on the
 * configuration.
 */
function shouldProcessFile(mimosaConfig, file) {
  if (!file.outputFileText) {
    return false;
  }

  var relativePath = path.relative(mimosaConfig.watch.sourceDir,
                                   file.inputFileName);
  if (isFileExcludedBasedOnName(mimosaConfig.jscs,
                                file.inputFileName,
                                relativePath))
  {
    return false;
  }

  return true;
}

/**
 * Returns true if a file should be excluded based on file name
 * according to the module configuration.
 */
function isFileExcludedBasedOnName(moduleConfig, absolutePath, relativePath) {
  // Note that config validation has modified moduleConfig.exlude and
  // create moduleConfig.excludeRegex
  if ((moduleConfig.exclude || []).indexOf(absolutePath) !== -1) {
    return true;
  }

  if (moduleConfig.excludeRegex &&
      relativePath.match(moduleConfig.excludeRegex))
  {
{'fraction_non_alphanumeric': 0.10996563573883161, 'fraction_numerical': 0.0005091001654575538, 'mean_word_length': 3.088449531737773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6383895', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2336, 'n_words': 621}