remove [#@]__PURE__ hint from comment when pure call dropped

This commit is contained in:
kzc 2017-02-20 21:49:47 -05:00
parent 58036cd0cc
commit d35b92cff6
3 changed files with 34 additions and 5 deletions

View File

@ -1273,21 +1273,26 @@ merge(Compressor.prototype, {
def(AST_Constant, return_false);
def(AST_This, return_false);
var pure_regex = /[@#]__PURE__/;
var pure_regex = /[@#]__PURE__/g;
function has_pure_annotation(node) {
if (node.pure !== undefined) return node.pure;
var pure = false;
var comments, last_comment;
if (node.start
&& node.start.comments_before
&& node.start.comments_before.length
&& pure_regex.test(node.start.comments_before[node.start.comments_before.length - 1].value)) {
&& (comments = node.start.comments_before)
&& comments.length
&& pure_regex.test((last_comment = comments[comments.length - 1]).value)) {
last_comment.value = last_comment.value.replace(pure_regex, ' ');
pure = true;
}
return node.pure = pure;
}
def(AST_Call, function(compressor){
if (has_pure_annotation(this)) return false;
if (has_pure_annotation(this)) {
compressor.warn("Dropping __PURE__ call [{file}:{line},{col}]", this.start);
return false;
}
var pure = compressor.option("pure_funcs");
if (!pure) return true;
if (typeof pure == "function") return pure(this);

View File

@ -48,4 +48,13 @@ pure_function_calls: {
baz(), quux();
a.b(), f.g();
}
expect_warnings: [
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:17,8]",
"WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:17,8]",
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:30,37]",
"WARN: Dropping unused variable iife2 [test/compress/issue-1261.js:30,16]",
"WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:28,8]",
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:38,8]",
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:39,31]"
]
}

View File

@ -95,4 +95,19 @@ describe("minify", function() {
assert.strictEqual(code, "var a=function(n){return n};");
});
});
describe("#__PURE__", function() {
it("should drop #__PURE__ hint after use", function() {
var result = Uglify.minify('//@__PURE__ comment1 #__PURE__ comment2\n foo(), bar();', {
fromString: true,
output: {
comments: "all",
beautify: false,
}
});
var code = result.code;
assert.strictEqual(code, "// comment1 comment2\nbar();");
});
});
});