diff --git a/.eslintrc.js b/.eslintrc.js index d8f683df7..a48eb530e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -29,6 +29,7 @@ module.exports = { "@typescript-eslint/no-unused-vars": "off", // or "@typescript-eslint/no-unused-vars": "off", "no-use-before-define": "off", "@typescript-eslint/no-use-before-define": ["error"], + "@typescript-eslint/prefer-nullish-coalescing": "error", "unused-imports/no-unused-imports": "error", "unused-imports/no-unused-vars": [ "warn", diff --git a/packages/binding-http/src/codecs/tuya-codec.ts b/packages/binding-http/src/codecs/tuya-codec.ts index 82d4eaedd..d6165cd45 100644 --- a/packages/binding-http/src/codecs/tuya-codec.ts +++ b/packages/binding-http/src/codecs/tuya-codec.ts @@ -35,7 +35,7 @@ export default class HttpTuyaCodec implements ContentCodec { const success = parsedBody.success ?? false; if (!success) { - throw new Error(parsedBody.msg != null ? parsedBody.msg : JSON.stringify(parsedBody)); + throw new Error(parsedBody.msg ?? JSON.stringify(parsedBody)); } for (const value of Object.values(parsedBody.result ?? {})) { diff --git a/packages/binding-mbus/src/mbus-client.ts b/packages/binding-mbus/src/mbus-client.ts index f5f66ca80..64e180418 100644 --- a/packages/binding-mbus/src/mbus-client.ts +++ b/packages/binding-mbus/src/mbus-client.ts @@ -94,7 +94,7 @@ export default class MBusClient implements ProtocolClient { debug(`Creating new MbusConnection for ${hostAndPort}`); this._connections.set( hostAndPort, - new MBusConnection(host, port, { connectionTimeout: form["mbus:timeout"] || DEFAULT_TIMEOUT }) + new MBusConnection(host, port, { connectionTimeout: form["mbus:timeout"] ?? DEFAULT_TIMEOUT }) ); connection = this._connections.get(hostAndPort); if (!connection) { diff --git a/packages/binding-modbus/src/modbus-client.ts b/packages/binding-modbus/src/modbus-client.ts index c191ad661..86dfa0b98 100644 --- a/packages/binding-modbus/src/modbus-client.ts +++ b/packages/binding-modbus/src/modbus-client.ts @@ -171,7 +171,7 @@ export default class ModbusClient implements ProtocolClient { debug(`Creating new ModbusConnection for ${hostAndPort}`); connection = new ModbusConnection(host, port, { - connectionTimeout: form["modbus:timeout"] || DEFAULT_TIMEOUT, + connectionTimeout: form["modbus:timeout"] ?? DEFAULT_TIMEOUT, }); this._connections.set(hostAndPort, connection); } else { diff --git a/packages/binding-netconf/src/netconf-client.ts b/packages/binding-netconf/src/netconf-client.ts index 67f1550a4..6690fbfeb 100644 --- a/packages/binding-netconf/src/netconf-client.ts +++ b/packages/binding-netconf/src/netconf-client.ts @@ -83,8 +83,8 @@ export default class NetconfClient implements ProtocolClient { const port = parseInt(url.port); const xpathQuery = url.pathname; const method = this.methodFromForm(form, "EDIT-CONFIG"); - let NSs = form["nc:NSs"] || {}; - const target = form["nc:target"] || DEFAULT_TARGET; + let NSs = form["nc:NSs"] ?? {}; + const target = form["nc:target"] ?? DEFAULT_TARGET; if (this.client.getRouter() === null) { try { @@ -112,8 +112,8 @@ export default class NetconfClient implements ProtocolClient { const port = parseInt(url.port); const xpathQuery = url.pathname; const method = this.methodFromForm(form, "RPC"); - let NSs = form["nc:NSs"] || {}; - const target = form["nc:target"] || DEFAULT_TARGET; + let NSs = form["nc:NSs"] ?? {}; + const target = form["nc:target"] ?? DEFAULT_TARGET; let result: string; if (this.client.getRouter() === null) { diff --git a/packages/binding-opcua/src/opcua-protocol-client.ts b/packages/binding-opcua/src/opcua-protocol-client.ts index 2b33a57d3..603c84a5f 100644 --- a/packages/binding-opcua/src/opcua-protocol-client.ts +++ b/packages/binding-opcua/src/opcua-protocol-client.ts @@ -121,7 +121,7 @@ export function findBasicDataTypeC( return callback(new Error("Internal Error")); } - browseResult.references = browseResult.references || /* istanbul ignore next */ []; + browseResult.references = browseResult.references ?? /* istanbul ignore next */ []; const baseDataType = browseResult.references[0].nodeId; return findBasicDataTypeC(session, baseDataType, callback); }); @@ -335,7 +335,7 @@ export class OPCUAProtocolClient implements ProtocolClient { session, form, argumentDefinition, - callResult.outputArguments || [] + callResult.outputArguments ?? [] ); return output; }); @@ -571,7 +571,7 @@ export class OPCUAProtocolClient implements ProtocolClient { const { name, dataType, /* description, */ arrayDimensions, valueRank } = argument; - if (bodyInput[name || "null"] === undefined) { + if (bodyInput[name ?? "null"] === undefined) { throw new Error("missing value in bodyInput for argument " + name); } const basicDataType = await this._findBasicDataType(session, dataType); @@ -587,7 +587,7 @@ export class OPCUAProtocolClient implements ProtocolClient { : VariantArrayType.Matrix; const n = (a: unknown) => Buffer.from(JSON.stringify(a)); - const v = await this._contentToVariant(content2.type, n(bodyInput[name || "null"]), basicDataType); + const v = await this._contentToVariant(content2.type, n(bodyInput[name ?? "null"]), basicDataType); variants.push({ dataType: basicDataType, @@ -607,14 +607,14 @@ export class OPCUAProtocolClient implements ProtocolClient { ): Promise { const outputArguments = (argumentDefinition.outputArguments || []) as unknown as Argument[]; - const contentType = form.contentType || "application/json"; + const contentType = form.contentType ?? "application/json"; const body: Record = {}; for (let index = 0; index < outputArguments.length; index++) { const argument = outputArguments[index]; const { name } = argument; const element = _variantToJSON(outputVariants[index], contentType); - body[name || "null"] = element; + body[name ?? "null"] = element; } return new Content("application/json", Readable.from(JSON.stringify(body))); diff --git a/packages/core/src/codecs/text-codec.ts b/packages/core/src/codecs/text-codec.ts index e4f9061de..0fa133a4c 100644 --- a/packages/core/src/codecs/text-codec.ts +++ b/packages/core/src/codecs/text-codec.ts @@ -23,7 +23,7 @@ export default class TextCodec implements ContentCodec { private subMediaType: string; constructor(subMediaType?: string) { - this.subMediaType = subMediaType == null ? "text/plain" : subMediaType; + this.subMediaType = subMediaType ?? "text/plain"; } getMediaType(): string { diff --git a/packages/td-tools/src/thing-model-helpers.ts b/packages/td-tools/src/thing-model-helpers.ts index 552ee799e..82d307ccb 100644 --- a/packages/td-tools/src/thing-model-helpers.ts +++ b/packages/td-tools/src/thing-model-helpers.ts @@ -613,7 +613,7 @@ export class ThingModelHelpers { const regex = "{{.*?}}"; const modelString = JSON.stringify(model); // first check if model needs map - let keys: string[] = modelString.match(new RegExp(regex, "g")) || []; + let keys: string[] = modelString.match(new RegExp(regex, "g")) ?? []; keys = keys.map((el) => el.replace("{{", "").replace("}}", "")); let isValid = true; let errors;