Fix start position for HTML comments and add tests.

This commit is contained in:
Max Schaefer 2014-09-03 18:20:39 +01:00 committed by Marijn Haverbeke
parent 6c854ad221
commit 7da3b6f1fd
3 changed files with 69 additions and 16 deletions

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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);
}
});
})();
})();