Skip to content

Commit

Permalink
[CS2] Don’t require async/await support to run coffee (#4679)
Browse files Browse the repository at this point in the history
* Get `coffee` command working again in Node 6, by converting the ‘await’ wrapper in the REPL to use a Promise instead of the ‘await’ keyword; add tests for REPL ‘await’ wrapper, including test to skip async tests if the runtime doesn’t support them

* Code review

* Let's support Node 6+ if we can
  • Loading branch information
GeoffreyBooth authored Sep 1, 2017
1 parent 4a4f752 commit 6714869
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
13 changes: 13 additions & 0 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ runTests = (CoffeeScript) ->
description: description if description?
source: fn.toString() if fn.toString?

global.supportsAsync = if global.testingBrowser
try
new Function('async () => {}')()
yes
catch
no
else
[major, minor, build] = process.versions.node.split('.').map (version) ->
parseInt version, 10
major >= 8 or (major is 7 and minor >= 6)

helpers.extend global, require './test/support/helpers'

# When all the tests have run, collect and print errors.
Expand All @@ -428,6 +439,8 @@ runTests = (CoffeeScript) ->

# Run every test in the `test` folder, recording failures.
files = fs.readdirSync 'test'
unless global.supportsAsync # Except for async tests, if async isn’t supported.
files = files.filter (filename) -> filename isnt 'async.coffee'

for file in files when helpers.isCoffee file
literate = helpers.isLiterate file
Expand Down
11 changes: 6 additions & 5 deletions lib/coffeescript/repl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"version": "2.0.0-beta4",
"license": "MIT",
"engines": {
"node": ">=7.6.0"
"node": ">=6"
},
"directories": {
"lib": "./lib/coffeescript"
Expand Down
6 changes: 3 additions & 3 deletions src/repl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ replDefaults =
result = runInContext js, context, filename
# Await an async result, if necessary
if isAsync
result = await result
cb null, result unless sawSIGINT
sawSIGINT = false
result.then (resolvedResult) ->
cb null, resolvedResult unless sawSIGINT
sawSIGINT = no
else
cb null, result
catch err
Expand Down
7 changes: 7 additions & 0 deletions test/repl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ testRepl "keeps running after runtime error", (input, output) ->
input.emitLine 'a'
eq 'undefined', output.lastWrite()

testRepl "#4604: wraps an async function", (input, output) ->
return unless global.supportsAsync
input.emitLine 'await new Promise (resolve) -> setTimeout (-> resolve 33), 10'
setTimeout ->
eq '33', output.lastWrite()
, 20

process.on 'exit', ->
try
fs.unlinkSync historyFile
Expand Down

0 comments on commit 6714869

Please sign in to comment.