dk00 / livescript-next

Enable latest ES features for LiveScript
https://lsn.netlify.com/
The Unlicense
39 stars 3 forks source link

Add generator function and yield expression support #8

Closed dk00 closed 7 years ago

dk00 commented 7 years ago

Fix #5.

->
  yield
  yield 1
  yield from [2 3]
(function* () {
  yield;
  yield 1;
  return yield* [2, 3];
});

yield implicitly marks functions as generator like await does, ->* are function* are also supported.

This also enables async generator functions, but not bound generator ~>*.

Bound functions ~> are converted to arrow functions =>, but arrow functions cannot be used as generators directly.

~function* gen a, b
  yield a b

There various options to work around:

0. Use additional variables to bind this

let this$ = this
function* gen(a, b){
  return yield a(b)
}

1. Wrap with IIFE

let gen = (a, b) => (function*() {
  return yield a(b)
}).call(this)

2. Use bind

let gen = (function*(a, b) {
  return yield a(b)
}).bind(this)

Laugh for 0, hooray for 1, heart for 2 and comment for your ideas.

codecov[bot] commented 7 years ago

Codecov Report

Merging #8 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master     #8   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines         388    394    +6     
  Branches      169    170    +1     
=====================================
+ Hits          388    394    +6
Impacted Files Coverage Δ
src/convert.ls 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 37e2435...b62b039. Read the comment docs.

codecov[bot] commented 7 years ago

Codecov Report

Merging #8 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master     #8   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines         388    394    +6     
  Branches      169    170    +1     
=====================================
+ Hits          388    394    +6
Impacted Files Coverage Δ
src/convert.ls 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 37e2435...b62b039. Read the comment docs.

codecov-io commented 7 years ago

Codecov Report

Merging #8 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master     #8   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines         388    397    +9     
  Branches      169    172    +3     
=====================================
+ Hits          388    397    +9
Impacted Files Coverage Δ
src/convert.ls 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 37e2435...346d99a. Read the comment docs.

dk00 commented 7 years ago

bind is used to convert bound generator functions.

~function gen a
  yield a @
let gen;
gen = function* (a) {
  return yield a(this);
}.bind(this);