Skip to content

Commit

Permalink
Test or remove some edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Aug 20, 2019
1 parent 0549d99 commit f5441a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
42 changes: 14 additions & 28 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -7,13 +7,7 @@ import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import FunctionScope from '../scopes/FunctionScope';
import { ImmutableEntityPathTracker } from '../utils/ImmutableEntityPathTracker';
import {
EMPTY_PATH,
LiteralValueOrUnknown,
ObjectPath,
UNKNOWN_EXPRESSION,
UNKNOWN_VALUE
} from '../values';
import { EMPTY_PATH, LiteralValueOrUnknown, ObjectPath } from '../values';
import GlobalVariable from '../variables/GlobalVariable';
import LocalVariable from '../variables/LocalVariable';
import Variable from '../variables/Variable';
Expand Down Expand Up @@ -69,24 +63,20 @@ export default class Identifier extends NodeBase implements PatternNode {
case 'parameter':
variable = (this.scope as FunctionScope).addParameterDeclaration(this);
break;
/* istanbul ignore next */
default:
throw new Error(`Unexpected identifier kind ${kind}.`);
/* istanbul ignore next */
throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
}
return [(this.variable = variable)];
}

deoptimizePath(path: ObjectPath) {
if (!this.bound) this.bind();
if (this.variable !== null) {
if (
path.length === 0 &&
this.name in this.context.importDescriptions &&
!this.scope.contains(this.name)
) {
this.disallowImportReassignment();
}
this.variable.deoptimizePath(path);
if (path.length === 0 && !this.scope.contains(this.name)) {
this.disallowImportReassignment();
}
(this.variable as Variable).deoptimizePath(path);
}

getLiteralValueAtPath(
Expand All @@ -95,10 +85,7 @@ export default class Identifier extends NodeBase implements PatternNode {
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
if (!this.bound) this.bind();
if (this.variable !== null) {
return this.variable.getLiteralValueAtPath(path, recursionTracker, origin);
}
return UNKNOWN_VALUE;
return (this.variable as Variable).getLiteralValueAtPath(path, recursionTracker, origin);
}

getReturnExpressionWhenCalledAtPath(
Expand All @@ -107,10 +94,11 @@ export default class Identifier extends NodeBase implements PatternNode {
origin: DeoptimizableEntity
) {
if (!this.bound) this.bind();
if (this.variable !== null) {
return this.variable.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin);
}
return UNKNOWN_EXPRESSION;
return (this.variable as Variable).getReturnExpressionWhenCalledAtPath(
path,
recursionTracker,
origin
);
}

hasEffects(): boolean {
Expand Down Expand Up @@ -147,9 +135,7 @@ export default class Identifier extends NodeBase implements PatternNode {
}

includeCallArguments(args: (ExpressionNode | SpreadElement)[]): void {
if (this.variable) {
this.variable.includeCallArguments(args);
}
(this.variable as Variable).includeCallArguments(args);
}

render(
Expand Down
7 changes: 6 additions & 1 deletion test/function/samples/wraps-simplified-expressions/main.js
@@ -1,6 +1,6 @@
const wrapper = {
foo() {
assert.notEqual(this, wrapper)
assert.notEqual(this, wrapper);
}
};

Expand All @@ -12,11 +12,16 @@ const wrapper = {
(true && (true ? wrapper.foo : null))();
(true && (1, 2, wrapper.foo))();

function evoke(callee, arg) {
return callee(arg);
}

// Indirectly invoked eval is executed in the global scope
function testEval() {
assert.notEqual((true && eval)('this'), 'test');
assert.notEqual((true ? eval : null)('this'), 'test');
assert.notEqual((1, 2, eval)('this'), 'test');
assert.equal(evoke(true && eval, '42'), '42');
}

testEval.call('test');

0 comments on commit f5441a9

Please sign in to comment.