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

Refactor getComponentName function #1

Merged

Conversation

kenzhemir
Copy link
Contributor

Why?

I noticed this code:

try {
  throw new Error('...')
} catch (error) {
  ...
}

I think there is no need to throw Error object just to get error stack, it's enough to create Error object and use it, like so:

const error = new Error('...')

Additionally, Error.prototype.stack is non-standard and there might be a situation where error.stack is undefined, so I added test for this case (previously it was ignored from coverage). In order to do that, I had to move error as optional first argument to this function. There might be other ways to do it, but this was the easiest one (couldn't correctly override global Error constructor)

There is no need to throw Error object just to get error stack, it's
enough to just create Error object and use it.
Additionally, Error.prototype.stack is non-standard so there might be a
situation where stack is undefined, so I added test for this case.
@dolfbarr dolfbarr self-requested a review December 22, 2022 13:05
@dolfbarr
Copy link
Owner

Great catch & great work! Thank you for your contribution!

nit, non-blocker: it would be also nice to add a small test to cover a stack parsing (line 81) as well since coverage ignore is removed; it was not added initially just because it's a little bit 'hacky' way to gather component name via non-standard Error feature.

@kenzhemir
Copy link
Contributor Author

I can take a look tomorrow into parsing part👍

- Added test where error.stack has some format that getComponentName
doesn't expect
- Changed the return value from "undefined" to "" in such situations
@kenzhemir kenzhemir requested a review from dolfbarr December 23, 2022 09:41
@dolfbarr
Copy link
Owner

LGTM! Great work!

@dolfbarr dolfbarr merged commit 10e4cee into dolfbarr:main Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants