From d35b92cff6c8a6c5cd8acd21bacfd0193e13c6ba Mon Sep 17 00:00:00 2001 From: kzc Date: Mon, 20 Feb 2017 21:49:47 -0500 Subject: [PATCH] remove [#@]__PURE__ hint from comment when pure call dropped --- lib/compress.js | 15 ++++++++++----- test/compress/issue-1261.js | 9 +++++++++ test/mocha/minify.js | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 0c6e4c31..5e4c483a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); diff --git a/test/compress/issue-1261.js b/test/compress/issue-1261.js index beb00949..eedf4d4f 100644 --- a/test/compress/issue-1261.js +++ b/test/compress/issue-1261.js @@ -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]" + ] } diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 70cf73ae..8fe1565f 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -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();"); + }); + }); + });