slice multiple prefixes from resolved filenames

self-references sometimes result in doubled
'node_modules/@types/self-package-name' prefixes.
This commit is contained in:
Nathan Shively-Sanders
2023-04-10 12:53:30 -07:00
parent a336a4ec64
commit ecac2cc66a
3 changed files with 12 additions and 8 deletions

View File

@@ -27,7 +27,7 @@ import {
createModuleResolutionHost,
} from "@definitelytyped/utils";
import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
import { slicePrefix } from "./utils";
import { slicePrefixes } from "./utils";
import path from "path";
function matchesVersion(
@@ -332,7 +332,7 @@ function getTypingDataForSingleTypesVersion(
path.resolve("/", fs.debugPath())
).options;
checkFilesFromTsConfig(packageName, tsconfig, fs.debugPath());
// TODO: tests should be Set<string>, not Map<string, SourceFile>
const { types, tests } = allReferencedFiles(
tsconfig.files!,
fs,
@@ -340,7 +340,7 @@ function getTypingDataForSingleTypesVersion(
moduleResolutionHost,
compilerOptions
);
const usedFiles = new Set([...types.keys(), ...tests.keys(), "tsconfig.json", "tslint.json"].map(f => slicePrefix(f, "node_modules/@types/" + packageName + "/")));
const usedFiles = new Set([...types.keys(), ...tests.keys(), "tsconfig.json", "tslint.json"].map(f => slicePrefixes(f, "node_modules/@types/" + packageName + "/")));
const otherFiles = ls.includes(unusedFilesName)
? fs
// tslint:disable-next-line:non-literal-fs-path -- Not a reference to the fs package

View File

@@ -26,6 +26,10 @@ export function withCache<T>(expiresInMs: number, getValue: () => Promise<T>): (
};
}
export function slicePrefix(s: string, prefix: string): string {
return s.startsWith(prefix) ? s.slice(prefix.length) : s;
export function slicePrefixes(s: string, prefix: string): string {
while (s.startsWith(prefix)) {
s = s.slice(prefix.length)
}
return s
}

View File

@@ -6,7 +6,7 @@ import * as semver from "semver";
import { readDataFile } from "./data-file";
import { scopeName, typesDirectoryName } from "./lib/settings";
import { parseVersionFromDirectoryName, parsePackageSemver } from "./lib/definition-parser";
import { slicePrefix } from "./lib/utils";
import { slicePrefixes } from "./lib/utils";
export class AllPackages {
static async read(dt: FS): Promise<AllPackages> {
@@ -145,14 +145,14 @@ export class AllPackages {
if (pkg.name === dtName) continue
const versions = this.data.get(dtName);
if (versions) {
yield versions.get(parsePackageSemver(version), undefined);
yield versions.get(parsePackageSemver(version), pkg.libraryName);
}
}
}
}
export function removeTypesScope(name: string) {
return slicePrefix(name, `@${scopeName}/`);
return slicePrefixes(name, `@${scopeName}/`);
}
// Same as the function in moduleNameResolver.ts in typescript