Skip to content

Commit

Permalink
✨ Give FunctionExpression names if possible (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 14, 2021
1 parent 994ee18 commit c5574ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/api/ast-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`
Expand Down
5 changes: 4 additions & 1 deletion src/get-function-name-with-kind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
11 changes: 11 additions & 0 deletions test/get-function-name-with-kind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down

0 comments on commit c5574ce

Please sign in to comment.