[serialport] Add error nullability (#36451)

* [serialport] Fix error in callback can be null

Most methods are inherited from Node stream and according to types that
means that the err parameter can be null or undefined.

See https://serialport.io/docs/guide-usage and look for function(err).
See ef80d83080/packages/serialport/test/integration.js (L55)

* [serialport] Add correct type to test_list_ports_promise

* [serialport] Enabled strict-type-predicates for testing
This commit is contained in:
Matthias Kunnen
2019-06-26 18:48:55 +02:00
committed by Benjamin Lichtman
parent 882da69fd0
commit d60528f838
3 changed files with 28 additions and 17 deletions

View File

@@ -21,12 +21,12 @@ declare class SerialPort extends Stream.Duplex {
open(callback?: SerialPort.ErrorCallback): void;
update(options: SerialPort.UpdateOptions, callback?: SerialPort.ErrorCallback): void;
write(data: string| number[] | Buffer, callback?: (error: any, bytesWritten: number) => void): boolean;
write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: any, bytesWritten: number) => void): boolean;
write(data: string| number[] | Buffer, callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean;
write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean;
read(size?: number): string | Buffer | null;
close(callback?: (error: Error) => void): void;
close(callback?: (error?: Error | null) => void): void;
set(options: SerialPort.SetOptions, callback?: SerialPort.ErrorCallback): void;
get(callback?: SerialPort.ModemBitsCallback): void;
@@ -46,9 +46,9 @@ declare class SerialPort extends Stream.Duplex {
declare namespace SerialPort {
// Callbacks Type Defs
type ErrorCallback = (error: Error) => void;
type ModemBitsCallback = (error: Error, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void;
type ListCallback = (error: Error, port: any[]) => void;
type ErrorCallback = (error?: Error | null) => void;
type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void;
type ListCallback = (error: Error | null | undefined, ports: any[]) => void;
// Options Type Defs
interface OpenOptions {

View File

@@ -8,7 +8,7 @@ function test_basic_connect() {
function test_connect_config() {
const port1 = new SerialPort('', {
}, (error: Error) => {});
}, error => {});
const port4 = new SerialPort('', {
autoOpen: false,
@@ -25,7 +25,13 @@ function test_connect_config() {
vmin: 1,
vtime: 1
}
}, (error: Error) => {});
},
error => {
if (error !== null) {
console.error(error);
}
}
);
}
function test_open() {
@@ -41,8 +47,8 @@ function test_update() {
function test_write() {
const port = new SerialPort('');
port.write('test', (error: Error) => {});
port.write('test', 'utf8', (error: Error) => {});
port.write('test', (error?: Error | null) => {});
port.write('test', 'utf8', (error?: Error | null) => {});
}
function test_read() {
@@ -54,13 +60,13 @@ function test_read() {
function test_close() {
const port = new SerialPort('');
port.close((error: Error) => {});
port.close((error?: Error | null) => {});
}
function test_set() {
const port = new SerialPort('');
port.set({}, (error: Error) => {});
port.set({}, (error?: Error | null) => {});
}
function test_get() {
@@ -72,13 +78,13 @@ function test_get() {
function test_flush() {
const port = new SerialPort('');
port.flush((error: Error) => {});
port.flush((error?: Error | null) => {});
}
function test_drain() {
const port = new SerialPort('');
port.drain((error: Error) => {});
port.drain((error?: Error | null) => {});
}
function test_pause_resume() {
@@ -130,10 +136,10 @@ function test_properties() {
function test_list_ports_promise() {
const ports = SerialPort
.list()
.then((ports: any) => {})
.then((ports: SerialPort.PortInfo[]) => {})
.catch((err: Error) => {});
}
function test_list_ports_callback() {
const ports = SerialPort.list((error: Error, port: any[]) => {});
const ports = SerialPort.list((error: Error | null | undefined, ports: any[]) => {});
}

View File

@@ -1 +1,6 @@
{ "extends": "dtslint/dt.json" }
{
"extends": "dtslint/dt.json",
"rules": {
"strict-type-predicates": true
}
}