Skip to content
Permalink
Browse files
Share path initializers
  • Loading branch information
lukastaegert committed May 15, 2018
1 parent 6be48e2 commit 77d0503ee7bb3199218394626c0cab2f1dd361f9
Showing 60 changed files with 125 additions and 110 deletions.
@@ -38,9 +38,9 @@ export default function loadConfigFile(
.then(({ code }: { code: string }) => {
// temporarily override require
const defaultLoader = require.extensions['.js'];
require.extensions['.js'] = (module, filename) => {
require.extensions['.js'] = (module: NodeModuleWithCompile, filename: string) => {
if (filename === configFile) {
(module as NodeModuleWithCompile)._compile(code, filename);
module._compile(code, filename);
} else {
defaultLoader(module, filename);
}
@@ -1,4 +1,4 @@
import ExecutionPathOptions from './ExecutionPathOptions';
import { ExecutionPathOptions } from './ExecutionPathOptions';
import { ObjectPath } from './values';

export interface Entity {
@@ -26,7 +26,7 @@ export type RESULT_KEY = {};
export const RESULT_KEY: RESULT_KEY = {};
export type KeyTypes = OptionTypes | Entity | RESULT_KEY;

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

static create() {
@@ -208,3 +208,5 @@ export default class ExecutionPathOptions {
return this.set(OptionTypes.IGNORE_RETURN_AWAIT_YIELD, value);
}
}

export const NEW_EXECUTION_PATH = ExecutionPathOptions.create();
@@ -3,7 +3,7 @@ import { SomeReturnExpressionCallback } from './shared/Expression';
import { ExpressionNode, NodeBase } from './shared/Node';
import * as NodeType from './NodeType';
import CallOptions from '../CallOptions';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import {
arrayMembers,
hasMemberEffectWhenCalled,
@@ -1,5 +1,5 @@
import { ObjectPath, UNKNOWN_EXPRESSION } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { EMPTY_PATH, ObjectPath, UNKNOWN_EXPRESSION } from '../values';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { PatternNode } from './shared/Pattern';
import { ExpressionEntity } from './shared/Expression';
import { NodeBase } from './shared/Node';
@@ -20,7 +20,8 @@ export default class ArrayPattern extends NodeBase implements PatternNode {
hasEffectsWhenAssignedAtPath(path: ObjectPath, options: ExecutionPathOptions) {
if (path.length > 0) return true;
for (const element of this.elements) {
if (element !== null && element.hasEffectsWhenAssignedAtPath([], options)) return true;
if (element !== null && element.hasEffectsWhenAssignedAtPath(EMPTY_PATH, options))
return true;
}
return false;
}
@@ -2,7 +2,7 @@ import Scope from '../scopes/Scope';
import ReturnValueScope from '../scopes/ReturnValueScope';
import BlockStatement, { isBlockStatement } from './BlockStatement';
import CallOptions from '../CallOptions';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { ForEachReturnExpressionCallback, SomeReturnExpressionCallback } from './shared/Expression';
import { PatternNode } from './shared/Pattern';
import { ExpressionNode, GenericEsTreeNode, NodeBase } from './shared/Node';
@@ -1,8 +1,8 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions, NEW_EXECUTION_PATH } from '../ExecutionPathOptions';
import { PatternNode } from './shared/Pattern';
import { ExpressionNode, NodeBase } from './shared/Node';
import * as NodeType from './NodeType';
import { ObjectPath, UNKNOWN_KEY } from '../values';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../values';

export default class AssignmentExpression extends NodeBase {
type: NodeType.tAssignmentExpression;
@@ -11,16 +11,16 @@ export default class AssignmentExpression extends NodeBase {

bind() {
super.bind();
this.left.reassignPath([], ExecutionPathOptions.create());
this.left.reassignPath(EMPTY_PATH, NEW_EXECUTION_PATH);
// We can not propagate mutations of the new binding to the old binding with certainty
this.right.reassignPath([UNKNOWN_KEY], ExecutionPathOptions.create());
this.right.reassignPath(UNKNOWN_PATH, NEW_EXECUTION_PATH);
}

hasEffects(options: ExecutionPathOptions): boolean {
return (
this.right.hasEffects(options) ||
this.left.hasEffects(options) ||
this.left.hasEffectsWhenAssignedAtPath([], options)
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, options)
);
}

@@ -1,9 +1,9 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions, NEW_EXECUTION_PATH } from '../ExecutionPathOptions';
import { PatternNode } from './shared/Pattern';
import { ExpressionEntity } from './shared/Expression';
import { ExpressionNode, NodeBase } from './shared/Node';
import * as NodeType from './NodeType';
import { ObjectPath } from '../values';
import { EMPTY_PATH, ObjectPath } from '../values';

export default class AssignmentPattern extends NodeBase implements PatternNode {
type: NodeType.tAssignmentPattern;
@@ -12,15 +12,15 @@ export default class AssignmentPattern extends NodeBase implements PatternNode {

bind() {
super.bind();
this.left.reassignPath([], ExecutionPathOptions.create());
this.left.reassignPath(EMPTY_PATH, NEW_EXECUTION_PATH);
}

declare(kind: string, init: ExpressionEntity | null) {
this.left.declare(kind, init);
}

hasEffectsWhenAssignedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
return path.length > 0 || this.left.hasEffectsWhenAssignedAtPath([], options);
return path.length > 0 || this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, options);
}

reassignPath(path: ObjectPath, options: ExecutionPathOptions) {
@@ -1,4 +1,4 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';

@@ -1,5 +1,5 @@
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_VALUE } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_VALUE, EMPTY_PATH } from '../values';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
import { LiteralValue } from './Literal';
@@ -39,10 +39,10 @@ export default class BinaryExpression extends NodeBase {

getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown {
if (path.length > 0) return UNKNOWN_VALUE;
const leftValue = this.left.getLiteralValueAtPath([]);
const leftValue = this.left.getLiteralValueAtPath(EMPTY_PATH);
if (leftValue === UNKNOWN_VALUE) return UNKNOWN_VALUE;

const rightValue = this.right.getLiteralValueAtPath([]);
const rightValue = this.right.getLiteralValueAtPath(EMPTY_PATH);
if (rightValue === UNKNOWN_VALUE) return UNKNOWN_VALUE;

const operatorFn = binaryOperators[this.operator];
@@ -1,6 +1,6 @@
import BlockScope from '../scopes/BlockScope';
import { UNKNOWN_EXPRESSION } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Scope from '../scopes/Scope';
import MagicString from 'magic-string';
import { Node, StatementBase, StatementNode } from './shared/Node';
@@ -1,4 +1,4 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { StatementBase } from './shared/Node';
@@ -1,11 +1,11 @@
import CallOptions from '../CallOptions';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions, NEW_EXECUTION_PATH } from '../ExecutionPathOptions';
import SpreadElement from './SpreadElement';
import Identifier from './Identifier';
import { ForEachReturnExpressionCallback, SomeReturnExpressionCallback } from './shared/Expression';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
import { ObjectPath, UNKNOWN_KEY } from '../values';
import { ObjectPath, UNKNOWN_PATH } from '../values';

export default class CallExpression extends NodeBase {
type: NodeType.tCallExpression;
@@ -42,7 +42,7 @@ export default class CallExpression extends NodeBase {
}
for (const argument of this.arguments) {
// This will make sure all properties of parameters behave as "unknown"
argument.reassignPath([UNKNOWN_KEY], ExecutionPathOptions.create());
argument.reassignPath(UNKNOWN_PATH, NEW_EXECUTION_PATH);
}
}

@@ -1,9 +1,9 @@
import { NodeBase } from './shared/Node';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import CallOptions from '../CallOptions';
import MethodDefinition from './MethodDefinition';
import * as NodeType from './NodeType';
import { ObjectPath } from '../values';
import { EMPTY_PATH, ObjectPath } from '../values';

export default class ClassBody extends NodeBase {
type: NodeType.tClassBody;
@@ -21,7 +21,7 @@ export default class ClassBody extends NodeBase {
}
return (
this.classConstructor !== null &&
this.classConstructor.hasEffectsWhenCalledAtPath([], callOptions, options)
this.classConstructor.hasEffectsWhenCalledAtPath(EMPTY_PATH, callOptions, options)
);
}

@@ -1,5 +1,5 @@
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_VALUE } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_VALUE, EMPTY_PATH } from '../values';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import CallOptions from '../CallOptions';
import MagicString from 'magic-string';
import { ForEachReturnExpressionCallback, SomeReturnExpressionCallback } from './shared/Expression';
@@ -185,7 +185,7 @@ export default class ConditionalExpression extends NodeBase {

private getTestValue() {
if (this.hasUnknownTestValue) return UNKNOWN_VALUE;
const value = this.test.getLiteralValueAtPath([]);
const value = this.test.getLiteralValueAtPath(EMPTY_PATH);
if (value === UNKNOWN_VALUE) {
this.hasUnknownTestValue = true;
}
@@ -1,4 +1,4 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { ExpressionNode, StatementNode, StatementBase } from './shared/Node';
import * as NodeType from './NodeType';

@@ -1,5 +1,5 @@
import { Node, NodeBase } from './shared/Node';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Literal from './Literal';
import MagicString from 'magic-string';
import ExportSpecifier from './ExportSpecifier';
@@ -1,12 +1,13 @@
import BlockScope from '../scopes/BlockScope';
import VariableDeclaration from './VariableDeclaration';
import Scope from '../scopes/Scope';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { PatternNode } from './shared/Pattern';
import * as NodeType from './NodeType';
import { ExpressionNode, Node, StatementBase, StatementNode } from './shared/Node';
import MagicString from 'magic-string';
import { NO_SEMICOLON, RenderOptions } from '../../utils/renderHelpers';
import { EMPTY_PATH } from '../values';

export function isForInStatement(node: Node): node is ForInStatement {
return node.type === NodeType.ForInStatement;
@@ -25,7 +26,8 @@ export default class ForInStatement extends StatementBase {
hasEffects(options: ExecutionPathOptions): boolean {
return (
(this.left &&
(this.left.hasEffects(options) || this.left.hasEffectsWhenAssignedAtPath([], options))) ||
(this.left.hasEffects(options) ||
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, options))) ||
(this.right && this.right.hasEffects(options)) ||
this.body.hasEffects(options.setIgnoreBreakStatements())
);
@@ -1,12 +1,13 @@
import BlockScope from '../scopes/BlockScope';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions, NEW_EXECUTION_PATH } from '../ExecutionPathOptions';
import VariableDeclaration from './VariableDeclaration';
import Scope from '../scopes/Scope';
import { PatternNode } from './shared/Pattern';
import * as NodeType from './NodeType';
import { ExpressionNode, Node, StatementBase, StatementNode } from './shared/Node';
import MagicString from 'magic-string';
import { NO_SEMICOLON, RenderOptions } from '../../utils/renderHelpers';
import { EMPTY_PATH } from '../values';

export function isForOfStatement(node: Node): node is ForOfStatement {
return node.type === NodeType.ForOfStatement;
@@ -20,7 +21,7 @@ export default class ForOfStatement extends StatementBase {

bind() {
super.bind();
this.left.reassignPath([], ExecutionPathOptions.create());
this.left.reassignPath(EMPTY_PATH, NEW_EXECUTION_PATH);
}

createScope(parentScope: Scope) {
@@ -30,7 +31,8 @@ export default class ForOfStatement extends StatementBase {
hasEffects(options: ExecutionPathOptions): boolean {
return (
(this.left &&
(this.left.hasEffects(options) || this.left.hasEffectsWhenAssignedAtPath([], options))) ||
(this.left.hasEffects(options) ||
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, options))) ||
(this.right && this.right.hasEffects(options)) ||
this.body.hasEffects(options.setIgnoreBreakStatements())
);
@@ -1,6 +1,6 @@
import BlockScope from '../scopes/BlockScope';
import VariableDeclaration from './VariableDeclaration';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Scope from '../scopes/Scope';
import * as NodeType from './NodeType';
import { ExpressionNode, Node, StatementBase, StatementNode } from './shared/Node';
@@ -1,7 +1,7 @@
import { Node, NodeBase } from './shared/Node';
import isReference from 'is-reference';
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Variable from '../variables/Variable';
import CallOptions from '../CallOptions';
import FunctionScope from '../scopes/FunctionScope';
@@ -1,9 +1,9 @@
import { UNKNOWN_VALUE } from '../values';
import { EMPTY_PATH, UNKNOWN_VALUE } from '../values';
import { ExpressionNode, StatementBase, StatementNode } from './shared/Node';
import MagicString from 'magic-string';
import * as NodeType from './NodeType';
import { RenderOptions } from '../../utils/renderHelpers';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';

export default class IfStatement extends StatementBase {
type: NodeType.tIfStatement;
@@ -53,7 +53,7 @@ export default class IfStatement extends StatementBase {
// Note that unknown test values are always included
const testValue = this.hasUnknownTestValue
? UNKNOWN_VALUE
: this.test.getLiteralValueAtPath([]);
: this.test.getLiteralValueAtPath(EMPTY_PATH);
if (
!this.test.included &&
(testValue ? this.alternate === null || !this.alternate.included : !this.consequent.included)
@@ -85,7 +85,7 @@ export default class IfStatement extends StatementBase {

private getTestValue() {
if (this.hasUnknownTestValue) return UNKNOWN_VALUE;
const value = this.test.getLiteralValueAtPath([]);
const value = this.test.getLiteralValueAtPath(EMPTY_PATH);
if (value === UNKNOWN_VALUE) {
this.hasUnknownTestValue = true;
}
@@ -1,4 +1,4 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { StatementBase, StatementNode } from './shared/Node';
@@ -1,4 +1,4 @@
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import MagicString from 'magic-string';
import { SomeReturnExpressionCallback } from './shared/Expression';
import { Node, NodeBase } from './shared/Node';
@@ -1,6 +1,6 @@
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_VALUE } from '../values';
import { ObjectPath, LiteralValueOrUnknown, UNKNOWN_VALUE, EMPTY_PATH } from '../values';
import CallOptions from '../CallOptions';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { ForEachReturnExpressionCallback, SomeReturnExpressionCallback } from './shared/Expression';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
@@ -201,7 +201,7 @@ export default class LogicalExpression extends NodeBase {

private getLeftValue() {
if (this.hasUnknownLeftValue) return UNKNOWN_VALUE;
const value = this.left.getLiteralValueAtPath([]);
const value = this.left.getLiteralValueAtPath(EMPTY_PATH);
if (value === UNKNOWN_VALUE) {
this.hasUnknownLeftValue = true;
}

0 comments on commit 77d0503

Please sign in to comment.