Skip to content

Commit

Permalink
Don't freeze temp vars for injecting this + arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 10, 2023
1 parent 0dea35c commit 95657a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/serialize/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,9 @@ module.exports = {
// Otherwise, can just make `arguments` a normal param.
// This can be the case if `arguments` is behind a `with (...)` block,
// or `arguments` is a user-defined variable, not `arguments` created by a function.
newName = transformVarName(paramName, true);
// If block contains a function containing `eval()`, prepend name of temp var with '_'
// as it'll be accessible to `eval()`. If param is frozen by `with ()`, this isn't required.
newName = transformVarName(paramName, containsEval);
if (paramName === 'this') {
frozenThisVarName = newName;
} else {
Expand Down
20 changes: 10 additions & 10 deletions test/with.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('with statements', () => {
}
return outer.call({x: 1});
},
out: '(_a=>function(){return()=>this}.call(_a))({x:1})',
out: '(a=>function(){return()=>this}.call(a))({x:1})',
validate(fn) {
expect(fn).toBeFunction();
expect(fn()).toEqual({x: 1});
Expand All @@ -99,11 +99,11 @@ describe('with statements', () => {
return outer.call({x: 1});
},
out: `
(y=>_a=>function(){
(y=>b=>function(){
return a=>{
with(a)return()=>[this,y]
}
}.call(_a))(2)({x:1})({this:3,a:4})
}.call(b))(2)({x:1})({this:3,a:4})
`,
validate(fn) {
expect(fn).toBeFunction();
Expand Down Expand Up @@ -170,11 +170,11 @@ describe('with statements', () => {
},
out: `
(
(_a,_b)=>function(){
(b,c)=>function(){
return a=>{
with(a)return()=>[this,arguments]
}
}.apply(_a,_b)
}.apply(b,c)
)({x:1},function(){return arguments}(2,3,4))({a:5})
`,
validate(fn) {
Expand All @@ -201,14 +201,14 @@ describe('with statements', () => {
},
out: `
(
(_a,_b)=>function(){
(b,c)=>function(){
return a=>{
with(a)return()=>{
"use strict";
return[this,arguments]
}
}
}.apply(_a,_b)
}.apply(b,c)
)({x:1},function(){return arguments}(2,3,4))({a:5})
`,
validate(fn) {
Expand All @@ -232,12 +232,12 @@ describe('with statements', () => {
},
out: `(()=>{
const a=(
(_a,b)=>function(){
(b,c)=>function(){
return[
()=>b,
()=>c,
a=>{with(a)return()=>[x,this]}
]
}.call(_a)
}.call(b)
)({y:2},function(){return arguments}(3,4,5));return[a[1]({x:1}),a[0]]
})()`,
validate([fn1, fn2]) {
Expand Down

0 comments on commit 95657a3

Please sign in to comment.