From b3cb9c98f3449e36f281cfd343560f5a12874301 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Wed, 16 Nov 2022 22:09:25 -0500 Subject: [PATCH] chore: upgrade ts and fix type warnings (#91) --- lib/bindings.test.ts | 2 ++ lib/load-bindings.ts | 1 + lib/mocks/linux-list.ts | 1 + lib/poller.test.ts | 19 ++++++++++++------- lib/poller.ts | 16 +++++++++++++--- lib/unix-read.test.ts | 1 + lib/unix-write.test.ts | 1 + package-lock.json | 28 ++++++++++++++-------------- package.json | 4 ++-- test/assert.ts | 2 +- test/makeTestFeature.ts | 1 + 11 files changed, 49 insertions(+), 27 deletions(-) diff --git a/lib/bindings.test.ts b/lib/bindings.test.ts index bd00cc3..e3e1d3c 100644 --- a/lib/bindings.test.ts +++ b/lib/bindings.test.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ import { assert, shouldReject } from '../test/assert' import { makeTestFeature } from '../test/makeTestFeature' import { BindingInterface, OpenOptions, PortInfo, SetOptions } from '@serialport/bindings-interface' diff --git a/lib/load-bindings.ts b/lib/load-bindings.ts index 2858265..3eb61f1 100644 --- a/lib/load-bindings.ts +++ b/lib/load-bindings.ts @@ -2,6 +2,7 @@ import nodeGypBuild from 'node-gyp-build' import { promisify } from 'util' import { join } from 'path' +// eslint-disable-next-line @typescript-eslint/no-explicit-any const binding = nodeGypBuild(join(__dirname, '../')) as any export const asyncClose = binding.close ? promisify(binding.close) : async () => { throw new Error('"binding.close" Method not implemented')} diff --git a/lib/mocks/linux-list.ts b/lib/mocks/linux-list.ts index 1d8b702..a664478 100644 --- a/lib/mocks/linux-list.ts +++ b/lib/mocks/linux-list.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ // Mocks fs.read for listLinux import { EventEmitter } from 'events' diff --git a/lib/poller.test.ts b/lib/poller.test.ts index 7d19baa..945be08 100644 --- a/lib/poller.test.ts +++ b/lib/poller.test.ts @@ -1,32 +1,37 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { Poller } from './poller' import { assert } from '../test/assert' class MockPollerBindings { fd: number callback: any - lastPollFlag: string + lastPollFlag: number constructor(fd: number, callback: any) { this.fd = fd this.callback = callback } - poll(flag: string) { + poll(flag: number) { this.lastPollFlag = flag setImmediate(() => this.callback(null, flag)) } + stop() {} + destroy() {} } class ErrorPollerBindings { fd: number callback: any - lastPollFlag: string + lastPollFlag: number constructor(fd: number, callback: any) { this.fd = fd this.callback = callback } - poll(flag: string) { + poll(flag: number) { this.lastPollFlag = flag setImmediate(() => this.callback(new Error('oh no!'), flag)) } + stop() {} + destroy() {} } describe('Poller', () => { @@ -37,7 +42,7 @@ describe('Poller', () => { const poller = new Poller(1, MockPollerBindings) poller.once('readable', err => { assert.equal(err, null) - assert.equal(poller.poller.lastPollFlag, 1) + assert.equal((poller.poller as MockPollerBindings).lastPollFlag, 1) done(err) }) }) @@ -45,7 +50,7 @@ describe('Poller', () => { const poller = new Poller(1, MockPollerBindings) poller.once('writable', err => { assert.equal(err, null) - assert.equal(poller.poller.lastPollFlag, 2) + assert.equal((poller.poller as MockPollerBindings).lastPollFlag, 2) done(err) }) }) @@ -53,7 +58,7 @@ describe('Poller', () => { const poller = new Poller(1, MockPollerBindings) poller.once('disconnect', err => { assert.equal(err, null) - assert.equal(poller.poller.lastPollFlag, 4) + assert.equal((poller.poller as MockPollerBindings).lastPollFlag, 4) done(err) }) }) diff --git a/lib/poller.ts b/lib/poller.ts index 24c0b92..a583080 100644 --- a/lib/poller.ts +++ b/lib/poller.ts @@ -4,9 +4,19 @@ import { join } from 'path' import nodeGypBuild from 'node-gyp-build' import { BindingsError } from './errors' -const { Poller: PollerBindings } = nodeGypBuild(join(__dirname, '../')) as any +const { Poller: PollerBindings } = nodeGypBuild(join(__dirname, '../')) as { Poller: PollerClass } const logger = debug('serialport/bindings-cpp/poller') +interface PollerClass { + new(fd: number, cb: (err: Error, flag: number)=> void): PollerInstance +} + +interface PollerInstance { + poll(flag: number): void + stop(): void + destroy(): void +} + export const EVENTS = { UV_READABLE: 0b0001, UV_WRITABLE: 0b0010, @@ -39,8 +49,8 @@ function handleEvent(error: Error, eventFlag: number) { * Polls unix systems for readable or writable states of a file or serialport */ export class Poller extends EventEmitter { - poller: any - constructor(fd: number, FDPoller = PollerBindings) { + poller: PollerInstance + constructor(fd: number, FDPoller: PollerClass = PollerBindings) { logger('Creating poller') super() this.poller = new FDPoller(fd, handleEvent.bind(this)) diff --git a/lib/unix-read.test.ts b/lib/unix-read.test.ts index 9c7a4e1..76284b0 100644 --- a/lib/unix-read.test.ts +++ b/lib/unix-read.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { BindingsError } from '.' import { assert, shouldReject } from '../test/assert' import { LinuxPortBinding } from './linux' diff --git a/lib/unix-write.test.ts b/lib/unix-write.test.ts index e6d0c11..057dd6e 100644 --- a/lib/unix-write.test.ts +++ b/lib/unix-write.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { assert, shouldReject } from '../test/assert' import { unixWrite } from './unix-write' import { randomBytes } from 'crypto' diff --git a/package-lock.json b/package-lock.json index 1c38fdb..1b00d73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "@types/chai-subset": "1.3.3", "@types/debug": "4.1.7", "@types/mocha": "10.0.0", - "@types/node": "17.0.15", + "@types/node": "18.11.9", "@typescript-eslint/eslint-plugin": "5.43.0", "@typescript-eslint/parser": "5.43.0", "cc": "3.0.1", @@ -41,7 +41,7 @@ "semantic-release": "19.0.5", "shx": "0.3.4", "sinon": "14.0.2", - "typescript": "4.6.3" + "typescript": "4.9.3" }, "engines": { "node": ">=12.17.0 <13.0 || >=14.0.0" @@ -1162,9 +1162,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "17.0.15", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.15.tgz", - "integrity": "sha512-zWt4SDDv1S9WRBNxLFxFRHxdD9tvH8f5/kg5/IaLFdnSNXsDY4eL3Q3XXN+VxUnWIhyVFDwcsmAprvwXoM/ClA==", + "version": "18.11.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", + "integrity": "sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==", "dev": true }, "node_modules/@types/normalize-package-data": { @@ -12716,9 +12716,9 @@ } }, "node_modules/typescript": { - "version": "4.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz", - "integrity": "sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==", + "version": "4.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.3.tgz", + "integrity": "sha512-CIfGzTelbKNEnLpLdGFgdyKhG23CKdKgQPOBc+OUNrkJ2vr+KSzsSV5kq5iWhEQbok+quxgGzrAtGWCyU7tHnA==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -14063,9 +14063,9 @@ "dev": true }, "@types/node": { - "version": "17.0.15", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.15.tgz", - "integrity": "sha512-zWt4SDDv1S9WRBNxLFxFRHxdD9tvH8f5/kg5/IaLFdnSNXsDY4eL3Q3XXN+VxUnWIhyVFDwcsmAprvwXoM/ClA==", + "version": "18.11.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", + "integrity": "sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==", "dev": true }, "@types/normalize-package-data": { @@ -22824,9 +22824,9 @@ } }, "typescript": { - "version": "4.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz", - "integrity": "sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==", + "version": "4.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.3.tgz", + "integrity": "sha512-CIfGzTelbKNEnLpLdGFgdyKhG23CKdKgQPOBc+OUNrkJ2vr+KSzsSV5kq5iWhEQbok+quxgGzrAtGWCyU7tHnA==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index b87960f..9220492 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@types/chai-subset": "1.3.3", "@types/debug": "4.1.7", "@types/mocha": "10.0.0", - "@types/node": "17.0.15", + "@types/node": "18.11.9", "@typescript-eslint/eslint-plugin": "5.43.0", "@typescript-eslint/parser": "5.43.0", "cc": "3.0.1", @@ -49,7 +49,7 @@ "semantic-release": "19.0.5", "shx": "0.3.4", "sinon": "14.0.2", - "typescript": "4.6.3" + "typescript": "4.9.3" }, "engines": { "node": ">=12.17.0 <13.0 || >=14.0.0" diff --git a/test/assert.ts b/test/assert.ts index c5f56fc..be0a78e 100644 --- a/test/assert.ts +++ b/test/assert.ts @@ -2,7 +2,7 @@ import { assert, use } from 'chai' import chaiSubset from 'chai-subset' use(chaiSubset) -export const shouldReject = async (promise: Promise, errType = Error, message = 'Should have rejected') => { +export const shouldReject = async (promise: Promise, errType = Error, message = 'Should have rejected') => { try { await promise } catch (err) { diff --git a/test/makeTestFeature.ts b/test/makeTestFeature.ts index 535b955..e144a0c 100644 --- a/test/makeTestFeature.ts +++ b/test/makeTestFeature.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import testConfig from './config.json' export const makeTestFeature = (envName: string, testPort?: string) => {