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

chore: enable strict typescript checks #200

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
16 changes: 12 additions & 4 deletions src/_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export function normalizeString(path: string, allowAboveRoot: boolean) {
let char: string | null = null;
for (let index = 0; index <= path.length; ++index) {
if (index < path.length) {
char = path[index];
// casted because we know it exists thanks to the length check
char = path[index] as string;
} else if (char === "/") {
break;
} else {
Expand Down Expand Up @@ -219,8 +220,15 @@ export const extname: typeof path.extname = function (p) {
};

export const relative: typeof path.relative = function (from, to) {
const _from = resolve(from).replace(_ROOT_FOLDER_RE, "$1").split("/");
const _to = resolve(to).replace(_ROOT_FOLDER_RE, "$1").split("/");
// we cast these because `split` will always be at least one string
const _from = resolve(from).replace(_ROOT_FOLDER_RE, "$1").split("/") as [
string,
...string[],
];
const _to = resolve(to).replace(_ROOT_FOLDER_RE, "$1").split("/") as [
string,
...string[],
];

// Different windows drive letters
if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) {
Expand All @@ -243,7 +251,7 @@ export const dirname: typeof path.dirname = function (p) {
.replace(/\/$/, "")
.split("/")
.slice(0, -1);
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) {
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0] as string)) {
segments[0] += "/";
}
return segments.join("/") || (isAbsolute(p) ? "/" : ".");
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const delimiter: ";" | ":" = /* @__PURE__ */ (() =>

// Mix namespaces without side-effects of object to allow tree-shaking

const _platforms = { posix: undefined, win32: undefined } as {
const _platforms = { posix: undefined, win32: undefined } as unknown as {
posix: NodePath["posix"];
win32: NodePath["win32"];
[key: PropertyKey]: unknown;
};

const mix = (del: ";" | ":" = delimiter) => {
Expand All @@ -25,7 +26,7 @@ const mix = (del: ";" | ":" = delimiter) => {
if (prop === "delimiter") return del;
if (prop === "posix") return posix;
if (prop === "win32") return win32;
return _platforms[prop] || _path[prop];
return _platforms[prop] || _path[prop as keyof typeof _path];
},
}) as unknown as NodePath;
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function normalizeAliases(_aliases: Record<string, string>) {
}

if (
aliases[key].startsWith(alias) &&
aliases[key]?.startsWith(alias) &&
pathSeparators.has(aliases[key][alias.length])
) {
aliases[key] = aliases[alias] + aliases[key].slice(alias.length);
Expand Down
40 changes: 28 additions & 12 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,38 +428,54 @@ runTest("matchesGlob", matchesGlob, [
["/foo/bar", "/{bar,foo}/**", true],
]);

function _s(item) {
function _s(item: unknown) {
return (JSON.stringify(_r(item)) || "undefined").replace(/"/g, "'");
}

function _r(item) {
function _r(item: unknown) {
return typeof item === "function" ? item() : item;
}

export function runTest(name, function_, items) {
type AnyFunction<TArgs extends unknown[] = never> = (...args: TArgs) => unknown;
type TestCaseArray<T extends AnyFunction> = [
...unknown[],
ReturnType<T> | (() => ReturnType<T>),
];

export function runTest<T extends AnyFunction>(
name: string,
function_: T,
items: Record<PropertyKey, ReturnType<T>> | Array<TestCaseArray<T>>,
) {
if (!Array.isArray(items)) {
items = Object.entries(items).map((e) => e.flat());
items = Object.entries(items).map((e) => e.flat()) as Array<
TestCaseArray<T>
>;
}
describe(`${name}`, () => {
let cwd;
for (const item of items) {
const expected = item.pop();
const arguments_ = item;
const expected = item.pop() as ReturnType<T>;
const arguments_ = item as unknown as Parameters<T>;
it(`${name}(${arguments_.map((i) => _s(i)).join(",")}) should be ${_s(
expected,
)}`, () => {
expect(function_(...arguments_.map((i) => _r(i)))).to.equal(
_r(expected),
);
expect(
(function_ as unknown as AnyFunction<unknown[]>)(
...arguments_.map((i) => _r(i)),
),
).to.equal(_r(expected));
});
it(`${name}(${arguments_.map((i) => _s(i)).join(",")}) should be ${_s(
expected,
)} on Windows`, () => {
cwd = process.cwd;
process.cwd = vi.fn(() => String.raw`C:\Windows\path\only`);
expect(function_(...arguments_.map((i) => _r(i)))).to.equal(
_r(expected),
);
expect(
(function_ as unknown as AnyFunction<unknown[]>)(
...arguments_.map((i) => _r(i)),
),
).to.equal(_r(expected));
process.cwd = cwd;
});
}
Expand Down
4 changes: 2 additions & 2 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("alias", () => {
"/root/index.js": ["@/index.js", "~"],
};
for (const [to, from] of Object.entries(aliases)) {
const expected = overrides[from] || [to];
const expected = overrides[from as keyof typeof overrides] || [to];
it(`reverseResolveAlias("${from}")`, () => {
expect(reverseResolveAlias(from, aliases)).toMatchObject(expected);
});
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("filename", () => {
};
for (const file in files) {
it(file, () => {
expect(filename(file)).toEqual(files[file]);
expect(filename(file)).toEqual(files[file as keyof typeof files]);
});
}
});
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
"module": "ESNext",
"moduleResolution": "Node",
"skipLibCheck": true,
"esModuleInterop": true
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"noEmit": true,
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"strict": true,
"verbatimModuleSyntax": true
},
"include": [
"src",
Expand Down
Loading