Skip to content

chore(core): added @typescript-eslint/no-unnecessary-condition rule and removed redundant existence checks#1482

Open
ViRUS-0-0 wants to merge 6 commits intoeclipse-thingweb:masterfrom
ViRUS-0-0:issue-1177-core
Open

chore(core): added @typescript-eslint/no-unnecessary-condition rule and removed redundant existence checks#1482
ViRUS-0-0 wants to merge 6 commits intoeclipse-thingweb:masterfrom
ViRUS-0-0:issue-1177-core

Conversation

@ViRUS-0-0
Copy link
Contributor

@ViRUS-0-0 ViRUS-0-0 commented Feb 13, 2026

Fixes #1177 : Node-wot/core package

PR Summary

This PR primarily focuses on cleaning up code in packages/core by removing unnecessary conditional checks and adopting safer property access patterns. These changes address potential warnings regarding redundant checks where types guarantee presence.

  1. Code Cleanup: Removed checks for properties like thing.properties, thing.actions, and evt.forms where they are guaranteed to exist, simplifying the logic in content-serdes.ts and serdes.ts.
  2. Safety Improvements: Replaced direct property checks with Object.prototype.hasOwnProperty.call in consumed-thing.ts, exposed-thing.ts, and helpers.ts to ensure safer property existence verification.
  3. Test Updates: Updated content-serdes-test.ts and server-test.ts to align with the simplified logic, removing unnecessary optional chaining or default values that are no longer needed.
  4. Refactoring: General cleanup of conditional logic to be more concise and type-safe across the core package.

@JKRhb
Copy link
Member

JKRhb commented Feb 15, 2026

The formatting check in the CI pipeline currently fails. Please run npm run format to fix the formatting issues.

Comment on lines 60 to 83
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;
}
Copy link
Member

@JKRhb JKRhb Feb 15, 2026

Choose a reason for hiding this comment

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

I think this change actually breaks the singleton pattern that is used with ContentSerdes and should be reverted or rewritten.

Copy link
Member

@JKRhb JKRhb Feb 15, 2026

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

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.

Enable @typescript-eslint/no-unnecessary-condition rule

2 participants