How to use detective - 9 common examples

To help you get started, we’ve selected a few detective examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github greenish / react-mount / node_modules / browserify-shim / node_modules / exposify / expose.js View on Github external
function expose(map, origSrc) {
  var regex, keys, id;
  var src = origSrc;

  keys = Object.keys(map);

  // ensure that at least one of the require statements we want to replace is in the code
  // before we perform the expensive operation of finding them by creating an AST
  var hasMatchingRequires = keys.some(function (id) {
    regex = new RegExp('require\\(\\s*[\'"]' + id + '[\'"]\\s*\\)');
    return regex.test(src)
  });
  if (!hasMatchingRequires) return src;

  var requires = detective.find(src, { nodes: true, parse: { range: true } });
  if (!requires.strings.length) return src;

  var replacements = keys
    .reduce(function (acc, id) {
      var r = getReplacements(id, map[id], requires);
      return acc.concat(r);
    }, [])
    .sort(rangeComparator);

  var offset = 0;
  return replacements
    .reduce(function(acc, replacement) {
      var from = replacement.from + offset
        , to   = replacement.to + offset
        , code = replacement.code;
github Lapple / ErrorBoard / node_modules / browserify-shim / node_modules / exposify / expose.js View on Github external
function expose(map, origSrc) {
  var regex, keys, id;
  var src = origSrc;

  keys = Object.keys(map);

  // ensure that at least one of the require statements we want to replace is in the code
  // before we perform the expensive operation of finding them by creating an AST
  var hasMatchingRequires = keys.some(function (id) {
    regex = new RegExp('require\\(\\s*[\'"]' + id + '[\'"]\\s*\\)');
    return regex.test(src)
  });
  if (!hasMatchingRequires) return src;

  var requires = detective.find(src, { nodes: true, parse: { range: true } });
  if (!requires.strings.length) return src;

  var replacements = keys
    .reduce(function (acc, id) {
      var r = getReplacements(id, map[id], requires);
      return acc.concat(r);
    }, [])
    .sort(rangeComparator);

  var offset = 0;
  return replacements
    .reduce(function(acc, replacement) {
      var from = replacement.from + offset
        , to   = replacement.to + offset
        , code = replacement.code;
github neo-one-suite / neo-one / packages / neo-one-editor-server / src / resolvePackage.ts View on Github external
return;
  }

  const file = files[resolvedPath];
  if (file !== undefined) {
    const content = file.toString('utf8');
    mutableResult[resolvedPath] = content;
    const dtsFilePath = nodePath.join(nodePath.dirname(resolvedPath), `${nodePath.basename(resolvedPath, '.js')}.d.ts`);
    const dtsFile = files[dtsFilePath];
    if (dtsFile !== undefined) {
      mutableResult[dtsFilePath] = dtsFile.toString('utf8');
    }

    let requires: ReadonlyArray | undefined;
    try {
      requires = detective(content);
    } catch {
      // do nothing
    }

    if (requires !== undefined) {
      requires
        .filter((req): req is string => typeof req === 'string')
        .filter((req) => req.startsWith('.'))
        .map((req) => nodePath.resolve(nodePath.dirname(resolvedPath), req))
        .forEach((req) => {
          resolveJavaScriptFiles(files, req, mapping, mutableResult);
        });
    }
  }
};
github splunk / splunk-sdk-javascript / node_modules / deputy / index.js View on Github external
deputy.find = function (src) {
        var h = hash(src);
        var c = cache[h];
        if (c) return c;
        else {
            c = detective.find(src);
            save(h, c);
            return c;
        }
    };
github johnkpaul / promethify / index.js View on Github external
function getAsyncRequires(source){
  var amdRequires = detective.find(source, {nodes: true});
  var amdRequireArray = amdRequires.expressions.map(function(arrString){
    var exps = require('esprima').parse(arrString).body[0].expression;
    var array = require('static-eval')(exps);
    return array;
  });
  return _.flatten(amdRequireArray);
}
github logicalparadox / folio / lib / require.js View on Github external
Require.prototype.require = function (module, opts) {
  var self = this;
  
  opts = opts || {};
  var file = resolve.sync(module, opts);
  var dir = path.dirname(file);
  var src = fs.readFileSync(file, 'utf-8');
  var reqs = detective.find(src);
  
  reqs.strings.forEach(function(req) {
    self.require(req, {
      basedir: dir
    });
  });
  
  this.modules[module] = {
    module: module,
    path: dir,
    body: src
  };
};
github nodejitsu / require-analyzer / lib / require-analyzer.js View on Github external
fs.readFile(options.target, function(err, data){
    if(err) return callback(err);

    var files;

    try {
      files = detective.find(data.toString('utf8'));
    } catch(e){
      return callback(err);
    }

    files.strings.forEach(function(name){
      if(name in options.packages) return;

      options.packages[name] = true;

      var absolutePath = analyzer.resolve(name, options.target);
      if(!absolutePath || path.relative(options.target, absolutePath).indexOf('node_modules') >= 0){
        return;
      }

      remaining++;
github RiseVision / rise-node / .devutils / package-deps / index.ts View on Github external
const parseNodeImports = (
  filePath: string,
  src: string
): IParseImportsResult => {
  return detective.find(src, { nodes: true });
};

detective

find all require() calls by walking the AST

MIT
Latest version published 2 years ago

Package Health Score

76 / 100
Full package analysis

Popular detective functions