Fix eslint configuration for mixed source with .js and .ts
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 5 Mar 2022 21:14:22 +0000 (22:14 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 5 Mar 2022 21:14:22 +0000 (22:14 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
.eslintrc
src/charging-station/ChargingStation.ts
src/performance/PerformanceStatistics.ts

index 255533c3d5adb84f85a281c53d412b4128273bbf..409a11b2bb901464f2b28fbf46d026daef3c571a 100644 (file)
--- a/.eslintrc
+++ b/.eslintrc
@@ -1,41 +1,20 @@
 {
   "root": true,
-  "parser": "@typescript-eslint/parser",
-  "parserOptions": {
-    "project": "./tsconfig.json"
-  },
   "extends": [
     "eslint:recommended",
-    "plugin:@typescript-eslint/recommended",
-    "plugin:@typescript-eslint/recommended-requiring-type-checking",
     "plugin:import/errors",
     "plugin:import/warnings",
-    "plugin:import/typescript",
     "plugin:jsdoc/recommended",
     "plugin:prettier/recommended"
   ],
-  "plugins": ["import", "jsdoc", "@typescript-eslint"],
+  "plugins": ["import", "jsdoc"],
   "settings": {
     "jsdoc": {
       "mode": "typescript"
     }
   },
   "rules": {
-    "@typescript-eslint/array-type": "off",
-    "semi": "off",
-    "@typescript-eslint/semi": ["error", "always"],
     "space-before-blocks": ["error", "always"],
-    "@typescript-eslint/no-empty-function": [
-      "warn",
-      {
-        "allow": ["arrowFunctions", "methods"]
-      }
-    ],
-    "@typescript-eslint/member-ordering": "error",
-    "@typescript-eslint/await-thenable": "error",
-    "@typescript-eslint/no-floating-promises": "error",
-    "@typescript-eslint/promise-function-async": "error",
-    "@typescript-eslint/no-misused-promises": "error",
     "curly": ["error", "all"],
     "brace-style": "error",
     "eqeqeq": ["error", "always"],
@@ -49,8 +28,6 @@
     "no-return-assign": ["error", "always"],
     "no-useless-catch": "error",
     "no-useless-return": "error",
-    "no-shadow": "off", // This one is generating false positive no-shadow errors on exported/const enums
-    "@typescript-eslint/no-shadow": "error",
     "no-multiple-empty-lines": [
       "error",
       {
     "no-lonely-if": "error",
     "no-trailing-spaces": "error",
     "no-whitespace-before-property": "error",
-    "space-before-function-paren": [
-      "error",
-      {
-        "anonymous": "never",
-        "named": "never",
-        "asyncArrow": "always"
-      }
-    ],
     "space-in-parens": ["error", "never"],
     "space-infix-ops": "error",
     "space-unary-ops": "error",
   },
   "overrides": [
     {
-      "files": ["*.js"],
+      "files": ["**/*.ts"],
+      "parser": "@typescript-eslint/parser",
+      "parserOptions": {
+        "project": "./tsconfig.json"
+      },
+      "extends": [
+        "plugin:@typescript-eslint/recommended",
+        "plugin:@typescript-eslint/recommended-requiring-type-checking",
+        "plugin:import/typescript"
+      ],
+      "plugins": ["@typescript-eslint"],
+      "rules": {
+        "@typescript-eslint/array-type": "off",
+        "semi": "off",
+        "@typescript-eslint/semi": ["error", "always"],
+        "@typescript-eslint/no-empty-function": "warn",
+        "@typescript-eslint/member-ordering": "error",
+        "@typescript-eslint/await-thenable": "error",
+        "@typescript-eslint/no-floating-promises": "error",
+        "@typescript-eslint/promise-function-async": "error",
+        "@typescript-eslint/no-misused-promises": "error",
+        "no-shadow": "off", // This one is generating false positive no-shadow errors on exported/const enums
+        "@typescript-eslint/no-shadow": "error"
+      }
+    },
+    {
+      "files": ["**/*.js"],
       "extends": "plugin:node/recommended",
       "rules": {
-        "node/shebang": "off",
-        "@typescript-eslint/no-unused-vars": "off",
-        "@typescript-eslint/no-var-requires": "off"
+        "node/shebang": "off"
       }
     }
   ]
index 8d5cac1d7a2a886b553e5cb563eb22540e585fcf..d136a1504e473d221c5b22cfa1df5fb40eeca143 100644 (file)
@@ -25,7 +25,7 @@ import {
 } from '../types/ocpp/Configuration';
 import { MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues';
 import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
-import WebSocket, { ClientOptions, Data, OPEN } from 'ws';
+import WebSocket, { ClientOptions, Data, OPEN, RawData } from 'ws';
 
 import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
 import { ChargePointStatus } from '../types/ocpp/ChargePointStatus';
@@ -451,6 +451,7 @@ export default class ChargingStation {
     if (interval > 0) {
       // eslint-disable-next-line @typescript-eslint/no-misused-promises
       this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(
+        // eslint-disable-next-line @typescript-eslint/no-misused-promises
         async (): Promise<void> => {
           await this.ocppRequestService.sendMeterValues(
             connectorId,
@@ -481,17 +482,26 @@ export default class ChargingStation {
     // Monitor station template file
     this.startStationTemplateFileMonitoring();
     // Handle WebSocket message
-    this.wsConnection.on('message', this.onMessage.bind(this));
+    this.wsConnection.on(
+      'message',
+      this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void
+    );
     // Handle WebSocket error
-    this.wsConnection.on('error', this.onError.bind(this));
+    this.wsConnection.on(
+      'error',
+      this.onError.bind(this) as (this: WebSocket, error: Error) => void
+    );
     // Handle WebSocket close
-    this.wsConnection.on('close', this.onClose.bind(this));
+    this.wsConnection.on(
+      'close',
+      this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void
+    );
     // Handle WebSocket open
-    this.wsConnection.on('open', this.onOpen.bind(this));
+    this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void);
     // Handle WebSocket ping
-    this.wsConnection.on('ping', this.onPing.bind(this));
+    this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void);
     // Handle WebSocket pong
-    this.wsConnection.on('pong', this.onPong.bind(this));
+    this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void);
     parentPort.postMessage({
       id: ChargingStationWorkerMessageEvents.STARTED,
       data: { id: this.stationInfo.chargingStationId },
@@ -1077,6 +1087,7 @@ export default class ChargingStation {
           break;
         // Error
         default:
+          // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
           errMsg = `${this.logPrefix()} Wrong message type ${messageType}`;
           logger.error(errMsg);
           throw new OCPPError(ErrorType.PROTOCOL_ERROR, errMsg);
@@ -1104,13 +1115,8 @@ export default class ChargingStation {
     logger.debug(this.logPrefix() + ' Received a WS pong (rfc6455) from the server');
   }
 
-  private async onError(error: WSError): Promise<void> {
+  private onError(error: WSError): void {
     logger.error(this.logPrefix() + ' WebSocket error: %j', error);
-    // switch (error.code) {
-    //   case 'ECONNREFUSED':
-    //     await this.reconnect(error);
-    //     break;
-    // }
   }
 
   private getTemplateChargingStationConfiguration(): ChargingStationConfiguration {
index 7338fa24cbfa8d3c1908b6c781ef2df7df4d1847..fdcabb33f6f89adcaf3adde29029b22493cf0771 100644 (file)
@@ -101,6 +101,7 @@ export default class PerformanceStatistics {
         }
         break;
       default:
+        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
         logger.error(`${this.logPrefix()} wrong message type ${messageType}`);
         break;
     }