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(context): remove unnecessary initialization add add tests for Context #2949

Merged
merged 2 commits into from
Jun 9, 2024

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Jun 9, 2024

This refactoring removed unnecessary initialization in c.get() and c.set(). And added tests for improving coverage.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Jun 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.55%. Comparing base (0a974ff) to head (9770d45).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2949      +/-   ##
==========================================
+ Coverage   94.44%   94.55%   +0.11%     
==========================================
  Files         136      136              
  Lines       13349    13346       -3     
  Branches     2284     2357      +73     
==========================================
+ Hits        12607    12619      +12     
+ Misses        742      727      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe yusukebe changed the title refactor(context): remove unnecessary initialization add add tests refactor(context): remove unnecessary initialization add add tests for Context Jun 9, 2024
src/context.ts Outdated
@@ -480,8 +480,8 @@ export class Context<
```
*/
set: Set<E> = (key: string, value: unknown) => {
this._var ??= {}
this._var[key as string] = value
// @ts-expect-error this._var is initialized as {}
Copy link
Member Author

Choose a reason for hiding this comment

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

It throws a type error that points to the possibility of undefined. But it has been initialized above. So ignore it.

@yusukebe
Copy link
Member Author

yusukebe commented Jun 9, 2024

@usualoma Sorry for calling you again and again. Please review this!

@usualoma
Copy link
Member

usualoma commented Jun 9, 2024

Hi @yusukebe

Some time ago, in applications that did not use app.set() or app.get(), this was done to avoid the cost of initialising the private this._var. It is this PR that this._var is now always initialised.

#1406
https://github.com/honojs/hono/pull/1406/files#diff-1c6b79733c620b41d7f8b63deb644f09e5fda79664918394b443c2a348731605L102

It is quite questionable whether this optimisation is still valid today, but if we were to reproduce the aims of that time, the following change would be appropriate.

diff --git a/src/context.ts b/src/context.ts
index e5eaa581..d067b048 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -237,7 +237,7 @@ export class Context<
    * ```
    */
   env: E['Bindings'] = {}
-  private _var: E['Variables'] = {}
+  private _var: E['Variables'] | undefined
   finalized: boolean = false
   /**
    * `.error` can get the error object from the middleware if the Handler throws an error.

If there is no need to reproduce previous aims, this is a good change as this PR change makes "context.ts" very simple.

Co-authored-by: Taku Amano <[email protected]>
@yusukebe
Copy link
Member Author

yusukebe commented Jun 9, 2024

Hi @usualoma

You are right! No need to initialize it. I've fixed it with your patch and will merge this now. Thanks!

@yusukebe yusukebe merged commit 7eb3e3b into main Jun 9, 2024
14 checks passed
@yusukebe yusukebe deleted the refactor/context branch June 9, 2024 22:04
nicolewhite referenced this pull request in autoblocksai/cli Jun 24, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [hono](https://hono.dev/) ([source](https://togithub.com/honojs/hono))
| [`4.4.4` ->
`4.4.7`](https://renovatebot.com/diffs/npm/hono/4.4.4/4.4.7) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/hono/4.4.7?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/hono/4.4.7?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/hono/4.4.4/4.4.7?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/hono/4.4.4/4.4.7?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/hono (hono)</summary>

### [`v4.4.7`](https://togithub.com/honojs/hono/releases/tag/v4.4.7)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.6...v4.4.7)

#### What's Changed

- use correct return type for c.html depending on input by
[@&#8203;asmadsen](https://togithub.com/asmadsen) in
[https://github.com/honojs/hono/pull/2973](https://togithub.com/honojs/hono/pull/2973)
- test: test uncovered return statement by
[@&#8203;yasuaki640](https://togithub.com/yasuaki640) in
[https://github.com/honojs/hono/pull/2985](https://togithub.com/honojs/hono/pull/2985)
- test: Update request.test.ts to remove duplicate checks by
[@&#8203;JoaquimLey](https://togithub.com/JoaquimLey) in
[https://github.com/honojs/hono/pull/2984](https://togithub.com/honojs/hono/pull/2984)
- fix(types): env variables override ContextVariableMap by
[@&#8203;KaelWD](https://togithub.com/KaelWD) in
[https://github.com/honojs/hono/pull/2987](https://togithub.com/honojs/hono/pull/2987)

#### New Contributors

- [@&#8203;asmadsen](https://togithub.com/asmadsen) made their first
contribution in
[https://github.com/honojs/hono/pull/2973](https://togithub.com/honojs/hono/pull/2973)
- [@&#8203;JoaquimLey](https://togithub.com/JoaquimLey) made their first
contribution in
[https://github.com/honojs/hono/pull/2984](https://togithub.com/honojs/hono/pull/2984)
- [@&#8203;KaelWD](https://togithub.com/KaelWD) made their first
contribution in
[https://github.com/honojs/hono/pull/2987](https://togithub.com/honojs/hono/pull/2987)

**Full Changelog**:
honojs/hono@v4.4.6...v4.4.7

### [`v4.4.6`](https://togithub.com/honojs/hono/releases/tag/v4.4.6)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.5...v4.4.6)

##### What's Changed

- fix(aws-lambda): handle multiple cookies in streaming responses by
[@&#8203;KnisterPeter](https://togithub.com/KnisterPeter) in
[https://github.com/honojs/hono/pull/2926](https://togithub.com/honojs/hono/pull/2926)

**Full Changelog**:
honojs/hono@v4.4.5...v4.4.6

### [`v4.4.5`](https://togithub.com/honojs/hono/releases/tag/v4.4.5)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.4...v4.4.5)

##### What's Changed

- fix(cors): allow custom vary header by
[@&#8203;fzn0x](https://togithub.com/fzn0x) in
[https://github.com/honojs/hono/pull/2934](https://togithub.com/honojs/hono/pull/2934)
- fix(jsx): rename `Hono` to `JSX` and export `JSX` namespace by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2937](https://togithub.com/honojs/hono/pull/2937)
- refactor(hono-base): make 2nd arg of `app.route()` required by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2945](https://togithub.com/honojs/hono/pull/2945)
- refactor(hono-base): don't check 1st argument of `app.on()` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2946](https://togithub.com/honojs/hono/pull/2946)
- refactor(context): remove unnecessary initialization add add tests for
Context by [@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2949](https://togithub.com/honojs/hono/pull/2949)
- test(hono-base): add tests for covering 100% by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2952](https://togithub.com/honojs/hono/pull/2952)
- fix(context): default JSONRespond and TextRespond StatusCode generic
arg by [@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2954](https://togithub.com/honojs/hono/pull/2954)
- refactor(request): shorten `parseBody` and remove unnecessary check by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2947](https://togithub.com/honojs/hono/pull/2947)
- refactor(jsx): reduce code size and improve maintainability by
[@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/hono/pull/2956](https://togithub.com/honojs/hono/pull/2956)

**Full Changelog**:
honojs/hono@v4.4.4...v4.4.5

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on Monday" in timezone
America/Chicago, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/autoblocksai/cli).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zOTMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjQxMC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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