Skip to content

Commit

Permalink
chore: enable strict typescript compilation (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored Jan 13, 2025
1 parent c740a24 commit 871ce83
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
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

0 comments on commit 871ce83

Please sign in to comment.