Skip to content

Commit

Permalink
fix: parse single arg lambda shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Mar 3, 2022
1 parent 988f8ce commit d53f631
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function annotate() {
// of a nested class, too.

var CONSTRUCTOR_ARGS = /constructor\s*[^(]*\(\s*([^)]*)\)/m;
var FN_ARGS = /^(?:async\s+)?(?:function\s*)?[^(]*\(\s*([^)]*)\)/m;
var FN_ARGS = /^(?:async\s+)?(?:function\s*[^(]*)?(?:\(\s*([^)]*)\)|(\w+))/m;
var FN_ARG = /\/\*([^*]*)\*\//m;

/**
Expand All @@ -64,8 +64,10 @@ export function parseAnnotations(fn) {
return [];
}

return match[1] && match[1].split(',').map(function(arg) {
match = arg.match(FN_ARG);
return match ? match[1].trim() : arg.trim();
var args = match[1] || match[2];

return args && args.split(',').map(function(arg) {
var argMatch = arg.match(FN_ARG);
return (argMatch && argMatch[1] || arg).trim();
}) || [];
}
20 changes: 20 additions & 0 deletions test/annotation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ describe('annotation', function() {
parseAnnotations((a, b) => a + b)
).to.eql([ 'a', 'b' ]);

expect(
parseAnnotations(a => a + 1)
).to.eql([ 'a' ]);

expect(
parseAnnotations(a => {
return a + 1;
})
).to.eql([ 'a' ]);

expect(
parseAnnotations(() => 1)
).to.eql([ ]);
Expand All @@ -103,6 +113,16 @@ describe('annotation', function() {
parseAnnotations(async (a, b) => a + b)
).to.eql([ 'a', 'b' ]);

expect(
parseAnnotations(async a => a + 1)
).to.eql([ 'a' ]);

expect(
parseAnnotations(async a => {
return a + 1;
})
).to.eql([ 'a' ]);

expect(
parseAnnotations(async () => 1)
).to.eql([ ]);
Expand Down

0 comments on commit d53f631

Please # to comment.