[Pkg-javascript-commits] [node-detective] 86/119: Better word fast-path detection

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:44:37 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-detective.

commit 69eb099176adb98445e37ce2689a54e9b2ba7f9e
Author: Andres Suarez <zertosh at gmail.com>
Date:   Thu May 7 00:28:09 2015 -0400

    Better word fast-path detection
---
 index.js            |  5 ++++-
 test/complicated.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/index.js b/index.js
index d8abb7d..906c4ce 100644
--- a/index.js
+++ b/index.js
@@ -1,6 +1,8 @@
 var aparse = require('acorn').parse;
 var defined = require('defined');
 
+var requireRe = /\brequire\b/;
+
 function parse (src, opts) {
     if (!opts) opts = {};
     return aparse(src, {
@@ -67,7 +69,8 @@ exports.find = function (src, opts) {
     var modules = { strings : [], expressions : [] };
     if (opts.nodes) modules.nodes = [];
     
-    if (src.indexOf(word) == -1) return modules;
+    var wordRe = word === 'require' ? requireRe : RegExp('\\b' + word + '\\b');
+    if (!wordRe.test(src)) return modules;
     
     walk(src, opts.parse, function (node) {
         if (!isRequire(node)) return;
diff --git a/test/complicated.js b/test/complicated.js
new file mode 100644
index 0000000..aba5b91
--- /dev/null
+++ b/test/complicated.js
@@ -0,0 +1,56 @@
+var 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' ]);
+    });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-detective.git



More information about the Pkg-javascript-commits mailing list