Added optional support for hashbang directives.

Fixes #180.
This commit is contained in:
Ingvar Stepanyan 2014-12-17 19:58:38 +02:00
parent 8e84aa02f4
commit e37c07248e
3 changed files with 17 additions and 1 deletions

View File

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

View File

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

View File

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