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(helpers)!: remove default value from arrayElement #2045

Merged
merged 26 commits into from
Apr 21, 2023

Conversation

xDivisionByZerox
Copy link
Member

@xDivisionByZerox xDivisionByZerox commented Apr 13, 2023

Closes #219.

@xDivisionByZerox xDivisionByZerox added p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug c: refactor PR that affects the runtime behavior, but doesn't add new features or fixes bugs breaking change Cannot be merged when next version is not a major release m: helpers Something is referring to the helpers module labels Apr 13, 2023
@xDivisionByZerox xDivisionByZerox self-assigned this Apr 13, 2023
@codecov
Copy link

codecov bot commented Apr 13, 2023

Codecov Report

Merging #2045 (cff4a33) into next (9161a06) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2045   +/-   ##
=======================================
  Coverage   99.61%   99.61%           
=======================================
  Files        2535     2535           
  Lines      242221   242231   +10     
  Branches     1291     1299    +8     
=======================================
+ Hits       241283   241303   +20     
+ Misses        911      901   -10     
  Partials       27       27           
Impacted Files Coverage Δ
src/modules/helpers/index.ts 99.06% <100.00%> (+<0.01%) ⬆️

... and 2 files with indirect coverage changes

src/modules/helpers/index.ts Outdated Show resolved Hide resolved
src/modules/helpers/index.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@matthewmayer matthewmayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regression:

faker.helpers.arrayElement(["foo","bar",undefined]) 

now throws an error 1/3 of the time.

@Shinigami92
Copy link
Member

regression:

faker.helpers.arrayElement(["foo","bar",undefined]) 

now throws an error 1/3 of the time.

Interesting case we might now have thought about

I think we can use something like this:

image

@xDivisionByZerox
Copy link
Member Author

xDivisionByZerox commented Apr 14, 2023

I think we can use something like this:

Or we simply check the array length on input and throw? Why get that complex...^^

If someone provides an array with undefined this will be inferred by the generic and correctly type the return type.

@Shinigami92
Copy link
Member

I think we can use something like this:

Or we simply check the array length on input and throw? Why get that complex...^^

If someone provides an array with undefined this will be inferred by the generic and correctly type the return type.

Because of

const value = array[index];
if (value === undefined) {
throw new FakerError('Cannot get value from empty set.');

@matthewmayer
Copy link
Contributor

I think we can use something like this:

Or we simply check the array length on input and throw? Why get that complex...^^
If someone provides an array with undefined this will be inferred by the generic and correctly type the return type.

Because of

const value = array[index];
if (value === undefined) {
throw new FakerError('Cannot get value from empty set.');

can't you just change the condition for throwing from

const value = array[index];
    if (value === undefined) {
      throw new FakerError('Cannot get value from empty set.');
    }

to

    if (array.length === 0) {
      throw new FakerError('Cannot get value from empty set.');
    }
const value = array[index];

@matthewmayer
Copy link
Contributor

To clarify, in Faker 7, the behavior was:

> faker.helpers.arrayElement([])
undefined
> faker.helpers.arrayElement()
'b'

Do we want it to now throw in both these cases (array is empty, array is undefined)?

@xDivisionByZerox
Copy link
Member Author

can't you just change the condition for throwing from

const value = array[index];
    if (value === undefined) {
      throw new FakerError('Cannot get value from empty set.');
    }

to

    if (array.length === 0) {
      throw new FakerError('Cannot get value from empty set.');
    }
const value = array[index];

Yes and no. This is precisely what I was stating in my previous comment. But we have to keep in n mind that this will cause trouble in later versions if we decide to activate more strict typescript features. I'm talking about the noUncheckedIndexedAccess rule.

Do we want it to now throw in both these cases (array is empty, array is undefined)?

Yes, we do want that.
Providing no argument at all doesn't make sense to me. The method is in the helper module so the general consent of "every function has to be callable without arguments" does NOT apply here.
If you provide an empty array, typescript correctly infers that the return type is never, which basically means "This function never returns anything". This is correct as it throws an error instead f returning.

@matthewmayer
Copy link
Contributor

Whether or not it makes sense, passing an undefined array happens in existing places in the code at the moment and ends up with "b".

We should add tests for various cases:

Undefined array
Empty array
Array which contains undefined as a possible value

@Shinigami92
Copy link
Member

Whether or not it makes sense, passing an undefined array happens in existing places in the code at the moment and ends up with "b".

We should add tests for various cases:

Undefined array Empty array Array which contains undefined as a possible value

I highly assume that this is the reason why this PR is in "draft", but thanks for also thinking about it 🙂

@xDivisionByZerox xDivisionByZerox marked this pull request as ready for review April 14, 2023 14:58
@xDivisionByZerox xDivisionByZerox requested a review from a team April 14, 2023 14:58
@xDivisionByZerox xDivisionByZerox requested a review from a team as a code owner April 14, 2023 14:58
@xDivisionByZerox xDivisionByZerox changed the title refactor(helpers): remove default value from arrayElement refactor(helpers)!: remove default value from arrayElement Apr 14, 2023
test/location.spec.ts Outdated Show resolved Hide resolved
@ST-DDT ST-DDT requested review from a team April 20, 2023 19:38
@ST-DDT ST-DDT removed the needs test More tests are needed label Apr 20, 2023
test/helpers.spec.ts Outdated Show resolved Hide resolved
test/helpers.spec.ts Outdated Show resolved Hide resolved
Co-authored-by: Shinigami <chrissi92@hotmail.de>
ST-DDT
ST-DDT previously approved these changes Apr 20, 2023
@ST-DDT ST-DDT requested a review from a team April 20, 2023 21:28
docs/guide/upgrading.md Outdated Show resolved Hide resolved
test/helpers.spec.ts Outdated Show resolved Hide resolved
test/helpers.spec.ts Outdated Show resolved Hide resolved
Co-authored-by: Eric Cheng <ericcheng9316@gmail.com>
@Shinigami92 Shinigami92 dismissed stale reviews from import-brain and matthewmayer April 21, 2023 16:51

please review again or approve

@ST-DDT
Copy link
Member

ST-DDT commented Apr 21, 2023

please review again or approve

@Shinigami92 we are not in a hurry.

@Shinigami92
Copy link
Member

please review again or approve

@Shinigami92 we are not in a hurry.

This was not related with being in a hurry in any case?!
I just wrote a message into the dismiss form that GitHub requires to dismiss stale review blockings

@ST-DDT
Copy link
Member

ST-DDT commented Apr 21, 2023

This was not related with being in a hurry in any case?!
I just wrote a message into the dismiss form that GitHub requires to dismiss stale review blockings

Then please wait with dismissing their review, so they actually have time to review again.
If sufficient time has passed and we actually want to proceed with merging this it is still early enough to dismiss them then.

@Shinigami92 Shinigami92 merged commit 0564446 into next Apr 21, 2023
17 checks passed
@Shinigami92 Shinigami92 deleted the refactor/helpers/array-elemt-remove-default-value branch April 21, 2023 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Cannot be merged when next version is not a major release c: refactor PR that affects the runtime behavior, but doesn't add new features or fixes bugs m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

array-like elements have default arguments which violate genericity
5 participants