Skip to content

Commit

Permalink
Also mark nested functions as pure
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 22, 2022
1 parent 7b0e0ff commit 865c3c5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/ast/nodes/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ export default class Identifier extends NodeBase implements PatternNode {
let currentPureFunction = this.context.manualPureFunctions[this.name];
for (const segment of path) {
if (currentPureFunction) {
if (currentPureFunction[PureFunctionKey]) {
return true;
}
currentPureFunction = currentPureFunction[segment as string];
} else {
return false;
Expand Down
5 changes: 4 additions & 1 deletion test/form/samples/manual-pure-functions/_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
solo: true,
description: 'allows to manually declare functions as pure by name',
options: {
treeshake: { manualPureFunctions: ['foo', 'bar.baz'] }
treeshake: { manualPureFunctions: ['foo', 'bar.a'] }
}
};
// TODO Lukas also tagged templates
// TODO Lukas also check "this" or arguments are not deoptimized
10 changes: 8 additions & 2 deletions test/form/samples/manual-pure-functions/_expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const lib = () => console.log();
lib.baz = console.log;
lib.a = () => {
console.log();
const result = () => console.log();
result.c = console.log;
return result;
};
lib.a.b = () => console.log();

lib(); // not removed
lib.quuz(); // not removed
lib.b(); // not removed
9 changes: 6 additions & 3 deletions test/form/samples/manual-pure-functions/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { lib as bar } from './other';
const foo = console.log;

foo(); // removed
bar.baz(); // removed
bar.a(); // removed
bar?.a(); // removed

bar(); // not removed
bar.quuz(); // not removed
bar?.baz(); // removed
bar.b(); // not removed

bar.a.b(); // removed
8 changes: 7 additions & 1 deletion test/form/samples/manual-pure-functions/other.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export const lib = () => console.log();
lib.baz = console.log;
lib.a = () => {
console.log();
const result = () => console.log();
result.c = console.log;
return result;
};
lib.a.b = () => console.log();

0 comments on commit 865c3c5

Please sign in to comment.