diff --git a/docs/api/ast-utils.md b/docs/api/ast-utils.md index daee178..7b6d0d1 100644 --- a/docs/api/ast-utils.md +++ b/docs/api/ast-utils.md @@ -160,6 +160,12 @@ Get the name and kind of a given function node. - `const foo = async () => {}` ........... `async arrow function 'foo'` - `foo = () => {}` ....................... `arrow function 'foo'` - `foo = async () => {}` ................. `async arrow function 'foo'` +- `const foo = function() {}` ............ `function 'foo'` +- `const foo = function* () {}` .......... `generator function 'foo'` +- `const foo = async function() {}` ...... `async function 'foo'` +- `foo = function() {}` .................. `function 'foo'` +- `foo = function* () {}` ................ `generator function 'foo'` +- `foo = async function() {}` ............ `async 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 779dce3..e5ad398 100644 --- a/src/get-function-name-with-kind.js +++ b/src/get-function-name-with-kind.js @@ -49,7 +49,10 @@ export function getFunctionNameWithKind(node) { } } - if (node.type === "ArrowFunctionExpression") { + if ( + node.type === "ArrowFunctionExpression" || + (node.type === "FunctionExpression" && node.id === null) + ) { if ( parent.type === "VariableDeclarator" && parent.id && diff --git a/test/get-function-name-with-kind.js b/test/get-function-name-with-kind.js index 3313ee0..02f92d1 100644 --- a/test/get-function-name-with-kind.js +++ b/test/get-function-name-with-kind.js @@ -18,6 +18,17 @@ describe("The 'getFunctionNameWithKind' function", () => { "foo = async () => {}": "async arrow function 'foo'", "foo.bar = () => {}": "arrow function", "foo.bar = async () => {}": "async arrow function", + "const foo = function() {}": "function 'foo'", + "const foo = function* () {}": "generator function 'foo'", + "const foo = async function() {}": "async function 'foo'", + "foo = function() {}": "function 'foo'", + "foo = function* () {}": "generator function 'foo'", + "foo = async function() {}": "async function 'foo'", + "const foo = function bar() {}": "function 'bar'", + "foo = function bar() {}": "function 'bar'", + "foo.bar = function() {}": "function", + "foo.bar = function* () {}": "generator function", + "foo.bar = async function() {}": "async function", "({ foo: function foo() {} })": "method 'foo'", "({ foo: function() {} })": "method 'foo'", "({ ['foo']: function() {} })": "method 'foo'",