chore(core): added @typescript-eslint/no-unnecessary-condition rule and removed redundant existence checks#1482
Conversation
|
The formatting check in the CI pipeline currently fails. Please run |
…g in eslint config.
…ule from warn to error.
| if (this.instance == null) { | ||
| this.instance = new ContentSerdes(); | ||
| // JSON | ||
| this.instance.addCodec(new JsonCodec(), true); | ||
| this.instance.addCodec(new JsonCodec("application/senml+json")); | ||
| this.instance.addCodec(new JsonCodec("application/td+json")); | ||
| this.instance.addCodec(new JsonCodec("application/ld+json")); | ||
| // CBOR | ||
| this.instance.addCodec(new CborCodec(), true); | ||
| // Text | ||
| this.instance.addCodec(new TextCodec()); | ||
| this.instance.addCodec(new TextCodec("text/html")); | ||
| this.instance.addCodec(new TextCodec("text/css")); | ||
| this.instance.addCodec(new TextCodec("application/xml")); | ||
| this.instance.addCodec(new TextCodec("application/xhtml+xml")); | ||
| this.instance.addCodec(new TextCodec("image/svg+xml")); | ||
| // Base64 | ||
| this.instance.addCodec(new Base64Codec("image/png")); | ||
| this.instance.addCodec(new Base64Codec("image/gif")); | ||
| this.instance.addCodec(new Base64Codec("image/jpeg")); | ||
| // OctetStream | ||
| this.instance.addCodec(new OctetstreamCodec()); | ||
| } | ||
| this.instance = new ContentSerdes(); | ||
| // JSON | ||
| this.instance.addCodec(new JsonCodec(), true); | ||
| this.instance.addCodec(new JsonCodec("application/senml+json")); | ||
| this.instance.addCodec(new JsonCodec("application/td+json")); | ||
| this.instance.addCodec(new JsonCodec("application/ld+json")); | ||
| // CBOR | ||
| this.instance.addCodec(new CborCodec(), true); | ||
| // Text | ||
| this.instance.addCodec(new TextCodec()); | ||
| this.instance.addCodec(new TextCodec("text/html")); | ||
| this.instance.addCodec(new TextCodec("text/css")); | ||
| this.instance.addCodec(new TextCodec("application/xml")); | ||
| this.instance.addCodec(new TextCodec("application/xhtml+xml")); | ||
| this.instance.addCodec(new TextCodec("image/svg+xml")); | ||
| // Base64 | ||
| this.instance.addCodec(new Base64Codec("image/png")); | ||
| this.instance.addCodec(new Base64Codec("image/gif")); | ||
| this.instance.addCodec(new Base64Codec("image/jpeg")); | ||
| // OctetStream | ||
| this.instance.addCodec(new OctetstreamCodec()); | ||
|
|
||
| return this.instance; | ||
| } |
There was a problem hiding this comment.
I think this change actually breaks the singleton pattern that is used with ContentSerdes and should be reverted or rewritten.
There was a problem hiding this comment.
The fact that the linter fails indicates that we may want to think about keeping the singleton pattern here, or whether we rather want to replace it with a different approach. As this is a bigger decision to be taken here, we may want to ignore this particular check and resolve the issue in a follow-up PR.
There was a problem hiding this comment.
Hey @JKRhb
I pushed a change. This fixes the lint issue. Can you please take a look and please let me know if this works?
As this is a bigger decision to be taken here, we may want to ignore this particular check and resolve the issue in a follow-up PR
I'd be happy to follow-up on this, once we decide on the approch, if my latest commit doesn't fix it.
Fixes #1177 : Node-wot/core package
PR Summary
This PR primarily focuses on cleaning up code in
packages/coreby removing unnecessary conditional checks and adopting safer property access patterns. These changes address potential warnings regarding redundant checks where types guarantee presence.thing.properties,thing.actions, andevt.formswhere they are guaranteed to exist, simplifying the logic in content-serdes.ts and serdes.ts.Object.prototype.hasOwnProperty.callinconsumed-thing.ts,exposed-thing.ts, andhelpers.tsto ensure safer property existence verification.content-serdes-test.tsandserver-test.tsto align with the simplified logic, removing unnecessary optional chaining or default values that are no longer needed.