Skip to content

Support arrow functions without a block statement #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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