Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/try-path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as path from "path";
import { MappingEntry } from "./mapping-entry";
import { dirname } from "path";
import { removeExtension } from "./filesystem";

export interface TryPath {
readonly type: "file" | "extension" | "index" | "package";
export type TryPath = {
readonly type: "file" | "index" | "package";
readonly path: string;
}
} | {
readonly type: "extension";
readonly path: string;
readonly extensionLength: number;
};

/**
* Builds a list of all physical paths to try by:
Expand Down Expand Up @@ -41,7 +44,7 @@ export function getPathsToTry(
pathsToTry.push({ type: "file", path: physicalPath });
pathsToTry.push(
...extensions.map(
e => ({ type: "extension", path: physicalPath + e } as TryPath)
e => ({ type: "extension", path: physicalPath + e, extensionLength: e.length } as TryPath)
)
);
pathsToTry.push({
Expand All @@ -67,7 +70,7 @@ export function getStrippedPath(tryPath: TryPath): string {
: tryPath.type === "file"
? tryPath.path
: tryPath.type === "extension"
? removeExtension(tryPath.path)
? tryPath.path.slice(0, -tryPath.extensionLength)
: tryPath.type === "package"
? tryPath.path
: exhaustiveTypeException(tryPath.type);
Expand All @@ -80,7 +83,7 @@ export function exhaustiveTypeException(check: never): never {
/**
* Matches pattern with a single star against search.
* Star must match at least one character to be considered a match.
* @param patttern for example "foo*"
* @param patttern for example "foo*"
* @param search for example "fooawesomebar"
* @returns the part of search that * matches, or undefined if no match.
*/
Expand Down
18 changes: 14 additions & 4 deletions test/try-path-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ describe("mapping-entry", () => {
{ type: "file", path: join("/absolute", "base", "url", "foo2", "bar") },
{
type: "extension",
path: join("/absolute", "base", "url", "foo2", "bar.ts")
path: join("/absolute", "base", "url", "foo2", "bar.ts"),
extensionLength: 3
},
{
type: "extension",
path: join("/absolute", "base", "url", "foo2", "bar.tsx")
path: join("/absolute", "base", "url", "foo2", "bar.tsx"),
extensionLength: 4
},
{
type: "package",
Expand All @@ -69,8 +71,16 @@ describe("mapping-entry", () => {
},
// "*"
{ type: "file", path: join("/absolute", "base", "url", "foo1") },
{ type: "extension", path: join("/absolute", "base", "url", "foo1.ts") },
{ type: "extension", path: join("/absolute", "base", "url", "foo1.tsx") },
{
type: "extension",
path: join("/absolute", "base", "url", "foo1.ts"),
extensionLength: 3
},
{
type: "extension",
path: join("/absolute", "base", "url", "foo1.tsx"),
extensionLength: 4
},
{
type: "package",
path: join("/absolute", "base", "url", "foo1", "package.json")
Expand Down