[CS2] Don’t require async/await support to run `coffee` (#4679)

* 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
This commit is contained in:
Geoffrey Booth 2017-09-01 12:19:15 -07:00 committed by GitHub
parent 4a4f752204
commit 671486989f
5 changed files with 30 additions and 9 deletions

View File

@ -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.
@ -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 isnt supported.
files = files.filter (filename) -> filename isnt 'async.coffee'
for file in files when helpers.isCoffee file
literate = helpers.isLiterate file

View File

@ -26,7 +26,7 @@
}
})(),
historyMaxInputSize: 10240,
eval: async function(input, context, filename, cb) {
eval: function(input, context, filename, cb) {
var Assign, Block, Call, Code, Literal, Value, ast, err, isAsync, js, referencedVars, result, token, tokens;
// XXX: multiline hack.
input = input.replace(/\uFF00/g, '\n');
@ -71,10 +71,11 @@
result = runInContext(js, context, filename);
// Await an async result, if necessary
if (isAsync) {
result = (await result);
if (!sawSIGINT) {
cb(null, result);
}
result.then(function(resolvedResult) {
if (!sawSIGINT) {
return cb(null, resolvedResult);
}
});
return sawSIGINT = false;
} else {
return cb(null, result);

View File

@ -11,7 +11,7 @@
"version": "2.0.0-beta4",
"license": "MIT",
"engines": {
"node": ">=7.6.0"
"node": ">=6"
},
"directories": {
"lib": "./lib/coffeescript"

View File

@ -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

View File

@ -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