pax_global_header00006660000000000000000000000064132066174360014522gustar00rootroot0000000000000052 comment=07ea3c5c8c15e933296c49c609d644d96d888e11 detective-4.6.0/000077500000000000000000000000001320661743600135055ustar00rootroot00000000000000detective-4.6.0/.gitignore000066400000000000000000000000411320661743600154700ustar00rootroot00000000000000node_modules bench/src/jquery.js detective-4.6.0/.npmignore000066400000000000000000000000241320661743600155000ustar00rootroot00000000000000bench/src/jquery.js detective-4.6.0/.travis.yml000066400000000000000000000000761320661743600156210ustar00rootroot00000000000000language: node_js node_js: - 9 - 8 - 6 - 4 - "0.12" detective-4.6.0/LICENSE000066400000000000000000000020611320661743600145110ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. detective-4.6.0/bench/000077500000000000000000000000001320661743600145645ustar00rootroot00000000000000detective-4.6.0/bench/detect.js000066400000000000000000000003141320661743600163700ustar00rootroot00000000000000var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/src/jquery.js', 'utf8'); var t0 = Date.now(); var requires = detective(src); console.log(Date.now() - t0); detective-4.6.0/bench/esprima_v_acorn.txt000066400000000000000000000002521320661743600204730ustar00rootroot00000000000000esprima: $ for i in {1..5}; do node detect.js; done 704 702 704 704 697 acorn: $ for i in {1..5}; do node detect.js; done 555 552 585 549 583 detective-4.6.0/example/000077500000000000000000000000001320661743600151405ustar00rootroot00000000000000detective-4.6.0/example/strings.js000066400000000000000000000002511320661743600171650ustar00rootroot00000000000000var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/strings_src.js'); var requires = detective(src); console.dir(requires); detective-4.6.0/example/strings_src.js000066400000000000000000000001021320661743600200270ustar00rootroot00000000000000var a = require('a'); var b = require('b'); var c = require('c'); detective-4.6.0/index.js000066400000000000000000000043451320661743600151600ustar00rootroot00000000000000var acorn = require('acorn'); var walk = require('acorn/dist/walk'); var defined = require('defined'); var requireRe = /\brequire\b/; function parse (src, opts) { if (!opts) opts = {}; return acorn.parse(src, { ecmaVersion: defined(opts.ecmaVersion, 8), sourceType: opts.sourceType, ranges: defined(opts.ranges, opts.range), locations: defined(opts.locations, opts.loc), allowReserved: defined(opts.allowReserved, true), allowReturnOutsideFunction: defined( opts.allowReturnOutsideFunction, true ), allowImportExportEverywhere: defined( opts.allowImportExportEverywhere, true ), allowHashBang: defined(opts.allowHashBang, true) }); } var exports = module.exports = function (src, opts) { return exports.find(src, opts).strings; }; exports.find = function (src, opts) { if (!opts) opts = {}; var word = opts.word === undefined ? 'require' : opts.word; if (typeof src !== 'string') src = String(src); var isRequire = opts.isRequire || function (node) { return node.callee.type === 'Identifier' && node.callee.name === word ; }; var modules = { strings : [], expressions : [] }; if (opts.nodes) modules.nodes = []; var wordRe = word === 'require' ? requireRe : RegExp('\\b' + word + '\\b'); if (!wordRe.test(src)) return modules; var ast = parse(src, opts.parse); function visit(node, st, c) { var hasRequire = wordRe.test(src.slice(node.start, node.end)); if (!hasRequire) return; walk.base[node.type](node, st, c); if (node.type !== 'CallExpression') return; if (isRequire(node)) { if (node.arguments.length) { var arg = node.arguments[0]; if (arg.type === 'Literal') { modules.strings.push(arg.value); } else { modules.expressions.push(src.slice(arg.start, arg.end)); } } if (opts.nodes) modules.nodes.push(node); } } walk.recursive(ast, null, { Statement: visit, Expression: visit }); return modules; }; detective-4.6.0/package.json000066400000000000000000000011431320661743600157720ustar00rootroot00000000000000{ "name": "detective", "description": "find all require() calls by walking the AST", "version": "4.6.0", "repository": { "type": "git", "url": "git://github.com/browserify/detective.git" }, "main": "index.js", "keywords": [ "require", "source", "analyze", "ast" ], "scripts": { "test": "tap test/*.js" }, "dependencies": { "acorn": "^5.2.1", "defined": "^1.0.0" }, "devDependencies": { "tap": "^10.7.3" }, "license": "MIT", "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" } } detective-4.6.0/readme.markdown000066400000000000000000000032421320661743600165070ustar00rootroot00000000000000# detective find all calls to `require()` by walking the AST [![build status](https://secure.travis-ci.org/browserify/detective.png)](http://travis-ci.org/browserify/detective) # example ## strings strings_src.js: ``` js var a = require('a'); var b = require('b'); var c = require('c'); ``` strings.js: ``` js var detective = require('detective'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/strings_src.js'); var requires = detective(src); console.dir(requires); ``` output: ``` $ node examples/strings.js [ 'a', 'b', 'c' ] ``` # methods ``` js var detective = require('detective'); ``` ## detective(src, opts) Give some source body `src`, return an array of all the `require()` calls with string arguments. The options parameter `opts` is passed along to `detective.find()`. ## var found = detective.find(src, opts) Give some source body `src`, return `found` with: * `found.strings` - an array of each string found in a `require()` * `found.expressions` - an array of each stringified expression found in a `require()` call * `found.nodes` (when `opts.nodes === true`) - an array of AST nodes for each argument found in a `require()` call Optionally: * `opts.word` - specify a different function name instead of `"require"` * `opts.nodes` - when `true`, populate `found.nodes` * `opts.isRequire(node)` - a function returning whether an AST `CallExpression` node is a require call * `opts.parse` - supply options directly to [acorn](https://npmjs.org/package/acorn) with some support for esprima-style options `range` and `loc` * `opts.ecmaVersion` - default: 8 # install With [npm](https://npmjs.org) do: ``` npm install detective ``` # license MIT detective-4.6.0/test/000077500000000000000000000000001320661743600144645ustar00rootroot00000000000000detective-4.6.0/test/both.js000066400000000000000000000015131320661743600157560ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/both.js'); test('both', function (t) { var modules = detective.find(src); t.deepEqual(modules.strings, [ 'a', 'b' ]); t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]); t.notOk(modules.nodes, 'has no nodes'); t.end(); }); test('both with nodes specified in opts', function (t) { var modules = detective.find(src, { nodes: true }); t.deepEqual(modules.strings, [ 'a', 'b' ]); t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]); t.deepEqual( modules.nodes.map(function (n) { var arg = n.arguments[0]; return arg.value || arg.left.value; }), [ 'a', 'b', 'c', 'd' ], 'has a node for each require'); t.end(); }); detective-4.6.0/test/chained.js000066400000000000000000000003711320661743600164160ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/chained.js'); test('chained', function (t) { t.deepEqual(detective(src), [ 'c', 'b', 'a' ]); t.end(); }); detective-4.6.0/test/complicated.js000066400000000000000000000033041320661743600173060ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var sources = [ 'require("a")', ';require("a")', ' require("a")', 'void require("a")', '+require("a")', '!require("a")', '/*comments*/require("a")', '(require("a"))', 'require/*comments*/("a")', ';require/*comments*/("a")', ' require/*comments*/("a")', 'void require/*comments*/("a")', '+require/*comments*/("a")', '!require/*comments*/("a")', '/*comments*/require/*comments*/("a")', '(require/*comments*/("a"))', 'require /*comments*/ ("a")', ';require /*comments*/ ("a")', ' require /*comments*/ ("a")', 'void require /*comments*/ ("a")', '+require /*comments*/ ("a")', '!require /*comments*/ ("a")', ' /*comments*/ require /*comments*/ ("a")', '(require /*comments*/ ("a"))', 'require /*comments*/ /*more comments*/ ("a")', ';require /*comments*/ /*more comments*/ ("a")', ' require /*comments*/ /*more comments*/ ("a")', 'void require /*comments*/ /*more comments*/ ("a")', '+require /*comments*/ /*more comments*/ ("a")', '!require /*comments*/ /*more comments*/ ("a")', ' /*comments*/ /*more comments*/ require /*comments*/ /*more comments*/ ("a")', '(require /*comments*/ /*more comments*/ ("a"))', 'require//comments\n("a")', ';require//comments\n("a")', ' require//comments\n("a")', 'void require//comments\n("a")', '+require//comments\n("a")', '!require//comments\n("a")', ' require//comments\n("a")', '(require//comments\n("a"))' ]; test('complicated', function (t) { t.plan(sources.length); sources.forEach(function(src) { t.deepEqual(detective(src), [ 'a' ]); }); }); detective-4.6.0/test/es6-module.js000066400000000000000000000004351320661743600170040ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/es6-module.js'); test('es6-module', function (t) { t.plan(1); t.deepEqual(detective(src, {parse: {sourceType: 'module'}}), [ 'a', 'b' ]); }); detective-4.6.0/test/files/000077500000000000000000000000001320661743600155665ustar00rootroot00000000000000detective-4.6.0/test/files/both.js000066400000000000000000000001161320661743600170560ustar00rootroot00000000000000require('a'); require('b'); require('c' + x); var moo = require('d' + y).moo; detective-4.6.0/test/files/chained.js000066400000000000000000000001031320661743600175110ustar00rootroot00000000000000 require('c').hello().goodbye() require('b').hello() require('a') detective-4.6.0/test/files/es6-module.js000066400000000000000000000001201320661743600200750ustar00rootroot00000000000000var a = require('a'); export default function () { var b = require('b'); } detective-4.6.0/test/files/generators.js000066400000000000000000000001001320661743600202640ustar00rootroot00000000000000var a = require('a'); function *gen() { yield require('b'); }detective-4.6.0/test/files/isrequire.js000066400000000000000000000005031320661743600201320ustar00rootroot00000000000000var a = require.async('a'); var b = require.async('b'); var c = require.async('c'); var abc = a.b(c); var EventEmitter = require.async('events').EventEmitter; var x = require.async('doom')(5,6,7); x(8,9); c.load('notthis'); var y = require.async('y') * 100; var EventEmitter2 = require.async('events2').EventEmitter(); detective-4.6.0/test/files/nested.js000066400000000000000000000004451320661743600174110ustar00rootroot00000000000000 if (true) { (function () { require('a'); })(); } if (false) { (function () { var x = 10; switch (x) { case 1 : require('b'); break; default : break; } })() } function qqq () { require ( "c" ); } detective-4.6.0/test/files/set-in-object-pattern.js000066400000000000000000000002341320661743600222410ustar00rootroot00000000000000var a = load('a'); var b = load('b'); var c = load('c'); var abc = a.b(c); function load2({set = 'hello'}) { return load('tt'); } var loadUse = load2(); detective-4.6.0/test/files/shebang.js000066400000000000000000000001271320661743600175330ustar00rootroot00000000000000#!/usr/bin/env node var a = require('a'); var b = require('b'); var c = require('c'); detective-4.6.0/test/files/sparse-array.js000066400000000000000000000000411320661743600205300ustar00rootroot00000000000000var o = [,,,,] require('./foo') detective-4.6.0/test/files/strings.js000066400000000000000000000004321320661743600176140ustar00rootroot00000000000000var a = require('a'); var b = require('b'); var c = require('c'); var abc = a.b(c); var EventEmitter = require('events').EventEmitter; var x = require('doom')(5,6,7); x(8,9); c.require('notthis'); var y = require('y') * 100; var EventEmitter2 = require('events2').EventEmitter();detective-4.6.0/test/files/word.js000066400000000000000000000004031320661743600170740ustar00rootroot00000000000000var a = load('a'); var b = load('b'); var c = load('c'); var abc = a.b(c); var EventEmitter = load('events').EventEmitter; var x = load('doom')(5,6,7); x(8,9); c.load('notthis'); var y = load('y') * 100; var EventEmitter2 = load('events2').EventEmitter(); detective-4.6.0/test/files/yield.js000066400000000000000000000001241320661743600172270ustar00rootroot00000000000000(function * () { var a = require('a'); var b = yield require('c')(a); })(); detective-4.6.0/test/generators.js000066400000000000000000000003741320661743600171770ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/generators.js'); test('generators', function (t) { t.plan(1); t.deepEqual(detective(src), [ 'a', 'b' ]); }); detective-4.6.0/test/isrequire.js000066400000000000000000000012471320661743600170360ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/isrequire.js'); test('word', function (t) { t.deepEqual( detective(src, { isRequire: function(node) { return (node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && node.callee.object.type == 'Identifier' && node.callee.object.name == 'require' && node.callee.property.type == 'Identifier' && node.callee.property.name == 'async') } }), [ 'a', 'b', 'c', 'events', 'doom', 'y', 'events2' ] ); t.end(); }); detective-4.6.0/test/nested.js000066400000000000000000000003671320661743600163120ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/nested.js'); test('nested', function (t) { t.deepEqual(detective(src), [ 'a', 'b', 'c' ]); t.end(); }); detective-4.6.0/test/noargs.js000066400000000000000000000015661320661743600163230ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); // in order to use detective to find any function // it needs to properly handle functions called without args var src = [ 'fn();', 'otherfn();', 'fn();' ].join('\n') test('noargs', function (t) { t.plan(1); t.deepEqual(detective(src, { word: 'fn' }).length, 0, 'finds no arg id'); }); test('find noargs with nodes', function (t) { t.plan(4); var modules = detective.find(src, { word: 'fn', nodes: true }); t.equal(modules.strings.length, 0, 'finds no arg id'); t.equal(modules.expressions.length, 0, 'finds no expressions'); t.equal(modules.nodes.length, 2, 'finds a node for each matching function call'); t.equal( modules.nodes.filter(function (x) { return x.callee.name === 'fn' }).length, 2, 'all matches are correct' ); }); detective-4.6.0/test/parseopts.js000066400000000000000000000043361320661743600170500ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/both.js'); test('nodes specified in opts and parseopts { range: true }', function (t) { var modules = detective.find(src, { nodes: true, parse: { range: true } }); t.deepEqual(modules.strings, [ 'a', 'b' ]); t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]); t.deepEqual( modules.nodes.map(function (n) { var arg = n.arguments[0]; return arg.value || arg.left.value; }), [ 'a', 'b', 'c', 'd' ], 'has a node for each require'); var range = modules.nodes[0].range; t.equal(range[0], 0, 'includes range start'); t.equal(range[1], 12, 'includes range end'); t.end(); }); test('nodes specified in opts and parseopts { range: false }', function (t) { var modules = detective.find(src, { nodes: true, parse: { range: false } }); t.deepEqual(modules.strings, [ 'a', 'b' ]); t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]); t.deepEqual( modules.nodes.map(function (n) { var arg = n.arguments[0]; return arg.value || arg.left.value; }), [ 'a', 'b', 'c', 'd' ], 'has a node for each require'); t.notOk(modules.nodes[0].range, 'includes no ranges'); t.end(); }); test('nodes specified in opts and parseopts { range: true, loc: true }', function (t) { var modules = detective.find(src, { nodes: true, parse: { range: true, loc: true } }); t.deepEqual(modules.strings, [ 'a', 'b' ]); t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]); t.deepEqual( modules.nodes.map(function (n) { var arg = n.arguments[0]; return arg.value || arg.left.value; }), [ 'a', 'b', 'c', 'd' ], 'has a node for each require'); var range = modules.nodes[0].range; t.equal(range[0], 0, 'includes range start'); t.equal(range[1], 12, 'includes range end'); var loc = modules.nodes[0].loc; t.equal(loc.start.line, 1, 'includes start line'); t.equal(loc.start.column, 0, 'includes start column'); t.equal(loc.end.line, 1, 'includes end line'); t.equal(loc.end.column, 12, 'includes end column'); t.end(); }); detective-4.6.0/test/return.js000066400000000000000000000003321320661743600163370ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = [ 'require("a")\nreturn' ]; test('return', function (t) { t.plan(1); t.deepEqual(detective(src), [ 'a' ]); }); detective-4.6.0/test/set-in-object-pattern.js000066400000000000000000000005031320661743600211360ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/set-in-object-pattern.js'); test('set in object pattern', function (t) { t.deepEqual( detective(src, { word : 'load' }), [ 'a', 'b', 'c', 'tt' ] ); t.end(); });detective-4.6.0/test/shebang.js000066400000000000000000000003731320661743600164340ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/shebang.js'); test('shebang', function (t) { t.plan(1); t.deepEqual(detective(src), [ 'a', 'b', 'c' ]); }); detective-4.6.0/test/sparse-array.js000066400000000000000000000004711320661743600174350ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/sparse-array.js'); test('sparse-array', function (t) { //just check that this does not crash. t.doesNotThrow(function () { detective(src) }) t.end(); }); detective-4.6.0/test/strings.js000066400000000000000000000004321320661743600165120ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/strings.js'); test('single', function (t) { t.deepEqual(detective(src), [ 'a', 'b', 'c', 'events', 'doom', 'y', 'events2' ]); t.end(); }); detective-4.6.0/test/word.js000066400000000000000000000004761320661743600160040ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/word.js'); test('word', function (t) { t.deepEqual( detective(src, { word : 'load' }), [ 'a', 'b', 'c', 'events', 'doom', 'y', 'events2' ] ); t.end(); }); detective-4.6.0/test/yield.js000066400000000000000000000003621320661743600161310ustar00rootroot00000000000000var test = require('tap').test; var detective = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/yield.js'); test('yield', function (t) { t.plan(1); t.deepEqual(detective(src), [ 'a', 'c' ]); });