improve error messages (#1506)

- better inheritance of `Error` sub-classes
- mark parse error against source in CLI

closes #235
closes #348
closes #524
closes #1356
closes #1405
This commit is contained in:
Alex Lam S.L
2017-02-27 03:40:54 +08:00
committed by GitHub
parent b1c593a041
commit 872270b149
15 changed files with 133 additions and 48 deletions

View File

@@ -199,4 +199,43 @@ describe("bin/uglifyjs", function () {
done();
});
});
it("Should fail with invalid syntax", function(done) {
var command = uglifyjscmd + ' test/input/invalid/simple.js';
exec(command, function (err, stdout, stderr) {
assert.ok(err);
var lines = stderr.split(/\n/);
assert.strictEqual(lines[0], "Parse error at test/input/invalid/simple.js:1,12");
assert.strictEqual(lines[1], "function f(a{}");
assert.strictEqual(lines[2], " ^");
assert.strictEqual(lines[3], "SyntaxError: Unexpected token punc «{», expected punc «,»");
done();
});
});
it("Should fail with correct marking of tabs", function(done) {
var command = uglifyjscmd + ' test/input/invalid/tab.js';
exec(command, function (err, stdout, stderr) {
assert.ok(err);
var lines = stderr.split(/\n/);
assert.strictEqual(lines[0], "Parse error at test/input/invalid/tab.js:1,12");
assert.strictEqual(lines[1], "\t\tfoo(\txyz, 0abc);");
assert.strictEqual(lines[2], "\t\t \t ^");
assert.strictEqual(lines[3], "SyntaxError: Invalid syntax: 0abc");
done();
});
});
it("Should fail with correct marking at start of line", function(done) {
var command = uglifyjscmd + ' test/input/invalid/eof.js';
exec(command, function (err, stdout, stderr) {
assert.ok(err);
var lines = stderr.split(/\n/);
assert.strictEqual(lines[0], "Parse error at test/input/invalid/eof.js:2,0");
assert.strictEqual(lines[1], "foo, bar(");
assert.strictEqual(lines[2], " ^");
assert.strictEqual(lines[3], "SyntaxError: Unexpected token: eof (undefined)");
done();
});
});
});

View File

@@ -13,7 +13,7 @@ describe("Comment", function() {
var fail = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "SyntaxError: Unexpected token: operator (>)" &&
e.message === "Unexpected token: operator (>)" &&
e.line === 2 &&
e.col === 0;
}
@@ -36,7 +36,7 @@ describe("Comment", function() {
var fail = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "SyntaxError: Unexpected token: operator (>)" &&
e.message === "Unexpected token: operator (>)" &&
e.line === 5 &&
e.col === 0;
}

View File

@@ -168,7 +168,7 @@ describe("Directives", function() {
throw new Error("Expected parser to fail");
} catch (e) {
assert.strictEqual(e instanceof uglify.JS_Parse_Error, true);
assert.strictEqual(e.message, "SyntaxError: Unexpected token: punc (])");
assert.strictEqual(e.message, "Unexpected token: punc (])");
}
test_directive(tokenizer, tests[i]);

View File

@@ -71,7 +71,7 @@ describe("Getters and setters", function() {
var fail = function(data) {
return function (e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "SyntaxError: Invalid getter/setter name: " + data.operator;
e.message === "Invalid getter/setter name: " + data.operator;
};
};

View File

@@ -50,7 +50,7 @@ describe("line-endings", function() {
}
var fail = function(e) {
return e instanceof Uglify.JS_Parse_Error &&
e.message === "SyntaxError: Unexpected line terminator";
e.message === "Unexpected line terminator";
}
for (var i = 0; i < inputs.length; i++) {
assert.throws(test(inputs[i]), fail);

View File

@@ -110,7 +110,7 @@ describe("minify", function() {
inSourceMap: "inline",
sourceMapInline: true
});
}, "multiple input and inline source map");
});
});
it("Should fail with SpiderMonkey and inline source map", function() {
assert.throws(function() {
@@ -119,7 +119,7 @@ describe("minify", function() {
sourceMapInline: true,
spidermonkey: true
});
}, "SpiderMonkey and inline source map");
});
});
});
@@ -156,4 +156,19 @@ describe("minify", function() {
});
});
describe("JS_Parse_Error", function() {
it("should throw syntax error", function() {
assert.throws(function() {
Uglify.minify("function f(a{}", { fromString: true });
}, function(err) {
assert.ok(err instanceof Error);
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
assert.strictEqual(err.filename, 0);
assert.strictEqual(err.line, 1);
assert.strictEqual(err.col, 12);
return true;
});
});
});
});

View File

@@ -15,7 +15,7 @@ describe("Number literals", function () {
}
var error = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "SyntaxError: Legacy octal literals are not allowed in strict mode";
e.message === "Legacy octal literals are not allowed in strict mode";
}
for (var i = 0; i < inputs.length; i++) {
assert.throws(test(inputs[i]), error, inputs[i]);

View File

@@ -19,7 +19,7 @@ describe("String literals", function() {
var error = function(e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "SyntaxError: Unterminated string constant";
e.message === "Unterminated string constant";
};
for (var input in inputs) {
@@ -49,7 +49,7 @@ describe("String literals", function() {
var error = function(e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "SyntaxError: Legacy octal escape sequences are not allowed in strict mode";
e.message === "Legacy octal escape sequences are not allowed in strict mode";
}
for (var input in inputs) {

View File

@@ -9,7 +9,7 @@ describe("With", function() {
}
var error = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "SyntaxError: Strict mode may not include a with statement";
e.message === "Strict mode may not include a with statement";
}
assert.throws(test, error);
});