Skip to content

Commit

Permalink
Tests WIP 2
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 23, 2023
1 parent 7ddbd88 commit f8cd3d3
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions test/eval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,110 @@ describe('eval', () => {
});
});

describe('freezes linked vars internal to function where one is in scope of eval', () => {
describe('function params + var bindings', () => {
itSerializes('where var binding is frozen', {
in: `
module.exports = function(param = 1, module, exports) {
var param;
eval('param');
return param;
};
`,
out: `(0,eval)("
(function(param=1,module,exports){var param;eval(\\"param\\");return param})
")`,
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toBe(1);
}
});

itSerializes('where function param is frozen', {
in: `
module.exports = function(param = {x: 1}, y = eval('param'), module, exports) {
var param;
return [param, y];
};
`,
out: `(0,eval)("
(function(param={x:1},y=eval(\\"param\\"),module,exports){
var param;
return[param,y]
})
")`,
validate(fn) {
expect(fn).toBeFunction();
const arr = fn();
expect(arr).toEqual([{x: 1}, {x: 1}]);
expect(arr[1]).toBe(arr[0]);
}
});
});

describe('for statement bindings', () => {
itSerializes('where left binding is frozen', {
in: `
module.exports = function(module, exports) {
let getLeft, getRight;
for (
let [x, y = getLeft = () => x, z = () => eval('x')]
of (getRight = () => typeof x, [[1]])
) ;
return {getLeft, getRight};
};
`,
out: `(0,eval)("
(function(module,exports){
let getLeft,getRight;
for(let[x,y=getLeft=()=>x,z=()=>eval(\\"x\\")]of(getRight=()=>typeof x,[[1]]));
return{getLeft,getRight}
})
")`,
validate(fn) {
expect(fn).toBeFunction();
const {getLeft, getRight} = fn();
expect(getLeft).toBeFunction();
expect(getLeft()).toBe(1);
expect(getRight).toBeFunction();
expect(getRight).toThrowWithMessage(
ReferenceError, "Cannot access 'x' before initialization"
);
}
});

itSerializes('where right binding is frozen', {
in: `
module.exports = function(module, exports) {
let getLeft, getRight;
for (
let [x, y = getLeft = () => x]
of (getRight = () => typeof x, () => eval('x'), [[1]])
) ;
return {getLeft, getRight};
};
`,
out: `(0,eval)("
(function(module,exports){
let getLeft,getRight;
for(let[x,y=getLeft=()=>x]of(getRight=()=>typeof x,()=>eval(\\"x\\"),[[1]]));
return{getLeft,getRight}
})
")`,
validate(fn) {
expect(fn).toBeFunction();
const {getLeft, getRight} = fn();
expect(getLeft).toBeFunction();
expect(getLeft()).toBe(1);
expect(getRight).toBeFunction();
expect(getRight).toThrowWithMessage(
ReferenceError, "Cannot access 'x' before initialization"
);
}
});
});
});

describe('prevents shadowed vars in upper scopes being accessible to eval', () => {
itSerializes('simple case', {
in: `
Expand Down

0 comments on commit f8cd3d3

Please sign in to comment.