From 145a0a04b6a91d23f4d7f16f01679197da2c3dd6 Mon Sep 17 00:00:00 2001 From: Yury Bondarenko Date: Thu, 25 Apr 2024 11:37:28 +0200 Subject: [PATCH] Fix lint plugins & tests on Windows --- lint/src/util/misc.ts | 12 ++++++++++++ lint/src/util/theme-support.ts | 14 +------------- lint/src/util/typescript.ts | 11 +++++++++-- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lint/src/util/misc.ts b/lint/src/util/misc.ts index 47357e7cd31..49cb60124e8 100644 --- a/lint/src/util/misc.ts +++ b/lint/src/util/misc.ts @@ -14,3 +14,15 @@ export function match(rangeA: number[], rangeB: number[]) { export function stringLiteral(value: string): string { return `'${value}'`; } + +/** + * Transform Windows-style paths into Unix-style paths + */ +export function toUnixStylePath(path: string): string { + // note: we're assuming that none of the directory/file names contain '\' or '/' characters. + // using these characters in paths is very bad practice in general, so this should be a safe assumption. + if (path.includes('\\')) { + return path.replace(/^[A-Z]:\\/, '/').replaceAll('\\', '/'); + } + return path; +} diff --git a/lint/src/util/theme-support.ts b/lint/src/util/theme-support.ts index 1f74b315041..64644145fae 100644 --- a/lint/src/util/theme-support.ts +++ b/lint/src/util/theme-support.ts @@ -16,8 +16,6 @@ import { isPartOfViewChild, } from './angular'; import { - AnyRuleContext, - getFilename, isPartOfClassDeclaration, isPartOfTypeExpression, } from './typescript'; @@ -152,6 +150,7 @@ class ThemeableComponentRegistry { const glob = require('glob'); + // note: this outputs Unix-style paths on Windows const wrappers: string[] = glob.GlobSync(prefix + 'src/app/**/themed-*.component.ts', { ignore: 'node_modules/**' }).found; for (const wrapper of wrappers) { @@ -245,17 +244,6 @@ export function inThemedComponentOverrideFile(filename: string): boolean { return themeableComponents.byBasePath.has(`src/${match[1]}`); } -export function inThemedComponentFile(context: AnyRuleContext): boolean { - themeableComponents.initialize(); - const filename = getFilename(context); - - return [ - () => themeableComponents.byBasePath.has(filename), - () => themeableComponents.byWrapperPath.has(filename), - () => inThemedComponentOverrideFile(filename), - ].some(predicate => predicate()); -} - export function allThemeableComponents(): ThemeableComponentRegistryEntry[] { themeableComponents.initialize(); return [...themeableComponents.entries]; diff --git a/lint/src/util/typescript.ts b/lint/src/util/typescript.ts index c0ede13967e..3fecad270e6 100644 --- a/lint/src/util/typescript.ts +++ b/lint/src/util/typescript.ts @@ -10,14 +10,21 @@ import { TSESTree, } from '@typescript-eslint/utils'; -import { match } from './misc'; +import { + match, + toUnixStylePath, +} from './misc'; export type AnyRuleContext = TSESLint.RuleContext; +/** + * Return the current filename based on the ESLint rule context as a Unix-style path. + * This is easier for regex and comparisons to glob paths. + */ export function getFilename(context: AnyRuleContext): string { // TSESLint claims this is deprecated, but the suggested alternative is undefined (could be a version mismatch between ESLint and TSESlint?) // eslint-disable-next-line deprecation/deprecation - return context.getFilename(); + return toUnixStylePath(context.getFilename()); } export function getSourceCode(context: AnyRuleContext): TSESLint.SourceCode {