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

fix: only optimize annoymous iife #5486

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion src/ast/nodes/ArrowFunctionExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import type ChildScope from '../scopes/ChildScope';
import ReturnValueScope from '../scopes/ReturnValueScope';
import { type ObjectPath } from '../utils/PathTracker';
import type BlockStatement from './BlockStatement';
import type CallExpression from './CallExpression';
import Identifier from './Identifier';
import type * as NodeType from './NodeType';
import * as NodeType from './NodeType';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import FunctionBase from './shared/FunctionBase';
import type { ExpressionNode, IncludeChildren } from './shared/Node';
Expand Down Expand Up @@ -67,6 +68,13 @@ export default class ArrowFunctionExpression extends FunctionBase {
return false;
}

protected onlyFunctionCallUsed(): boolean {
const isIIFE =
this.parent.type === NodeType.CallExpression &&
(this.parent as CallExpression).callee === this;
return isIIFE || super.onlyFunctionCallUsed();
}

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
super.include(context, includeChildrenRecursively);
for (const parameter of this.params) {
Expand Down
9 changes: 9 additions & 0 deletions src/ast/nodes/FunctionExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import ChildScope from '../scopes/ChildScope';
import type CallExpression from './CallExpression';
import type { IdentifierWithVariable } from './Identifier';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
Expand All @@ -25,6 +26,14 @@ export default class FunctionExpression extends FunctionNode {
return super.parseNode(esTreeNode);
}

protected onlyFunctionCallUsed(): boolean {
const isIIFE =
this.parent.type === NodeType.CallExpression &&
(this.parent as CallExpression).callee === this &&
(this.id === null || this.id.variable.getOnlyFunctionCallUsed());
return isIIFE || super.onlyFunctionCallUsed();
}

render(
code: MagicString,
options: RenderOptions,
Expand Down
10 changes: 3 additions & 7 deletions src/ast/nodes/shared/FunctionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { UNDEFINED_EXPRESSION } from '../../values';
import type ParameterVariable from '../../variables/ParameterVariable';
import type Variable from '../../variables/Variable';
import BlockStatement from '../BlockStatement';
import type CallExpression from '../CallExpression';
import type ExportDefaultDeclaration from '../ExportDefaultDeclaration';
import Identifier from '../Identifier';
import * as NodeType from '../NodeType';
Expand Down Expand Up @@ -317,16 +316,13 @@ export default abstract class FunctionBase extends NodeBase {

private functionParametersOptimized = false;
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
const isIIFE =
this.parent.type === NodeType.CallExpression &&
(this.parent as CallExpression).callee === this;
const shoulOptimizeFunctionParameters = isIIFE || this.onlyFunctionCallUsed();
if (shoulOptimizeFunctionParameters) {
const shouldOptimizeFunctionParameters = this.onlyFunctionCallUsed();
if (shouldOptimizeFunctionParameters) {
this.applyFunctionParameterOptimization();
} else if (this.functionParametersOptimized) {
this.deoptimizeFunctionParameters();
}
this.functionParametersOptimized = shoulOptimizeFunctionParameters;
this.functionParametersOptimized = shouldOptimizeFunctionParameters;

if (!this.deoptimized) this.applyDeoptimizations();
this.included = true;
Expand Down
18 changes: 18 additions & 0 deletions test/form/samples/tree-shake-literal-parameter/iife/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,23 @@ const result2 = (function (enable) {
}
})();

const result3 = (function foo (enable) {
{
return 'enabled';
}
})();

// lose track of iife
const result4 = (function foo (enable) {
if (enable) {
unknown_global_function(foo);
return 'enabled';
} else {
return 'disabled';
}
})(true);

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);
22 changes: 21 additions & 1 deletion test/form/samples/tree-shake-literal-parameter/iife/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,25 @@ const result2 = (function (enable) {
}
})(true);

const result3 = (function foo (enable) {
if (enable) {
return 'enabled';
} else {
return 'disabled';
}
})(true);

// lose track of iife
const result4 = (function foo (enable) {
if (enable) {
unknown_global_function(foo);
return 'enabled';
} else {
return 'disabled';
}
})(true);

console.log(result1);
console.log(result2);
console.log(result2);
console.log(result3);
console.log(result4);
3 changes: 3 additions & 0 deletions test/function/samples/acorn-walk/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'in acorn walk, immediate function lose track so we do not optimize parameter'
});
26 changes: 26 additions & 0 deletions test/function/samples/acorn-walk/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
let found = false;

const base = {
ExpressionStatement(node, c) {
c(node.value, "Expression");
},
Expression() { },
Identifier() { }
};

function simple(node, visitors, baseVisitor) {
if (!baseVisitor) baseVisitor = base
; (function c(node, override) {
let type = override || node.type
baseVisitor[type](node, c)
if (visitors[type]) visitors[type](node)
})(node)
}

simple({ type: "ExpressionStatement", value: { type: "Identifier" } }, {
Expression(node) {
found = true;
}
});

assert.equal(found, true);