Skip to content

Commit

Permalink
Do not take the /*#__PURE__*/ annotation into account when the side_e…
Browse files Browse the repository at this point in the history
…ffects option is false. Closes #672
  • Loading branch information
fabiosantoscode committed Dec 6, 2023
1 parent 14cc5e1 commit aa2a72d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/compress/inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,10 @@ AST_Call.DEFMETHOD("is_callee_pure", function(compressor) {
if ((this instanceof AST_New) && compressor.option("pure_new")) {
return true;
}
return !!has_annotation(this, _PURE) || !compressor.pure_funcs(this);
if (compressor.option("side_effects") && has_annotation(this, _PURE)) {
return true;
}
return !compressor.pure_funcs(this);
});

// If I call this, is it a pure function?
Expand Down
9 changes: 8 additions & 1 deletion lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ import {
const CODE_LINE_BREAK = 10;
const CODE_SPACE = 32;

const r_annotation = /[@#]__(PURE|INLINE|NOINLINE)__/g;
const r_annotation = /[@#]__(PURE|INLINE|NOINLINE)__/;

function is_some_comments(comment) {
// multiline comment
Expand Down Expand Up @@ -321,6 +321,13 @@ function OutputStream(options) {
}
}

if (options.preserve_annotations) {
let prev_comment_filter = comment_filter;
comment_filter = function (comment) {
return r_annotation.test(comment.value) || prev_comment_filter.apply(this, arguments);
};
}

var indentation = 0;
var current_col = 0;
var current_line = 1;
Expand Down
1 change: 1 addition & 0 deletions test/compress/harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ classes_extending_classes_out_of_pure_iifes: {
options = {
toplevel: true,
unused: true,
side_effects: true,
}
input: {
let Base = /*@__PURE__*/ (() => {
Expand Down
60 changes: 43 additions & 17 deletions test/compress/pure_funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ issue_2629_1: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand All @@ -318,7 +317,6 @@ issue_2629_2: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand All @@ -343,7 +341,6 @@ issue_2629_3: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand Down Expand Up @@ -406,7 +403,6 @@ issue_2638: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand All @@ -424,7 +420,6 @@ issue_2705_1: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand All @@ -443,7 +438,6 @@ issue_2705_2: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand All @@ -468,7 +462,6 @@ issue_2705_3: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
Expand All @@ -488,8 +481,7 @@ issue_2705_3: {
(/*@__PURE__*/ new n.x(1).y(2).z(3));
}
expect_exact: [
"new/*@__PURE__*/h.x(1).y(2).z(3);",
"new/*@__PURE__*/i.x(1).y(2).z(3);",
"new/*@__PURE__*/h.x(1).y(2).z(3);new/*@__PURE__*/i.x(1).y(2).z(3);",
"/*@__PURE__*/new j.x(1).y(2).z(3);",
"/*@__PURE__*/new k.x(1).y(2).z(3);",
"/*@__PURE__*/new l.x(1).y(2).z(3);",
Expand Down Expand Up @@ -531,33 +523,67 @@ issue_2705_6: {
side_effects: true,
}
beautify = {
comments: "all",
preserve_annotations: true,
}
input: {
/*@__PURE__*/new (g() || h())(x(), y());
/* */ new (/*@__PURE__*/ (a() || b()))(c(), d());
}
expect_exact: [
"/*@__PURE__*/x(),y();",
"new(/*@__PURE__*/a()||b())(c(),d());",
"/*@__PURE__*/x(),y();new(/*@__PURE__*/a()||b())(c(),d());",
]
}

issue_526_1: {
options = {
side_effects: true,
}
beautify = {
comments: "all",
}
input: {
/*@__PURE__*/new (g() || h())(x(), y());
/* */ new (/*@__PURE__*/ (a() || b()))(c(), d());
}
expect_exact: "x(),y();new(a()||b())(c(),d());"
}

issue_t672: {
options = {
defaults: true,
side_effects: false,
}
beautify = {
preserve_annotations: true,
}
input: {
function f () {
/*#__PURE__*/a();
/*#__PURE__*/b();
}
}
expect_exact: [
"function f(){",
"/*#__PURE__*/a(),",
"/*#__PURE__*/b()}"
]
}

issue_t672_2: {
options = {
defaults: true,
side_effects: false,
}
beautify = {
preserve_annotations: true,
}
input: {
function f () {
/*#__PURE__*/a();
/*#__PURE__*/b();
}
}
expect_exact: [
"x(),y();",
"new(a()||b())(c(),d());",
"function f(){",
"/*#__PURE__*/a(),",
"/*#__PURE__*/b()}"
]
}

Expand Down

0 comments on commit aa2a72d

Please sign in to comment.