Skip to content

Commit

Permalink
Correctly include functions with side effects from side-effect-free m…
Browse files Browse the repository at this point in the history
…odules (#3944)
  • Loading branch information
lukastaegert committed Jan 31, 2021
1 parent bc2bf95 commit be2e06d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
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');

0 comments on commit be2e06d

Please sign in to comment.