зеркало из https://github.com/microsoft/boll.git
src detector line number fix (#66)
* Fixing src-detector line numbers * Prettier fix * Removed console logs * Updated test description Co-authored-by: Vivek Patil <vipati@microsoft.com>
This commit is contained in:
Родитель
b3b59d6ea9
Коммит
c97d70d3cc
|
@ -0,0 +1,16 @@
|
||||||
|
import * as FakeImport from "@foo/src/FakeImport";
|
||||||
|
/*
|
||||||
|
correct import
|
||||||
|
*/
|
||||||
|
import * as CorrectImport from "@foo/foo/CorrectImport";
|
||||||
|
|
||||||
|
//SrcImport
|
||||||
|
import * as SrcImport from "@foo/src/SrcImport";
|
||||||
|
|
||||||
|
const dummyCode = "foo";
|
||||||
|
const moreDummyCode = dummyCode.split("o");
|
||||||
|
const dummyScriptPath = "../../node_modules/some-package/lib/some-script.js";
|
||||||
|
const fakeObject = new FakeImport(dummyCode, moreDummyCode, dummyScriptPath);
|
||||||
|
|
||||||
|
// This is a comment containing the word node_modules. This should not be flagged.
|
||||||
|
fakeObject.doNothing();
|
|
@ -50,11 +50,14 @@ export class SrcDetector 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 { asBollFile, ResultStatus } from "@boll/core";
|
import { asBollDirectory, getSourceFile, asBollFile, Failure, Package, ResultStatus } from "@boll/core";
|
||||||
import { SrcDetector } from "../src-detector";
|
import { SrcDetector } from "../src-detector";
|
||||||
|
import { inFixtureDir } from "@boll/test-internal";
|
||||||
export const test: any = baretest("Source detector");
|
export const test: any = baretest("Source detector");
|
||||||
|
|
||||||
test("Should pass if no `src` detected in imports", async () => {
|
test("Should pass if no `src` detected in imports", async () => {
|
||||||
|
@ -23,3 +24,17 @@ test("Should fail if `src` detected in imports", 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 if references to `src` detected in imports", async () => {
|
||||||
|
inFixtureDir("src-detector", __dirname, async () => {
|
||||||
|
const sut = new SrcDetector();
|
||||||
|
const result = await sut.check(await getSourceFile(asBollDirectory("."), "src-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, result[0].status);
|
||||||
|
assert.strictEqual(ResultStatus.failure, result[1].status);
|
||||||
|
assert.strictEqual(1, failure.line);
|
||||||
|
assert.strictEqual(8, failure1.line);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Загрузка…
Ссылка в новой задаче