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 all 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
5 changes: 5 additions & 0 deletions README.html
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ <h3 id="sec-1-4"><span class="section-number-3">1.4</span> Usage </h3>
comment tokens in the generated code (assumed to be copyright information
etc.). If you pass this it will discard it.

</li>
<li><code>-nl</code> or <code>--no-licenses</code> &mdash; by default, <code>uglifyjs</code> will keep the text
marked /*! */ in the generated code (assumed to be license information
etc.). If you pass this it will discard it.

</li>
<li><code>-o filename</code> or <code>--output filename</code> &mdash; put the result in <code>filename</code>. If
this isn't given, the result goes to standard output (or see next one).
Expand Down
4 changes: 4 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ Supported options:
comment tokens in the generated code (assumed to be copyright information
etc.). If you pass this it will discard it.

- =-nl= or =--no-licenses= --- by default, =uglifyjs= will keep the text
marked /*! */ in the generated code (assumed to be license information
etc.). If you pass this it will discard it.

- =-o filename= or =--output filename= --- put the result in =filename=. If
this isn't given, the result goes to standard output (or see next one).

Expand Down
9 changes: 7 additions & 2 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");
var fs = require("fs");
var uglify = require("uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js
var uglify = require("../uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js
consolidator = uglify.consolidator,
jsp = uglify.parser,
pro = uglify.uglify;
Expand Down Expand Up @@ -32,7 +32,8 @@ var options = {
indent_start: 0,
quote_keys: false,
space_colon: false,
inline_script: false
inline_script: false,
licenses: true
},
make: false,
output: true // stdout
Expand Down Expand Up @@ -86,6 +87,10 @@ out: while (args.length > 0) {
case "-nc":
options.show_copyright = false;
break;
case "--no-licenses":
case "-nl":
options.codegen_options.licenses = false;
break;
case "-o":
case "--output":
options.output = args.shift();
Expand Down
37 changes: 33 additions & 4 deletions lib/parse-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,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 +545,18 @@ function tokenizer($TEXT) {
return token("operator", grow(prefix || next()));
};

function read_license() {
return with_eof_error("Unterminated license statement", 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 +566,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 +700,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 +772,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 +814,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 +911,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
12 changes: 11 additions & 1 deletion 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 @@ -1418,7 +1421,8 @@ function gen_code(ast, options) {
space_colon : false,
beautify : false,
ascii_only : false,
inline_script: false
inline_script: false,
licenses: true
});
var beautify = !!options.beautify;
var indentation = 0,
Expand Down Expand Up @@ -1547,6 +1551,12 @@ function gen_code(ast, options) {
"string": encode_string,
"num": make_num,
"name": make_name,
"license": function(text){
if (!options.licenses) {
return '';
}
return "/*!" + text + '*/';
},
"debugger": function(){ return "debugger" },
"toplevel": function(statements) {
return make_block_statements(statements)
Expand Down
1 change: 1 addition & 0 deletions test/testparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function ParserTestSuite(callback){
["var abc = 5;", "Regular variable statement with assignment"],
["/* */;", "Multiline comment"],
['/** **/;', 'Double star multiline comment'],
["/*! */;", "License statement"],
["var f = function(){;};", "Function expression in var assignment"],
['hi; // moo\n;', 'single line comment'],
['var varwithfunction;', 'Dont match keywords as substrings'], // difference between `var withsomevar` and `"str"` (local search and lits)
Expand Down
3 changes: 3 additions & 0 deletions test/unit/compress/expected/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*! License 1 *//*! Multiline
License 2
*/var a=1;/*! License 3 */var b=1
7 changes: 7 additions & 0 deletions test/unit/compress/test/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*! License 1 */
/*! Multiline
License 2
*/
var a=1;
/*! License 3 */
var b=1;