From 5d99fd5252127b39dcba42f889e1aed9f844a335 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Wed, 23 Sep 2020 13:06:43 +1000 Subject: [PATCH] chore: fix linting errors --- .eslintrc.json | 3 +-- src/encoding/iso2022.ts | 5 ++--- src/encoding/mbcs.ts | 13 +++++-------- src/encoding/sbcs.ts | 8 +++----- src/encoding/unicode.ts | 8 ++++---- src/encoding/utf8.ts | 6 +++--- src/match.ts | 6 +++--- 7 files changed, 21 insertions(+), 28 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 8233c3e..2906220 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,7 +5,6 @@ "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], "rules": { "@typescript-eslint/no-unused-vars": ["warn", { "varsIgnorePattern": "_" }], - "@typescript-eslint/no-inferrable-types": ["off"], - "@typescript-eslint/ban-ts-comment": ["warn"] + "@typescript-eslint/no-inferrable-types": ["off"] } } diff --git a/src/encoding/iso2022.ts b/src/encoding/iso2022.ts index 3da7678..dd92965 100644 --- a/src/encoding/iso2022.ts +++ b/src/encoding/iso2022.ts @@ -1,6 +1,5 @@ import { Context, Recogniser } from '.'; - -const match = require('../match').default; +import match, { Match } from '../match' /** * This is a superclass for the individual detectors for @@ -15,7 +14,7 @@ class ISO_2022 implements Recogniser { return 'ISO_2022'; } - match(det: Context) { + match(det: Context): Match | null { /** * Matching function shared among the 2022 detectors JP, CN and KR * Counts up the number of legal an unrecognized escape sequences in diff --git a/src/encoding/mbcs.ts b/src/encoding/mbcs.ts index 33bd364..14fceec 100644 --- a/src/encoding/mbcs.ts +++ b/src/encoding/mbcs.ts @@ -1,5 +1,5 @@ import { Context, Recogniser } from '.'; -const match = require('../match').default; +import match, { Match } from '../match' /** * Binary search implementation (recursive) @@ -97,7 +97,7 @@ class mbcs implements Recogniser { * bits 0-7: the match confidence, ranging from 0-100 * bits 8-15: The match reason, an enum-like value. */ - match(det: Context) { + match(det: Context): Match | null { let singleByteCharCount = 0, //TODO Do we really need this? doubleByteCharCount = 0, commonCharCount = 0, @@ -108,7 +108,7 @@ class mbcs implements Recogniser { const iter = new IteratedChar(); detectBlock: { - for (iter.reset(); this.nextChar(iter, det); ) { + for (iter.reset(); this.nextChar(iter, det);) { totalCharCount++; if (iter.error) { badCharCount++; @@ -159,7 +159,7 @@ class mbcs implements Recogniser { } if (this.commonChars == null) { - // We have no statistics on frequently occuring characters. + // We have no statistics on frequently occurring characters. // Assess confidence purely on having a reasonable number of // multi-byte characters (the more the better confidence = 30 + doubleByteCharCount - 20 * badCharCount; @@ -167,11 +167,8 @@ class mbcs implements Recogniser { confidence = 100; } } else { - // // Frequency of occurrence statistics exist. - // - // @ts-ignore - const maxVal = Math.log(parseFloat(doubleByteCharCount) / 4); + const maxVal = Math.log(doubleByteCharCount / 4); const scaleFactor = 90.0 / maxVal; confidence = Math.floor( Math.log(commonCharCount + 1) * scaleFactor + 10 diff --git a/src/encoding/sbcs.ts b/src/encoding/sbcs.ts index 5635475..cda6ebe 100644 --- a/src/encoding/sbcs.ts +++ b/src/encoding/sbcs.ts @@ -1,6 +1,5 @@ import { Context, Recogniser } from '../encoding/index'; - -const match = require('../match').default; +import match, { Match } from '../match'; /** * This class recognizes single-byte encodings. Because the encoding scheme is so @@ -119,12 +118,11 @@ class sbcs implements Recogniser { return []; } - // @ts-ignore name(input: Context): string { return 'sbcs'; } - match(det: Context) { + match(det: Context): Match | null { const ngrams = this.ngrams(); if (isFlatNgrams(ngrams)) { @@ -134,7 +132,7 @@ class sbcs implements Recogniser { } let bestConfidenceSoFar = -1; - let lang = null; + let lang; for (let i = ngrams.length - 1; i >= 0; i--) { const ngl = ngrams[i]; diff --git a/src/encoding/unicode.ts b/src/encoding/unicode.ts index 2530b27..acd53a1 100644 --- a/src/encoding/unicode.ts +++ b/src/encoding/unicode.ts @@ -1,5 +1,5 @@ import { Context, Recogniser } from '.'; -const match = require('../match').default; +import match, { Match } from '../match'; /** * This class matches UTF-16 and UTF-32, both big- and little-endian. The @@ -10,7 +10,7 @@ export class UTF_16BE implements Recogniser { return 'UTF-16BE'; } - match(det: Context) { + match(det: Context): Match | null { const input = det.fRawInput; if ( @@ -30,7 +30,7 @@ export class UTF_16LE implements Recogniser { name() { return 'UTF-16LE'; } - match(det: Context) { + match(det: Context): Match | null { const input = det.fRawInput; if ( @@ -64,7 +64,7 @@ class UTF_32 implements Recogniser, WithGetChar { return -1; } - match(det: Context) { + match(det: Context): Match | null { let numValid = 0, numInvalid = 0, hasBOM = false, diff --git a/src/encoding/utf8.ts b/src/encoding/utf8.ts index d19a80d..9a831f6 100644 --- a/src/encoding/utf8.ts +++ b/src/encoding/utf8.ts @@ -1,12 +1,12 @@ import { Context, Recogniser } from '.'; -const match = require('../match').default; +import match, { Match } from '../match'; export default class Utf8 implements Recogniser { name() { return 'UTF-8'; } - match(det: Context) { + match(det: Context): Match | null { let hasBOM = false, numValid = 0, numInvalid = 0, @@ -42,7 +42,7 @@ export default class Utf8 implements Recogniser { } // Verify that we've got the right number of trail bytes in the sequence - for (;;) { + for (; ;) { i++; if (i >= det.fRawLength) break; diff --git a/src/match.ts b/src/match.ts index f9b7390..6245fc6 100644 --- a/src/match.ts +++ b/src/match.ts @@ -1,12 +1,12 @@ +import { Context, Recogniser } from "./encoding"; export interface Match { confidence: number; name: string; - lang: string; + lang?: string; } -// @ts-ignore -export default (det, rec, confidence, name, lang): Match => ({ +export default (det: Context, rec: Recogniser, confidence: number, name?: string, lang?: string): Match => ({ confidence, name: name || rec.name(det), lang,