Added logic to filter error messages from results based on disable ne… (#59)

* Added logic to filter error messages from results based on disable next line rule

* Prettier fix

* NIT Fix

Co-authored-by: Vivek Patil <vipati@microsoft.com>
This commit is contained in:
vipati 2020-12-09 13:23:58 -08:00 коммит произвёл GitHub
Родитель 36c88dcde7
Коммит 094ff98ebc
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 93 добавлений и 13 удалений

Просмотреть файл

@ -56,10 +56,8 @@ export class FileContext {
get ignoredChecksByLine(): Map<number, string[]> { get ignoredChecksByLine(): Map<number, string[]> {
if (this._parsedIgnoreChecksByLine) return this._ignoredChecksByLine; if (this._parsedIgnoreChecksByLine) return this._ignoredChecksByLine;
this.content.split(/\r?\n/).forEach((n, lineNumber) => {
this.source.forEachChild(n => { const trimmedNodeText = n.trim();
const lineNumber = this.source.getLineAndCharacterOfPosition(n.pos).line;
const trimmedNodeText = n.getFullText().trim();
let ignoredChecks: string[] = []; let ignoredChecks: string[] = [];
trimmedNodeText.match(/boll-disable-next-line.*/g)?.forEach(line => { trimmedNodeText.match(/boll-disable-next-line.*/g)?.forEach(line => {
@ -71,7 +69,9 @@ export class FileContext {
.forEach(rule => ignoredChecks.push(rule)); .forEach(rule => ignoredChecks.push(rule));
} }
}); });
if (ignoredChecks.length > 0) this._ignoredChecksByLine.set(lineNumber, ignoredChecks);
if (ignoredChecks.length > 0) this._ignoredChecksByLine.set(lineNumber + 1, ignoredChecks);
lineNumber = lineNumber + 1;
}); });
this._parsedIgnoreChecksByLine = true; this._parsedIgnoreChecksByLine = true;

Просмотреть файл

@ -1,11 +1,12 @@
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import { asBollDirectory } from "./boll-directory"; import { asBollDirectory } from "./boll-directory";
import { getSourceFile } from "./file-context"; import { FileContext, getSourceFile } from "./file-context";
import { Logger } from "./logger"; import { Logger } from "./logger";
import { Package } from "./package"; import { Package } from "./package";
import { ResultSet } from "./result-set"; import { Failure, Result, ResultSet } from "./result-set";
import { RuleSet } from "./rule-set"; import { RuleSet } from "./rule-set";
import { ResultStatus } from "./types";
import { promisify } from "util"; import { promisify } from "util";
const readFileAsync = promisify(fs.readFile); const readFileAsync = promisify(fs.readFile);
@ -38,7 +39,8 @@ export class Suite {
sourceFiles.forEach(async s => { sourceFiles.forEach(async s => {
if (s.shouldSkip(r)) return; if (s.shouldSkip(r)) return;
const results = await r.check(s); const results = await r.check(s);
resultSet.add(results); const filterResults = await this.filterIgnoredChecksByLine(results, s);
resultSet.add(filterResults);
}); });
}); });
return true; return true;
@ -56,4 +58,24 @@ export class Suite {
throw e; throw e;
} }
} }
async filterIgnoredChecksByLine(results: Result[], sourceFile: FileContext) {
const ignoredChecksByLine = sourceFile.ignoredChecksByLine;
const filteredResults: Result[] = [];
results.forEach(l => {
if (l.status === ResultStatus.failure) {
const failure = l as Failure;
const skipLineNumber = failure.line - 1;
if (
!(
ignoredChecksByLine.has(skipLineNumber) &&
ignoredChecksByLine.get(skipLineNumber)?.includes(failure.ruleName)
)
) {
filteredResults.push(l);
}
}
});
return filteredResults;
}
} }

Просмотреть файл

@ -6,5 +6,6 @@ import { test as GitUtilsTest } from "./git-utils.test";
import { test as GlobTest } from "./glob.test"; import { test as GlobTest } from "./glob.test";
import { test as IgnoreTest } from "./ignore.test"; import { test as IgnoreTest } from "./ignore.test";
import { test as PragmaTest } from "./pragma.test"; import { test as PragmaTest } from "./pragma.test";
import { test as SuiteTest } from "./suite.test";
suite(ConfigTest, FormatTest, GitUtilsTest, GlobTest, IgnoreTest, PragmaTest); suite(ConfigTest, FormatTest, GitUtilsTest, GlobTest, IgnoreTest, PragmaTest, SuiteTest);

Просмотреть файл

@ -19,11 +19,11 @@ test("should keep track of multiple disabled rules in a FileContext", async () =
}); });
}); });
test("should keep track of a disabled next line rules in a FileContext", async () => { test("should keep track of a disabled next line rule in a FileContext", async () => {
await inFixtureDir("standalone-source-files", __dirname, async cwd => { await inFixtureDir("standalone-source-files", __dirname, async cwd => {
const sut = await getSourceFile(cwd, "simple-disable-next-line.ts", new Package({}, {})); const sut = await getSourceFile(cwd, "simple-disable-next-line.ts", new Package({}, {}));
const expectedResult = new Map(); const expectedResult = new Map();
expectedResult.set(1, ["MadeUpCheckName"]); expectedResult.set(3, ["MadeUpCheckName"]);
assert.deepStrictEqual(sut.ignoredChecksByLine, expectedResult); assert.deepStrictEqual(sut.ignoredChecksByLine, expectedResult);
}); });
}); });
@ -32,8 +32,8 @@ test("should keep track of a disabled next line rules in a FileContext", async (
await inFixtureDir("standalone-source-files", __dirname, async cwd => { await inFixtureDir("standalone-source-files", __dirname, async cwd => {
const sut = await getSourceFile(cwd, "multiple-disable-next-line.ts", new Package({}, {})); const sut = await getSourceFile(cwd, "multiple-disable-next-line.ts", new Package({}, {}));
const expectedResult = new Map(); const expectedResult = new Map();
expectedResult.set(0, ["MadeUpCheckName", "AlsoMadeUpName"]); expectedResult.set(1, ["MadeUpCheckName", "AlsoMadeUpName"]);
expectedResult.set(2, ["MadeUpCheckName"]); expectedResult.set(4, ["MadeUpCheckName"]);
assert.deepStrictEqual(sut.ignoredChecksByLine, expectedResult); assert.deepStrictEqual(sut.ignoredChecksByLine, expectedResult);
}); });
}); });

Просмотреть файл

@ -0,0 +1,57 @@
import * as assert from "assert";
import baretest from "baretest";
import { getSourceFile } from "../file-context";
import { inFixtureDir } from "@boll/test-internal";
import { Package } from "../package";
import { Failure, Result, ResultSet } from "../result-set";
import { Suite } from "../suite";
import { asBollLineNumber } from "../boll-line-number";
export const test: any = baretest("Source detector");
test("should skip a single disabled next line rules in a FileContext", async () => {
await inFixtureDir("standalone-source-files", __dirname, async cwd => {
const sut = await getSourceFile(cwd, "simple-disable-next-line.ts", new Package({}, {}));
const suite = new Suite();
const results: Result[] = [];
const failure1: Failure = new Failure(
"MadeUpCheckName",
sut.filename,
asBollLineNumber(4),
"Failed due to MadeUpCheckName rule"
);
results.push(failure1);
const filteredResult = await suite.filterIgnoredChecksByLine(results, sut);
assert.strictEqual(0, filteredResult.length);
});
});
test("should skip a multiple disabled next line rules in a FileContext", async () => {
await inFixtureDir("standalone-source-files", __dirname, async cwd => {
const sut = await getSourceFile(cwd, "multiple-disable-next-line.ts", new Package({}, {}));
const suite = new Suite();
const results: Result[] = [];
const failure: Failure = new Failure(
"MadeUpCheckName",
sut.filename,
asBollLineNumber(2),
"Failed due to MadeUpCheckName rule"
);
results.push(failure);
const failure1: Failure = new Failure(
"AlsoMadeUpName",
sut.filename,
asBollLineNumber(2),
"Failed due to MadeUpCheckName rule"
);
results.push(failure1);
const failure2: Failure = new Failure(
"MadeUpCheckName",
sut.filename,
asBollLineNumber(5),
"Failed due to MadeUpCheckName rule"
);
results.push(failure2);
const filteredResult = await suite.filterIgnoredChecksByLine(results, sut);
assert.strictEqual(0, filteredResult.length);
});
});