From ea9a4c8bd6f2869af7a49fa26e265724690b4ec5 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Mon, 18 Sep 2023 08:22:33 +0200 Subject: [PATCH] chore(binding-coap): enable strict-boolean-expressions and null checks --- packages/binding-coap/.eslintrc.json | 5 ++++- packages/binding-coap/src/coap-client.ts | 13 ++++++------- packages/binding-coap/src/coap-server.ts | 4 +--- packages/binding-coap/test/tsconfig.json | 3 ++- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/binding-coap/.eslintrc.json b/packages/binding-coap/.eslintrc.json index c4237119e..70b9f134b 100644 --- a/packages/binding-coap/.eslintrc.json +++ b/packages/binding-coap/.eslintrc.json @@ -1,3 +1,6 @@ { - "extends": "../../.eslintrc.js" + "extends": "../../.eslintrc.js", + "rules": { + "@typescript-eslint/strict-boolean-expressions": ["error"] + } } diff --git a/packages/binding-coap/src/coap-client.ts b/packages/binding-coap/src/coap-client.ts index 253afd349..2ff936518 100644 --- a/packages/binding-coap/src/coap-client.ts +++ b/packages/binding-coap/src/coap-client.ts @@ -117,7 +117,7 @@ export default class CoapClient implements ProtocolClient { }); req.on("error", (err: Error) => reject(err)); (async () => { - if (content && content.body) { + if (content != null) { const buffer = await content.toBuffer(); req.setOption("Content-Format", content.type); req.write(buffer); @@ -159,8 +159,7 @@ export default class CoapClient implements ProtocolClient { debug(`CoapClient received Content-Format: ${res.headers["Content-Format"]}`); // FIXME does not work with blockwise because of node-coap - let contentType = res.headers["Content-Format"]; - if (!contentType) contentType = form.contentType; + const contentType = res.headers["Content-Format"] ?? form.contentType ?? ContentSerdes.DEFAULT; res.on("data", (data: Buffer) => { next(new Content(`${contentType}`, Readable.from(res.payload))); @@ -203,10 +202,10 @@ export default class CoapClient implements ProtocolClient { const options: CoapRequestParams = { agent: this.agent, - hostname: requestUri.hostname || "", - port: requestUri.port ? parseInt(requestUri.port, 10) : 5683, - pathname: requestUri.pathname || "", - query: requestUri.query || "", + hostname: requestUri.hostname ?? "", + port: requestUri.port != null ? parseInt(requestUri.port, 10) : 5683, + pathname: requestUri.pathname ?? "", + query: requestUri.query ?? "", observe: false, multicast: false, confirmable: true, diff --git a/packages/binding-coap/src/coap-server.ts b/packages/binding-coap/src/coap-server.ts index 1bc354c90..c78c51944 100644 --- a/packages/binding-coap/src/coap-server.ts +++ b/packages/binding-coap/src/coap-server.ts @@ -550,9 +550,7 @@ export default class CoapServer implements ProtocolServer { res.end(); res.on("finish", (err: Error) => { - if (err) { - error(`CoapServer on port ${this.port} failed on observe with: ${err.message}`); - } + error(`CoapServer on port ${this.port} failed on observe with: ${err.message}`); thing.handleUnobserveProperty(affordanceKey, listener, interactionOptions); }); diff --git a/packages/binding-coap/test/tsconfig.json b/packages/binding-coap/test/tsconfig.json index 786b1b616..c2cefa10e 100644 --- a/packages/binding-coap/test/tsconfig.json +++ b/packages/binding-coap/test/tsconfig.json @@ -1,7 +1,8 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "rootDir": ".." + "rootDir": "..", + "strictNullChecks": true }, "include": ["*.ts", "**/*.ts", "../src/**/*.ts"] }