Skip to content

Commit

Permalink
Tests for eval
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 17, 2023
1 parent 7077a3f commit 0aa5b4b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
11 changes: 11 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
* Tests for `with` in `eval()`
* Tests for `this` where implicitly used by `super()` and `super`

* Raise Github issue for getting `eval`:

```js
with ({eval: 123}) {
return () => eval;
}
```

Instrumentation replaces `eval` with `livepack_tracker.evalIndirect`, and therefore avoids
getting `eval` from the `with ()` object.

* Raise Github issue for interaction with const violations

e.g. How to deal with this? Whether `x = 2` is a const violation depends on whether `obj` has a property called `x` or not.
Expand Down
47 changes: 46 additions & 1 deletion test/with.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,52 @@ describe('with statements', () => {
}
});

describe('allows access to `eval`', () => {
/* eslint-disable no-eval */
// This test fails because instrumentation replaces `eval` with `livepack_tracker.evalIndirect`,
// and therefore avoids getting `eval` from `obj.eval` in the `with ()`.
// TODO: Fix this
itSerializes.skip('global', {
in() {
const obj = {eval: 123};
with (obj) {
return [() => eval, obj];
}
},
out: `(()=>{
const a={eval:123};
return[(a=>{with(a)return()=>eval})(a),a]
})()`,
validate([fn, obj]) {
expect(fn).toBeFunction();
expect(fn()).toBe(123);
delete obj.eval;
expect(fn()).toBe(eval);
}
});

itSerializes('local var', {
in() {
const eval = 123; // eslint-disable-line no-shadow-restricted-names
const obj = {eval: 456};
with (obj) {
return [() => eval, obj];
}
},
out: `(()=>{
const a={eval:456};
return[(eval=>a=>{with(a)return()=>eval})(123)(a),a]
})()`,
validate([fn, obj]) {
expect(fn).toBeFunction();
expect(fn()).toBe(456);
delete obj.eval;
expect(fn()).toBe(123);
}
});
/* eslint-enable no-eval */
});

describe('calls method with correct `this` value', () => {
itSerializes('executed before serialization', {
in() {
Expand Down Expand Up @@ -583,6 +629,5 @@ describe('with statements', () => {
/* eslint-enable no-restricted-properties */
});

// TODO: Test for access to `eval` - as a global, and as local var.
// TODO: Test for `with` nested in `with` value:
// `with ( (() => { const o = {x: 123}; with ({}) return o; })() ) x;`

0 comments on commit 0aa5b4b

Please sign in to comment.