Generalize and normalize scope var statements generation.
Add initial tests.