Skip to content

Commit c62fd62

Browse files
kivlorVitaly Puzrin
authored andcommitted
support es6 arrow functions, fixes #389 (#393)
1 parent bee7e99 commit c62fd62

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

lib/js-yaml/type/js/function.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function resolveJavascriptFunction(data) {
3030
if (ast.type !== 'Program' ||
3131
ast.body.length !== 1 ||
3232
ast.body[0].type !== 'ExpressionStatement' ||
33-
ast.body[0].expression.type !== 'FunctionExpression') {
33+
(ast.body[0].expression.type !== 'ArrowFunctionExpression' &&
34+
ast.body[0].expression.type !== 'FunctionExpression')) {
3435
return false;
3536
}
3637

@@ -51,7 +52,8 @@ function constructJavascriptFunction(data) {
5152
if (ast.type !== 'Program' ||
5253
ast.body.length !== 1 ||
5354
ast.body[0].type !== 'ExpressionStatement' ||
54-
ast.body[0].expression.type !== 'FunctionExpression') {
55+
(ast.body[0].expression.type !== 'ArrowFunctionExpression' &&
56+
ast.body[0].expression.type !== 'FunctionExpression')) {
5557
throw new Error('Failed to resolve function');
5658
}
5759

test/samples-common/construct-javascript-function.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function testHandler(actual) {
99

1010
assert.strictEqual(actual.length, expected.length);
1111

12+
assert.strictEqual(actual.length, expected.length);
13+
1214
assert.strictEqual(
1315
actual[0](),
1416
expected[0]());
@@ -26,6 +28,9 @@ testHandler.expected = [
2628
function () {
2729
return 42;
2830
},
31+
function () {
32+
return 72;
33+
},
2934
function (x, y) {
3035
return x + y;
3136
},

test/samples-common/construct-javascript-function.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- !!js/function 'function () { return 42 }'
2+
- !!js/function '() => { return 72 }'
23
- !!js/function 'function (x, y) { return x + y; } '
34
- !!js/function |
45
function (foo) {
@@ -9,4 +10,4 @@
910
second: 'sum',
1011
third: result
1112
};
12-
}
13+
}

0 commit comments

Comments
 (0)