Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly strip .d.ts file extensions #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/try-path.ts
@@ -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
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