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(core): fix eslint warning #5382

Merged
merged 1 commit into from
Jan 29, 2025

Conversation

chancancode
Copy link
Contributor

Which problem is this PR solving?

Fixes the following eslint warning in the core pacakge:

/home/runner/work/opentelemetry-js/opentelemetry-js/packages/opentelemetry-core/src/utils/lodash.merge.ts
  44:24  warning  Don't use `Function` as a type  @typescript-eslint/ban-types

Ref #5365

Short description of the changes

This could have been addressed in various ways with no change to the code, but upon further inspection, we can probably shed some weight here.

This code was inherited from lodash, which defines:

const getPrototype = overArg(Object.getPrototypeOf, Object);

overArg allows each argument of the passed in function to be transformed by a mapper function, in this case, effective it amounts to:

const getPrototypeOf = Object.getPrototype;

function getPrototype(value) {
  return getPrototypeOf(Object(value));
}

So at minimum, we can just inline that and get rid of overArg, since that's the only place we use that function.

The intent here appears to be making a "safe" version of Object.getPrototypeOf that can be called with primitive values.

For example, Object.getPrototypeOf(null) would throw, but Object.getPrototypeOf(Object(null)) wouldn't, and same for 1, true, etc.

Essentially, Object(value) will box the primitive value and the function will return the prototype of the boxed primitive object.

I suppose this is useful in the rest of the lodash codebase. However, it's unclear that it is necessary in our stripped down version.

getPrototype is only used in isPlainObject. The first thing that function does is reject any non-object values which would necessarily exclude all primitive values.

So I am pretty sure the Object() does nothing for us and we can just call Object.getPrototypeOf directly and shed all the extra stuff.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • eslint

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

```
/home/runner/work/opentelemetry-js/opentelemetry-js/packages/opentelemetry-core/src/utils/lodash.merge.ts
  44:24  warning  Don't use `Function` as a type  @typescript-eslint/ban-types
```

This could have been addressed in various ways with no change to
the code, but upon further inspection, we can probably shed some
weight here.

This code was inherited from lodash, which defines:

```js
const getPrototype = overArg(Object.getPrototypeOf, Object);
```

`overArg` allows each argument of the passed in function to be
transformed by a mapper function, in this case, effective it
amounts to:

```js
const getPrototypeOf = Object.getPrototype;

function getPrototype(value) {
  return getPrototypeOf(Object(value));
}
```

So at minimum, we can just inline _that_ and get rid of
`overArg`, since that's the only place we use that function.

The intent here appears to be making a "safe" version of
`Object.getPrototypeOf` that can be called with primitive
values.

For example, `Object.getPrototypeOf(null)` would throw, but
`Object.getPrototypeOf(Object(null))` wouldn't, and same for
`1`, `true`, etc.

Essentially, `Object(value)` will box the primitive value and
the function will return the prototype of the boxed primitive
object.

I suppose this is useful in the rest of the lodash codebase.
However, it's unclear that it is necessary in our stripped
down version.

`getPrototype` is only used in `isPlainObject`. The first
thing that function does is reject any non-object values
which would necessarily exclude all primitive values.

So I am pretty sure the `Object()` does nothing for us and
we can just call `Object.getPrototypeOf` directly and shed
all the extra stuff.

Ref open-telemetry#5365
@chancancode chancancode requested a review from a team as a code owner January 27, 2025 23:03
@chancancode chancancode mentioned this pull request Jan 27, 2025
25 tasks
Copy link

codecov bot commented Jan 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.62%. Comparing base (199fd8d) to head (8091e7c).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5382      +/-   ##
==========================================
- Coverage   94.63%   94.62%   -0.02%     
==========================================
  Files         318      318              
  Lines        8036     8033       -3     
  Branches     1685     1685              
==========================================
- Hits         7605     7601       -4     
- Misses        431      432       +1     
Files with missing lines Coverage Δ
...kages/opentelemetry-core/src/utils/lodash.merge.ts 60.86% <100.00%> (-2.40%) ⬇️

... and 1 file with indirect coverage changes

@pichlermarc pichlermarc added this pull request to the merge queue Jan 29, 2025
Merged via the queue into open-telemetry:main with commit 1c6c39b Jan 29, 2025
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants