You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.7 KiB

2 years ago
  1. # detective
  2. find all calls to `require()` by walking the AST
  3. [![build status](https://secure.travis-ci.org/browserify/detective.png)](http://travis-ci.org/browserify/detective)
  4. # example
  5. ## strings
  6. strings_src.js:
  7. ``` js
  8. var a = require('a');
  9. var b = require('b');
  10. var c = require('c');
  11. ```
  12. strings.js:
  13. ``` js
  14. var detective = require('detective');
  15. var fs = require('fs');
  16. var src = fs.readFileSync(__dirname + '/strings_src.js');
  17. var requires = detective(src);
  18. console.dir(requires);
  19. ```
  20. output:
  21. ```
  22. $ node examples/strings.js
  23. [ 'a', 'b', 'c' ]
  24. ```
  25. # methods
  26. ``` js
  27. var detective = require('detective');
  28. ```
  29. ## detective(src, opts)
  30. Give some source body `src`, return an array of all the `require()` calls with
  31. string arguments.
  32. The options parameter `opts` is passed along to `detective.find()`.
  33. ## var found = detective.find(src, opts)
  34. Give some source body `src`, return `found` with:
  35. * `found.strings` - an array of each string found in a `require()`
  36. * `found.expressions` - an array of each stringified expression found in a
  37. `require()` call
  38. * `found.nodes` (when `opts.nodes === true`) - an array of AST nodes for each
  39. argument found in a `require()` call
  40. Optionally:
  41. * `opts.word` - specify a different function name instead of `"require"`
  42. * `opts.nodes` - when `true`, populate `found.nodes`
  43. * `opts.isRequire(node)` - a function returning whether an AST `CallExpression`
  44. node is a require call
  45. * `opts.parse` - supply options directly to
  46. [acorn](https://npmjs.org/package/acorn) with some support for esprima-style
  47. options `range` and `loc`
  48. * `opts.ecmaVersion` - default: 9
  49. # install
  50. With [npm](https://npmjs.org) do:
  51. ```
  52. npm install detective
  53. ```
  54. # license
  55. MIT