My solution is to start putting all code into functions early, and then break out subfunctions whenever it makes sense. That way random code is always in a reasonably small lexical scope, and I don't have to care much if a random lexical has a slightly larger scope than I'd ideally want.