Skip to content

Commit

Permalink
implement directives (mishoo#3203)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored and aleclarson committed Jul 1, 2018
1 parent 87b89f3 commit ef65819
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 206 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -620,6 +620,8 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u

- `dead_code` (default: `true`) -- remove unreachable code

- `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 @@ -54,6 +54,7 @@ function Compressor(options, false_by_default) {
comparisons : !false_by_default,
conditionals : !false_by_default,
dead_code : !false_by_default,
directives : !false_by_default,
drop_console : false,
drop_debugger : !false_by_default,
evaluate : !false_by_default,
Expand Down Expand Up @@ -3264,8 +3265,10 @@ merge(Compressor.prototype, {

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

var directives = makePredicate(["use asm", "use strict"]);
OPT(AST_Directive, function(self, compressor) {
if (compressor.has_directive(self.value) !== self) {
if (compressor.option("directives")
&& (!directives[self.value] || compressor.has_directive(self.value) !== self)) {
return make_node(AST_EmptyStatement, self);
}
return self;
Expand Down
22 changes: 22 additions & 0 deletions test/compress/functions.js
Expand Up @@ -2025,6 +2025,7 @@ deduplicate_parenthesis: {

drop_lone_use_strict: {
options = {
directives: true,
side_effects: true,
}
input: {
Expand Down Expand Up @@ -2052,6 +2053,27 @@ drop_lone_use_strict: {
}
}

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
6 changes: 3 additions & 3 deletions test/mocha/cli.js
Expand Up @@ -8,7 +8,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(30000);
var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS';
exec(command, function(err, stdout) {
Expand Down Expand Up @@ -68,15 +68,15 @@ 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) {
if (err) throw err;
assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n");
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 ef65819

Please sign in to comment.