[Pkg-javascript-commits] [node-shell-quote] 67/137: nearly passing with a clunky state env parser, array issues

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 19:19:40 UTC 2017


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

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

commit d6d64160f2fc8a23018410ffe84ab7f1b0c4fa02
Author: James Halliday <mail at substack.net>
Date:   Mon May 13 05:55:30 2013 -0700

    nearly passing with a clunky state env parser, array issues
---
 index.js | 49 +++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 41 insertions(+), 8 deletions(-)

diff --git a/index.js b/index.js
index 1c98298..ad6a0fa 100644
--- a/index.js
+++ b/index.js
@@ -29,16 +29,52 @@ exports.parse = function parse (s, env) {
     var match = s.match(chunker);
     if (!match) return [];
     if (!env) env = {};
-    return match.map(function (s) {
+    return [].concat.apply([], match.map(function (s) {
         if (/^'/.test(s)) {
             return s
                 .replace(/^'|'$/g, '')
                 .replace(/\\(["'\\$`(){}!#&*|])/g, '$1')
             ;
         }
+        else if (/^"/.test(s) && typeof env === 'function') {
+            var res = [];
+            s = s.replace(/^"|"$/g, '');
+            
+            var begin = 0, bracket = false;
+            for (var i = 0; i < s.length; i++) {
+                if (s.charAt(i) !== '$' || s.charAt(i-1) === '\\') continue;
+                if (s.charAt(i+1) === '{') {
+                    i ++;
+                    bracket = true;
+                }
+                if (/^[*@#?$!0_-]$/.test(s.charAt(i+1))
+                && (!bracket || (bracket && s.charAt(i+2) === '}'))) {
+                    res.push(s.slice(begin, i));
+                    var r = env(s.charAt(i+1));
+                    if (typeof r === 'object') res.push(r);
+                    else res[res.length-1] += r;
+                    i ++;
+                    if (bracket) i++;
+                    begin = i + 1;
+                    continue;
+                }
+                for (var j=i+1; j < s.length && /\w/.test(s.charAt(j)); j++);
+                if (j-(i+1) > 1) {
+                    res.push(s.slice(begin, i));
+                    var r = env(s.slice(i+1, j));
+                    if (typeof r === 'object') res.push(r);
+                    else res[res.length-1] += r;
+                    begin = j;
+                }
+            }
+            res.push(s.slice(begin));
+            return res.map(function (c) {
+                if (typeof c === 'object') return c;
+                return c.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1');
+            });
+        }
         else if (/^"/.test(s)) {
-            return s
-                .replace(/^"|"$/g, '')
+            return s.replace(/^"|"$/g, '')
                 .replace(/(^|[^\\])\$(\w+|[*@#?$!0_-])/g, getVar)
                 .replace(/(^|[^\\])\${(\w+|[*@#?$!0_-])}/g, getVar)
                 .replace(/\\([ "'\\$`(){}!#&*|])/g, '$1')
@@ -54,12 +90,9 @@ exports.parse = function parse (s, env) {
                 return parse('"' + s + '"', env);
             }
         );
-    });
+    }));
     
     function getVar (_, pre, key) {
-        if (typeof env === 'function') {
-            return pre + String(env(key) || '');
-        }
-        else return pre + String(env[key] || '');
+        return pre + String(env[key] || '');
     }
 };

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



More information about the Pkg-javascript-commits mailing list