Skip to content

Commit

Permalink
extract pure function check
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 18, 2022
1 parent f0eb99e commit d21e596
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
} from './utils/error';
import { analyseModuleExecution } from './utils/executionOrder';
import { addAnnotations } from './utils/pureComments';
import { getPureFunctions } from './utils/pureFunctions';
import type { PureFunctions } from './utils/pureFunctions';
import { timeEnd, timeStart } from './utils/timers';
import { markModuleAndImpureDependenciesAsExecuted } from './utils/traverseStaticDependencies';

Expand Down Expand Up @@ -59,6 +61,7 @@ export default class Graph {
needsTreeshakingPass = false;
phase: BuildPhase = BuildPhase.LOAD_AND_PARSE;
readonly pluginDriver: PluginDriver;
readonly pureFunctions: PureFunctions;
readonly scope = new GlobalScope();
readonly watchFiles: Record<string, true> = Object.create(null);
watchMode = false;
Expand Down Expand Up @@ -94,6 +97,7 @@ export default class Graph {
this.acornParser = acorn.Parser.extend(...(options.acornInjectPlugins as any[]));
this.moduleLoader = new ModuleLoader(this, this.modulesById, this.options, this.pluginDriver);
this.fileOperationQueue = new Queue(options.maxParallelFileOps);
this.pureFunctions = getPureFunctions(options);
}

async build(): Promise<void> {
Expand Down
3 changes: 1 addition & 2 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import {
} from './utils/parseAssertions';
import { basename, extname } from './utils/path';
import type { PureFunctions } from './utils/pureFunctions';
import { getPureFunctions } from './utils/pureFunctions';
import type { RenderOptions } from './utils/renderHelpers';
import { timeEnd, timeStart } from './utils/timers';
import { markModuleAndImpureDependenciesAsExecuted } from './utils/traverseStaticDependencies';
Expand Down Expand Up @@ -783,7 +782,7 @@ export default class Module {
includeDynamicImport: this.includeDynamicImport.bind(this),
includeVariableInModule: this.includeVariableInModule.bind(this),
magicString: this.magicString,
manualPureFunctions: getPureFunctions(this.options),
manualPureFunctions: this.graph.pureFunctions,
module: this,
moduleContext: this.context,
options: this.options,
Expand Down
43 changes: 16 additions & 27 deletions src/ast/nodes/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,9 @@ export default class Identifier extends NodeBase implements PatternNode {
): boolean {
switch (interaction.type) {
case INTERACTION_ACCESSED: {
// TODO Lukas extract and cache lazily?
let currentPureFunction = this.context.manualPureFunctions[this.name];
for (const segment of path) {
if (currentPureFunction) {
currentPureFunction = currentPureFunction[segment as string];
} else {
break;
}
}
if (currentPureFunction?.[PureFunctionKey]) {
return false;
}
return (
this.variable !== null &&
!this.isPureFunction(path) &&
this.getVariableRespectingTDZ()!.hasEffectsOnInteractionAtPath(path, interaction, context)
);
}
Expand All @@ -187,21 +176,9 @@ export default class Identifier extends NodeBase implements PatternNode {
)!.hasEffectsOnInteractionAtPath(path, interaction, context);
}
case INTERACTION_CALLED: {
let currentPureFunction = this.context.manualPureFunctions[this.name];
for (const segment of path) {
if (currentPureFunction) {
currentPureFunction = currentPureFunction[segment as string];
} else {
break;
}
}
if (currentPureFunction?.[PureFunctionKey]) {
return false;
}
return this.getVariableRespectingTDZ()!.hasEffectsOnInteractionAtPath(
path,
interaction,
context
return (
!this.isPureFunction(path) &&
this.getVariableRespectingTDZ()!.hasEffectsOnInteractionAtPath(path, interaction, context)
);
}
}
Expand Down Expand Up @@ -311,6 +288,18 @@ export default class Identifier extends NodeBase implements PatternNode {
}
return this.variable;
}

private isPureFunction(path: ObjectPath) {
let currentPureFunction = this.context.manualPureFunctions[this.name];
for (const segment of path) {
if (currentPureFunction) {
currentPureFunction = currentPureFunction[segment as string];
} else {
return false;
}
}
return currentPureFunction?.[PureFunctionKey];
}
}

function closestParentFunctionOrProgram(node: any): any {
Expand Down
1 change: 0 additions & 1 deletion src/utils/pureFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface PureFunctions {
[PureFunctionKey]?: boolean;
}

// TODO Lukas put on Graph
export const getPureFunctions = ({ treeshake }: NormalizedInputOptions): PureFunctions => {
const pureFunctions: PureFunctions = Object.create(null);
for (const functionName of treeshake ? treeshake.manualPureFunctions : []) {
Expand Down

0 comments on commit d21e596

Please sign in to comment.