From f69ad608cc4ed2a378d09942ef4297980301d404 Mon Sep 17 00:00:00 2001 From: str Date: Sun, 10 Jan 2021 22:04:46 +0100 Subject: [PATCH 1/2] fix: fixes #38 by handling Windows dir separator before comparing to TS file path --- src/utils/AbsoluteImportResolver.ts | 21 +++++++++++---------- src/utils/DirUtils.ts | 4 ++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/utils/AbsoluteImportResolver.ts b/src/utils/AbsoluteImportResolver.ts index 680f2d9..bfa888a 100644 --- a/src/utils/AbsoluteImportResolver.ts +++ b/src/utils/AbsoluteImportResolver.ts @@ -1,10 +1,10 @@ +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; @@ -19,18 +19,19 @@ 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((x) => x.replace(/\/\*$/, "")); 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) { @@ -57,7 +58,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: diff --git a/src/utils/DirUtils.ts b/src/utils/DirUtils.ts index 6d85825..8b53c12 100644 --- a/src/utils/DirUtils.ts +++ b/src/utils/DirUtils.ts @@ -11,4 +11,8 @@ export namespace DirUtils { return cleaned; } + + export function convertWindowsToUnix(filePath: string): string { + return filePath.split("\\").join("/"); + } } From b0fc0475a1361dfcdd7e47682082e65a72004bd8 Mon Sep 17 00:00:00 2001 From: str Date: Sun, 10 Jan 2021 22:12:28 +0100 Subject: [PATCH 2/2] chore: extract some funs to make code clearer --- src/utils/AbsoluteImportResolver.ts | 8 ++++++-- src/utils/DirUtils.ts | 12 ++++++++++-- src/utils/ImportRuleUtils.ts | 25 +++++++++++-------------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/utils/AbsoluteImportResolver.ts b/src/utils/AbsoluteImportResolver.ts index bfa888a..d25250b 100644 --- a/src/utils/AbsoluteImportResolver.ts +++ b/src/utils/AbsoluteImportResolver.ts @@ -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, @@ -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) => { @@ -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); } } }); diff --git a/src/utils/DirUtils.ts b/src/utils/DirUtils.ts index 8b53c12..c1eeaa8 100644 --- a/src/utils/DirUtils.ts +++ b/src/utils/DirUtils.ts @@ -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; } diff --git a/src/utils/ImportRuleUtils.ts b/src/utils/ImportRuleUtils.ts index 897759e..986f46f 100644 --- a/src/utils/ImportRuleUtils.ts +++ b/src/utils/ImportRuleUtils.ts @@ -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"; @@ -14,7 +13,7 @@ import { TsConfig } from "./TsConfigParser"; export enum PathSource { SourceFilePath, - ImportText + ImportText, } export type PackageLocation = { @@ -46,7 +45,7 @@ export namespace ImportRuleUtils { !PackageConfigHelper.hasPackage(config, packageName)) ) { return { - packageName: filePath + packageName: filePath, }; } @@ -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, }; } @@ -124,7 +123,7 @@ export namespace ImportRuleUtils { return { packageFolder: packageFolder, - packageName: activePackageName + packageName: activePackageName, }; } @@ -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; @@ -216,11 +215,9 @@ export namespace ImportRuleUtils { } break; case PathSource.SourceFilePath: { - // xxx - let hasPackageDir = false; - dirs.forEach(dir => { + dirs.forEach((dir) => { if (packageSubFolder) { return; } @@ -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; @@ -252,7 +249,7 @@ export namespace ImportRuleUtils { return { packageName: activePackageName, packageFolder: packageFolder, - packageSubFolder: packageSubFolder + packageSubFolder: packageSubFolder, }; } @@ -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); } }