Skip to content

Commit

Permalink
implement directives (#3203)
Browse files Browse the repository at this point in the history
fixes #3166
  • Loading branch information
alexlamsl authored and fabiosantoscode committed Jul 5, 2018
1 parent 1442a0f commit dcccb60
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 141 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -666,6 +666,8 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
enabled `compress` transforms. Useful when you only want to enable a few
`compress` options while disabling the rest.

- `directives` (default: `true`) -- remove redundant or non-standard directives

- `drop_console` (default: `false`) -- Pass `true` to discard calls to
`console.*` functions. If you wish to drop a specific function call
such as `console.info` and/or retain side effects from function arguments
Expand Down
5 changes: 4 additions & 1 deletion lib/compress.js
Expand Up @@ -58,6 +58,7 @@ function Compressor(options, false_by_default) {
conditionals : !false_by_default,
dead_code : !false_by_default,
defaults : true,
directives : !false_by_default,
drop_console : false,
drop_debugger : !false_by_default,
ecma : 5,
Expand Down Expand Up @@ -3211,8 +3212,10 @@ merge(Compressor.prototype, {

/* -----[ optimizers ]----- */

var directives = ["use asm", "use strict"];
OPT(AST_Directive, function(self, compressor) {
if (compressor.has_directive(self.value) !== self) {
if (compressor.option("directives")
&& (!member(self.value, directives) || compressor.has_directive(self.value) !== self)) {
return make_node(AST_EmptyStatement, self);
}
return self;
Expand Down
3 changes: 3 additions & 0 deletions test/compress/directives.js
@@ -1,4 +1,7 @@
class_directives_compression: {
options = {
directives: true,
}
input: {
class foo {
foo() {
Expand Down
43 changes: 33 additions & 10 deletions test/compress/functions.js
Expand Up @@ -2246,6 +2246,37 @@ issue_2898: {
expect_stdout: "2"
}

deduplicate_parenthesis: {
input: {
({}).a = b;
(({}).a = b)();
(function() {}).a = b;
((function() {}).a = b)();
}
expect_exact: "({}).a=b;({}.a=b)();(function(){}).a=b;(function(){}.a=b)();"
}

issue_3166: {
options = {
directives: true,
}
input: {
"foo";
"use strict";
function f() {
"use strict";
"bar";
"use asm";
}
}
expect: {
"use strict";
function f() {
"use asm";
}
}
}

issue_3016_1: {
options = {
inline: true,
Expand Down Expand Up @@ -2507,6 +2538,7 @@ issue_3125: {

drop_lone_use_strict: {
options = {
directives: true,
side_effects: true,
}
input: {
Expand Down Expand Up @@ -2537,6 +2569,7 @@ drop_lone_use_strict: {
drop_lone_use_strict_arrows_1: {
options = {
side_effects: true,
directives: true,
}
input: {
var f0 = () => 0;
Expand Down Expand Up @@ -2594,13 +2627,3 @@ drop_lone_use_strict_arrows_2: {
}
node_version: ">=6"
}

deduplicate_parenthesis: {
input: {
({}).a = b;
(({}).a = b)();
(function() {}).a = b;
((function() {}).a = b)();
}
expect_exact: "({}).a=b;({}.a=b)();(function(){}).a=b;(function(){}.a=b)();"
}
6 changes: 3 additions & 3 deletions test/mocha/cli.js
Expand Up @@ -9,7 +9,7 @@ function read(path) {

describe("bin/uglifyjs", function() {
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
it("should produce a functional build when using --self", function (done) {
it("Should produce a functional build when using --self", function (done) {
this.timeout(120000);

var command = uglifyjscmd + ' --self -mc ecma=';
Expand Down Expand Up @@ -70,7 +70,7 @@ describe("bin/uglifyjs", function() {
done();
});
});
it("should not append source map to output when not using --source-map url=inline", function (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';

exec(command, function (err, stdout) {
Expand All @@ -80,7 +80,7 @@ describe("bin/uglifyjs", function() {
done();
});
});
it("should not consider source map file content as source map file name (issue #2082)", function (done) {
it("Should not consider source map file content as source map file name (issue #2082)", function (done) {
var command = [
uglifyjscmd,
"test/input/issue-2082/sample.js",
Expand Down

0 comments on commit dcccb60

Please sign in to comment.