Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give FunctionExpression names if possible #14

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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