MatAtBread / fast-async

605 stars 21 forks source link

Await above for...of causes compilation error #22

Closed STRML closed 7 years ago

STRML commented 7 years ago

This only occurs in combination with transform-es2015-for-of in loose mode.

This code:

async function run(filePath) {
  const fileContents = await readFileAsync(filePath, 'utf8');
  for (const field of fileContents) {
  }
}

Compiles to:

    Duplicate declaration '_iterator = _isArray ? _iterator : _iterator[Symbol.iterator]()'
import _default from 'nodent-runtime';
function run(languages) {
  return new Promise(function ($return, $error) {
    var fileContents, _iterator, _isArray, _i, _ref, field;

    return readFileAsync(filePath, 'utf8').then(function ($await_1) {
      fileContents = $await_1;

      for ({
        _iterator = fileContents
        _isArray = Array.isArray(_iterator)
        _i = 0
        _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]()
      };;) {
        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref = _i.value;
        }

        field = _ref;
      }
      return $return();
    }.$asyncbind(this, $error), $error);
  }.$asyncbind(this));
}

Notice the error in the top line and the very odd for() syntax.

.babelrc:

{
  "plugins": [
    ['transform-es2015-for-of', {loose: true}],
    'syntax-async-functions',
    ['fast-async', {
      useRuntimeModule: 'nodent-runtime'
    }]
  ]
}
STRML commented 7 years ago

In loose mode, transform-es2015-for-of generates the following code:

async function run(languages) {
  const fileContents = await readFileAsync(filePath, 'utf8');
  for (var _iterator = fileContents, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
    var _ref;

    if (_isArray) {
      if (_i >= _iterator.length) break;
      _ref = _iterator[_i++];
    } else {
      _i = _iterator.next();
      if (_i.done) break;
      _ref = _i.value;
    }

    const field = _ref;
  }
}

I can confirm the breakage on the nodent example page.

STRML commented 7 years ago

Continued in https://github.com/MatAtBread/nodent/issues/67