From fbfd9d9492d8825b0d0017616b3a7460f508e286 Mon Sep 17 00:00:00 2001 From: Plai Date: Thu, 21 Sep 2023 11:53:36 +0700 Subject: [PATCH] Handle all possible NaN and return undefined instead (#154) Co-authored-by: pdujtipiya --- src/Parser/DartLintParser.ts | 5 +++-- src/Parser/DotnetBuildParser.spec.ts | 4 ++-- src/Parser/DotnetBuildParser.ts | 5 +++-- src/Parser/MSBuildParser.ts | 5 +++-- src/Parser/utils/number.util.spec.ts | 13 +++++++++++++ src/Parser/utils/number.util.ts | 4 ++++ 6 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/Parser/utils/number.util.spec.ts create mode 100644 src/Parser/utils/number.util.ts diff --git a/src/Parser/DartLintParser.ts b/src/Parser/DartLintParser.ts index 73f7b43..babf437 100644 --- a/src/Parser/DartLintParser.ts +++ b/src/Parser/DartLintParser.ts @@ -3,6 +3,7 @@ import { LintItem } from './@types'; import { LintSeverity } from './@enums/LintSeverity'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; +import { NoNaN } from './utils/number.util'; export class DartLintParser extends Parser { parse(content: string): LintItem[] { @@ -23,8 +24,8 @@ export class DartLintParser extends Parser { return { ruleId: log, log: log, - line: Number(line), - lineOffset: Number(offset), + line: NoNaN(line), + lineOffset: NoNaN(offset), msg: message, source: source, severity: DartLintParser.stringToSeverity(severityText), diff --git a/src/Parser/DotnetBuildParser.spec.ts b/src/Parser/DotnetBuildParser.spec.ts index 81d621c..867e3e5 100644 --- a/src/Parser/DotnetBuildParser.spec.ts +++ b/src/Parser/DotnetBuildParser.spec.ts @@ -35,8 +35,8 @@ describe('DotnetBuildParser tests', () => { ruleId: 'CS5001', source: `Broken.csproj`, severity: LintSeverity.error, - line: NaN, - lineOffset: NaN, + line: undefined, + lineOffset: undefined, msg: `CS5001: Program does not contain a static 'Main' method suitable for an entry point`, log: logWithNoSource, valid: false, diff --git a/src/Parser/DotnetBuildParser.ts b/src/Parser/DotnetBuildParser.ts index 16ccc89..14de9ba 100644 --- a/src/Parser/DotnetBuildParser.ts +++ b/src/Parser/DotnetBuildParser.ts @@ -8,6 +8,7 @@ import { LintItem } from './@types'; import { mapSeverity } from './utils/dotnetSeverityMap'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; +import { NoNaN } from './utils/number.util'; export class DotnetBuildParser extends Parser { parse(content: string): LintItem[] { @@ -48,8 +49,8 @@ export class DotnetBuildParser extends Parser { return { ruleId: errorCode, log, - line: Number(_line), - lineOffset: Number(_lineOffset), + line: NoNaN(_line), + lineOffset: NoNaN(_lineOffset), msg: `${errorCode.trim()}: ${content.trim()}`, source: relativeSrcPath ?? basename(slash(_csproj)), severity: mapSeverity(severityText), diff --git a/src/Parser/MSBuildParser.ts b/src/Parser/MSBuildParser.ts index 3e40783..5579d4d 100644 --- a/src/Parser/MSBuildParser.ts +++ b/src/Parser/MSBuildParser.ts @@ -8,6 +8,7 @@ import { LintItem } from './@types'; import { mapSeverity } from './utils/dotnetSeverityMap'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; +import { NoNaN } from './utils/number.util'; export class MSBuildParser extends Parser { parse(content: string): LintItem[] { @@ -46,8 +47,8 @@ export class MSBuildParser extends Parser { return { ruleId: code, log, - line: Number(_line), - lineOffset: Number(_lineOffset), + line: NoNaN(_line), + lineOffset: NoNaN(_lineOffset), msg: `${code.trim()}: ${content.trim()}`, source: fileRelativePath ?? fileFullPath, severity: mapSeverity(severityText), diff --git a/src/Parser/utils/number.util.spec.ts b/src/Parser/utils/number.util.spec.ts new file mode 100644 index 0000000..01c28cf --- /dev/null +++ b/src/Parser/utils/number.util.spec.ts @@ -0,0 +1,13 @@ +import { NoNaN } from './number.util'; + +describe('NoNaN', () => { + it('should return number when provided a number string', () => { + const result = NoNaN('69'); + expect(result).toBe(69); + }); + + it('should return undefined when provided NaN string', () => { + const result = NoNaN('this is NaN for sure'); + expect(result).toBeUndefined(); + }); +}); diff --git a/src/Parser/utils/number.util.ts b/src/Parser/utils/number.util.ts new file mode 100644 index 0000000..52f9a84 --- /dev/null +++ b/src/Parser/utils/number.util.ts @@ -0,0 +1,4 @@ +export function NoNaN(nStr: string): number | undefined { + const result = Number(nStr); + return isNaN(result) ? undefined : result; +}