Skip to content

Commit

Permalink
✨ give arrow function names if possible (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jun 15, 2020
1 parent fd094d7 commit f811251
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api/ast-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Get the name and kind of a given function node.
- `(function*() {})` ..................... `generator function`
- `() => {}` ............................. `arrow function`
- `async () => {}` ....................... `async arrow function`
- `const foo = () => {}` ................. `arrow function 'foo'`
- `const foo = async () => {}` ........... `async arrow function 'foo'`
- `foo = () => {}` ....................... `arrow function 'foo'`
- `foo = async () => {}` ................. `async arrow function 'foo'`
- `({ foo: function foo() {} })` ......... `method 'foo'`
- `({ foo: function() {} })` ............. `method 'foo'`
- `({ ['foo']: function() {} })` ......... `method 'foo'`
Expand Down
17 changes: 17 additions & 0 deletions src/get-function-name-with-kind.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,22 @@ export function getFunctionNameWithKind(node) {
}
}

if (node.type === "ArrowFunctionExpression") {
if (
parent.type === "VariableDeclarator" &&
parent.id &&
parent.id.type === "Identifier"
) {
tokens.push(`'${parent.id.name}'`)
}
if (
parent.type === "AssignmentExpression" &&
parent.left &&
parent.left.type === "Identifier"
) {
tokens.push(`'${parent.left.name}'`)
}
}

return tokens.join(" ")
}
6 changes: 6 additions & 0 deletions test/get-function-name-with-kind.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ describe("The 'getFunctionNameWithKind' function", () => {
"(function*() {})": "generator function",
"() => {}": "arrow function",
"async () => {}": "async arrow function",
"const foo = () => {}": "arrow function 'foo'",
"const foo = async () => {}": "async arrow function 'foo'",
"foo = () => {}": "arrow function 'foo'",
"foo = async () => {}": "async arrow function 'foo'",
"foo.bar = () => {}": "arrow function",
"foo.bar = async () => {}": "async arrow function",
"({ foo: function foo() {} })": "method 'foo'",
"({ foo: function() {} })": "method 'foo'",
"({ ['foo']: function() {} })": "method 'foo'",
Expand Down

0 comments on commit f811251

Please sign in to comment.