0.0.1 • Published 5 years ago

babel-plugin-goto v0.0.1

Weekly downloads
2
License
GPL-3.0
Repository
github
Last release
5 years ago

Goto in JavaScript

A Babel transformer which implements goto (within function scope) for JavaScript.

Example

function loop() {
  var i = 0;
  
  start:
    i = i + 1;
    console.log(i);
    if (i == 10) goto(end);
    goto(start);
    
  end:
    console.log("all done");
}

loop();

How does this work?

Blocks that include labels are rewritten as labeled infinite loops containing a switch statement; labels become cases, and goto sets the appropriate case of the switch statement and then jumps using a labeled continue. Such a technique might be termed a trampoline since control jumps back to the beginning (via the labeled continue) and then to the appropriate labeled position (via the switch statement).

There are limitations: it isn't possible to goto a label in a deeper block. On the other hand, it is possible to jump both forward and backward.

Credits

Inspired by Alex Sexton's Summer of Goto.