Skip to content

Commit

Permalink
Support arrow functions without a block statement (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
aengl authored and Vitaly Puzrin committed Jun 1, 2018
1 parent bab69b5 commit 290705b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/js-yaml/type/js/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ function constructJavascriptFunction(data) {

// Esprima's ranges include the first '{' and the last '}' characters on
// function expressions. So cut them out.
if (ast.body[0].expression.body.type === 'BlockStatement') {
/*eslint-disable no-new-func*/
return new Function(params, source.slice(body[0] + 1, body[1] - 1));
}
// ES6 arrow functions can omit the BlockStatement. In that case, just return
// the body.
/*eslint-disable no-new-func*/
return new Function(params, source.slice(body[0] + 1, body[1] - 1));
return new Function(params, 'return ' + source.slice(body[0], body[1]));
}

function representJavascriptFunction(object /*, style*/) {
Expand Down
3 changes: 3 additions & 0 deletions test/samples-common/construct-javascript-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ testHandler.expected = [
function () {
return 72;
},
function () {
return 23;
},
function (x, y) {
return x + y;
},
Expand Down
1 change: 1 addition & 0 deletions test/samples-common/construct-javascript-function.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- !!js/function 'function () { return 42 }'
- !!js/function '() => { return 72 }'
- !!js/function '() => 23'
- !!js/function 'function (x, y) { return x + y; } '
- !!js/function |
function (foo) {
Expand Down

0 comments on commit 290705b

Please sign in to comment.