[Pkg-javascript-commits] [uglifyjs] 18/50: fix parser bugs & CLI reporting (#1827)
Jonas Smedegaard
dr at jones.dk
Thu Aug 17 23:06:44 UTC 2017
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository uglifyjs.
commit 4027a0c962b266f72b303e9367a6bad430097f59
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date: Wed Apr 19 04:27:13 2017 +0800
fix parser bugs & CLI reporting (#1827)
fixes #1825
---
bin/uglifyjs | 16 ++++-----
lib/parse.js | 27 ++++++---------
test/input/invalid/assign_4.js | 1 +
test/input/invalid/dot_1.js | 1 +
test/input/invalid/dot_2.js | 1 +
test/input/invalid/dot_3.js | 1 +
test/input/invalid/object.js | 1 +
test/mocha/cli.js | 79 ++++++++++++++++++++++++++++++++++++++++--
8 files changed, 101 insertions(+), 26 deletions(-)
diff --git a/bin/uglifyjs b/bin/uglifyjs
index 635ca36..63b2f26 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -367,19 +367,19 @@ var index = 0;
if (ex instanceof UglifyJS.JS_Parse_Error) {
print_error("Parse error at " + file + ":" + ex.line + "," + ex.col);
var col = ex.col;
- var line = code.split(/\r?\n/)[ex.line - (col ? 1 : 2)];
+ var lines = code.split(/\r?\n/);
+ var line = lines[ex.line - 1];
+ if (!line && !col) {
+ line = lines[ex.line - 2];
+ col = line.length;
+ }
if (line) {
if (col > 40) {
line = line.slice(col - 40);
col = 40;
}
- if (col) {
- print_error(line.slice(0, 80));
- print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
- } else {
- print_error(line.slice(-40));
- print_error(line.slice(-40).replace(/\S/g, " ") + "^");
- }
+ print_error(line.slice(0, 80));
+ print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
}
print_error(ex.stack);
process.exit(1);
diff --git a/lib/parse.js b/lib/parse.js
index c34e13d..6055a39 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -111,7 +111,7 @@ var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u20
var NEWLINE_CHARS = makePredicate(characters("\n\r\u2028\u2029"));
-var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,.;:"));
+var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,;:"));
var PUNC_CHARS = makePredicate(characters("[]{}(),;:"));
@@ -1354,14 +1354,15 @@ function parse($TEXT, options) {
function as_property_name() {
var tmp = S.token;
- next();
switch (tmp.type) {
+ case "operator":
+ if (!KEYWORDS(tmp.value)) unexpected();
case "num":
case "string":
case "name":
- case "operator":
case "keyword":
case "atom":
+ next();
return tmp.value;
default:
unexpected();
@@ -1370,16 +1371,9 @@ function parse($TEXT, options) {
function as_name() {
var tmp = S.token;
+ if (tmp.type != "name") unexpected();
next();
- switch (tmp.type) {
- case "name":
- case "operator":
- case "keyword":
- case "atom":
- return tmp.value;
- default:
- unexpected();
- }
+ return tmp.value;
};
function _make_symbol(type) {
@@ -1440,14 +1434,14 @@ function parse($TEXT, options) {
if (is("operator") && UNARY_PREFIX(start.value)) {
next();
handle_regexp();
- var ex = make_unary(AST_UnaryPrefix, start.value, maybe_unary(allow_calls));
+ var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls));
ex.start = start;
ex.end = prev();
return ex;
}
var val = expr_atom(allow_calls);
while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) {
- val = make_unary(AST_UnaryPostfix, S.token.value, val);
+ val = make_unary(AST_UnaryPostfix, S.token, val);
val.start = start;
val.end = S.token;
next();
@@ -1455,9 +1449,10 @@ function parse($TEXT, options) {
return val;
};
- function make_unary(ctor, op, expr) {
+ function make_unary(ctor, token, expr) {
+ var op = token.value;
if ((op == "++" || op == "--") && !is_assignable(expr))
- croak("Invalid use of " + op + " operator", null, ctor === AST_UnaryPrefix ? expr.start.col - 1 : null);
+ croak("Invalid use of " + op + " operator", token.line, token.col, token.pos);
return new ctor({ operator: op, expression: expr });
};
diff --git a/test/input/invalid/assign_4.js b/test/input/invalid/assign_4.js
new file mode 100644
index 0000000..d4d6b11
--- /dev/null
+++ b/test/input/invalid/assign_4.js
@@ -0,0 +1 @@
+++null
diff --git a/test/input/invalid/dot_1.js b/test/input/invalid/dot_1.js
new file mode 100644
index 0000000..7c4f3a6
--- /dev/null
+++ b/test/input/invalid/dot_1.js
@@ -0,0 +1 @@
+a.=
diff --git a/test/input/invalid/dot_2.js b/test/input/invalid/dot_2.js
new file mode 100644
index 0000000..32c027f
--- /dev/null
+++ b/test/input/invalid/dot_2.js
@@ -0,0 +1 @@
+%.a;
diff --git a/test/input/invalid/dot_3.js b/test/input/invalid/dot_3.js
new file mode 100644
index 0000000..6557382
--- /dev/null
+++ b/test/input/invalid/dot_3.js
@@ -0,0 +1 @@
+a./();
diff --git a/test/input/invalid/object.js b/test/input/invalid/object.js
new file mode 100644
index 0000000..46216d8
--- /dev/null
+++ b/test/input/invalid/object.js
@@ -0,0 +1 @@
+console.log({%: 1});
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index b956309..d5c9585 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -298,12 +298,87 @@ describe("bin/uglifyjs", function () {
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,18",
+ "Parse error at test/input/invalid/assign_3.js:1,17",
"console.log(3 || ++this);",
- " ^",
+ " ^",
+ "SyntaxError: Invalid use of ++ operator"
+ ].join("\n"));
+ done();
+ });
+ });
+ it("Should throw syntax error (++null)", function(done) {
+ 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",
+ "^",
"SyntaxError: Invalid use of ++ operator"
].join("\n"));
done();
});
});
+ it("Should throw syntax error (a.=)", function(done) {
+ 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.=",
+ " ^",
+ "SyntaxError: Unexpected token: operator (=)"
+ ].join("\n"));
+ done();
+ });
+ });
+ it("Should throw syntax error (%.a)", function(done) {
+ 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;",
+ "^",
+ "SyntaxError: Unexpected token: operator (%)"
+ ].join("\n"));
+ done();
+ });
+ });
+ it("Should throw syntax error (a./();)", function(done) {
+ 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./();",
+ " ^",
+ "SyntaxError: Unexpected token: operator (/)"
+ ].join("\n"));
+ done();
+ });
+ });
+ it("Should throw syntax error ({%: 1})", function(done) {
+ 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});",
+ " ^",
+ "SyntaxError: Unexpected token: operator (%)"
+ ].join("\n"));
+ done();
+ });
+ });
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git
More information about the Pkg-javascript-commits
mailing list