Skip to content
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

inlining of the function definitions that have conflicting arguments #908

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 41 additions & 2 deletions lib/compress/index.js
Expand Up @@ -5603,15 +5603,54 @@ def_optimize(AST_Call, function(self, compressor) {
if (has_flag(arg, UNUSED)) continue;
if (!safe_to_inject
|| block_scoped.has(arg.name)
|| identifier_atom.has(arg.name)
|| scope.conflicting_def(arg.name)) {
|| identifier_atom.has(arg.name)) {
return false;
}
if (scope.conflicting_def(arg.name)) {
rename_arg(arg);
}
if (in_loop) in_loop.push(arg.definition());
}
return true;
}

function rename_arg(arg) {
const oldName = arg.name;
const newName = fn.create_symbol(AST_SymbolFunarg, {
source: fn,
scope: scope,
tentative_name: "argument_" + fn.argnames.length,
}).name;
fn.body[0].walk(new TreeWalker(function(node) {
if (node instanceof AST_Scope) {
for (let iVar = 0, numVars = node.variables.length; iVar < numVars; iVar++) {
node.variables[iVar] = rename(node.variables[iVar], newName, oldName);
}
}
if (node instanceof AST_SymbolCatch || node instanceof AST_SymbolRef) {
node.thedef = rename(node.definition(), newName, oldName);
}
return node;
}));
}

function rename(def, newName, oldName) {
if (def.name === oldName) {
def.name = newName;
}
for (const sym of def.orig) {
if (sym.name === oldName) {
sym.name = newName;
}
}
for (const sym of def.references) {
if (sym.name === oldName) {
sym.name = newName;
}
}
return def;
}

function can_inject_vars(block_scoped, safe_to_inject) {
var len = fn.body.length;
for (var i = 0; i < len; i++) {
Expand Down
73 changes: 73 additions & 0 deletions test/compress/toplevel-pure-method.js
@@ -0,0 +1,73 @@
assign_and_inline: {
options = {
toplevel: true,
defaults: true,
}
input: {
class MyClass {
method(a1) {
myPureFunction(a1)
return a1 + 2
}
}

function myPureFunction (a) {
console.log(a)
}

new MyClass();
}
expect: {
new class{method(a1) {var a;return a=a1,console.log(a),a1+2}};
}
}

inline_different_name: {
options = {
toplevel: true,
defaults: true,
pure_getters: true
}
input: {
class MyClass {
method(a1) {
myPureFunction(a1)
return a1 + 2
}
}

function myPureFunction (a) {
console.log(a)
}

new MyClass();
}
expect: {
new class{method(a1) {return console.log(a1),a1+2}};
}
}

rename_and_inline: {
options = {
toplevel: true,
defaults: true,
pure_getters: false // TODO This even works without pure getters which is not consistent with the previous test
Copy link
Author

@aminya aminya May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inconsistent behavior is surprising.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pure_getters option only assumes that a.b is pure (not always safe due to proxies and impure getters). This test doesn't use any getters so it's not a surprise that the results don't change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this changes the results for the first two tests, where the only difference is the value of pure_getters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't notice this at first.

}
input: {
class MyClass {
method(a) {
myPureFunction(a)
return a + 2
}
}

function myPureFunction (a) {
console.log(a)
}

new MyClass();
}
expect: {
new class{method(a) {return console.log(a),a+2}};
}
}