Implement shebang support

This commit is contained in:
Anthony Van de Gejuchte
2015-02-20 16:08:01 +01:00
parent 2a06c7758e
commit c69294c449
2 changed files with 69 additions and 54 deletions

View File

@@ -60,6 +60,7 @@ function OutputStream(options) {
bracketize : false, bracketize : false,
semicolons : true, semicolons : true,
comments : false, comments : false,
shebang : true,
preserve_line : false, preserve_line : false,
screw_ie8 : false, screw_ie8 : false,
preamble : null, preamble : null,
@@ -403,7 +404,6 @@ function OutputStream(options) {
AST_Node.DEFMETHOD("add_comments", function(output){ AST_Node.DEFMETHOD("add_comments", function(output){
var c = output.option("comments"), self = this; var c = output.option("comments"), self = this;
if (c) {
var start = self.start; var start = self.start;
if (start && !start._comments_dumped) { if (start && !start._comments_dumped) {
start._comments_dumped = true; start._comments_dumped = true;
@@ -426,13 +426,17 @@ function OutputStream(options) {
})); }));
} }
if (c.test) { if (!c) {
comments = comments.filter(function(comment) { comments = comments.filter(function(comment) {
return c.test(comment.value); return comment.type == "comment5";
});
} else if (c.test) {
comments = comments.filter(function(comment){
return c.test(comment.value) || comment.type == "comment5";
}); });
} else if (typeof c == "function") { } else if (typeof c == "function") {
comments = comments.filter(function(comment){ comments = comments.filter(function(comment){
return c(self, comment); return c(self, comment) || comment.type == "comment5";
}); });
} }
@@ -458,8 +462,11 @@ function OutputStream(options) {
output.space(); output.space();
} }
} }
}); else if (output.pos() === 0 && c.type == "comment5" && output.option("shebang")) {
output.print("#!" + c.value + "\n");
output.indent();
} }
});
} }
}); });

View File

@@ -210,7 +210,7 @@ function is_token(token, type, val) {
var EX_EOF = {}; var EX_EOF = {};
function tokenizer($TEXT, filename, html5_comments) { function tokenizer($TEXT, filename, html5_comments, shebang) {
var S = { var S = {
text : $TEXT, text : $TEXT,
@@ -568,6 +568,13 @@ function tokenizer($TEXT, filename, html5_comments) {
if (PUNC_CHARS(ch)) return token("punc", next()); if (PUNC_CHARS(ch)) return token("punc", next());
if (OPERATOR_CHARS(ch)) return read_operator(); if (OPERATOR_CHARS(ch)) return read_operator();
if (code == 92 || is_identifier_start(code)) return read_word(); if (code == 92 || is_identifier_start(code)) return read_word();
if (shebang) {
if (S.pos == 0 && looking_at("#!")) {
forward(2);
return skip_line_comment("comment5");
}
}
parse_error("Unexpected character '" + ch + "'"); parse_error("Unexpected character '" + ch + "'");
}; };
@@ -637,12 +644,13 @@ function parse($TEXT, options) {
expression : false, expression : false,
html5_comments : true, html5_comments : true,
bare_returns : false, bare_returns : false,
shebang : true,
}); });
var S = { var S = {
input : (typeof $TEXT == "string" input : (typeof $TEXT == "string"
? tokenizer($TEXT, options.filename, ? tokenizer($TEXT, options.filename,
options.html5_comments) options.html5_comments, options.shebang)
: $TEXT), : $TEXT),
token : null, token : null,
prev : null, prev : null,