Skip to content

Commit

Permalink
Ensure properties are deoptimized by default
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 14, 2022
1 parent bc60156 commit 9b71326
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rollup.config.ts
Expand Up @@ -6,7 +6,7 @@ import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import type { RollupOptions, WarningHandlerWithDefault } from 'rollup';
import type { Plugin, RollupOptions, WarningHandlerWithDefault } from 'rollup';
import { string } from 'rollup-plugin-string';
import { terser } from 'rollup-plugin-terser';
import addCliEntry from './build-plugins/add-cli-entry';
Expand Down Expand Up @@ -65,7 +65,7 @@ const treeshake = {
tryCatchDeoptimization: false
};

const nodePlugins = [
const nodePlugins: Plugin[] = [
alias(moduleAliases),
nodeResolve(),
json(),
Expand Down
1 change: 0 additions & 1 deletion src/ast/nodes/ObjectExpression.ts
Expand Up @@ -26,7 +26,6 @@ import { NodeBase } from './shared/Node';
import { ObjectEntity, type ObjectProperty } from './shared/ObjectEntity';
import { OBJECT_PROTOTYPE } from './shared/ObjectPrototype';

// TODO Lukas ensure deoptimizations always happen twice in hasEffects and include
export default class ObjectExpression extends NodeBase implements DeoptimizableEntity {
declare properties: readonly (Property | SpreadElement)[];
declare type: NodeType.tObjectExpression;
Expand Down
29 changes: 24 additions & 5 deletions src/ast/nodes/shared/Node.ts
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../ExecutionContext';
import { getAndCreateKeys, keys } from '../../keys';
import type ChildScope from '../../scopes/ChildScope';
import { UNKNOWN_PATH } from '../../utils/PathTracker';
import type Variable from '../../variables/Variable';
import * as NodeType from '../NodeType';
import { ExpressionEntity, InclusionOptions } from './Expression';
Expand All @@ -23,7 +24,6 @@ export interface GenericEsTreeNode extends acorn.Node {
export const INCLUDE_PARAMETERS = 'variables' as const;
export type IncludeChildren = boolean | typeof INCLUDE_PARAMETERS;

// TODO Lukas consider deoptimizing all properties by default unless explicitly overridden
export interface Node extends Entity {
annotations?: acorn.Comment[];
context: AstContext;
Expand Down Expand Up @@ -240,14 +240,33 @@ export class NodeBase extends ExpressionEntity implements ExpressionNode {
return this.included || (!context.brokenFlow && this.hasEffects(createHasEffectsContext()));
}

protected applyDeoptimizations(): void {}
/**
* Just deoptimize everything by default so that when e.g. we do not track
* something properly, it is deoptimized.
* @protected
*/
protected applyDeoptimizations(): void {
for (const key of this.keys) {
const value = (this as GenericEsTreeNode)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
child?.deoptimizePath(UNKNOWN_PATH);
}
} else {
value.deoptimizePath(UNKNOWN_PATH);
}
}
}
}

export { NodeBase as StatementBase };

export function locateNode(node: Node): Location {
const location = locate(node.context.code, node.start, { offsetLine: 1 });
(location as any).file = node.context.fileName;
export function locateNode(node: Node): Location & { file: string } {
const location = locate(node.context.code, node.start, { offsetLine: 1 }) as Location & {
file: string;
};
location.file = node.context.fileName;
location.toString = () => JSON.stringify(location);

return location;
Expand Down

0 comments on commit 9b71326

Please sign in to comment.