[Pkg-javascript-commits] [node-acorn-jsx] 96/484: Allow passing a test predicate to findNodeAt and findNodeAround

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:10 UTC 2017


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

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

commit da83f80d295b6e87ceba09574235475756a499a9
Author: Marijn Haverbeke <marijnh at gmail.com>
Date:   Tue Feb 12 17:35:04 2013 +0100

    Allow passing a test predicate to findNodeAt and findNodeAround
---
 util/walk.js | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/util/walk.js b/util/walk.js
index 8e9233c..f75aa1a 100644
--- a/util/walk.js
+++ b/util/walk.js
@@ -41,12 +41,22 @@
     c(node, state);
   };
 
+  function makeTest(test) {
+    if (typeof test == "string")
+      return function(node) { node.type == test; };
+    else if (!test)
+      return function() { return true; };
+    else
+      return test;
+  }
+
   function Found(node, state) { this.node = node; this.state = state; }
 
   // Find a node with a given start, end, and type (all are optional,
   // null can be used as wildcard). Returns a {node, state} object, or
   // undefined when it doesn't find a matching node.
-  exports.findNodeAt = function(node, start, end, targetType, base, state) {
+  exports.findNodeAt = function(node, start, end, test, base, state) {
+    test = makeTest(test);
     try {
       if (!base) base = exports.base;
       var c = function(node, st, override) {
@@ -54,7 +64,7 @@
         if ((start == null || node.start <= start) &&
             (end == null || node.end >= end))
           base[type](node, st, c);
-        if ((targetType == null || type == targetType) &&
+        if (test(node) &&
             (start == null || node.start == start) &&
             (end == null || node.end == end))
           throw new Found(node, st);
@@ -68,7 +78,8 @@
 
   // Find the innermost node of a given type that contains the given
   // position. Interface similar to findNodeAt.
-  exports.findNodeAround = function(node, pos, targetType, base, state) {
+  exports.findNodeAround = function(node, pos, test, base, state) {
+    test = makeTest(test);
     try {
       if (!base) base = exports.base;
       var c = function(node, st, override) {
@@ -76,7 +87,7 @@
         var inside = node.start <= pos && node.end >= pos;
         if (inside)
           base[type](node, st, c);
-        if (inside && (targetType == null || type == targetType))
+        if (inside && test(node))
           throw new Found(node, st);
       };
       c(node, state);

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



More information about the Pkg-javascript-commits mailing list