Skip to content

Commit

Permalink
* Refactor ExecutionPathOptions types
Browse files Browse the repository at this point in the history
* Improve some names
  • Loading branch information
lukastaegert committed Jan 9, 2018
1 parent a27884d commit 181b016
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 95 deletions.
109 changes: 54 additions & 55 deletions src/ast/ExecutionPathOptions.ts
Expand Up @@ -8,60 +8,59 @@ import { Entity, WritableEntity } from './Entity';
import Property from './nodes/Property';
import { ExpressionEntity } from './nodes/shared/Expression';

const OPTION_IGNORED_LABELS = 'IGNORED_LABELS';
const OPTION_ACCESSED_NODES = 'ACCESSED_NODES';
const OPTION_ARGUMENTS_VARIABLES = 'ARGUMENTS_VARIABLES';
const OPTION_ASSIGNED_NODES = 'ASSIGNED_NODES';
const OPTION_IGNORE_BREAK_STATEMENTS = 'IGNORE_BREAK_STATEMENTS';
const OPTION_IGNORE_RETURN_AWAIT_YIELD = 'IGNORE_RETURN_AWAIT_YIELD';
const OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS =
'NODES_CALLED_AT_PATH_WITH_OPTIONS';
const OPTION_REPLACED_VARIABLE_INITS = 'REPLACED_VARIABLE_INITS';
const OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH =
'RETURN_EXPRESSIONS_ACCESSED_AT_PATH';
const OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH =
'RETURN_EXPRESSIONS_ASSIGNED_AT_PATH';
const OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH =
'RETURN_EXPRESSIONS_CALLED_AT_PATH';
export enum OptionTypes {
IGNORED_LABELS,
ACCESSED_NODES,
ARGUMENTS_VARIABLES,
ASSIGNED_NODES,
IGNORE_BREAK_STATEMENTS,
IGNORE_RETURN_AWAIT_YIELD,
NODES_CALLED_AT_PATH_WITH_OPTIONS,
REPLACED_VARIABLE_INITS,
RETURN_EXPRESSIONS_ACCESSED_AT_PATH,
RETURN_EXPRESSIONS_ASSIGNED_AT_PATH,
RETURN_EXPRESSIONS_CALLED_AT_PATH,
}

export type RESULT_KEY = {};
export const RESULT_KEY: RESULT_KEY = {};
export type KeyTypes = OptionTypes | Entity | RESULT_KEY;

export default class ExecutionPathOptions {
_optionValues: Immutable.Map<string, any>;
private optionValues: Immutable.Map<KeyTypes, boolean | Entity | ExpressionEntity[]>;

static create () {
return new this(Immutable.Map());
}

constructor (optionValues: Immutable.Map<string, any>) {
this._optionValues = optionValues;
private constructor (optionValues: Immutable.Map<KeyTypes, boolean | Entity | ExpressionEntity[]>) {
this.optionValues = optionValues;
}

get (option: string) {
return this._optionValues.get(option);
private get (option: OptionTypes) {
return this.optionValues.get(option);
}

remove (option: string) {
return new ExecutionPathOptions(this._optionValues.remove(option));
private remove (option: OptionTypes) {
return new ExecutionPathOptions(this.optionValues.remove(option));
}

set (option: string, value: any) {
return new ExecutionPathOptions(this._optionValues.set(option, value));
private set (option: OptionTypes, value: boolean | ExpressionEntity[]) {
return new ExecutionPathOptions(this.optionValues.set(option, value));
}

setIn (optionPath: (string | Entity | RESULT_KEY)[], value: any) {
return new ExecutionPathOptions(this._optionValues.setIn(optionPath, value));
private setIn (optionPath: (string | Entity | RESULT_KEY)[], value: boolean | Entity) {
return new ExecutionPathOptions(this.optionValues.setIn(optionPath, value));
}

addAccessedNodeAtPath (path: ObjectPath, node: ExpressionEntity) {
return this.setIn([OPTION_ACCESSED_NODES, node, ...path, RESULT_KEY], true);
return this.setIn([OptionTypes.ACCESSED_NODES, node, ...path, RESULT_KEY], true);
}

addAccessedReturnExpressionAtPath (path: ObjectPath, callExpression: CallExpression | Property) {
return this.setIn(
[
OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH,
OptionTypes.RETURN_EXPRESSIONS_ACCESSED_AT_PATH,
callExpression,
...path,
RESULT_KEY
Expand All @@ -71,13 +70,13 @@ export default class ExecutionPathOptions {
}

addAssignedNodeAtPath (path: ObjectPath, node: WritableEntity) {
return this.setIn([OPTION_ASSIGNED_NODES, node, ...path, RESULT_KEY], true);
return this.setIn([OptionTypes.ASSIGNED_NODES, node, ...path, RESULT_KEY], true);
}

addAssignedReturnExpressionAtPath (path: ObjectPath, callExpression: CallExpression | Property) {
return this.setIn(
[
OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH,
OptionTypes.RETURN_EXPRESSIONS_ASSIGNED_AT_PATH,
callExpression,
...path,
RESULT_KEY
Expand All @@ -89,7 +88,7 @@ export default class ExecutionPathOptions {
addCalledNodeAtPathWithOptions (path: ObjectPath, node: ExpressionEntity, callOptions: CallOptions) {
return this.setIn(
[
OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS,
OptionTypes.NODES_CALLED_AT_PATH_WITH_OPTIONS,
node,
...path,
RESULT_KEY,
Expand All @@ -102,7 +101,7 @@ export default class ExecutionPathOptions {
addCalledReturnExpressionAtPath (path: ObjectPath, callExpression: CallExpression | Property) {
return this.setIn(
[
OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH,
OptionTypes.RETURN_EXPRESSIONS_CALLED_AT_PATH,
callExpression,
...path,
RESULT_KEY
Expand All @@ -112,7 +111,7 @@ export default class ExecutionPathOptions {
}

getArgumentsVariables (): ExpressionEntity[] {
return <ExpressionEntity[]>(this.get(OPTION_ARGUMENTS_VARIABLES) || []);
return <ExpressionEntity[]>(this.get(OptionTypes.ARGUMENTS_VARIABLES) || []);
}

getHasEffectsWhenCalledOptions () {
Expand All @@ -122,30 +121,30 @@ export default class ExecutionPathOptions {
}

getReplacedVariableInit (variable: ThisVariable | ParameterVariable): ExpressionEntity {
return this._optionValues.getIn([OPTION_REPLACED_VARIABLE_INITS, variable]);
return this.optionValues.getIn([OptionTypes.REPLACED_VARIABLE_INITS, variable]);
}

hasNodeBeenAccessedAtPath (path: ObjectPath, node: ExpressionEntity): boolean {
return this._optionValues.getIn([
OPTION_ACCESSED_NODES,
return this.optionValues.getIn([
OptionTypes.ACCESSED_NODES,
node,
...path,
RESULT_KEY
]);
}

hasNodeBeenAssignedAtPath (path: ObjectPath, node: WritableEntity): boolean {
return this._optionValues.getIn([
OPTION_ASSIGNED_NODES,
return this.optionValues.getIn([
OptionTypes.ASSIGNED_NODES,
node,
...path,
RESULT_KEY
]);
}

hasNodeBeenCalledAtPathWithOptions (path: ObjectPath, node: ExpressionEntity, callOptions: CallOptions): boolean {
const previousCallOptions = this._optionValues.getIn([
OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS,
const previousCallOptions = this.optionValues.getIn([
OptionTypes.NODES_CALLED_AT_PATH_WITH_OPTIONS,
node,
...path,
RESULT_KEY
Expand All @@ -159,65 +158,65 @@ export default class ExecutionPathOptions {
}

hasReturnExpressionBeenAccessedAtPath (path: ObjectPath, callExpression: CallExpression | Property): boolean {
return this._optionValues.getIn([
OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH,
return this.optionValues.getIn([
OptionTypes.RETURN_EXPRESSIONS_ACCESSED_AT_PATH,
callExpression,
...path,
RESULT_KEY
]);
}

hasReturnExpressionBeenAssignedAtPath (path: ObjectPath, callExpression: CallExpression | Property): boolean {
return this._optionValues.getIn([
OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH,
return this.optionValues.getIn([
OptionTypes.RETURN_EXPRESSIONS_ASSIGNED_AT_PATH,
callExpression,
...path,
RESULT_KEY
]);
}

hasReturnExpressionBeenCalledAtPath (path: ObjectPath, callExpression: CallExpression | Property): boolean {
return this._optionValues.getIn([
OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH,
return this.optionValues.getIn([
OptionTypes.RETURN_EXPRESSIONS_CALLED_AT_PATH,
callExpression,
...path,
RESULT_KEY
]);
}

ignoreBreakStatements () {
return this.get(OPTION_IGNORE_BREAK_STATEMENTS);
return this.get(OptionTypes.IGNORE_BREAK_STATEMENTS);
}

ignoreLabel (labelName: string) {
return this._optionValues.getIn([OPTION_IGNORED_LABELS, labelName]);
return this.optionValues.getIn([OptionTypes.IGNORED_LABELS, labelName]);
}

ignoreReturnAwaitYield () {
return this.get(OPTION_IGNORE_RETURN_AWAIT_YIELD);
return this.get(OptionTypes.IGNORE_RETURN_AWAIT_YIELD);
}

replaceVariableInit (variable: ThisVariable | ParameterVariable, init: ExpressionEntity) {
return this.setIn([OPTION_REPLACED_VARIABLE_INITS, variable], init);
return this.setIn([OptionTypes.REPLACED_VARIABLE_INITS, variable], init);
}

setArgumentsVariables (variables: ExpressionEntity[]) {
return this.set(OPTION_ARGUMENTS_VARIABLES, variables);
return this.set(OptionTypes.ARGUMENTS_VARIABLES, variables);
}

setIgnoreBreakStatements (value = true) {
return this.set(OPTION_IGNORE_BREAK_STATEMENTS, value);
return this.set(OptionTypes.IGNORE_BREAK_STATEMENTS, value);
}

setIgnoreLabel (labelName: string) {
return this.setIn([OPTION_IGNORED_LABELS, labelName], true);
return this.setIn([OptionTypes.IGNORED_LABELS, labelName], true);
}

setIgnoreNoLabels () {
return this.remove(OPTION_IGNORED_LABELS);
return this.remove(OptionTypes.IGNORED_LABELS);
}

setIgnoreReturnAwaitYield (value = true) {
return this.set(OPTION_IGNORE_RETURN_AWAIT_YIELD, value);
return this.set(OptionTypes.IGNORE_RETURN_AWAIT_YIELD, value);
}
}
4 changes: 2 additions & 2 deletions src/ast/nodes/ArrayPattern.ts
Expand Up @@ -2,10 +2,10 @@ import { UNKNOWN_EXPRESSION } from '../values';
import Scope from '../scopes/Scope';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ObjectPath } from '../variables/VariableReassignmentTracker';
import { GenericPatternNode, PatternNode } from './shared/Pattern';
import { PatternBase, PatternNode } from './shared/Pattern';
import { ExpressionEntity } from './shared/Expression';

export default class ArrayPattern extends GenericPatternNode {
export default class ArrayPattern extends PatternBase {
type: 'ArrayPattern';
elements: (PatternNode | null)[];

Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/AssignmentPattern.ts
@@ -1,10 +1,10 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import Scope from '../scopes/Scope';
import { ObjectPath } from '../variables/VariableReassignmentTracker';
import { GenericPatternNode, PatternNode } from './shared/Pattern';
import { PatternBase, PatternNode } from './shared/Pattern';
import { ExpressionEntity, ExpressionNode } from './shared/Expression';

export default class AssignmentPattern extends GenericPatternNode {
export default class AssignmentPattern extends PatternBase {
type: 'AssignmentPattern';
left: PatternNode;
right: ExpressionNode;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/BlockStatement.ts
Expand Up @@ -4,13 +4,13 @@ import ExecutionPathOptions from '../ExecutionPathOptions';
import Scope from '../scopes/Scope';
import MagicString from 'magic-string';
import { Node } from './shared/Node';
import { GenericStatementNode, StatementNode } from './shared/Statement';
import { StatementBase, StatementNode } from './shared/Statement';

export function isBlockStatement (node: Node): node is BlockStatement {
return node.type === 'BlockStatement';
}

export default class BlockStatement extends GenericStatementNode {
export default class BlockStatement extends StatementBase {
type: 'BlockStatement';
scope: Scope;
body: StatementNode[];
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/BreakStatement.ts
@@ -1,8 +1,8 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import Identifier from './Identifier';
import { GenericStatementNode } from './shared/Statement';
import { StatementBase } from './shared/Statement';

export default class BreakStatement extends GenericStatementNode {
export default class BreakStatement extends StatementBase {
type: 'BreakStatement';
label: Identifier | null;

Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/DoWhileStatement.ts
@@ -1,8 +1,8 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { GenericStatementNode, StatementNode } from './shared/Statement';
import { StatementBase, StatementNode } from './shared/Statement';
import { ExpressionNode } from './shared/Expression';

export default class DoWhileStatement extends GenericStatementNode {
export default class DoWhileStatement extends StatementBase {
type: 'DoWhileStatement';
body: StatementNode;
test: ExpressionNode;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/EmptyStatement.ts
@@ -1,7 +1,7 @@
import MagicString from 'magic-string';
import { GenericStatementNode } from './shared/Statement';
import { StatementBase } from './shared/Statement';

export default class EmptyStatement extends GenericStatementNode {
export default class EmptyStatement extends StatementBase {
type: 'EmptyStatement';

render (code: MagicString, _es: boolean) {
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/ExpressionStatement.ts
@@ -1,7 +1,7 @@
import MagicString from 'magic-string';
import { GenericStatementNode } from './shared/Statement';
import { StatementBase } from './shared/Statement';

export default class ExpressionStatement extends GenericStatementNode {
export default class ExpressionStatement extends StatementBase {
render (code: MagicString, es: boolean) {
super.render(code, es);
if (this.included) this.insertSemicolon(code);
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/ForInStatement.ts
Expand Up @@ -3,11 +3,11 @@ import VariableDeclaration from './VariableDeclaration';
import Scope from '../scopes/Scope';
import ExecutionPathOptions from '../ExecutionPathOptions';
import BlockStatement from './BlockStatement';
import { GenericStatementNode, StatementNode } from './shared/Statement';
import { StatementBase, StatementNode } from './shared/Statement';
import { PatternNode } from './shared/Pattern';
import { ExpressionNode } from './shared/Expression';

export default class ForInStatement extends GenericStatementNode {
export default class ForInStatement extends StatementBase {
type: 'ForInStatement';
left: VariableDeclaration | PatternNode;
right: ExpressionNode;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/ForOfStatement.ts
Expand Up @@ -3,11 +3,11 @@ import ExecutionPathOptions from '../ExecutionPathOptions';
import VariableDeclaration from './VariableDeclaration';
import Scope from '../scopes/Scope';
import BlockStatement from './BlockStatement';
import { GenericStatementNode, StatementNode } from './shared/Statement';
import { StatementBase, StatementNode } from './shared/Statement';
import { PatternNode } from './shared/Pattern';
import { ExpressionNode } from './shared/Expression';

export default class ForOfStatement extends GenericStatementNode {
export default class ForOfStatement extends StatementBase {
type: 'ForOfStatement';
left: VariableDeclaration | PatternNode;
right: ExpressionNode;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/ForStatement.ts
Expand Up @@ -2,10 +2,10 @@ import BlockScope from '../scopes/BlockScope';
import VariableDeclaration from './VariableDeclaration';
import ExecutionPathOptions from '../ExecutionPathOptions';
import Scope from '../scopes/Scope';
import { GenericStatementNode, StatementNode } from './shared/Statement';
import { StatementBase, StatementNode } from './shared/Statement';
import { ExpressionNode } from './shared/Expression';

export default class ForStatement extends GenericStatementNode {
export default class ForStatement extends StatementBase {
type: 'ForStatement';
init: VariableDeclaration | ExpressionNode | null;
test: ExpressionNode | null;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -4,7 +4,7 @@ import Scope from '../scopes/Scope';
import { Node } from './shared/Node';
import { isVariableDeclaration } from './VariableDeclaration';
import MagicString from 'magic-string';
import { GenericStatementNode, StatementNode } from './shared/Statement';
import { StatementBase, StatementNode } from './shared/Statement';
import { ExpressionNode } from './shared/Expression';

// Statement types which may contain if-statements as direct children.
Expand Down Expand Up @@ -40,7 +40,7 @@ function getHoistedVars (node: StatementNode, scope: Scope) {
return hoistedVars;
}

export default class IfStatement extends GenericStatementNode {
export default class IfStatement extends StatementBase {
type: 'IfStatement';
test: ExpressionNode;
consequent: StatementNode;
Expand Down

0 comments on commit 181b016

Please sign in to comment.