Skip to content

Commit f811251

Browse files
authored
✨ give arrow function names if possible (#5)
1 parent fd094d7 commit f811251

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

docs/api/ast-utils.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ Get the name and kind of a given function node.
156156
- `(function*() {})` ..................... `generator function`
157157
- `() => {}` ............................. `arrow function`
158158
- `async () => {}` ....................... `async arrow function`
159+
- `const foo = () => {}` ................. `arrow function 'foo'`
160+
- `const foo = async () => {}` ........... `async arrow function 'foo'`
161+
- `foo = () => {}` ....................... `arrow function 'foo'`
162+
- `foo = async () => {}` ................. `async arrow function 'foo'`
159163
- `({ foo: function foo() {} })` ......... `method 'foo'`
160164
- `({ foo: function() {} })` ............. `method 'foo'`
161165
- `({ ['foo']: function() {} })` ......... `method 'foo'`

src/get-function-name-with-kind.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,22 @@ export function getFunctionNameWithKind(node) {
4949
}
5050
}
5151

52+
if (node.type === "ArrowFunctionExpression") {
53+
if (
54+
parent.type === "VariableDeclarator" &&
55+
parent.id &&
56+
parent.id.type === "Identifier"
57+
) {
58+
tokens.push(`'${parent.id.name}'`)
59+
}
60+
if (
61+
parent.type === "AssignmentExpression" &&
62+
parent.left &&
63+
parent.left.type === "Identifier"
64+
) {
65+
tokens.push(`'${parent.left.name}'`)
66+
}
67+
}
68+
5269
return tokens.join(" ")
5370
}

test/get-function-name-with-kind.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ describe("The 'getFunctionNameWithKind' function", () => {
1212
"(function*() {})": "generator function",
1313
"() => {}": "arrow function",
1414
"async () => {}": "async arrow function",
15+
"const foo = () => {}": "arrow function 'foo'",
16+
"const foo = async () => {}": "async arrow function 'foo'",
17+
"foo = () => {}": "arrow function 'foo'",
18+
"foo = async () => {}": "async arrow function 'foo'",
19+
"foo.bar = () => {}": "arrow function",
20+
"foo.bar = async () => {}": "async arrow function",
1521
"({ foo: function foo() {} })": "method 'foo'",
1622
"({ foo: function() {} })": "method 'foo'",
1723
"({ ['foo']: function() {} })": "method 'foo'",

0 commit comments

Comments
 (0)