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

Correctly include functions with side effects from side-effect-free modules #3944

Merged
merged 1 commit into from Jan 31, 2021
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
12 changes: 7 additions & 5 deletions src/Module.ts
Expand Up @@ -1061,14 +1061,16 @@ export default class Module {
variable.include();
this.graph.needsTreeshakingPass = true;
const variableModule = variable.module;
if (variableModule && variableModule !== this && variableModule instanceof Module) {
if (variableModule && variableModule instanceof Module) {
if (!variableModule.isExecuted) {
markModuleAndImpureDependenciesAsExecuted(variableModule);
}
const sideEffectModules = getAndExtendSideEffectModules(variable, this);
for (const module of sideEffectModules) {
if (!module.isExecuted) {
markModuleAndImpureDependenciesAsExecuted(module);
if (variableModule !== this) {
const sideEffectModules = getAndExtendSideEffectModules(variable, this);
for (const module of sideEffectModules) {
if (!module.isExecuted) {
markModuleAndImpureDependenciesAsExecuted(module);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/shared/Node.ts
Expand Up @@ -286,5 +286,5 @@ export function locateNode(node: Node) {
}

export function logNode(node: Node) {
console.log(node.context.code.slice(node.start, node.end));
return node.context.code.slice(node.start, node.end);
}
11 changes: 11 additions & 0 deletions test/function/samples/side-effect-free-module-function/_config.js
@@ -0,0 +1,11 @@
module.exports = {
description:
'correctly include functions with side effects from side-effect-free modules (#3942)',
options: {
plugins: {
transform() {
return { moduleSideEffects: false };
}
}
}
};
@@ -0,0 +1,4 @@
export function curry(fn, args = []) {
return (..._args) =>
(rest => (rest.length >= fn.length ? fn(...rest) : curry(fn, rest)))([...args, ..._args]);
}
11 changes: 11 additions & 0 deletions test/function/samples/side-effect-free-module-function/main.js
@@ -0,0 +1,11 @@
import { curry } from './curry';
let result;

function foo() {
result = 'foo';
}

var fooc = curry(foo);
fooc();

assert.strictEqual(result, 'foo');