Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for license comments /*! license */ #332

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions lib/parse-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ function JS_Parse_Error(message, line, col, pos) {
this.col = col + 1;
this.pos = pos + 1;
this.stack = new Error().stack;
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this have to do with the pull request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing. It just reports errors with context, which makes it far far easier to debug this sort of thing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you then remove line 260?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this, for a cleaner pull request.

({})();
} catch(ex) {
this.stack = ex.stack;
console.log(this.toString());
};
};

JS_Parse_Error.prototype.toString = function() {
Expand Down Expand Up @@ -466,7 +472,6 @@ function tokenizer($TEXT) {
};

function read_multiline_comment() {
next();
return with_eof_error("Unterminated multiline comment", function(){
var i = find("*/", true),
text = S.text.substring(S.pos, i);
Expand Down Expand Up @@ -546,6 +551,18 @@ function tokenizer($TEXT) {
return token("operator", grow(prefix || next()));
};

function read_license() {
return with_eof_error("Unterminated multiline comment", function(){
var i = find("*/", true),
text = S.text.substring(S.pos, i);
for (var j=0; j<text.length+2; j++) {
next();
}
S.newline_before = text.indexOf("\n") >= 0;
return token("license", text);
});
};

function handle_slash() {
next();
var regex_allowed = S.regex_allowed;
Expand All @@ -555,8 +572,15 @@ function tokenizer($TEXT) {
S.regex_allowed = regex_allowed;
return next_token();
case "*":
S.comments_before.push(read_multiline_comment());
next();
S.regex_allowed = regex_allowed;
if (peek()=='!') {
// A license statment starts /*!
next();
return read_license();
} else {
S.comments_before.push(read_multiline_comment());
}
return next_token();
}
return S.regex_allowed ? read_regexp("") : read_operator("/");
Expand Down Expand Up @@ -682,7 +706,6 @@ function NodeWithToken(str, start, end) {
NodeWithToken.prototype.toString = function() { return this.name; };

function parse($TEXT, exigent_mode, embed_tokens) {

var S = {
input : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT,
token : null,
Expand Down Expand Up @@ -755,7 +778,12 @@ function parse($TEXT, exigent_mode, embed_tokens) {
};

function as() {
return slice(arguments);
var arr = slice(arguments);
if (S.token.comments_before && S.token.comments_before.length) {
arr.comments_before = S.token.comments_before;
}
if (S.token.line) arr.line = S.token.line;
return arr;
};

function parenthesised() {
Expand Down Expand Up @@ -792,6 +820,9 @@ function parse($TEXT, exigent_mode, embed_tokens) {
case "atom":
return simple_statement();

case "license":
return as("license", prog1(S.token.value, next));

case "name":
return is_token(peek(), "punc", ":")
? labeled_statement(prog1(S.token.value, next, next))
Expand Down Expand Up @@ -886,6 +917,10 @@ function parse($TEXT, exigent_mode, embed_tokens) {
return as("label", label, stat);
};

function license() {
return as("license", prog1(expression));
};

function simple_statement() {
return as("stat", prog1(expression, semicolon));
};
Expand Down
6 changes: 6 additions & 0 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ function ast_walker() {
},
"atom": function(name) {
return [ this[0], name ];
},
"license": function(name) {
return [ this[0], name ];
}
};

Expand Down Expand Up @@ -1547,6 +1550,9 @@ function gen_code(ast, options) {
"string": encode_string,
"num": make_num,
"name": make_name,
"license": function(x){
return "/*!"+x+'*/';
},
"debugger": function(){ return "debugger" },
"toplevel": function(statements) {
return make_block_statements(statements)
Expand Down