diff --git a/docs/api/ast-utils.md b/docs/api/ast-utils.md index 4004355..daee178 100644 --- a/docs/api/ast-utils.md +++ b/docs/api/ast-utils.md @@ -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'` diff --git a/src/get-function-name-with-kind.js b/src/get-function-name-with-kind.js index 0b7519f..779dce3 100644 --- a/src/get-function-name-with-kind.js +++ b/src/get-function-name-with-kind.js @@ -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(" ") } diff --git a/test/get-function-name-with-kind.js b/test/get-function-name-with-kind.js index 433e86e..3313ee0 100644 --- a/test/get-function-name-with-kind.js +++ b/test/get-function-name-with-kind.js @@ -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'",