From 7bf3a198bea319fdc97173deb92f0edcfdb1e119 Mon Sep 17 00:00:00 2001 From: AndreMaz Date: Thu, 19 Oct 2023 10:58:46 +0100 Subject: [PATCH] lint the code --- .eslintrc.json | 2 +- examples/tcp_client_abort_signal.js | 70 ++++++++++++++--------------- ports/tcpport.js | 6 +-- test/ports/tcpport.test.js | 39 ++++++++-------- 4 files changed, 59 insertions(+), 58 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index eccfe20..f6ffd75 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,7 @@ "es6": true }, "parserOptions": { - "ecmaVersion": 2017 + "ecmaVersion": 2020 }, "extends": "eslint:recommended", "rules": { diff --git a/examples/tcp_client_abort_signal.js b/examples/tcp_client_abort_signal.js index 59884d1..9979522 100644 --- a/examples/tcp_client_abort_signal.js +++ b/examples/tcp_client_abort_signal.js @@ -1,58 +1,58 @@ "use strict"; // create an empty modbus client -//let ModbusRTU = require("modbus-serial"); +// let ModbusRTU = require("modbus-serial"); const ModbusRTU = require("../index"); const client = new ModbusRTU(); const abortController = new AbortController(); const { signal } = abortController; signal.addEventListener("abort", () => { - console.log("Abort signal received by the abort controller"); + console.log("Abort signal received by the abort controller"); }); async function connect() { - await client.connectTCP("127.0.0.1", { - port: 8502, - socketOpts: { - signal: signal, - }, - }); - client.setID(1); - client.setTimeout(2000); + await client.connectTCP("127.0.0.1", { + port: 8502, + socketOpts: { + signal: signal + } + }); + client.setID(1); + client.setTimeout(2000); } async function readRegisters() { - const data = await client.readHoldingRegisters(5, 4); - console.log("Received:", data.data); + const data = await client.readHoldingRegisters(5, 4); + console.log("Received:", data.data); } async function runner() { - await connect(); + await connect(); - setTimeout(() => { - abortController.abort("Aborting request"); - }, 1000); + setTimeout(() => { + abortController.abort("Aborting request"); + }, 1000); - await readRegisters(); + await readRegisters(); } runner() - .then(() => { - if (signal.aborted) { - if (signal.reason) { - console.log(`Request aborted with reason: ${signal.reason}`); - } else { - console.log("Request aborted but no reason was given."); - } - } else { - console.log("Request not aborted"); - } - }) - .catch((error) => { - console.error(error); - }) - .finally(async () => { - console.log("Close client"); - await client.close(); - }); + .then(() => { + if (signal.aborted) { + if (signal.reason) { + console.log(`Request aborted with reason: ${signal.reason}`); + } else { + console.log("Request aborted but no reason was given."); + } + } else { + console.log("Request not aborted"); + } + }) + .catch((error) => { + console.error(error); + }) + .finally(async() => { + console.log("Close client"); + await client.close(); + }); diff --git a/ports/tcpport.js b/ports/tcpport.js index cfe710f..5e4133b 100644 --- a/ports/tcpport.js +++ b/ports/tcpport.js @@ -66,10 +66,10 @@ class TcpPort extends EventEmitter { // Default options ...{ host: ip || options.ip, - port: MODBUS_PORT, + port: MODBUS_PORT }, // User options - ...options, + ...options }; if(options.socket) { @@ -94,7 +94,7 @@ class TcpPort extends EventEmitter { this._client = this._externalSocket || new net.Socket(this.socketOpts); if (options.timeout) this._client.setTimeout(options.timeout); - + // register events handlers this._client.on("data", function(data) { let buffer; diff --git a/test/ports/tcpport.test.js b/test/ports/tcpport.test.js index 7695393..7449bb7 100644 --- a/test/ports/tcpport.test.js +++ b/test/ports/tcpport.test.js @@ -28,45 +28,46 @@ describe("Modbus TCP port methods", function() { const TcpPort = require("./../../ports/tcpport"); it("with ip as string", function() { const port = new TcpPort("localhost"); - + expect(port).to.be.an.instanceOf(TcpPort); expect(port.connectOptions.host).to.equal("localhost"); expect(port.connectOptions.port).to.equal(502); }); - + it("with ip as object", function() { - const port = new TcpPort({ip: "localhost"}); - + const port = new TcpPort({ ip: "localhost" }); + expect(port).to.be.an.instanceOf(TcpPort); expect(port.connectOptions.host).to.equal("localhost"); expect(port.connectOptions.port).to.equal(502); }); - + it("with ip as object and port as number", function() { - const port = new TcpPort({ip: "localhost", port: 9999}); - + const port = new TcpPort({ ip: "localhost", port: 9999 }); + expect(port).to.be.an.instanceOf(TcpPort); expect(port.connectOptions.host).to.equal("localhost"); expect(port.connectOptions.port).to.equal(9999); }); - + it("with ip as string and options as object", function() { const port = new TcpPort("localhost", { port: 9999 }); - + expect(port).to.be.an.instanceOf(TcpPort); expect(port.connectOptions.host).to.equal("localhost"); expect(port.connectOptions.port).to.equal(9999); }); - + it("with socket creation options", function() { const controller = new AbortController(); - const port = new TcpPort("localhost", { port: 9999, socketOpts: { - allowHalfOpen: true, - readable: true, - writable: true, - signal: controller.signal, - }}); - + const port = new TcpPort("localhost", { port: 9999, + socketOpts: { + allowHalfOpen: true, + readable: true, + writable: true, + signal: controller.signal + } }); + expect(port).to.be.an.instanceOf(TcpPort); expect(port.connectOptions.host).to.equal("localhost"); expect(port.connectOptions.port).to.equal(9999); @@ -74,8 +75,8 @@ describe("Modbus TCP port methods", function() { allowHalfOpen: true, readable: true, writable: true, - signal: controller.signal, - }); + signal: controller.signal + }); }); });