[Pkg-javascript-commits] [node-shell-quote] 110/137: add comment parsing feature

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 19:19:44 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 b8b5c31c16a15aa4ab26c8f23d362a24b9fa57c4
Author: 59naga <i59naga at icloud.com>
Date:   Sun Apr 24 13:27:20 2016 +0900

    add comment parsing feature
---
 index.js        | 39 ++++++++++++++++++++++++++++-----------
 readme.markdown | 14 ++++++++++++++
 test/comment.js | 14 ++++++++++++++
 3 files changed, 56 insertions(+), 11 deletions(-)

diff --git a/index.js b/index.js
index b78e692..473b3d2 100644
--- a/index.js
+++ b/index.js
@@ -55,11 +55,15 @@ function parse (s, env, opts) {
         '(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
     ].join('|'), 'g');
     var match = filter(s.match(chunker), Boolean);
+    var commented = false;
 
     if (!match) return [];
     if (!env) env = {};
     if (!opts) opts = {};
-    return map(match, function (s) {
+    return map(match, function (s, j) {
+        if (commented) {
+            return;
+        }
         if (RegExp('^' + CONTROL + '$').test(s)) {
             return { op: s };
         }
@@ -80,14 +84,13 @@ function parse (s, env, opts) {
         var DS = '$';
         var BS = opts.escape || '\\';
         var quote = false;
-        var varname = false;
         var esc = false;
         var out = '';
         var isGlob = false;
 
         for (var i = 0, len = s.length; i < len; i++) {
             var c = s.charAt(i);
-            isGlob = isGlob || (!quote && (c === '*' || c === '?'))
+            isGlob = isGlob || (!quote && (c === '*' || c === '?'));
             if (esc) {
                 out += c;
                 esc = false;
@@ -113,18 +116,25 @@ function parse (s, env, opts) {
                         out += parseEnvVar();
                     }
                     else {
-                        out += c
+                        out += c;
                     }
                 }
             }
             else if (c === DQ || c === SQ) {
-                quote = c
+                quote = c;
             }
             else if (RegExp('^' + CONTROL + '$').test(c)) {
                 return { op: s };
             }
+            else if (RegExp('^#$').test(c)) {
+                commented = true;
+                if (out.length){
+                    return [out, { comment: s.slice(i+1) + match.slice(j+1).join(' ') }];
+                }
+                return [{ comment: s.slice(i+1) + match.slice(j+1).join(' ') }];
+            }
             else if (c === BS) {
-                esc = true
+                esc = true;
             }
             else if (c === DS) {
                 out += parseEnvVar();
@@ -134,14 +144,14 @@ function parse (s, env, opts) {
 
         if (isGlob) return {op: 'glob', pattern: out};
 
-        return out
+        return out;
 
         function parseEnvVar() {
             i += 1;
             var varend, varname;
             //debugger
             if (s.charAt(i) === '{') {
-                i += 1
+                i += 1;
                 if (s.charAt(i) === '}') {
                     throw new Error("Bad substitution: " + s.substr(i - 2, 3));
                 }
@@ -162,13 +172,20 @@ function parse (s, env, opts) {
                     varname = s.substr(i);
                     i = s.length;
                 } else {
-                    varname = s.substr(i, varend.index)
+                    varname = s.substr(i, varend.index);
                     i += varend.index - 1;
                 }
             }
             return getVar(null, '', varname);
         }
-    });
+    })
+    // finalize parsed aruments
+    .reduce(function(prev, arg){
+        if (arg === undefined){
+            return prev;
+        }
+        return prev.concat(arg);
+    },[]);
 
     function getVar (_, pre, key) {
         var r = typeof env === 'function' ? env(key) : env[key];
@@ -179,4 +196,4 @@ function parse (s, env, opts) {
         }
         else return pre + r;
     }
-};
+}
diff --git a/readme.markdown b/readme.markdown
index 6146e9b..70edb3c 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -78,6 +78,20 @@ output:
 [ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
 ```
 
+## parsing shell comment
+
+``` js
+var parse = require('shell-quote').parse;
+var xs = parse('beep > boop # > kaboom');
+console.dir(xs);
+```
+
+output:
+
+```
+[ 'beep', { op: '>' }, 'boop', { comment: '> kaboom' } ]
+```
+
 # methods
 
 ``` js
diff --git a/test/comment.js b/test/comment.js
new file mode 100644
index 0000000..bc6fbf2
--- /dev/null
+++ b/test/comment.js
@@ -0,0 +1,14 @@
+var test = require('tape');
+var parse = require('../').parse;
+
+test('comment', function (t) {
+    t.same(parse('beep#boop'), [ 'beep', { comment: 'boop' } ]);
+    t.same(parse('beep #boop'), [ 'beep', { comment: 'boop' } ]);
+    t.same(parse('beep # boop'), [ 'beep', { comment: 'boop' } ]);
+    t.same(parse('beep # > boop'), [ 'beep', { comment: '> boop' } ]);
+    t.same(parse('beep # "> boop"'), [ 'beep', { comment: '"> boop"' } ]);
+    t.same(parse('beep "#"'), [ 'beep', '#' ]);
+    t.same(parse('beep #"#"#'), [ 'beep', { comment: '"#"#' } ]);
+    t.same(parse('beep > boop # > foo'), [ 'beep', {op: '>'}, 'boop', { comment: '> foo' } ]);
+    t.end();
+});

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