[Pkg-javascript-commits] [node-acorn-jsx] 366/484: Added optional support for hashbang directives.

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:57 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 e37c07248e7ad553b6b4df451c6ba1a935cb379a
Author: Ingvar Stepanyan <me at rreverser.com>
Date:   Wed Dec 17 19:58:38 2014 +0200

    Added optional support for hashbang directives.
    
    Fixes #180.
---
 acorn.js       |  6 ++++++
 acorn_loose.js |  1 -
 test/tests.js  | 11 +++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/acorn.js b/acorn.js
index 2e8fa7c..291f7b4 100644
--- a/acorn.js
+++ b/acorn.js
@@ -74,6 +74,9 @@
     // When enabled, import/export statements are not constrained to
     // appearing at the top of the program.
     allowImportExportEverywhere: false,
+    // When enabled, hashbang directive in the beginning of file
+    // is allowed and treated as a line comment.
+    allowHashBang: false,
     // When `locations` is on, `loc` properties holding objects with
     // `start` and `end` properties in `{line, column}` form (with
     // line being 1-based and column 0-based) will be attached to the
@@ -615,6 +618,9 @@
     tokRegexpAllowed = true;
     metParenL = 0;
     templates = [];
+    if (tokPos === 0 && options.allowHashBang && input.slice(0, 2) === '#!') {
+      skipLineComment(2);
+    }
   }
 
   // Called at the end of every token. Sets `tokEnd`, `tokVal`, and
diff --git a/acorn_loose.js b/acorn_loose.js
index 97a3df6..6262162 100644
--- a/acorn_loose.js
+++ b/acorn_loose.js
@@ -45,7 +45,6 @@
   exports.parse_dammit = function(inpt, opts) {
     if (!opts) opts = {};
     input = String(inpt);
-    if (/^#!.*/.test(input)) input = "//" + input.slice(2);
     fetchToken = acorn.tokenize(input, opts);
     options = fetchToken.options;
     sourceFile = options.sourceFile || null;
diff --git a/test/tests.js b/test/tests.js
index 72b9340..f6df2ed 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -28834,3 +28834,14 @@ test('var x = (1 + 2)', {}, {
 });
 
 test("function f(f) { 'use strict'; }", {});
+
+// https://github.com/marijnh/acorn/issues/180
+test("#!/usr/bin/node\n;", {}, {
+  allowHashBang: true,
+  onComment: [{
+    type: "Line",
+    value: "/usr/bin/node",
+    start: 0,
+    end: 15
+  }]
+});

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