Skip to content

Commit

Permalink
add ruleId and type to codecoach output (#139)
Browse files Browse the repository at this point in the history
* add problem and type to codecoach output

* rename problem to ruleId

* change scalastyle ruleId to source

* fix provider mock data

* remove unused imports

---------

Co-authored-by: Natchanon Nuntanirund <[email protected]>
  • Loading branch information
jeeyo and Natchanon Nuntanirund authored Jun 22, 2023
1 parent 1d18a9d commit e9ff8e2
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/Parser/@types/log.type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { LogSeverity } from '..';
import { ProjectType } from '../../Config/@enums';

export type LogType = {
ruleId: string;
log: string;
msg: string;
severity: LogSeverity;
source: string;
line?: number;
lineOffset?: number;
valid: boolean;
type: ProjectType;
};
6 changes: 6 additions & 0 deletions src/Parser/AndroidLintStyleParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@ describe('AndroidLintStyleParser', () => {
expect(result).toHaveLength(3);

expect(result[0]).toEqual({
ruleId: 'GradleDependency',
source: 'app/build.gradle',
severity: LogSeverity.warning,
line: 42,
lineOffset: 5,
msg: `A newer version of org.jetbrains.kotlin:kotlin-stdlib than 1.3.72 is available: 1.4.20`,
log: `implementation org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version`,
valid: true,
type: 'androidlint',
});

expect(result[1]).toEqual({
ruleId: 'MissingTranslation',
source: `app/src/main/res/values/strings.xml`,
severity: LogSeverity.error,
line: 4,
lineOffset: 13,
msg: `esp is not translated in (Thai)`,
log: `<string name=esp>My Application</string>`,
valid: true,
type: 'androidlint',
});

expect(result[2]).toEqual({
ruleId: 'SetJavaScriptEnabled',
source: `app/src/main/java/com/example/app/MainActivity.kt`,
severity: LogSeverity.warning,
line: 16,
lineOffset: 9,
msg: `Using \`setJavaScriptEnabled\` can introduce XSS vulnerabilities into your application, review carefully`,
log: `webView.settings.javaScriptEnabled = true`,
valid: true,
type: 'androidlint',
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/Parser/AndroidLintStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { xml2js } from 'xml-js';
import { AndroidLintStyleIssue } from './@types/AndroidLintStyleIssue';
import { AndroidLintStyleLog } from './@types/AndroidLintStyleLog';
import { AndroidLintStyleLocation } from './@types/AndroidLintStyleLocation';
import { ProjectType } from '../Config/@enums';

export class AndroidLintStyleParser extends Parser {
parse(content: string): LogType[] {
Expand All @@ -31,6 +32,7 @@ export class AndroidLintStyleParser extends Parser {
cwd: string,
): LogType {
return {
ruleId: issue._attributes.id,
log: issue._attributes.errorLine1?.trim(),
line: location._attributes.line,
lineOffset: location._attributes.column,
Expand All @@ -40,6 +42,7 @@ export class AndroidLintStyleParser extends Parser {
issue._attributes.severity.toLowerCase(),
),
valid: true,
type: ProjectType.androidlint,
};
}

Expand Down
8 changes: 8 additions & 0 deletions src/Parser/DartLintParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,51 @@ describe('DartLintStyleParser', () => {
expect(result).toHaveLength(4);

expect(result[0]).toEqual({
ruleId: 'unused_import',
source: 'api/modules/lib/auth/auth.dart',
severity: LogSeverity.info,
line: 1,
lineOffset: 8,
msg: `Unused import: 'dart:async'`,
log: `unused_import`,
valid: true,
type: 'dartlint',
});

expect(result[1]).toEqual({
ruleId: 'await_only_futures',
source: `lib/domain/providers/sharable_images_repo.dart`,
severity: LogSeverity.info,
line: 114,
lineOffset: 5,
msg: `'await' applied to 'void', which is not a 'Future'`,
log: `await_only_futures`,
valid: true,
type: 'dartlint',
});

expect(result[2]).toEqual({
ruleId: 'sort_child_properties_last',
source: `lib/presentation/widgets/platform_flat_button.dart`,
severity: LogSeverity.error,
line: 34,
lineOffset: 9,
msg: `Sort child properties last in widget instance creations`,
log: `sort_child_properties_last`,
valid: true,
type: 'dartlint',
});

expect(result[3]).toEqual({
ruleId: 'invalid_annotation_target',
source: `test_driver/tests/offline/offline_test.dart`,
severity: LogSeverity.error,
line: 13,
lineOffset: 2,
msg: `The annotation 'Timeout' can only be used on libraries`,
log: `invalid_annotation_target`,
valid: true,
type: 'dartlint',
});
});

Expand Down
5 changes: 5 additions & 0 deletions src/Parser/DartLintParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Parser } from './@interfaces/parser.interface';
import { LogType } from './@types';
import { LogSeverity } from './@enums/log.severity.enum';
import { splitByLine } from './utils/lineBreak.util';
import { ProjectType } from '../Config/@enums';

export class DartLintParser extends Parser {
parse(content: string): LogType[] {
Expand All @@ -18,13 +19,15 @@ export class DartLintParser extends Parser {
private static lineMatchToLog(lineMatch: RegExpMatchArray): LogType {
const [, severityText, message, source, line, offset, log] = lineMatch;
return {
ruleId: log,
log: log,
line: Number(line),
lineOffset: Number(offset),
msg: message,
source: source,
severity: DartLintParser.stringToSeverity(severityText),
valid: true,
type: ProjectType.dartlint,
};
}

Expand All @@ -42,10 +45,12 @@ export class DartLintParser extends Parser {
}

private static emptyLog: LogType = {
ruleId: '',
log: '',
msg: '',
severity: LogSeverity.unknown,
source: '',
valid: false,
type: ProjectType.dartlint,
};
}
6 changes: 6 additions & 0 deletions src/Parser/DotnetBuildParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,47 @@ describe('DotnetBuildParser tests', () => {
const result = new DotnetBuildParser(cwdWin).parse(logWithSource);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
ruleId: 'AG0030',
source: `Broken.cs`,
severity: LogSeverity.warning,
line: 6,
lineOffset: 8,
msg: `AG0030: Prevent use of dynamic`,
log: logWithSource,
valid: true,
type: 'dotnetbuild',
} as LogType);
});

it('Should parse log without source path correctly and flag as invalid and use csproj as source', () => {
const result = new DotnetBuildParser(cwdWin).parse(logWithNoSource);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
ruleId: 'CS5001',
source: `Broken.csproj`,
severity: LogSeverity.error,
line: NaN,
lineOffset: NaN,
msg: `CS5001: Program does not contain a static 'Main' method suitable for an entry point`,
log: logWithNoSource,
valid: false,
type: 'dotnetbuild',
} as LogType);
});

it('Should parse log unrelated source path correctly and flag as invalid and use csproj as source', () => {
const result = new DotnetBuildParser(cwdUnix).parse(logWithUnrelatedSource);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
ruleId: 'MSB3277',
source: `project.csproj`,
severity: LogSeverity.warning,
line: 2084,
lineOffset: 5,
msg: `MSB3277: some message`,
log: logWithUnrelatedSource,
valid: false,
type: 'dotnetbuild',
} as LogType);
});

Expand Down
3 changes: 3 additions & 0 deletions src/Parser/DotnetBuildParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Parser } from './@interfaces/parser.interface';
import { LogType } from './@types';
import { mapSeverity } from './utils/dotnetSeverityMap';
import { splitByLine } from './utils/lineBreak.util';
import { ProjectType } from '../Config/@enums';

export class DotnetBuildParser extends Parser {
parse(content: string): LogType[] {
Expand Down Expand Up @@ -45,13 +46,15 @@ export class DotnetBuildParser extends Parser {
}

return {
ruleId: errorCode,
log,
line: Number(_line),
lineOffset: Number(_lineOffset),
msg: `${errorCode.trim()}: ${content.trim()}`,
source: relativeSrcPath ?? basename(slash(_csproj)),
severity: mapSeverity(severityText),
valid: !!relativeSrcPath,
type: ProjectType.dotnetbuild,
};
}
}
4 changes: 4 additions & 0 deletions src/Parser/ESLintParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,27 @@ describe('ESLintParser', () => {
expect(result).toHaveLength(2);

expect(result[0]).toEqual({
ruleId: '',
source: '',
severity: LogSeverity.error,
line: 59,
lineOffset: 8,
msg: `Parsing error: ')' expected.`,
log: JSON.stringify(mockedContent[0].messages[0]),
valid: false,
type: 'eslint',
});

expect(result[1]).toEqual({
ruleId: '@typescript-eslint/no-unused-vars',
source: `src/app.ts`,
severity: LogSeverity.warning,
line: 24,
lineOffset: 15,
msg: `'content' is defined but never used.`,
log: JSON.stringify(mockedContent[1].messages[0]),
valid: true,
type: 'eslint',
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/Parser/ESLintParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProjectType } from '../Config/@enums';
import { Log } from '../Logger';
import { getRelativePath } from '../Provider/utils/path.util';
import { LogSeverity } from './@enums/log.severity.enum';
Expand All @@ -24,13 +25,15 @@ export class ESLintParser extends Parser {

private static toLog(log: ESLintIssue, source: string | null): LogType {
return {
ruleId: log.ruleId ?? '',
log: JSON.stringify(log),
line: log.line,
lineOffset: log.column,
msg: log.message,
source: source ?? '',
severity: ESLintParser.getSeverity(log.severity),
valid: source !== null,
type: ProjectType.eslint,
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/Parser/MSBuildParser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LogSeverity } from './@enums/log.severity.enum';
import { MSBuildParser } from './MSBuildParser';
import * as fs from 'fs/promises';

describe('MSBuildParser tests', () => {
const cwd = 'C:\\source';
Expand All @@ -12,13 +11,15 @@ describe('MSBuildParser tests', () => {

expect(result).toHaveLength(1);
expect(result[0]).toEqual({
ruleId: 'CS0414',
source: `Project/Service/Provider.cs`,
severity: LogSeverity.warning,
line: 67,
lineOffset: 29,
msg: `CS0414: The field 'Data.field' is assigned but its value is never used`,
log,
valid: true,
type: 'msbuild',
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/Parser/MSBuildParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Parser } from './@interfaces/parser.interface';
import { LogType } from './@types';
import { mapSeverity } from './utils/dotnetSeverityMap';
import { splitByLine } from './utils/lineBreak.util';
import { ProjectType } from '../Config/@enums';

export class MSBuildParser extends Parser {
parse(content: string): LogType[] {
Expand Down Expand Up @@ -43,13 +44,15 @@ export class MSBuildParser extends Parser {
const fileRelativePath = getRelativePath(this.cwd, fileFullPath);

return {
ruleId: code,
log,
line: Number(_line),
lineOffset: Number(_lineOffset),
msg: `${code.trim()}: ${content.trim()}`,
source: fileRelativePath ?? fileFullPath,
severity: mapSeverity(severityText),
valid: fileRelativePath !== null,
type: ProjectType.msbuild,
};
}
}
8 changes: 8 additions & 0 deletions src/Parser/ScalaStyleParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,51 @@ describe('ScalaStyleParser', () => {
expect(result).toHaveLength(4);

expect(result[0]).toEqual({
ruleId: 'some.gibberish.text.that.i.dont.wanna.keep.it',
source: '',
severity: LogSeverity.error,
line: 53,
lineOffset: 4,
msg: `Avoid mutable fields`,
log: `<error column="4" line="53" source="some.gibberish.text.that.i.dont.wanna.keep.it" severity="error" message="Avoid mutable fields"/>`,
valid: false,
type: 'scalastyle',
});

expect(result[1]).toEqual({
ruleId: '',
source: `src/main/scala/code/dir/subdir/code-a.scala`,
severity: LogSeverity.error,
line: undefined,
lineOffset: undefined,
msg: `illegal start of definition: Token(VARID,yplTaxWithValue,1704,yplTaxWithValue)`,
log: `<error severity="error" message="illegal start of definition: Token(VARID,yplTaxWithValue,1704,yplTaxWithValue)"/>`,
valid: true,
type: 'scalastyle',
});

expect(result[2]).toEqual({
ruleId: 'some.gibberish.text.that.i.dont.wanna.keep.it',
source: `src/main/scala/code/dir/subdir/code-a.scala`,
severity: LogSeverity.error,
line: 7,
lineOffset: 7,
msg: `Number of methods in class exceeds 30`,
log: `<error column="7" line="7" source="some.gibberish.text.that.i.dont.wanna.keep.it" severity="error" message="Number of methods in class exceeds 30"/>`,
valid: true,
type: 'scalastyle',
});

expect(result[3]).toEqual({
ruleId: 'some.gibberish.text.that.i.dont.wanna.keep.it',
source: `src/main/scala/code/code-c.scala`,
severity: LogSeverity.warning,
line: 207,
lineOffset: 6,
msg: `Avoid mutable local variables`,
log: `<error column="6" line="207" source="some.gibberish.text.that.i.dont.wanna.keep.it" severity="warning" message="Avoid mutable local variables"/>`,
valid: true,
type: 'scalastyle',
});
});

Expand Down
Loading

0 comments on commit e9ff8e2

Please sign in to comment.