Skip to content

Commit

Permalink
chore: extract some funs to make code clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
mrseanryan committed Jan 10, 2021
1 parent f69ad60 commit b0fc047
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/utils/AbsoluteImportResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const IS_DEBUG_ENABLED = false;

// Resolves an absolute file path, to a 'package' path, as specified in tsconfig.json
export namespace AbsoluteImportResolver {
function removeTrailingSlashStarFrom(packagePath: string): string {
return packagePath.replace(/\/\*$/, "");
}

export function resolvePathToPackageName(
filePath: string,
tsConfig: TsConfig,
Expand All @@ -20,7 +24,7 @@ export namespace AbsoluteImportResolver {

let resolvedToPackageName: string | null = null;
packageNames.forEach((packageName) => {
const paths = tsConfig.paths[packageName].map((x) => x.replace(/\/\*$/, ""));
const paths = tsConfig.paths[packageName].map(removeTrailingSlashStarFrom);

if (
paths.some((partialPath) => {
Expand All @@ -37,7 +41,7 @@ export namespace AbsoluteImportResolver {
if (resolvedToPackageName && IS_DEBUG_ENABLED) {
console.warn(`Multiple configured paths matching to path '${filePath}'`);
} else {
resolvedToPackageName = packageName.replace(/\/\*$/, "");
resolvedToPackageName = removeTrailingSlashStarFrom(packageName);
}
}
});
Expand Down
12 changes: 10 additions & 2 deletions src/utils/DirUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ export namespace DirUtils {
return cleanPath(filePath).split("/");
}

function removeQuotesAndTicksFrom(filePath: string): string {
return filePath.replace(/['"]+/g, "");
}

function replaceRepeatedBackslashesIn(filePath: string): string {
return filePath.replace(/[\\]+/g, "\\");
}

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

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

return cleaned;
}
Expand Down
25 changes: 11 additions & 14 deletions src/utils/ImportRuleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import * as ts from "typescript";
import {
ImportsBetweenPackagesRuleConfig,
PackageFolder,
PackageSubFolder
PackageSubFolder,
} from "../model/ImportsBetweenPackagesRuleConfig";

import { AbsoluteImportResolver } from "./AbsoluteImportResolver";
import { DirUtils } from "./DirUtils";
import { GeneralRuleUtils } from "./GeneralRuleUtils";
Expand All @@ -14,7 +13,7 @@ import { TsConfig } from "./TsConfigParser";

export enum PathSource {
SourceFilePath,
ImportText
ImportText,
}

export type PackageLocation = {
Expand Down Expand Up @@ -46,7 +45,7 @@ export namespace ImportRuleUtils {
!PackageConfigHelper.hasPackage(config, packageName))
) {
return {
packageName: filePath
packageName: filePath,
};
}

Expand All @@ -63,14 +62,14 @@ export namespace ImportRuleUtils {

if (!packageFolderAndName.packageFolder) {
return {
packageName: packageName
packageName: packageName,
};
}

if (packageFolderAndName.packageFolder.subFolders.length === 0) {
return {
packageName: packageName,
packageFolder: packageFolderAndName.packageFolder
packageFolder: packageFolderAndName.packageFolder,
};
}

Expand Down Expand Up @@ -124,7 +123,7 @@ export namespace ImportRuleUtils {

return {
packageFolder: packageFolder,
packageName: activePackageName
packageName: activePackageName,
};
}

Expand Down Expand Up @@ -203,7 +202,7 @@ export namespace ImportRuleUtils {
if (isImportingFromSubFolder) {
for (let i = 1; i < dirs.length; i++) {
const subFolder = packageFolder.subFolders.find(
folder => folder.importPath === dirs[i]
(folder) => folder.importPath === dirs[i]
);
if (subFolder) {
packageSubFolder = subFolder;
Expand All @@ -216,11 +215,9 @@ export namespace ImportRuleUtils {
}
break;
case PathSource.SourceFilePath: {
// xxx

let hasPackageDir = false;

dirs.forEach(dir => {
dirs.forEach((dir) => {
if (packageSubFolder) {
return;
}
Expand All @@ -233,7 +230,7 @@ export namespace ImportRuleUtils {
{
if (hasPackageDir) {
const subFolder = packageFolder!.subFolders.find(
folder => folder.importPath === dir
(folder) => folder.importPath === dir
);
if (subFolder) {
packageSubFolder = subFolder;
Expand All @@ -252,7 +249,7 @@ export namespace ImportRuleUtils {
return {
packageName: activePackageName,
packageFolder: packageFolder,
packageSubFolder: packageSubFolder
packageSubFolder: packageSubFolder,
};
}

Expand Down Expand Up @@ -311,6 +308,6 @@ export namespace ImportRuleUtils {
}

export function shouldIgnorePath(path: string, ignorePaths: string[]): boolean {
return ignorePaths.some(ignore => path.indexOf(ignore) >= 0);
return ignorePaths.some((ignore) => path.indexOf(ignore) >= 0);
}
}

0 comments on commit b0fc047

Please sign in to comment.