<commit_before>const { urlToRequest } = require('loader-utils');

function replaceTemplateUrl(variableName, lines, resolver) {
  const regEx = /(^\s*templateUrl:\s*)['"](.*)['"](,*)$/;
  const lineNumbers = lines.reduce(
    (result, line, i) => (/templateUrl/.test(line) ? result.concat(i) : result),
    []
  );
  const resolverFunc = resolver || urlToRequest;

  if (!lineNumbers.length) {
    return lines;
  }

  const templateRequires = lineNumbers
    .map((lineNumber, i) => {
      const [, , templateUrl] = regEx.exec(lines[lineNumber]) || [];

      if (!templateUrl) {
        return null;
      }

      if (resolver && typeof resolver(templateUrl) !== 'string') {
        throw new Error(
          `Expected path resolver to return string for ${templateUrl}`
        );
      }

      const lineReplacement = lines[lineNumber].replace(
        regEx,
        `$1${variableName}${i + 1}$3`
      );

      return {
        templateUrl,
        lineNumber,
        lineReplacement
      };
    })
    .filter(i => i !== null);

  if (templateRequires.length === 0) {
    return lines;
  }

  const updatedLines = lines;

  templateRequires.forEach(x => {
    updatedLines[x.lineNumber] = x.lineReplacement;
  });

  return [
    ...templateRequires.map(
      (x, i) =>
        `const ${variableName}${i + 1} = require('${resolverFunc(
          x.templateUrl
        )}');`
    ),
    ``,
    ...updatedLines
  ];
}

module.exports = {
  replaceTemplateUrl
};
<commit_msg>fix template variable when templateUrl is null<commit_after>const { urlToRequest } = require('loader-utils');

function replaceTemplateUrl(variableName, lines, resolver) {
  const regEx = /(^\s*templateUrl:\s*)['"](.*)['"](,*)$/;
  const lineNumbers = lines.reduce(
    (result, line, i) => (/templateUrl/.test(line) ? result.concat(i) : result),
    []
  );
  const resolverFunc = resolver || urlToRequest;

  if (!lineNumbers.length) {
    return lines;
  }

  const templateRequires = lineNumbers
    .map((lineNumber, i) => {
      const [, , templateUrl] = regEx.exec(lines[lineNumber]) || [];

      if (!templateUrl) {
        return null;
      }

      if (resolver && typeof resolver(templateUrl) !== 'string') {
        throw new Error(
          `Expected path resolver to return string for ${templateUrl}`
        );
      }

      const lineReplacement = lines[lineNumber].replace(
        regEx,
        `$1${variableName}${i + 1}$3`
      );

      const lineVariable = `${variableName}${i + 1}`
      return {
        lineVariable,
        templateUrl,
        lineNumber,
        lineReplacement
      };
    })
    .filter(i => i !== null);

  if (templateRequires.length === 0) {
    return lines;
  }

  const updatedLines = lines;

  templateRequires.forEach(x => {
    updatedLines[x.lineNumber] = x.lineReplacement;
  });

  return [
    ...templateRequires.map(
      (x, i) =>
        `const ${x.lineVariable} = require('${resolverFunc(
          x.templateUrl
        )}');`
    ),
    ``,
    ...updatedLines
  ];
}

module.exports = {
  replaceTemplateUrl
};

{'fraction_non_alphanumeric': 0.16005204944697463, 'fraction_numerical': 0.0032530904359141183, 'mean_word_length': 2.4824462061155153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1006938', 'n_tokens_mistral': 991, 'n_tokens_neox': 958, 'n_words': 192}