All files ContextDependencyHelpers.js

10.53% Statements 2/19
0% Branches 0/15
0% Functions 0/2
10.53% Lines 2/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34    1x                     1x                                        
'use strict';
 
const ContextDependencyHelpers = exports;
 
/**
 * Escapes regular expression metacharacters
 * @param {string} str String to quote
 * @return {string} Escaped string
 */
function quotemeta(str) {
  return str.replace(/[-[\]\\/{}()*+?.^$|]/g, '\\$&');
}
 
ContextDependencyHelpers.create = function (Dep, range, param, expr, options, chunkName) {
  if (param.isWrapped() && (param.prefix && param.prefix.isString())) {
    let prefix = param.prefix.string;
    const prefixRange = param.prefix.range;
    const valueRange = [prefixRange ? prefixRange[1] : param.range[0], param.range[1]];
    const idx = prefix.lastIndexOf('/');
    let context = '.';
    if (idx >= 0) {
      context = prefix.substr(0, idx);
      prefix = `.${prefix.substr(idx)}`;
    }
    const regExp = new RegExp(`^${quotemeta(prefix)}.*$`);
    const dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, chunkName);
    dep.loc = expr.loc;
    dep.prepend = param.prefix && param.prefix.isString() ? prefix : null;
    dep.critical = options.wrappedContextCritical && 'a part of the request of a dependency is an expression';
    return dep;
  }
  return null;
};