Skip to content

Commit

Permalink
Handle all possible NaN and return undefined instead (#154)
Browse files Browse the repository at this point in the history
Co-authored-by: pdujtipiya <[email protected]>
  • Loading branch information
encX and pdujtipiya authored Sep 21, 2023
1 parent c7c9102 commit fbfd9d9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Parser/DartLintParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/DotnetBuildParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/DotnetBuildParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/MSBuildParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions src/Parser/utils/number.util.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
4 changes: 4 additions & 0 deletions src/Parser/utils/number.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function NoNaN(nStr: string): number | undefined {
const result = Number(nStr);
return isNaN(result) ? undefined : result;
}

0 comments on commit fbfd9d9

Please sign in to comment.