-
-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: fix: don't drop side effects of default param assignments #1148
Draft
WofWca
wants to merge
1
commit into
terser:master
Choose a base branch
from
WofWca:param-default-assign-side-effects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
// dsa: { | ||
// options = { | ||
// toplevel: true, | ||
// unused: true, | ||
// reduce_vars: true, | ||
// side_effects: true, | ||
|
||
// inline: true, | ||
// } | ||
// input: { | ||
// // function cause_side_effect(a = console.log("side effect")) {} | ||
// // cause_side_effect(); | ||
|
||
// function cause_side_effect( | ||
// // {a} = {a:console.log("side effect 1")}, | ||
// // p2 = console.log("side effect 2"), | ||
// // p3 = console.log("side effect 3"), | ||
|
||
// p1 = 2, | ||
// p2 = (()=>{})(), | ||
// ) {} | ||
// cause_side_effect(undefined, 2); | ||
|
||
// // function dummy() {} | ||
// // dummy(cause_side_effect()); | ||
// } | ||
// expect: { | ||
// console.log("side effect 1"); | ||
// console.log("side effect 2"); | ||
// console.log("side effect 3"); | ||
// } | ||
// } | ||
// asd: { | ||
// options = { | ||
// toplevel: true, | ||
// unused: true, | ||
// reduce_vars: true, | ||
// side_effects: true, | ||
|
||
// inline: true, | ||
// // passes: 10, | ||
|
||
// // collapse_vars: true, | ||
// // conditionals: true, | ||
// // evaluate: true, | ||
// // inline: true, | ||
// // passes: 3, | ||
// // reduce_funcs: true, | ||
// // reduce_vars: true, | ||
// // sequences: true, | ||
// // unused: true, | ||
// } | ||
// input: { | ||
// // function cause_side_effect(a = console.log("side effect")) {} | ||
// // cause_side_effect(); | ||
|
||
// function cause_side_effect( | ||
// p1 = console.log("side effect 1"), | ||
// p2 = console.log("side effect 2"), | ||
// p3 = console.log("side effect 3"), | ||
// ) {} | ||
// cause_side_effect(undefined, 2); | ||
|
||
// // function dummy() {} | ||
// // dummy(cause_side_effect()); | ||
// } | ||
// expect: { | ||
// console.log("side effect 1"); | ||
// console.log("side effect 2"); | ||
// console.log("side effect 3"); | ||
// } | ||
// } | ||
simple: { | ||
options = { | ||
toplevel: true, | ||
unused: true, | ||
reduce_vars: true, | ||
side_effects: true, | ||
// passes: 5 | ||
} | ||
input: { | ||
function cause_side_effect(p = console.log("side effect")) {} | ||
|
||
cause_side_effect(); | ||
// If the argument evaluates to `undefined`, default assignment is also performed. | ||
cause_side_effect(undefined); | ||
// If it is unknown what the argument evaluates to, keep. | ||
// cause_side_effect(Math.random() > 0.5 ? undefined : true); | ||
cause_side_effect(id()); | ||
cause_side_effect(id); | ||
} | ||
expect: { | ||
function cause_side_effect(p = console.log("side effect")) {} | ||
|
||
cause_side_effect(); | ||
cause_side_effect(void 0); | ||
cause_side_effect(id()); | ||
cause_side_effect(id); | ||
} | ||
// TODO when I uncomment this it starts saying "failed running reminified input" and producing incorrect output. | ||
// expect_stdout: [ | ||
// "side effect", | ||
// "side effect", | ||
// "side effect", | ||
// ] | ||
} | ||
drop_if_assignment_not_used: { | ||
options = { | ||
toplevel: true, | ||
unused: true, | ||
reduce_vars: true, | ||
side_effects: true, | ||
} | ||
input: { | ||
function cause_side_effect(p = console.log("side effect")) {} | ||
|
||
cause_side_effect("a"); | ||
cause_side_effect(null); | ||
cause_side_effect({ a: 1 }); | ||
cause_side_effect(() => 1); | ||
} | ||
expect: {} | ||
} | ||
// undefined_as_argument: { | ||
// options = { | ||
// toplevel: true, | ||
// unused: true, | ||
// reduce_vars: true, | ||
// side_effects: true, | ||
// } | ||
// input: { | ||
// function cause_side_effect(p = console.log("side effect")) {} | ||
// cause_side_effect(undefined); | ||
// } | ||
// expect: { | ||
// console.log("side effect") | ||
// } | ||
// } | ||
no_false_positives: { | ||
options = { | ||
toplevel: true, | ||
unused: true, | ||
reduce_vars: true, | ||
side_effects: true, | ||
} | ||
input: { | ||
function no_side_effects( | ||
p1, | ||
p2 = "a", | ||
p3 = (() => {})(), | ||
p4 = undefined, | ||
p5 = [p1, p2, p3, p4], | ||
p6, | ||
...rest | ||
) { | ||
// return [p1, p2, p3, p4, p5, p6, ...rest]; | ||
} | ||
no_side_effects(); | ||
} | ||
expect: {} | ||
} | ||
object_destructuring_getter_side_effect: { | ||
options = { | ||
toplevel: true, | ||
unused: true, | ||
reduce_vars: true, | ||
side_effects: true, | ||
|
||
pure_getters: false, | ||
} | ||
input: { | ||
// Evaluating the object in itself is fine, but destructuring it can cause a side effect. | ||
const obj = { get a() { console.log("side effect") } }; | ||
function cause_side_effect( | ||
// {a, b} = { a: 1, b: 2 }, | ||
{a} = obj, | ||
) {} | ||
cause_side_effect(); | ||
} | ||
// expect: { | ||
// console.log("side effect") | ||
// } | ||
expect_stdout: [ | ||
"side effect", | ||
] | ||
} | ||
array_destructuring_getter_side_effect: { | ||
options = { | ||
toplevel: true, | ||
unused: true, | ||
reduce_vars: true, | ||
side_effects: true, | ||
|
||
pure_getters: false, | ||
} | ||
input: { | ||
const arr = [0, 0, 0]; | ||
arr.__defineGetter__(0, function () { | ||
console.log("side effect"); | ||
return 1; | ||
}) | ||
function cause_side_effect( | ||
// {a, b} = { a: 1, b: 2 }, | ||
[a, b] = arr, | ||
) {} | ||
cause_side_effect(); | ||
} | ||
// expect: { | ||
// console.log("side effect") | ||
// } | ||
expect_stdout: [ | ||
"side effect", | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's best to create a
is_not_undefined
in compress/inference.js and use that. This way there's no implicit dependency between is_constant and !is_undefined that needs to be maintained and remembered.