[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:
parent
4a4f752204
commit
671486989f
13
Cakefile
13
Cakefile
|
@ -409,6 +409,17 @@ runTests = (CoffeeScript) ->
|
||||||
description: description if description?
|
description: description if description?
|
||||||
source: fn.toString() if fn.toString?
|
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'
|
helpers.extend global, require './test/support/helpers'
|
||||||
|
|
||||||
# When all the tests have run, collect and print errors.
|
# 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.
|
# Run every test in the `test` folder, recording failures.
|
||||||
files = fs.readdirSync 'test'
|
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
|
for file in files when helpers.isCoffee file
|
||||||
literate = helpers.isLiterate file
|
literate = helpers.isLiterate file
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
historyMaxInputSize: 10240,
|
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;
|
var Assign, Block, Call, Code, Literal, Value, ast, err, isAsync, js, referencedVars, result, token, tokens;
|
||||||
// XXX: multiline hack.
|
// XXX: multiline hack.
|
||||||
input = input.replace(/\uFF00/g, '\n');
|
input = input.replace(/\uFF00/g, '\n');
|
||||||
|
@ -71,10 +71,11 @@
|
||||||
result = runInContext(js, context, filename);
|
result = runInContext(js, context, filename);
|
||||||
// Await an async result, if necessary
|
// Await an async result, if necessary
|
||||||
if (isAsync) {
|
if (isAsync) {
|
||||||
result = (await result);
|
result.then(function(resolvedResult) {
|
||||||
if (!sawSIGINT) {
|
if (!sawSIGINT) {
|
||||||
cb(null, result);
|
return cb(null, resolvedResult);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
return sawSIGINT = false;
|
return sawSIGINT = false;
|
||||||
} else {
|
} else {
|
||||||
return cb(null, result);
|
return cb(null, result);
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
"version": "2.0.0-beta4",
|
"version": "2.0.0-beta4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=7.6.0"
|
"node": ">=6"
|
||||||
},
|
},
|
||||||
"directories": {
|
"directories": {
|
||||||
"lib": "./lib/coffeescript"
|
"lib": "./lib/coffeescript"
|
||||||
|
|
|
@ -44,9 +44,9 @@ replDefaults =
|
||||||
result = runInContext js, context, filename
|
result = runInContext js, context, filename
|
||||||
# Await an async result, if necessary
|
# Await an async result, if necessary
|
||||||
if isAsync
|
if isAsync
|
||||||
result = await result
|
result.then (resolvedResult) ->
|
||||||
cb null, result unless sawSIGINT
|
cb null, resolvedResult unless sawSIGINT
|
||||||
sawSIGINT = false
|
sawSIGINT = no
|
||||||
else
|
else
|
||||||
cb null, result
|
cb null, result
|
||||||
catch err
|
catch err
|
||||||
|
|
|
@ -115,6 +115,13 @@ testRepl "keeps running after runtime error", (input, output) ->
|
||||||
input.emitLine 'a'
|
input.emitLine 'a'
|
||||||
eq 'undefined', output.lastWrite()
|
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', ->
|
process.on 'exit', ->
|
||||||
try
|
try
|
||||||
fs.unlinkSync historyFile
|
fs.unlinkSync historyFile
|
||||||
|
|
Loading…
Reference in New Issue