From ba12997ce10a1904b34a2b8186d157dc9b5aa6d0 Mon Sep 17 00:00:00 2001 From: jiavan Date: Mon, 11 Jun 2018 02:42:02 +0800 Subject: [PATCH] add test case for lint --- test/input/lint/input.js | 20 ++++++++++++++++++ test/mocha/cli.js | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/input/lint/input.js diff --git a/test/input/lint/input.js b/test/input/lint/input.js new file mode 100644 index 00000000..3d510dd1 --- /dev/null +++ b/test/input/lint/input.js @@ -0,0 +1,20 @@ +function assignToGlobal() { + x = 1; +} + +function eval() { + eval("var x = 1"); +} + +function funcArguments() { + console.log("args: ", arguments); +} + +function nestedDefuns() { + if (true) { + function fn() { } + } +} + +function unreferencedFnc() { } +undeclaredFnc(); \ No newline at end of file diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 2e1c3b64..8aa3b852 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -776,4 +776,49 @@ describe("bin/uglifyjs", function() { done(); }); }); + it("Should warn for default options with --lint", function(done) { + var command = uglifyjscmd + " test/input/lint/input.js --lint"; + exec(command, function(err, stdout, stderr) { + if (err) throw err; + assert.strictEqual(stdout, 'function assignToGlobal(){x=1}function eval(){eval("var x = 1")}' + + 'function funcArguments(){console.log("args: ",arguments)}function nestedDefuns(){if(true){function fn(){}}}function unreferencedFnc(){}undeclaredFnc();\n'); + assert.strictEqual(stderr, [ + 'WARN: Accidental global?: x [test/input/lint/input.js:2,4]', + 'WARN: arguments used in function funcArguments [test/input/lint/input.js:9,0]', + 'WARN: Function fn declared in nested statement "BlockStatement" [test/input/lint/input.js:15,8]', + 'WARN: Symbol fn is declared but not referenced [test/input/lint/input.js:15,17]', + '' + ].join('\n')); + done(); + }); + }); + it("Should warn for all lint options", function(done) { + var command = uglifyjscmd + " test/input/lint/input.js --lint undeclared=true"; + exec(command, function(err, stdout, stderr) { + if (err) throw err; + assert.strictEqual(stdout, 'function assignToGlobal(){x=1}function eval(){eval("var x = 1")}function funcArguments()' + + '{console.log("args: ",arguments)}function nestedDefuns(){if(true){function fn(){}}}function unreferencedFnc(){}undeclaredFnc();\n'); + assert.strictEqual(stderr, [ + 'WARN: Accidental global?: x [test/input/lint/input.js:2,4]', + 'WARN: Undeclared symbol: x [test/input/lint/input.js:2,4]', + 'WARN: arguments used in function funcArguments [test/input/lint/input.js:9,0]', + 'WARN: Undeclared symbol: console [test/input/lint/input.js:10,4]', + 'WARN: Function fn declared in nested statement "BlockStatement" [test/input/lint/input.js:15,8]', + 'WARN: Symbol fn is declared but not referenced [test/input/lint/input.js:15,17]', + 'WARN: Undeclared symbol: undeclaredFnc [test/input/lint/input.js:20,0]', + '' + ].join('\n')); + done(); + }); + }); + it("Should work with --lint arg=value", function(done) { + var command = uglifyjscmd + " test/input/lint/input.js --lint assign_to_global=false,eval=false,func_arguments=false,nested_defuns=false,undeclared=false,unreferenced=true"; + exec(command, function(err, stdout, stderr) { + if (err) throw err; + assert.strictEqual(stdout, 'function assignToGlobal(){x=1}function eval(){eval("var x = 1")}function funcArguments()' + + '{console.log("args: ",arguments)}function nestedDefuns(){if(true){function fn(){}}}function unreferencedFnc(){}undeclaredFnc();\n'); + assert.strictEqual(stderr, 'WARN: Symbol fn is declared but not referenced [test/input/lint/input.js:15,17]\n'); + done(); + }); + }); });