[Pkg-javascript-commits] [node-detective] 61/119: adding support to provide custom parse opts

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 181cfc2571a48ab6e9739e1f285bc960c1a1decf
Author: Thorsten Lorenz <thlorenz at gmx.de>
Date:   Fri Jan 17 11:22:15 2014 -0500

    adding support to provide custom parse opts
---
 index.js          |  8 ++++---
 readme.markdown   |  2 ++
 test/parseopts.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index b33ca72..7855c26 100644
--- a/index.js
+++ b/index.js
@@ -21,8 +21,8 @@ var traverse = function (node, cb) {
     }
 };
 
-var walk = function (src, cb) {
-    var ast = esprima.parse(src);
+var walk = function (src, opts, cb) {
+    var ast = esprima.parse(src, opts);
     traverse(ast, cb);
 };
 
@@ -32,6 +32,8 @@ var exports = module.exports = function (src, opts) {
 
 exports.find = function (src, opts) {
     if (!opts) opts = {};
+    opts.parse = opts.parse || {};
+
     var word = opts.word === undefined ? 'require' : opts.word;
     if (typeof src !== 'string') src = String(src);
     src = '(function(){' + src.replace(/^#![^\n]*\n/, '') + '\n})()';
@@ -50,7 +52,7 @@ exports.find = function (src, opts) {
     
     if (src.indexOf(word) == -1) return modules;
     
-    walk(src, function (node) {
+    walk(src, opts.parse, function (node) {
         if (!isRequire(node)) return;
         if (node.arguments.length
         && node.arguments[0].type === 'Literal') {
diff --git a/readme.markdown b/readme.markdown
index df88da4..5223d50 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -63,6 +63,8 @@ which contains an AST node for each of the require() calls.
 You can use `opts.isRequire(node)` to return a boolean signifying whether an
 esprima AST `node` is a require call.
 
+You can use `opts.parse` to supply options parsed to the parser ([esprima](http://esprima.org/doc/index.html)).
+
 # install
 
 With [npm](https://npmjs.org) do:
diff --git a/test/parseopts.js b/test/parseopts.js
new file mode 100644
index 0000000..a8cd4d1
--- /dev/null
+++ b/test/parseopts.js
@@ -0,0 +1,62 @@
+var 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], 12, 'includes range start');
+    t.equal(range[1], 24, '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], 12, 'includes range start');
+    t.equal(range[1], 24, 'includes range end');
+
+    var loc = modules.nodes[0].loc;
+    t.equal(loc.start.line, 1, 'includes start line');
+    t.equal(loc.start.column, 12, 'includes start column');
+    t.equal(loc.end.line, 1, 'includes end line');
+    t.equal(loc.end.column, 24, 'includes end column');
+    t.end();
+});

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