Skip to content

Commit

Permalink
Merge pull request #16 from mrseanryan/feature/handle-paths-with-same…
Browse files Browse the repository at this point in the history
…-initial-chars

Feature - handle paths from tsconfig
  • Loading branch information
mrseanryan authored Jan 14, 2020
2 parents f90aa00 + e530c24 commit 1ffa6f7
Show file tree
Hide file tree
Showing 26 changed files with 609 additions and 357 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"test:prod": "yarn build && yarn lint && yarn test -- --no-cache",
"tsl:build-and-test": "yarn build && yarn tsl:test",
"tsl:test": "tslint --test test/rules/**/tslint.json",
"tsl:test-one": "tslint --test test/rules/tsf-folders-disabled-test/**/tslint.json",
"tsl:test-one": "tslint --test test/rules/tsf-folders-imports-between-packages/**/tslint.json",
"deploy-docs": "ts-node tools/gh-pages-publish",
"report-coverage": "cat ./coverage/lcov.info | coveralls",
"commit": "git-cz",
Expand Down
17 changes: 12 additions & 5 deletions src/tsfFoldersImportsBetweenPackagesRule.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import * as Lint from "tslint";
import * as ts from "typescript";

import { ConfigFactory } from "./config/ConfigFactory";
import {
CheckImportsBetweenPackages,
ImportsBetweenPackagesRuleConfig
} from "./model/ImportsBetweenPackagesRuleConfig";
import { RuleId } from "./RuleId";
import { GeneralRuleUtils } from "./utils/GeneralRuleUtils";
import { ImportRuleUtils, PathSource } from "./utils/ImportRuleUtils";

import { ConfigFactory } from "./config/ConfigFactory";
import { GeneralRuleUtils } from "./utils/GeneralRuleUtils";
import { RuleId } from "./RuleId";
import { TsConfigParser } from "./utils/TsConfigParser";

const DISALLOW_IMPORT_FROM_SELF_MESSAGE =
"do not import a package from itself - use a relative path";

Expand Down Expand Up @@ -66,11 +68,15 @@ function validate(
- check if the import is allowed
*/

const filePath = node.getSourceFile().fileName;
const tsConfig = TsConfigParser.parseConfigNear(filePath);

const thisPackageLocation = ImportRuleUtils.determinePackageLocationFromPath(
node.getSourceFile().fileName,
filePath,
RuleId.TsfFoldersImportsBetweenPackages,
ctx.options,
PathSource.SourceFilePath
PathSource.SourceFilePath,
tsConfig
);

if (ImportRuleUtils.isThisPackageThirdParty(thisPackageLocation, node)) {
Expand All @@ -88,6 +94,7 @@ function validate(
RuleId.TsfFoldersImportsBetweenPackages,
ctx.options,
PathSource.ImportText,
tsConfig,
thisPackageLocation
);

Expand Down
76 changes: 76 additions & 0 deletions src/utils/AbsoluteImportResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as path from "path";

import { DirUtils } from "./DirUtils";
import { ImportsBetweenPackagesRuleConfig } from "../tslint-folders";
import { PackageConfigHelper } from "./PackageConfigHelper";
import { TsConfig } from "./TsConfigParser";
import { existsSync } from "fs";

export const IS_DEBUG_ENABLED = false;

// Resolves an absolute file path, to a 'package' path, as specified in tsconfig.json
export namespace AbsoluteImportResolver {
export function resolvePathToPackageName(
filePath: string,
tsConfig: TsConfig,
config: ImportsBetweenPackagesRuleConfig
): string | null {
if (tsConfig.paths) {
const packageNames = Object.keys(tsConfig.paths);

let resolvedToPackageName: string | null = null;
packageNames.forEach(packageName => {
const paths = tsConfig.paths[packageName];

if (
paths.some(partialPath => {
const absolutePaths = tsConfig.include
.map(include => {
return path.join(tsConfig.baseUrl, include, partialPath);
})
.filter(p => existsSync(p));

return absolutePaths.some(abs => filePath.startsWith(abs));
})
) {
if (resolvedToPackageName && IS_DEBUG_ENABLED) {
console.warn(`Multiple configured paths matching to path '${filePath}'`);
} else {
resolvedToPackageName = packageName;
}
}
});

if (resolvedToPackageName) {
return resolvedToPackageName;
}
}

// Fallback, in case 'paths' not set, or incomplete:
return resolvePathToPackageNameViaSplitting(filePath, config);
}

function resolvePathToPackageNameViaSplitting(
pathToSplit: string,
config: ImportsBetweenPackagesRuleConfig
): string | null {
const dirs = DirUtils.splitPath(pathToSplit);
let packageName: string | null = null;

dirs.forEach(dir => {
if (PackageConfigHelper.hasPackage(config, dir)) {
if (packageName === null) {
// take the 1st recognised folder:
packageName = dir;
} else if (IS_DEBUG_ENABLED) {
// this can occur with package names like 'utils'
console.warn(
`import has more than one recognised package: [${packageName},${dir}]`
);
}
}
});

return packageName;
}
}
14 changes: 14 additions & 0 deletions src/utils/DirUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export namespace DirUtils {
export function splitPath(filePath: string): string[] {
return cleanPath(filePath).split("/");
}

function cleanPath(filePath: string): string {
let cleaned = filePath.trim();

cleaned = cleaned.replace(/['"]+/g, "");
cleaned = cleaned.replace(/[\\]+/g, "\\");

return cleaned;
}
}
Loading

0 comments on commit 1ffa6f7

Please sign in to comment.