From 96327dd38f8d61483d0f12e9a03d7b44e2738b48 Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Mon, 29 Oct 2018 12:16:51 -0400 Subject: [PATCH] fix path mapping checks for scoped and versiond packages --- src/lib/definition-parser.ts | 11 +++++++---- src/util/util.ts | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/lib/definition-parser.ts b/src/lib/definition-parser.ts index 2db210ed..491f6c91 100644 --- a/src/lib/definition-parser.ts +++ b/src/lib/definition-parser.ts @@ -3,7 +3,7 @@ import * as ts from "typescript"; import { FS } from "../get-definitely-typed"; import { - computeHash, filter, flatMap, hasWindowsSlashes, join, mapAsyncOrdered, mapDefined, split, unique, unmangleScopedPackage, withoutStart, + computeHash, filter, flatMap, hasWindowsSlashes, join, mapAsyncOrdered, mapDefined, split, stripVersion, unique, unmangleScopedPackage, withoutStart, } from "../util/util"; import getModuleInfo, { getTestDependencies } from "./module-info"; @@ -282,8 +282,8 @@ async function calculateDependencies( } const pathMapping = pathMappingList[0]; - // Path mapping may be for "@foo/bar" -> "foo__bar". - const scopedPackageName = unmangleScopedPackage(pathMapping); + // Path mapping may be for "@foo/bar" -> "foo__bar" or "@foo/bar" -> "foo__bar/v2" + const scopedPackageName = unmangleScopedPackage(stripVersion(pathMapping)); if (scopedPackageName !== undefined) { if (dependencyName !== scopedPackageName) { throw new Error(`Expected directory ${pathMapping} to be the path mapping for ${dependencyName}`); @@ -308,7 +308,10 @@ async function calculateDependencies( pathMappings.push({ packageName: dependencyName, majorVersion }); } - if (oldMajorVersion !== undefined && !(paths && packageName in paths)) { + // Path mapping may be for "foo" -> "foo/v2" or "@foo/bar" -> "foo__bar/v2" + const scopedName = unmangleScopedPackage(packageName); + const hasSelfPathMapping = scopedName !== undefined ? scopedName in paths : packageName in paths; + if (oldMajorVersion !== undefined && !hasSelfPathMapping) { throw new Error(`${packageName}: Older version ${oldMajorVersion} must have a path mapping for itself.`); } diff --git a/src/util/util.ts b/src/util/util.ts index 77b7cedf..480e5797 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -436,6 +436,11 @@ export function unmangleScopedPackage(packageName: string): string | undefined { return packageName.includes(separator) ? `@${packageName.replace(separator, "/")}` : undefined; } +const versionAtEndRegex = /\/v\d+$/; +export function stripVersion(packageName: string): string { + return packageName.replace(versionAtEndRegex, ""); +} + /** Returns [values that cb returned undefined for, defined results of cb]. */ export function split(inputs: ReadonlyArray, cb: (t: T) => U | undefined): [ReadonlyArray, ReadonlyArray] { const keep: T[] = [];