Skip to content

Commit 1f57140

Browse files
committed
more lints
1 parent 6cfdd93 commit 1f57140

File tree

6 files changed

+62
-64
lines changed

6 files changed

+62
-64
lines changed

connection/connection_params.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { parseConnectionUri } from "../utils/utils.ts";
22
import { ConnectionParamsError } from "../client/error.ts";
33
import { fromFileUrl, isAbsolute } from "@std/path";
4-
import { OidType } from "../query/oid.ts";
5-
import { DebugControls } from "../debug.ts";
6-
import { ParseArrayFunction } from "../query/array_parser.ts";
4+
import type { OidType } from "../query/oid.ts";
5+
import type { DebugControls } from "../debug.ts";
6+
import type { ParseArrayFunction } from "../query/array_parser.ts";
77

88
/**
99
* The connection string must match the following URI structure. All parameters but database and user are optional

query/decode.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { bold, yellow } from "@std/fmt/colors";
2-
import { Oid, OidType, OidTypes, OidValue } from "./oid.ts";
2+
import type { OidType, OidValue } from "./oid.ts";
3+
import { Oid, OidTypes } from "./oid.ts";
34
import {
45
decodeBigint,
56
decodeBigintArray,
@@ -35,7 +36,7 @@ import {
3536
decodeTid,
3637
decodeTidArray,
3738
} from "./decoders.ts";
38-
import { ClientControls } from "../connection/connection_params.ts";
39+
import type { ClientControls } from "../connection/connection_params.ts";
3940
import { parseArray } from "./array_parser.ts";
4041

4142
export class Column {
@@ -198,10 +199,9 @@ function decodeText(value: string, typeOid: number) {
198199
}
199200
} catch (_e) {
200201
console.error(
201-
bold(yellow(`Error decoding type Oid ${typeOid} value`)) +
202-
_e.message +
203-
"\n" +
204-
bold("Defaulting to null."),
202+
`${
203+
bold(yellow(`Error decoding type Oid ${typeOid} value`))
204+
}${_e.message}\n${bold("Defaulting to null.")}`,
205205
);
206206
// If an error occurred during decoding, return null
207207
return null;
@@ -224,9 +224,9 @@ export function decode(
224224

225225
if (decoderFunc) {
226226
return decoderFunc(strValue, column.typeOid, parseArray);
227-
} // if no custom decoder is found and the oid is for an array type, check if there is
227+
}
228228
// a decoder for the base type and use that with the array parser
229-
else if (oidType?.includes("_array")) {
229+
if (oidType?.includes("_array")) {
230230
const baseOidType = oidType.replace("_array", "") as OidType;
231231
// check if the base type is in the Oid object
232232
if (baseOidType in Oid) {
@@ -251,9 +251,9 @@ export function decode(
251251
// else, default to 'auto' mode, which uses the typeOid to determine the decoding strategy
252252
if (column.format === Format.BINARY) {
253253
return decodeBinary();
254-
} else if (column.format === Format.TEXT) {
254+
}
255+
if (column.format === Format.TEXT) {
255256
return decodeText(strValue, column.typeOid);
256-
} else {
257-
throw new Error(`Unknown column format: ${column.format}`);
258257
}
258+
throw new Error(`Unknown column format: ${column.format}`);
259259
}

query/decoders.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ export function decodeBoxArray(value: string) {
7575
export function decodeBytea(byteaStr: string): Uint8Array {
7676
if (HEX_PREFIX_REGEX.test(byteaStr)) {
7777
return decodeByteaHex(byteaStr);
78-
} else {
79-
return decodeByteaEscape(byteaStr);
8078
}
79+
return decodeByteaEscape(byteaStr);
8180
}
8281

8382
export function decodeByteaArray(value: string) {
@@ -94,7 +93,7 @@ function decodeByteaEscape(byteaStr: string): Uint8Array {
9493
++i;
9594
} else {
9695
if (/[0-7]{3}/.test(byteaStr.substr(i + 1, 3))) {
97-
bytes.push(parseInt(byteaStr.substr(i + 1, 3), 8));
96+
bytes.push(Number.parseInt(byteaStr.substr(i + 1, 3), 8));
9897
i += 4;
9998
} else {
10099
let backslashes = 1;
@@ -118,7 +117,7 @@ function decodeByteaHex(byteaStr: string): Uint8Array {
118117
const bytesStr = byteaStr.slice(2);
119118
const bytes = new Uint8Array(bytesStr.length / 2);
120119
for (let i = 0, j = 0; i < bytesStr.length; i += 2, j++) {
121-
bytes[j] = parseInt(bytesStr[i] + bytesStr[i + 1], HEX);
120+
bytes[j] = Number.parseInt(bytesStr[i] + bytesStr[i + 1], HEX);
122121
}
123122
return bytes;
124123
}
@@ -128,7 +127,7 @@ export function decodeCircle(value: string): Circle {
128127
.substring(1, value.length - 1)
129128
.split(/,(?![^(]*\))/) as [string, Float8];
130129

131-
if (Number.isNaN(parseFloat(radius))) {
130+
if (Number.isNaN(Number.parseFloat(radius))) {
132131
throw new Error(
133132
`Invalid Circle: "${value}". Circle radius "${radius}" must be a valid number.`,
134133
);
@@ -152,9 +151,10 @@ export function decodeDate(dateStr: string): Date | number {
152151
// there are special `infinity` and `-infinity`
153152
// cases representing out-of-range dates
154153
if (dateStr === "infinity") {
155-
return Number(Infinity);
156-
} else if (dateStr === "-infinity") {
157-
return Number(-Infinity);
154+
return Number(Number.POSITIVE_INFINITY);
155+
}
156+
if (dateStr === "-infinity") {
157+
return Number(Number.NEGATIVE_INFINITY);
158158
}
159159

160160
return parseDate(dateStr, "yyyy-MM-dd");
@@ -178,16 +178,16 @@ export function decodeDatetime(dateStr: string): number | Date {
178178

179179
const isBC = BC_RE.test(dateStr);
180180

181-
const year = parseInt(matches[1], 10) * (isBC ? -1 : 1);
181+
const year = Number.parseInt(matches[1], 10) * (isBC ? -1 : 1);
182182
// remember JS dates are 0-based
183-
const month = parseInt(matches[2], 10) - 1;
184-
const day = parseInt(matches[3], 10);
185-
const hour = parseInt(matches[4], 10);
186-
const minute = parseInt(matches[5], 10);
187-
const second = parseInt(matches[6], 10);
183+
const month = Number.parseInt(matches[2], 10) - 1;
184+
const day = Number.parseInt(matches[3], 10);
185+
const hour = Number.parseInt(matches[4], 10);
186+
const minute = Number.parseInt(matches[5], 10);
187+
const second = Number.parseInt(matches[6], 10);
188188
// ms are written as .007
189189
const msMatch = matches[7];
190-
const ms = msMatch ? 1000 * parseFloat(msMatch) : 0;
190+
const ms = msMatch ? 1000 * Number.parseFloat(msMatch) : 0;
191191

192192
let date: Date;
193193

@@ -213,15 +213,15 @@ export function decodeDatetimeArray(value: string) {
213213
}
214214

215215
export function decodeInt(value: string): number {
216-
return parseInt(value, 10);
216+
return Number.parseInt(value, 10);
217217
}
218218

219219
export function decodeIntArray(value: string) {
220220
return parseArray(value, decodeInt);
221221
}
222222

223223
export function decodeFloat(value: string): number {
224-
return parseFloat(value);
224+
return Number.parseFloat(value);
225225
}
226226

227227
export function decodeFloatArray(value: string) {
@@ -249,13 +249,13 @@ export function decodeLine(value: string): Line {
249249
);
250250
}
251251

252-
equationConsts.forEach((c) => {
253-
if (Number.isNaN(parseFloat(c))) {
252+
for (const c of equationConsts) {
253+
if (Number.isNaN(Number.parseFloat(c))) {
254254
throw new Error(
255255
`Invalid Line: "${value}". Line constant "${c}" must be a valid number.`,
256256
);
257257
}
258-
});
258+
}
259259

260260
const [a, b, c] = equationConsts;
261261

@@ -326,10 +326,12 @@ export function decodePoint(value: string): Point {
326326

327327
const [x, y] = coordinates;
328328

329-
if (Number.isNaN(parseFloat(x)) || Number.isNaN(parseFloat(y))) {
329+
if (
330+
Number.isNaN(Number.parseFloat(x)) || Number.isNaN(Number.parseFloat(y))
331+
) {
330332
throw new Error(
331333
`Invalid Point: "${value}". Coordinate "${
332-
Number.isNaN(parseFloat(x)) ? x : y
334+
Number.isNaN(Number.parseFloat(x)) ? x : y
333335
}" must be a valid number.`,
334336
);
335337
}
@@ -393,9 +395,9 @@ function decodeTimezoneOffset(dateStr: string): null | number {
393395
// offsets and vice-versa
394396
const sign = type === "-" ? 1 : -1;
395397

396-
const hours = parseInt(matches[2], 10);
397-
const minutes = parseInt(matches[3] || "0", 10);
398-
const seconds = parseInt(matches[4] || "0", 10);
398+
const hours = Number.parseInt(matches[2], 10);
399+
const minutes = Number.parseInt(matches[3] || "0", 10);
400+
const seconds = Number.parseInt(matches[4] || "0", 10);
399401

400402
const offset = hours * 3600 + minutes * 60 + seconds;
401403

query/encode.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ export type EncodedArg = null | string | Uint8Array;
9191
export function encodeArgument(value: unknown): EncodedArg {
9292
if (value === null || typeof value === "undefined") {
9393
return null;
94-
} else if (value instanceof Uint8Array) {
94+
}
95+
if (value instanceof Uint8Array) {
9596
return encodeBytes(value);
96-
} else if (value instanceof Date) {
97+
}
98+
if (value instanceof Date) {
9799
return encodeDate(value);
98-
} else if (value instanceof Array) {
100+
}
101+
if (Array.isArray(value)) {
99102
return encodeArray(value);
100-
} else if (value instanceof Object) {
103+
}
104+
if (value instanceof Object) {
101105
return JSON.stringify(value);
102-
} else {
103-
return String(value);
104106
}
107+
return String(value);
105108
}

query/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { encodeArgument, type EncodedArg } from "./encode.ts";
22
import { type Column, decode } from "./decode.ts";
3-
import { type Notice } from "../connection/message.ts";
4-
import { type ClientControls } from "../connection/connection_params.ts";
3+
import type { Notice } from "../connection/message.ts";
4+
import type { ClientControls } from "../connection/connection_params.ts";
55

66
// TODO
77
// Limit the type of parameters that can be passed

query/transaction.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type QueryClient } from "../client.ts";
1+
import type { QueryClient } from "../client.ts";
22
import {
33
Query,
44
type QueryArguments,
@@ -218,7 +218,7 @@ export class Transaction {
218218
);
219219
}
220220

221-
let isolation_level;
221+
let isolation_level: string;
222222
switch (this.#isolation_level) {
223223
case "read_committed": {
224224
isolation_level = "READ COMMITTED";
@@ -238,7 +238,7 @@ export class Transaction {
238238
);
239239
}
240240

241-
let permissions;
241+
let permissions: string;
242242
if (this.#read_only) {
243243
permissions = "READ ONLY";
244244
} else {
@@ -257,9 +257,8 @@ export class Transaction {
257257
} catch (e) {
258258
if (e instanceof PostgresError) {
259259
throw new TransactionError(this.name, e);
260-
} else {
261-
throw e;
262260
}
261+
throw e;
263262
}
264263

265264
this.#updateClientLock(this.name);
@@ -312,9 +311,8 @@ export class Transaction {
312311
} catch (e) {
313312
if (e instanceof PostgresError) {
314313
throw new TransactionError(this.name, e);
315-
} else {
316-
throw e;
317314
}
315+
throw e;
318316
}
319317
}
320318

@@ -467,9 +465,8 @@ export class Transaction {
467465
if (e instanceof PostgresError) {
468466
await this.commit();
469467
throw new TransactionError(this.name, e);
470-
} else {
471-
throw e;
472468
}
469+
throw e;
473470
}
474471
}
475472

@@ -565,9 +562,8 @@ export class Transaction {
565562
if (e instanceof PostgresError) {
566563
await this.commit();
567564
throw new TransactionError(this.name, e);
568-
} else {
569-
throw e;
570565
}
566+
throw e;
571567
}
572568
}
573569

@@ -701,9 +697,8 @@ export class Transaction {
701697
if (e instanceof PostgresError) {
702698
await this.commit();
703699
throw new TransactionError(this.name, e);
704-
} else {
705-
throw e;
706700
}
701+
throw e;
707702
}
708703

709704
this.#resetTransaction();
@@ -792,9 +787,8 @@ export class Transaction {
792787
if (e instanceof PostgresError) {
793788
await this.commit();
794789
throw new TransactionError(this.name, e);
795-
} else {
796-
throw e;
797790
}
791+
throw e;
798792
}
799793
} else {
800794
savepoint = new Savepoint(
@@ -813,9 +807,8 @@ export class Transaction {
813807
if (e instanceof PostgresError) {
814808
await this.commit();
815809
throw new TransactionError(this.name, e);
816-
} else {
817-
throw e;
818810
}
811+
throw e;
819812
}
820813
this.#savepoints.push(savepoint);
821814
}

0 commit comments

Comments
 (0)