From 265008c94870365feeb7dcdd6572b893e1fc0889 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 15 May 2017 23:02:55 +0800 Subject: [PATCH 1/6] improve keyword-related parser errors (#1941) fixes #1937 --- lib/parse.js | 56 +++-- test/input/invalid/else.js | 1 + test/input/invalid/return.js | 1 + test/mocha/cli.js | 476 +++++++++++++++++++---------------- 4 files changed, 295 insertions(+), 239 deletions(-) create mode 100644 test/input/invalid/else.js create mode 100644 test/input/invalid/return.js diff --git a/lib/parse.js b/lib/parse.js index 74c00b74..eab9b64d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -801,17 +801,16 @@ function parse($TEXT, options) { }; var statement = embed_tokens(function() { - var tmp; handle_regexp(); switch (S.token.type) { case "string": if (S.in_directives) { - tmp = peek(); + var token = peek(); if (S.token.raw.indexOf("\\") == -1 - && (tmp.nlb - || is_token(tmp, "eof") - || is_token(tmp, "punc", ";") - || is_token(tmp, "punc", "}"))) { + && (token.nlb + || is_token(token, "eof") + || is_token(token, "punc", ";") + || is_token(token, "punc", "}"))) { S.input.add_directive(S.token.value); } else { S.in_directives = false; @@ -850,72 +849,97 @@ function parse($TEXT, options) { } case "keyword": - switch (tmp = S.token.value, next(), tmp) { + switch (S.token.value) { case "break": + next(); return break_cont(AST_Break); case "continue": + next(); return break_cont(AST_Continue); case "debugger": + next(); semicolon(); return new AST_Debugger(); case "do": + next(); + var body = in_loop(statement); + expect_token("keyword", "while"); + var condition = parenthesised(); + semicolon(true); return new AST_Do({ - body : in_loop(statement), - condition : (expect_token("keyword", "while"), tmp = parenthesised(), semicolon(true), tmp) + body : body, + condition : condition }); case "while": + next(); return new AST_While({ condition : parenthesised(), body : in_loop(statement) }); case "for": + next(); return for_(); case "function": + next(); return function_(AST_Defun); case "if": + next(); return if_(); case "return": if (S.in_function == 0 && !options.bare_returns) croak("'return' outside of function"); + next(); + var value = null; + if (is("punc", ";")) { + next(); + } else if (!can_insert_semicolon()) { + value = expression(true); + semicolon(); + } return new AST_Return({ - value: ( is("punc", ";") - ? (next(), null) - : can_insert_semicolon() - ? null - : (tmp = expression(true), semicolon(), tmp) ) + value: value }); case "switch": + next(); return new AST_Switch({ expression : parenthesised(), body : in_loop(switch_body_) }); case "throw": + next(); if (S.token.nlb) croak("Illegal newline after 'throw'"); + var value = expression(true); + semicolon(); return new AST_Throw({ - value: (tmp = expression(true), semicolon(), tmp) + value: value }); case "try": + next(); return try_(); case "var": - return tmp = var_(), semicolon(), tmp; + next(); + var node = var_(); + semicolon(); + return node; case "with": if (S.input.has_directive("use strict")) { croak("Strict mode may not include a with statement"); } + next(); return new AST_With({ expression : parenthesised(), body : statement() diff --git a/test/input/invalid/else.js b/test/input/invalid/else.js new file mode 100644 index 00000000..89e8e501 --- /dev/null +++ b/test/input/invalid/else.js @@ -0,0 +1 @@ +if (0) else 1; diff --git a/test/input/invalid/return.js b/test/input/invalid/return.js new file mode 100644 index 00000000..d232c623 --- /dev/null +++ b/test/input/invalid/return.js @@ -0,0 +1 @@ +return 42; diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 38f61f39..335b224d 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -55,15 +55,15 @@ describe("bin/uglifyjs", function () { }); }); it("Should append source map to output when using --source-map url=inline", function (done) { - var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=inline"; + var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=inline"; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" + - "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n"); - done(); - }); + assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" + + "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n"); + done(); + }); }); it("should not append source map to output when not using --source-map url=inline", function (done) { var command = uglifyjscmd + ' test/input/issue-1323/sample.js'; @@ -76,84 +76,84 @@ describe("bin/uglifyjs", function () { }); }); it("Should work with --keep-fnames (mangle only)", function (done) { - var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m'; + var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); - done(); - }); + assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); + done(); + }); }); it("Should work with --keep-fnames (mangle & compress)", function (done) { - var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false'; + var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n"); - done(); - }); + assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n"); + done(); + }); }); it("Should work with keep_fnames under mangler options", function (done) { - var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true'; + var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); - done(); - }); + assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); + done(); + }); }); it("Should work with --define (simple)", function (done) { - var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c'; + var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "console.log(5);\n"); - done(); - }); + assert.strictEqual(stdout, "console.log(5);\n"); + done(); + }); }); it("Should work with --define (nested)", function (done) { - var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c'; + var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "console.log(3,5);\n"); - done(); - }); + assert.strictEqual(stdout, "console.log(3,5);\n"); + done(); + }); }); it("Should work with --define (AST_Node)", function (done) { - var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c'; + var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "stdout.println(D);\n"); - done(); - }); + assert.strictEqual(stdout, "stdout.println(D);\n"); + done(); + }); }); it("Should work with `--beautify`", function (done) { - var command = uglifyjscmd + ' test/input/issue-1482/input.js -b'; + var command = uglifyjscmd + ' test/input/issue-1482/input.js -b'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, read("test/input/issue-1482/default.js")); - done(); - }); + assert.strictEqual(stdout, read("test/input/issue-1482/default.js")); + done(); + }); }); it("Should work with `--beautify bracketize`", function (done) { - var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize'; + var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, read("test/input/issue-1482/bracketize.js")); - done(); - }); + assert.strictEqual(stdout, read("test/input/issue-1482/bracketize.js")); + done(); + }); }); it("Should process inline source map", function(done) { var command = uglifyjscmd + " test/input/issue-520/input.js -mc toplevel --source-map content=inline,url=inline"; @@ -260,214 +260,244 @@ describe("bin/uglifyjs", function () { }); }); it("Should throw syntax error (5--)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_1.js'; + var command = uglifyjscmd + ' test/input/invalid/assign_1.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_1.js:1,18", - "console.log(1 || 5--);", - " ^", - "ERROR: Invalid use of -- operator" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_1.js:1,18", + "console.log(1 || 5--);", + " ^", + "ERROR: Invalid use of -- operator" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (Math.random() /= 2)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_2.js'; + var command = uglifyjscmd + ' test/input/invalid/assign_2.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_2.js:1,32", - "console.log(2 || (Math.random() /= 2));", - " ^", - "ERROR: Invalid assignment" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_2.js:1,32", + "console.log(2 || (Math.random() /= 2));", + " ^", + "ERROR: Invalid assignment" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (++this)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_3.js'; + var command = uglifyjscmd + ' test/input/invalid/assign_3.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_3.js:1,17", - "console.log(3 || ++this);", - " ^", - "ERROR: Invalid use of ++ operator" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_3.js:1,17", + "console.log(3 || ++this);", + " ^", + "ERROR: Invalid use of ++ operator" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (++null)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_4.js'; + var command = uglifyjscmd + ' test/input/invalid/assign_4.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_4.js:1,0", - "++null", - "^", - "ERROR: Invalid use of ++ operator" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_4.js:1,0", + "++null", + "^", + "ERROR: Invalid use of ++ operator" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (a.=)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/dot_1.js'; + var command = uglifyjscmd + ' test/input/invalid/dot_1.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/dot_1.js:1,2", - "a.=", - " ^", - "ERROR: Unexpected token: operator (=)" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/dot_1.js:1,2", + "a.=", + " ^", + "ERROR: Unexpected token: operator (=)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (%.a)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/dot_2.js'; + var command = uglifyjscmd + ' test/input/invalid/dot_2.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/dot_2.js:1,0", - "%.a;", - "^", - "ERROR: Unexpected token: operator (%)" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/dot_2.js:1,0", + "%.a;", + "^", + "ERROR: Unexpected token: operator (%)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (a./();)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/dot_3.js'; + var command = uglifyjscmd + ' test/input/invalid/dot_3.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/dot_3.js:1,2", - "a./();", - " ^", - "ERROR: Unexpected token: operator (/)" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/dot_3.js:1,2", + "a./();", + " ^", + "ERROR: Unexpected token: operator (/)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error ({%: 1})", function(done) { - var command = uglifyjscmd + ' test/input/invalid/object.js'; + var command = uglifyjscmd + ' test/input/invalid/object.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/object.js:1,13", - "console.log({%: 1});", - " ^", - "ERROR: Unexpected token: operator (%)" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/object.js:1,13", + "console.log({%: 1});", + " ^", + "ERROR: Unexpected token: operator (%)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (delete x)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/delete.js'; + var command = uglifyjscmd + ' test/input/invalid/delete.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/delete.js:13,11", - " delete x;", - " ^", - "ERROR: Calling delete on expression not allowed in strict mode" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/delete.js:13,11", + " delete x;", + " ^", + "ERROR: Calling delete on expression not allowed in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (function g(arguments))", function(done) { - var command = uglifyjscmd + ' test/input/invalid/function_1.js'; + var command = uglifyjscmd + ' test/input/invalid/function_1.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/function_1.js:4,11", - "function g(arguments) {", - " ^", - "ERROR: Unexpected arguments in strict mode" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/function_1.js:4,11", + "function g(arguments) {", + " ^", + "ERROR: Unexpected arguments in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (function eval())", function(done) { - var command = uglifyjscmd + ' test/input/invalid/function_2.js'; + var command = uglifyjscmd + ' test/input/invalid/function_2.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/function_2.js:4,9", - "function eval() {", - " ^", - "ERROR: Unexpected eval in strict mode" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/function_2.js:4,9", + "function eval() {", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (iife arguments())", function(done) { - var command = uglifyjscmd + ' test/input/invalid/function_3.js'; + var command = uglifyjscmd + ' test/input/invalid/function_3.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/function_3.js:4,10", - "!function arguments() {", - " ^", - "ERROR: Unexpected arguments in strict mode" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/function_3.js:4,10", + "!function arguments() {", + " ^", + "ERROR: Unexpected arguments in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (catch(eval))", function(done) { - var command = uglifyjscmd + ' test/input/invalid/try.js'; + var command = uglifyjscmd + ' test/input/invalid/try.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/try.js:7,18", - " try {} catch (eval) {}", - " ^", - "ERROR: Unexpected eval in strict mode" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/try.js:7,18", + " try {} catch (eval) {}", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (var eval)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/var.js'; + var command = uglifyjscmd + ' test/input/invalid/var.js'; - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/var.js:7,8", - " var eval;", - " ^", - "ERROR: Unexpected eval in strict mode" - ].join("\n")); - done(); - }); + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/var.js:7,8", + " var eval;", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); + }); + it("Should throw syntax error (else)", function(done) { + var command = uglifyjscmd + ' test/input/invalid/else.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/else.js:1,7", + "if (0) else 1;", + " ^", + "ERROR: Unexpected token: keyword (else)" + ].join("\n")); + done(); + }); + }); + it("Should throw syntax error (return)", function(done) { + var command = uglifyjscmd + ' test/input/invalid/return.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/return.js:1,0", + "return 42;", + "^", + "ERROR: 'return' outside of function" + ].join("\n")); + done(); + }); }); it("Should handle literal string as source map input", function(done) { var command = [ From 3be06ad0855b5fcb72e0c73b261c859e6e120ba2 Mon Sep 17 00:00:00 2001 From: kzc Date: Mon, 15 May 2017 13:12:00 -0400 Subject: [PATCH 2/6] reorg README for 3.x (#1942) --- README.md | 532 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 310 insertions(+), 222 deletions(-) diff --git a/README.md b/README.md index 87079395..f1927bc8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ UglifyJS 3 UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit. #### Note: -- **`uglify-js@3.x` has a simplified API and CLI that is not backwards compatible with [`uglify-js@2.x`](https://github.com/mishoo/UglifyJS2/tree/v2.x)**. +- **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS2/tree/v2.x)**. - **Documentation for UglifyJS `2.x` releases can be found [here](https://github.com/mishoo/UglifyJS2/tree/v2.x)**. - `uglify-js` only supports ECMAScript 5 (ES5). - Those wishing to minify @@ -25,8 +25,7 @@ From NPM for programmatic use: npm install uglify-js -Usage ------ +# Command line usage uglifyjs [input files] [options] @@ -43,7 +42,7 @@ a double dash to prevent input files being used as option arguments: uglifyjs --compress --mangle -- input.js -The available options are: +### Command line options ``` -h, --help Print usage information. @@ -142,7 +141,7 @@ The available options are: Specify `--output` (`-o`) to declare the output file. Otherwise the output goes to STDOUT. -## Source map options +## CLI source map options UglifyJS can generate a source map file, which is highly useful for debugging your compressed JavaScript. To get a source map, pass @@ -186,7 +185,20 @@ To use this feature pass `--source-map content="/path/to/input/source.map"` or `--source-map content=inline` if the source map is included inline with the sources. -## Mangler options +## CLI compress options + +You need to pass `--compress` (`-c`) to enable the compressor. Optionally +you can pass a comma-separated list of [compress options](#compress-options). + +Options are in the form `foo=bar`, or just `foo` (the latter implies +a boolean option that you want to set `true`; it's effectively a +shortcut for `foo=true`). + +Example: + + uglifyjs file.js -c toplevel,sequences=false + +## CLI mangle options To enable the mangler you need to pass `--mangle` (`-m`). The following (comma-separated) options are supported: @@ -205,7 +217,7 @@ comma-separated list of names. For example: to prevent the `require`, `exports` and `$` names from being changed. -### Mangling property names (`--mangle-props`) +### CLI mangling property names (`--mangle-props`) **Note:** this will probably break your code. Mangling property names is a separate step, different from variable name mangling. Pass @@ -255,7 +267,7 @@ of mangled property names. Using the name cache is not necessary if you compress all your files in a single call to UglifyJS. -#### Mangling unquoted names (`--mangle-props keep_quoted`) +### Mangling unquoted names (`--mangle-props keep_quoted`) Using quoted property name (`o["foo"]`) reserves the property name (`foo`) so that it is not mangled throughout the entire script even when used in an @@ -266,7 +278,7 @@ $ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo); ``` -#### Debugging property name mangling +### Debugging property name mangling You can also pass `--mangle-props debug` in order to mangle property names without completely obscuring them. For example the property `o.foo` @@ -281,12 +293,144 @@ random number on every compile to simulate mangling changing with different inputs (e.g. as you update the input script with new properties), and to help identify mistakes like writing mangled keys to storage. -## Compressor options -You need to pass `--compress` (`-c`) to enable the compressor. Optionally -you can pass a comma-separated list of options. Options are in the form -`foo=bar`, or just `foo` (the latter implies a boolean option that you want -to set `true`; it's effectively a shortcut for `foo=true`). +# API Reference + +Assuming installation via NPM, you can load UglifyJS in your application +like this: +```javascript +var UglifyJS = require("uglify-js"); +``` + +There is a single high level minification function, `minify(files, options)`, which will +performs all the steps in a configurable manner. +Example: +```javascript +var result = UglifyJS.minify("var b = function() {};"); +console.log(result.code); // minified output +console.log(result.error); // runtime error +``` + +You can also compress multiple files: +```javascript +var result = UglifyJS.minify({ + "file1.js": "var a = function() {};", + "file2.js": "var b = function() {};" +}); +console.log(result.code); +``` + +## Minify options + +- `warnings` (default `false`) — pass `true` to display compressor warnings. + +- `parse` (default `{}`) — pass an object if you wish to specify some + additional [parse options](#parse-options). + +- `compress` (default `{}`) — pass `false` to skip compressing entirely. + Pass an object to specify custom [compress options](#compress-options). + +- `mangle` (default `true`) — pass `false` to skip mangling names, or pass + an object to specify [mangle options](#mangle-options) (see below). + + - `mangle.properties` (default `false`) — a subcategory of the mangle option. + Pass an object to specify custom [mangle property options](#mangle-properties-options). + +- `output` (default `null`) — pass an object if you wish to specify + additional [output options](#output-options). The defaults are optimized + for best compression. + +- `sourceMap` (default `false`) - pass an object if you wish to specify + [source map options](#source-map-options). + +- `toplevel` (default `false`) - set to `true` if you wish to enable top level + variable and function name mangling and to drop unused variables and functions. + +- `ie8` (default `false`) - set to `true` to support IE8. + +## Minify option structure + +``` +{ + warnings: false, + parse: { + // parse options + }, + compress: { + // compress options + }, + mangle: { + // mangle options + + properties: { + // mangle property options + } + }, + output: { + // output options + }, + sourceMap: { + // source map options + }, + toplevel: false, + ie8: false, +} +``` + +### Source map options + +To generate a source map: +```javascript +var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, { + sourceMap: { + filename: "out.js", + url: "out.js.map" + } +}); +console.log(result.code); // minified output +console.log(result.map); // source map +``` + +Note that the source map is not saved in a file, it's just returned in +`result.map`. The value passed for `sourceMap.url` is only used to set +`//# sourceMappingURL=out.js.map` in `result.code`. The value of +`filename` is only used to set `file` attribute (see [the spec][sm-spec]) +in source map file. + +You can set option `sourceMap.url` to be `"inline"` and source map will +be appended to code. + +You can also specify sourceRoot property to be included in source map: +```javascript +var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, { + sourceMap: { + root: "http://example.com/src", + url: "out.js.map" + } +}); +``` + +If you're compressing compiled JavaScript and have a source map for it, you +can use `sourceMap.content`: +```javascript +var result = UglifyJS.minify({"compiled.js": "compiled code"}, { + sourceMap: { + content: "content from compiled.js.map", + url: "minified.js.map" + } +}); +// same as before, it returns `code` and `map` +``` + +If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.url`. + +## Parse options + +- `bare_returns` (default `false`) -- support top level `return` statements +- `html5_comments` (default `true`) +- `shebang` (default `true`) -- support `#!command` as the first line + +## Compress options - `sequences` (default: true) -- join consecutive simple statements using the comma operator. May be set to a positive integer to specify the maximum number @@ -414,8 +558,128 @@ marked as "pure". A function call is marked as "pure" if a comment annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For example: `/*@__PURE__*/foo()`; +## Mangle options -### The `unsafe` option +- `reserved` - pass an array of identifiers that should be excluded from mangling + +- `toplevel` — mangle names declared in the toplevel scope (disabled by +default). + +- `eval` — mangle names visible in scopes where eval or with are used +(disabled by default). + +- `keep_fnames` -- default `false`. Pass `true` to not mangle +function names. Useful for code relying on `Function.prototype.name`. +See also: the `keep_fnames` [compress option](#compress-options). + +Examples: + +```javascript +// test.js +var globalVar; +function funcName(firstLongName, anotherLongName) +{ + var myVariable = firstLongName + anotherLongName; +} +``` +```javascript +var code = fs.readFileSync("test.js", "utf8"); + +UglifyJS.minify(code).code; +// 'function funcName(a,n){}var globalVar;' + +UglifyJS.minify(code, { mangle: { reserved: ['firstLongName'] } }).code; +// 'function funcName(firstLongName,a){}var globalVar;' + +UglifyJS.minify(code, { mangle: { toplevel: true } }).code; +// 'function n(n,a){}var a;' +``` + +### Mangle properties options + +- `regex` — Pass a RegExp to only mangle certain names +- `keep_quoted` — Only mangle unquoted property names +- `debug` — Mangle names with the original name still present. Defaults to `false`. + Pass an empty string to enable, or a non-empty string to set the suffix. + +## Output options + +The code generator tries to output shortest code possible by default. In +case you want beautified output, pass `--beautify` (`-b`). Optionally you +can pass additional arguments that control the code output: + +- `beautify` (default `true`) -- whether to actually beautify the output. + Passing `-b` will set this to true, but you might need to pass `-b` even + when you want to generate minified code, in order to specify additional + arguments, so you can use `-b beautify=false` to override it. +- `indent_level` (default 4) +- `indent_start` (default 0) -- prefix all lines by that many spaces +- `quote_keys` (default `false`) -- pass `true` to quote all keys in literal + objects +- `space_colon` (default `true`) -- insert a space after the colon signs +- `ascii_only` (default `false`) -- escape Unicode characters in strings and + regexps (affects directives with non-ascii characters becoming invalid) +- `inline_script` (default `false`) -- escape the slash in occurrences of + ` Date: Tue, 16 May 2017 01:33:01 +0800 Subject: [PATCH 3/6] minor fixes to README.md --- README.md | 87 +++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index f1927bc8..adae5794 100644 --- a/README.md +++ b/README.md @@ -187,11 +187,11 @@ the sources. ## CLI compress options -You need to pass `--compress` (`-c`) to enable the compressor. Optionally +You need to pass `--compress` (`-c`) to enable the compressor. Optionally you can pass a comma-separated list of [compress options](#compress-options). -Options are in the form `foo=bar`, or just `foo` (the latter implies -a boolean option that you want to set `true`; it's effectively a +Options are in the form `foo=bar`, or just `foo` (the latter implies +a boolean option that you want to set `true`; it's effectively a shortcut for `foo=true`). Example: @@ -224,9 +224,9 @@ separate step, different from variable name mangling. Pass `--mangle-props`. It will mangle all properties that are seen in some object literal, or that are assigned to. For example: -```js +```javascript var x = { - foo: 1 + foo: 1 }; x.bar = 2; @@ -255,10 +255,10 @@ mangled to the same name in all of them. For this, pass `--name-cache filename. and UglifyJS will maintain these mappings in a file which can then be reused. It should be initially empty. Example: -``` -rm -f /tmp/cache.json # start fresh -uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js -uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js +```bash +$ rm -f /tmp/cache.json # start fresh +$ uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js +$ uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js ``` Now, `part1.js` and `part2.js` will be consistent with each other in terms @@ -273,7 +273,7 @@ Using quoted property name (`o["foo"]`) reserves the property name (`foo`) so that it is not mangled throughout the entire script even when used in an unquoted style (`o.foo`). Example: -``` +```bash $ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props keep_quoted -mc var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo); ``` @@ -314,8 +314,8 @@ console.log(result.error); // runtime error You can also compress multiple files: ```javascript var result = UglifyJS.minify({ - "file1.js": "var a = function() {};", - "file2.js": "var b = function() {};" + "file1.js": "var a = function() {};", + "file2.js": "var b = function() {};" }); console.log(result.code); ``` @@ -332,8 +332,8 @@ console.log(result.code); - `mangle` (default `true`) — pass `false` to skip mangling names, or pass an object to specify [mangle options](#mangle-options) (see below). - - - `mangle.properties` (default `false`) — a subcategory of the mangle option. + + - `mangle.properties` (default `false`) — a subcategory of the mangle option. Pass an object to specify custom [mangle property options](#mangle-properties-options). - `output` (default `null`) — pass an object if you wish to specify @@ -342,15 +342,15 @@ console.log(result.code); - `sourceMap` (default `false`) - pass an object if you wish to specify [source map options](#source-map-options). - + - `toplevel` (default `false`) - set to `true` if you wish to enable top level variable and function name mangling and to drop unused variables and functions. - + - `ie8` (default `false`) - set to `true` to support IE8. ## Minify option structure -``` +```javascript { warnings: false, parse: { @@ -361,10 +361,10 @@ console.log(result.code); }, mangle: { // mangle options - - properties: { - // mangle property options - } + + properties: { + // mangle property options + } }, output: { // output options @@ -382,10 +382,10 @@ console.log(result.code); To generate a source map: ```javascript var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, { - sourceMap: { - filename: "out.js", - url: "out.js.map" - } + sourceMap: { + filename: "out.js", + url: "out.js.map" + } }); console.log(result.code); // minified output console.log(result.map); // source map @@ -403,10 +403,10 @@ be appended to code. You can also specify sourceRoot property to be included in source map: ```javascript var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, { - sourceMap: { - root: "http://example.com/src", - url: "out.js.map" - } + sourceMap: { + root: "http://example.com/src", + url: "out.js.map" + } }); ``` @@ -414,10 +414,10 @@ If you're compressing compiled JavaScript and have a source map for it, you can use `sourceMap.content`: ```javascript var result = UglifyJS.minify({"compiled.js": "compiled code"}, { - sourceMap: { - content: "content from compiled.js.map", - url: "minified.js.map" - } + sourceMap: { + content: "content from compiled.js.map", + url: "minified.js.map" + } }); // same as before, it returns `code` and `map` ``` @@ -577,8 +577,7 @@ Examples: ```javascript // test.js var globalVar; -function funcName(firstLongName, anotherLongName) -{ +function funcName(firstLongName, anotherLongName) { var myVariable = firstLongName + anotherLongName; } ``` @@ -664,11 +663,11 @@ Note, however, that there might be situations where comments are lost. For example: ```javascript function f() { - /** @preserve Foo Bar */ - function g() { - // this function is never called - } - return something(); + /** @preserve Foo Bar */ + function g() { + // this function is never called + } + return something(); } ``` @@ -703,7 +702,7 @@ scope). For example if you pass `--define DEBUG=false` then, coupled with dead code removal UglifyJS will discard the following from the output: ```javascript if (DEBUG) { - console.log("debug stuff"); + console.log("debug stuff"); } ``` @@ -737,7 +736,7 @@ using `var` with `reduce_vars` (enabled by default) should suffice. You can also use conditional compilation via the programmatic API. With the difference that the property name is `global_defs` and is a compressor property: -```js +```javascript var result = uglifyJS.minify(fs.readFileSync("input.js", "utf8"), { compress: { dead_code: true, @@ -749,7 +748,7 @@ var result = uglifyJS.minify(fs.readFileSync("input.js", "utf8"), { ``` ### Using native Uglify AST with `minify()` -``` +```javascript // example: parse only, produce native Uglify AST var result = UglifyJS.minify(code, { @@ -764,7 +763,7 @@ var result = UglifyJS.minify(code, { // result.ast contains native Uglify AST ``` -``` +```javascript // example: accept native Uglify AST input and then compress and mangle // to produce both code and native AST. From 9464d3c20f60d7abe7fde5b48cc00a699566dbe7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 16 May 2017 05:40:49 +0800 Subject: [PATCH 4/6] fix parsing of property access after new line (#1944) Account for comments when detecting property access in `tokenizer`. fixes #1943 --- lib/parse.js | 6 +++++- test/compress/issue-1943.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/compress/issue-1943.js diff --git a/lib/parse.js b/lib/parse.js index eab9b64d..97dd6d4b 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -285,7 +285,11 @@ function tokenizer($TEXT, filename, html5_comments, shebang) { S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX(value)) || (type == "keyword" && KEYWORDS_BEFORE_EXPRESSION(value)) || (type == "punc" && PUNC_BEFORE_EXPRESSION(value))); - prev_was_dot = (type == "punc" && value == "."); + if (type == "punc" && value == ".") { + prev_was_dot = true; + } else if (!is_comment) { + prev_was_dot = false; + } var ret = { type : type, value : value, diff --git a/test/compress/issue-1943.js b/test/compress/issue-1943.js new file mode 100644 index 00000000..69bb9e64 --- /dev/null +++ b/test/compress/issue-1943.js @@ -0,0 +1,31 @@ +operator: { + input: { + a. //comment + typeof + } + expect_exact: "a.typeof;" +} + +name: { + input: { + a. //comment + b + } + expect_exact: "a.b;" +} + +keyword: { + input: { + a. //comment + default + } + expect_exact: "a.default;" +} + +atom: { + input: { + a. //comment + true + } + expect_exact: "a.true;" +} From f6c805ae1d7ffe76f71d3deaff2f49a6526bbd7b Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 16 May 2017 06:34:32 +0800 Subject: [PATCH 5/6] print package name alongside version in CLI (#1946) fixes #1945 --- bin/uglifyjs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 65b761c6..0194269d 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -21,8 +21,7 @@ var options = { compress: false, mangle: false }; -program._name = info.name; -program.version(info.version); +program.version(info.name + ' ' + info.version); program.parseArgv = program.parse; program.parse = undefined; program.option("-p, --parse ", "Specify parser options.", parse_js("parse", true)); From 050474ab44f02dc8bb0b47f0878c3ecda3e34fa5 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 16 May 2017 06:38:58 +0800 Subject: [PATCH 6/6] v3.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c84ef8b1..6948df6e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "http://lisperator.net/uglifyjs", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.0.5", + "version": "3.0.6", "engines": { "node": ">=0.8.0" },