Skip to content

Commit

Permalink
Merge pull request #42 from mrseanryan/fix/issue-38-multicomponent-pa…
Browse files Browse the repository at this point in the history
…th-from-tsconfig-fails-on-windows

Fix multicomponent path from tsconfig fails on windows [fixes unit tests also]
  • Loading branch information
mrseanryan authored Jan 10, 2021
2 parents 65dba84 + b0fc047 commit 23e0fd4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
27 changes: 16 additions & 11 deletions src/utils/AbsoluteImportResolver.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Dirent, existsSync } from "fs";
import * as path from "path";

import { DirUtils } from "./DirUtils";
import { ImportsBetweenPackagesRuleConfig } from "../tslint-folders";
import { DirUtils } from "./DirUtils";
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 {
function removeTrailingSlashStarFrom(packagePath: string): string {
return packagePath.replace(/\/\*$/, "");
}

export function resolvePathToPackageName(
filePath: string,
tsConfig: TsConfig,
Expand All @@ -19,24 +23,25 @@ export namespace AbsoluteImportResolver {
const packageNames = Object.keys(tsConfig.paths);

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

if (
paths.some(partialPath => {
paths.some((partialPath) => {
const absolutePaths = tsConfig.include
.map(include => {
return path.join(tsConfig.baseUrl, include, partialPath);
.map((include) => {
const joined = path.join(tsConfig.baseUrl, include, partialPath);
return DirUtils.convertWindowsToUnix(joined); // needs to be Unix style, to handle multicomponent paths like 'my-editor/api'
})
.filter(p => existsSync(p));
.filter((p) => existsSync(p));

return absolutePaths.some(abs => filePath.startsWith(abs));
return absolutePaths.some((abs) => filePath.startsWith(abs));
})
) {
if (resolvedToPackageName && IS_DEBUG_ENABLED) {
console.warn(`Multiple configured paths matching to path '${filePath}'`);
} else {
resolvedToPackageName = packageName.replace(/\/\*$/, "");
resolvedToPackageName = removeTrailingSlashStarFrom(packageName);
}
}
});
Expand All @@ -57,7 +62,7 @@ export namespace AbsoluteImportResolver {
const dirs = DirUtils.splitPath(pathToSplit);
let packageName: string | null = null;

dirs.forEach(dir => {
dirs.forEach((dir) => {
if (PackageConfigHelper.hasPackage(config, dir)) {
if (packageName === null) {
// take the 1st recognised folder:
Expand Down
16 changes: 14 additions & 2 deletions src/utils/DirUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ 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;
}

export function convertWindowsToUnix(filePath: string): string {
return filePath.split("\\").join("/");
}
}
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 23e0fd4

Please sign in to comment.