[Pkg-javascript-commits] [node-detective] 65/119: Adding support for finding requires in ES6 code

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:44:35 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 be95efb7abac4bd0c3fc52f19158cef747b108b4
Author: Joakim Bengtson <joakim.bengtson at gmail.com>
Date:   Thu Jan 23 20:14:00 2014 +0100

    Adding support for finding requires in ES6 code
---
 index.js                 | 14 +++++------
 package.json             | 60 ++++++++++++++++++++++++------------------------
 test/files/generators.js |  5 ++++
 test/files/yield.js      |  2 ++
 test/generators.js       |  9 ++++++++
 test/yield.js            |  9 ++++++++
 6 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/index.js b/index.js
index d0c89bf..ce6faa3 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,4 @@
-var esprima = require('esprima');
+var esprima = require('esprima-six');
 var escodegen = require('escodegen');
 
 var traverse = function (node, cb) {
@@ -12,7 +12,7 @@ var traverse = function (node, cb) {
     }
     else if (node && typeof node === 'object') {
         cb(node);
-        
+
         Object.keys(node).forEach(function (key) {
             if (key === 'parent' || !node[key]) return;
             node[key].parent = node;
@@ -38,7 +38,7 @@ exports.find = function (src, opts) {
     var word = opts.word === undefined ? 'require' : opts.word;
     if (typeof src !== 'string') src = String(src);
     src = src.replace(/^#![^\n]*\n/, '');
-    
+
     var isRequire = opts.isRequire || function (node) {
         var c = node.callee;
         return c
@@ -47,12 +47,12 @@ exports.find = function (src, opts) {
             && c.name === word
         ;
     }
-    
+
     var modules = { strings : [], expressions : [] };
     if (opts.nodes) modules.nodes = [];
-    
+
     if (src.indexOf(word) == -1) return modules;
-    
+
     walk(src, opts.parse, function (node) {
         if (!isRequire(node)) return;
         if (node.arguments.length
@@ -64,6 +64,6 @@ exports.find = function (src, opts) {
         }
         if (opts.nodes) modules.nodes.push(node);
     });
-    
+
     return modules;
 };
diff --git a/package.json b/package.json
index 1e15768..9ddc647 100644
--- a/package.json
+++ b/package.json
@@ -1,32 +1,32 @@
 {
-    "name" : "detective",
-    "description" : "find all require() calls by walking the AST",
-    "version" : "2.3.0",
-    "repository" : {
-        "type" : "git",
-        "url" : "git://github.com/substack/node-detective.git"
-    },
-    "main" : "index.js",
-    "keywords" : [
-        "require",
-        "source",
-        "analyze",
-        "ast"
-    ],
-    "scripts" : {
-        "test" : "tap test/*.js"
-    },
-    "dependencies" : {
-        "esprima" : "1.0.2",
-        "escodegen": "0.0.15"
-    },
-    "devDependencies" : {
-        "tap" : "~0.4.0"
-    },
-    "license" : "MIT",
-    "author" : {
-        "name" : "James Halliday",
-        "email" : "mail at substack.net",
-        "url" : "http://substack.net"
-    }
+  "name": "detective",
+  "description": "find all require() calls by walking the AST",
+  "version": "2.3.0",
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/substack/node-detective.git"
+  },
+  "main": "index.js",
+  "keywords": [
+    "require",
+    "source",
+    "analyze",
+    "ast"
+  ],
+  "scripts": {
+    "test": "tap test/*.js"
+  },
+  "dependencies": {
+    "escodegen": "0.0.15",
+    "esprima-six": "0.0.3"
+  },
+  "devDependencies": {
+    "tap": "~0.4.0"
+  },
+  "license": "MIT",
+  "author": {
+    "name": "James Halliday",
+    "email": "mail at substack.net",
+    "url": "http://substack.net"
+  }
 }
diff --git a/test/files/generators.js b/test/files/generators.js
new file mode 100644
index 0000000..1c1c2c9
--- /dev/null
+++ b/test/files/generators.js
@@ -0,0 +1,5 @@
+var a = require('a');
+
+function *gen() {
+  yield require('b');
+}
\ No newline at end of file
diff --git a/test/files/yield.js b/test/files/yield.js
new file mode 100644
index 0000000..8f4ac5e
--- /dev/null
+++ b/test/files/yield.js
@@ -0,0 +1,2 @@
+var a = require('a');
+var b = yield require('c')(a);
diff --git a/test/generators.js b/test/generators.js
new file mode 100644
index 0000000..c16d534
--- /dev/null
+++ b/test/generators.js
@@ -0,0 +1,9 @@
+var 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' ]);
+});
diff --git a/test/yield.js b/test/yield.js
new file mode 100644
index 0000000..85560ab
--- /dev/null
+++ b/test/yield.js
@@ -0,0 +1,9 @@
+var 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' ]);
+});

-- 
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