Skip to content

dangerousForOf

username-removed-383729 requested to merge dangerousForOf into master

This adds a dangerousForOf transform which converts a for-of statement into a simple array iterator, per #19 (closed):

// in
for ( let member of [ 'a', 'b', 'c' ] ) {
  console.log( member );
}

// out
for ( var i = 0, list = [ 'a', 'b', 'c' ]; i < list.length; i += 1 ) {
  var member = list[i];

  console.log( member );
}

It's dangerous because it only works with things that have a length property, with members that can be accessed using array notation (arrays, strings, nodelists, etc). It does not work with all collections (Map, Set etc), or generators, because it doesn't support the iterator protocol (doing so involves bloating out the generated code in a way that would surprise most people innocently using for-of – perhaps we should one day add an analForOf transform for the people who need it). In this regard, it produces very similar output to TypeScript, with the exception that TypeScript doesn't support nodelists.

Merge request reports