[Pkg-javascript-commits] [node-acorn-jsx] 274/484: Fix start position for HTML comments and add tests.

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:42 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 7da3b6f1fd3c97063b169daa4305638508d8098d
Author: Max Schaefer <max at semmle.com>
Date:   Wed Sep 3 18:20:39 2014 +0100

    Fix start position for HTML comments and add tests.
---
 acorn.js       | 14 ++++++--------
 test/driver.js | 24 +++++++++++++++++++++---
 test/tests.js  | 47 ++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 69 insertions(+), 16 deletions(-)

diff --git a/acorn.js b/acorn.js
index acf76f6..d7e6ce4 100644
--- a/acorn.js
+++ b/acorn.js
@@ -565,16 +565,16 @@
                         startLoc, options.locations && new Position);
   }
 
-  function skipLineComment() {
+  function skipLineComment(startSkip) {
     var start = tokPos;
     var startLoc = options.onComment && options.locations && new Position;
-    var ch = input.charCodeAt(tokPos+=2);
+    var ch = input.charCodeAt(tokPos+=startSkip);
     while (tokPos < inputLen && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) {
       ++tokPos;
       ch = input.charCodeAt(tokPos);
     }
     if (options.onComment)
-      options.onComment(false, input.slice(start + 2, tokPos), start, tokPos,
+      options.onComment(false, input.slice(start + startSkip, tokPos), start, tokPos,
                         startLoc, options.locations && new Position);
   }
 
@@ -609,7 +609,7 @@
         if (next === 42) { // '*'
           skipBlockComment();
         } else if (next === 47) { // '/'
-          skipLineComment();
+          skipLineComment(2);
         } else break;
       } else if (ch === 160) { // '\xa0'
         ++tokPos;
@@ -678,8 +678,7 @@
       if (next == 45 && input.charCodeAt(tokPos + 2) == 62 &&
           newline.test(input.slice(lastEnd, tokPos))) {
         // A `-->` line comment
-        tokPos += 3;
-        skipLineComment();
+        skipLineComment(3);
         skipSpace();
         return readToken();
       }
@@ -700,8 +699,7 @@
     if (next == 33 && code == 60 && input.charCodeAt(tokPos + 2) == 45 &&
         input.charCodeAt(tokPos + 3) == 45) {
       // `<!--`, an XML-style comment that should be interpreted as a line comment
-      tokPos += 4;
-      skipLineComment();
+      skipLineComment(4);
       skipSpace();
       return readToken();
     }
diff --git a/test/driver.js b/test/driver.js
index 729550c..3b15510 100644
--- a/test/driver.js
+++ b/test/driver.js
@@ -2,8 +2,8 @@
   var tests = [];
   var acorn = typeof require == "undefined" ? window.acorn : require("../acorn.js");
 
-  exports.test = function(code, ast, options) {
-    tests.push({code: code, ast: ast, options: options});
+  exports.test = function(code, ast, options, comments) {
+    tests.push({code: code, ast: ast, options: options, comments: comments});
   };
   exports.testFail = function(code, message, options) {
     tests.push({code: code, error: message, options: options});
@@ -13,10 +13,26 @@
   };
 
   exports.runTests = function(callback) {
-    var opts = {locations: true};
+    var comments;
+
+    function onComment(block, text, start, end, startLoc, endLoc) {
+        comments.push({
+          block: block,
+          text: text,
+          start: start,
+          end: end,
+          startLoc: { line: startLoc.line, column: startLoc.column },
+          endLoc: { line: endLoc.line, column: endLoc.column }
+        });
+    }
+
+    var opts = {locations: true, onComment: onComment};
+
     for (var i = 0; i < tests.length; ++i) {
       var test = tests[i];
       try {
+        comments = [];
+        if (test.options && !test.options.onComment) test.options.onComment = onComment;
         var ast = acorn.parse(test.code, test.options || opts);
         if (test.error) callback("fail", test.code,
                                  "Expected error message: " + test.error + "\nBut parsing succeeded.");
@@ -27,6 +43,8 @@
           else callback("ok", test.code);
         } else {
           var mis = misMatch(test.ast, ast);
+          if (mis) callback("fail", test.code, mis);
+          if (test.comments) mis = misMatch(test.comments, comments);
           if (!mis) callback("ok", test.code);
           else callback("fail", test.code, mis);
         }
diff --git a/test/tests.js b/test/tests.js
index 2259f75..211d4a1 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -28645,11 +28645,48 @@ testFail("for(const x = 0;;);", "Unexpected token (1:4)", {ecmaVersion: 6});
   );
 })();
 
+test("<!--\n;", {
+  type: "Program",
+  body: [
+    {
+      type: "EmptyStatement"
+    }
+  ]
+}
+);
+
 (function() {
-  var comments = 0;
-  testAssert("\nfunction plop() {\n'use strict';\n/* Comment */\n}", function() {
-    if (comments != 1) return "Comment after strict counted twice.";
-  }, {onComment: function() {++comments;}});
+  test("\nfunction plop() {\n'use strict';\n/* Comment */\n}", {}, {locations: true},
+  [{
+    block: true,
+    text: " Comment ",
+    startLoc: { line: 4, column: 0 },
+    endLoc: { line: 4, column: 13 }
+  }]);
+
+  test("// line comment", {}, {locations: true},
+  [{
+    block: false,
+    text: " line comment",
+    startLoc: { line: 1, column: 0 },
+    endLoc: { line: 1, column: 15 }
+  }]);
+
+  test("<!-- HTML comment", {}, {locations: true},
+  [{
+    block: false,
+    text: " HTML comment",
+    startLoc: { line: 1, column: 0 },
+    endLoc: { line: 1, column: 17 }
+  }]);
+
+  test(";\n--> HTML comment", {}, {locations: true},
+  [{
+    block: false,
+    text: " HTML comment",
+    startLoc: { line: 2, column: 0 },
+    endLoc: { line: 2, column: 16 }
+  }]);
 })();
 
 (function() {
@@ -28747,4 +28784,4 @@ testFail("for(const x = 0;;);", "Unexpected token (1:4)", {ecmaVersion: 6});
       actualTokens.push(token);
     }
   });
-})();
\ No newline at end of file
+})();

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