Fixing line numbers for boll rules (#67)

* Fixing transitive-dependency-detector line numbers

* Updated the test description

* Updated the test

* Fixing redundant-imports-detector line numbers

Co-authored-by: Vivek Patil <vipati@microsoft.com>
This commit is contained in:
vipati 2021-01-07 10:15:41 -08:00 коммит произвёл GitHub
Родитель c97d70d3cc
Коммит fadc80c745
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 77 добавлений и 4 удалений

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

@ -0,0 +1,15 @@
import { foo } from '@foo/foo-package';
// test
import { foo } from '@some/other-package';
/*
transitive
*/
import { foo } from '@some/other-package';
import { test } from '@test/test-package';
/*
Multi Comment
*/
//single comment
import { foo } from '@some/other-package';

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

@ -0,0 +1,9 @@
import { foo } from '@foo/foo-package';
// test
import { foo } from '@some/other-package';
/*
transitive
*/t
import { test } from '@test/test-package';
import { bar } from '@bar/bar-package';

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

@ -49,11 +49,14 @@ export class RedundantImportsDetector implements PackageRule {
getImportPaths(sourceFile: SourceFile): ImportPathAndLineNumber[] { getImportPaths(sourceFile: SourceFile): ImportPathAndLineNumber[] {
const importPaths: ImportPathAndLineNumber[] = []; const importPaths: ImportPathAndLineNumber[] = [];
let lineNumber = 1;
sourceFile.forEachChild(n => { sourceFile.forEachChild(n => {
const totalLines = n.getFullText().split(/\r?\n/).length;
lineNumber = lineNumber + totalLines - 1;
if (isImportDeclaration(n)) { if (isImportDeclaration(n)) {
importPaths.push({ importPaths.push({
path: n.moduleSpecifier.getText(), path: n.moduleSpecifier.getText(),
lineNumber: sourceFile.getLineAndCharacterOfPosition(n.pos).line lineNumber: lineNumber
}); });
} }
}); });

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

@ -1,7 +1,8 @@
import * as assert from "assert"; import * as assert from "assert";
import baretest from "baretest"; import baretest from "baretest";
import { RedundantImportsDetector } from "../redundant-imports-detector"; import { RedundantImportsDetector } from "../redundant-imports-detector";
import { asBollFile, ResultStatus } from "@boll/core"; import { asBollDirectory, asBollFile, getSourceFile, Failure, Package, ResultStatus } from "@boll/core";
import { inFixtureDir } from "@boll/test-internal";
export const test: any = baretest("Redunant imports detector"); export const test: any = baretest("Redunant imports detector");
test("Should pass if no redundant import paths", async () => { test("Should pass if no redundant import paths", async () => {
@ -25,3 +26,19 @@ test("Should fail if there are redundant import paths", async () => {
const result = sut.checkImportPaths(asBollFile("a"), importPaths); const result = sut.checkImportPaths(asBollFile("a"), importPaths);
assert.strictEqual(ResultStatus.failure, result[0].status); assert.strictEqual(ResultStatus.failure, result[0].status);
}); });
test("Should fail for missing imports if few imports are declared in devDependencies and devDeps mode is enabled", async () => {
await inFixtureDir("redundant-imports-detector", __dirname, async () => {
const sut = new RedundantImportsDetector();
const result = await sut.check(
await getSourceFile(asBollDirectory("."), "redundant-imports-detector.ts", new Package({}, {}))
);
const failure = result[0] as Failure;
const failure1 = result[1] as Failure;
assert.strictEqual(2, result.length);
assert.strictEqual(ResultStatus.failure, failure.status);
assert.strictEqual(8, failure.line);
assert.strictEqual(ResultStatus.failure, failure1.status);
assert.strictEqual(15, failure1.line);
});
});

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

@ -1,7 +1,7 @@
import * as assert from "assert"; import * as assert from "assert";
import baretest from "baretest"; import baretest from "baretest";
import { TransitiveDependencyDetector } from "../transitive-dependency-detector"; import { TransitiveDependencyDetector } from "../transitive-dependency-detector";
import { asBollDirectory, getSourceFile, Package, ResultStatus } from "@boll/core"; import { asBollDirectory, getSourceFile, Failure, Package, ResultStatus } from "@boll/core";
import { inFixtureDir } from "@boll/test-internal"; import { inFixtureDir } from "@boll/test-internal";
export const test: any = baretest("Transitive dep detector"); export const test: any = baretest("Transitive dep detector");
@ -51,5 +51,31 @@ test("Should fail if all imports are declared in devDependencies and devDeps mod
await getSourceFile(asBollDirectory("."), "foo.ts", new Package({}, { "@some/other-package": "0" })) await getSourceFile(asBollDirectory("."), "foo.ts", new Package({}, { "@some/other-package": "0" }))
); );
assert.strictEqual(1, result.length); assert.strictEqual(1, result.length);
const failure = result[0] as Failure;
assert.strictEqual(ResultStatus.failure, failure.status);
assert.strictEqual(1, failure.line);
});
});
test("Should fail for missing imports if few imports are declared in devDependencies and devDeps mode is enabled", async () => {
await inFixtureDir("transitive-reference", __dirname, async () => {
const sut = new TransitiveDependencyDetector({ allowDevDependencies: true });
const result = await sut.check(
await getSourceFile(
asBollDirectory("."),
"transitive-reference.ts",
new Package({}, { "@some/other-package": "0" })
)
);
const failure = result[0] as Failure;
const failure1 = result[1] as Failure;
const failure2 = result[2] as Failure;
assert.strictEqual(3, result.length);
assert.strictEqual(ResultStatus.failure, failure.status);
assert.strictEqual(1, failure.line);
assert.strictEqual(ResultStatus.failure, failure1.status);
assert.strictEqual(8, failure1.line);
assert.strictEqual(ResultStatus.failure, failure2.status);
assert.strictEqual(9, failure2.line);
}); });
}); });

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

@ -74,11 +74,14 @@ export class TransitiveDependencyDetector implements PackageRule {
getModuleImports(sourceFile: SourceFile): ImportPathAndLineNumber[] { getModuleImports(sourceFile: SourceFile): ImportPathAndLineNumber[] {
const importPaths: ImportPathAndLineNumber[] = []; const importPaths: ImportPathAndLineNumber[] = [];
let lineNumber = 1;
sourceFile.forEachChild(n => { sourceFile.forEachChild(n => {
const totalLines = n.getFullText().split(/\r?\n/).length;
lineNumber = lineNumber + totalLines - 1;
if (isImportDeclaration(n)) { if (isImportDeclaration(n)) {
const path = this.getPathFromNode(n); const path = this.getPathFromNode(n);
if (!path.startsWith(".")) { if (!path.startsWith(".")) {
importPaths.push({ path, lineNumber: sourceFile.getLineAndCharacterOfPosition(n.pos).line }); importPaths.push({ path, lineNumber: lineNumber });
} }
} }
}); });