Skip to content

Commit

Permalink
fix: adjust code to adhere to no-explicit-any eslint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusBaldi committed Jan 14, 2025
1 parent c4ef44c commit 754599c
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 36 deletions.
10 changes: 4 additions & 6 deletions src/types/KeyValueStringObject.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { isObject } from '../utils/is-object';
import { isArray } from '../utils/is-array';
import { isArguments } from '../utils/is-arguments';
import { isArrayOfStrings } from '../utils/is-array-of-strings';
import { isString } from '../utils/is-string';
import { isStringUnknownMap } from './StringUnknownMap';

/**
* `KeyValueStringObject`s have `string`s as keys and one of:
Expand Down Expand Up @@ -32,15 +30,15 @@ export interface KeyValueStringObject { [k: string]: (string | string[] | KeyVal
*
* @returns `true` if `o` is a `KeyValueStringObject`
*/
export function isKeyValueStringObject(o: any): o is KeyValueStringObject {
export function isKeyValueStringObject(o: unknown): o is KeyValueStringObject {
// Arrays and the array-like `arguments` variable are objects, so they would not be
// caught by an `isObject` check
if (!isObject(o) || isArray(o) || isArguments(o)) {
if (!isStringUnknownMap(o)) {
return false;
}

for (const k of Object.keys(o)) {
const v: any = (o as any)[k];
const v: unknown = o[k];

if (!isString(v) && !isArrayOfStrings(v) && !isKeyValueStringObject(v)) {
return false;
Expand Down
10 changes: 4 additions & 6 deletions src/types/StringArrayOfStringsMap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { isArguments } from '../utils/is-arguments';
import { isArrayOfStrings } from '../utils/is-array-of-strings';
import { isObject } from '../utils/is-object';
import { isArray } from '../utils/is-array';
import { isStringUnknownMap } from './StringUnknownMap';

/**
* `StringArrayOfStringsMap`s have `string`s as keys and `string[]` as values. For
Expand All @@ -21,15 +19,15 @@ export interface StringArrayOfStringsMap { [s: string]: string[] }
*
* @returns `true` if `o` is a `StringArrayOfStringsMap`
*/
export function isStringArrayOfStringsMap(o: any): o is StringArrayOfStringsMap {
export function isStringArrayOfStringsMap(o: unknown): o is StringArrayOfStringsMap {
// Arrays and the array-like `arguments` variable are objects, so they would not be
// caught by an `isObject` check
if (!isObject(o) || isArray(o) || isArguments(o)) {
if (!isStringUnknownMap(o)) {
return false;
}

for (const k of Object.keys(o)) {
if (!isArrayOfStrings((o as any)[k])) {
if (!isArrayOfStrings(o[k])) {
return false;
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/types/StringMap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { isObject } from '../utils/is-object';
import { isArray } from '../utils/is-array';
import { isArguments } from '../utils/is-arguments';
import { isString } from '../utils/is-string';
import { isStringUnknownMap } from './StringUnknownMap';

/**
* `StringMap`s have `string`s as keys and `string`s as values. For example:
Expand All @@ -19,15 +17,15 @@ export interface StringMap { [s: string]: string }
*
* @returns `true` if `o` is a `StringMap`
*/
export function isStringMap(o: any): o is StringMap {
export function isStringMap(o: unknown): o is StringMap {
// Arrays and the array-like `arguments` variable are objects, so they would not be
// caught by an `isObject` check
if (!isObject(o) || isArray(o) || isArguments(o)) {
if (!isStringUnknownMap(o)) {
return false;
}

for (const k of Object.keys(o)) {
if (!isString((o as any)[k])) {
if (!isString(o[k])) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/StringUnknownMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ export interface StringUnknownMap { [s: string]: unknown }
*
* @returns `true` if `o` is a `StringUnknownMap`
*/
export function isStringUnknownMap(o: any): o is StringUnknownMap {
export function isStringUnknownMap(o: unknown): o is StringUnknownMap {
return isObject(o) && !isArray(o) && !isArguments(o);
}
2 changes: 1 addition & 1 deletion src/utils/get-tag-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
* @see https://github.com/jashkenas/underscore/blob/d5fe0fd4060f13b40608cb9d92eda6d857e8752c/underscore.js#L1326
* @see https://github.com/lodash/lodash/blob/750067f42d3aa5f927604ece2c6df0ff2b2e9d72/isNumber.js#L31
*/
export function getTagString(o: any): string {
export function getTagString(o: unknown): string {
return Object.prototype.toString.call(o);
}
11 changes: 8 additions & 3 deletions src/utils/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function toKey(value: unknown): string | symbol {
const IS_DEEP_PROP_REGEX = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
IS_PLAIN_PROP_REGEX = /^\w*$/;

function isKey(value: any, object: any): boolean {
function isKey(value: unknown, object: unknown): boolean {
if (Array.isArray(value)) {
return false;
}
Expand Down Expand Up @@ -93,7 +93,7 @@ function stringToPath(str: string): string[] {
return result;
}

function createPathArray(value: any, object: any): string[] {
function createPathArray(value: unknown, object: unknown): string[] {
if (Array.isArray(value)) {
return value;
}
Expand Down Expand Up @@ -123,6 +123,11 @@ function get<TObject extends object, TResult = unknown>(
obj: TObject,
path: StringRepresentable | StringRepresentable[]
): TResult | undefined;
function get<TResult = unknown>(
obj: unknown,
path: StringRepresentable | StringRepresentable[],
defaultValue?: TResult
): TResult | undefined
function get<TResult = unknown>(
obj: unknown,
path: StringRepresentable | StringRepresentable[],
Expand All @@ -139,7 +144,7 @@ function get<TResult = unknown>(
resultObj: unknown = obj;

while (resultObj !== null && resultObj !== undefined && index < length) {
resultObj = (resultObj as any)[toKey(pathArray[index])];
resultObj = (resultObj as Record<string, object>)[toKey(pathArray[index]) as string];

index += 1;
}
Expand Down
8 changes: 6 additions & 2 deletions src/utils/is-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isArray } from './is-array';
import { isString } from './is-string';
import { isArguments } from './is-arguments';
import { isUndefined } from './is-undefined';
import { isObject } from './is-object';

/**
* Checks if `o` is an empty object. An object is "empty" if it:
Expand All @@ -12,12 +13,15 @@ import { isUndefined } from './is-undefined';
*
* @returns `true` if `o` is empty
*/
export function isEmpty(o: any): boolean {
export function isEmpty(o: unknown): boolean {
if (o === null || isUndefined(o)) {
return true;
}
if (isArray(o) || isString(o) || isArguments(o)) {
return o.length === 0;
}
return Object.keys(o).length === 0;
if (isObject(o)) {
return Object.keys(o).length === 0;
}
return true;
}
4 changes: 2 additions & 2 deletions src/utils/is-map-with-values-of-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { isStringUnknownMap } from '../types/StringUnknownMap';
* @returns `true` if `o` is a map with `string` keys and values that pass the provided
* type guard.
*/
export function isMapWithValuesOfType<T>(guard: (x: any) => x is T, o: any): o is Record<string, T> {
export function isMapWithValuesOfType<T>(guard: (x: unknown) => x is T, o: unknown): o is Record<string, T> {
if (!isStringUnknownMap(o)) {
return false;
}

for (const k of Object.keys(o)) {
if (!guard((o as any)[k])) {
if (!guard(o[k])) {
return false;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/is-promise-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import { isObject } from './is-object';
* @returns `true` if `o` is `Promise`-like (i.e. has a `then` function)
*/
export function isPromiseLike(o: unknown): o is PromiseLike<unknown> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return isPromise(o) || (isObject(o) && typeof (o as any).then === 'function');
}
6 changes: 3 additions & 3 deletions src/utils/make-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DEFAULT_SETTINGS: ToolboxTemplateSettings = {
interpolate: /<%=([\s\S]+?)%>/g,
};

function getValue(path: string, data: any): unknown {
function getValue(path: string, data: unknown): unknown {
return get(data, (path || '').trim(), '');
}

Expand All @@ -16,7 +16,7 @@ export interface ToolboxTemplateSettings {
}

export interface ToolboxTemplateFunction {
(data: Record<string, any>): string;
(data: Record<string, unknown>): string;
}

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ export function makeTemplate(text: string, userSettings?: ToolboxTemplateSetting
index = offset + match.length;

if (escape) {
parts.push((data: any) => {
parts.push((data: unknown) => {
return escapeHTML(getValue(escape, data));
});
} else if (interpolate) {
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/is-arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ describe('isArguments', () => {
});

it('correctly classifies non-arguments', () => {
(function(...args: any[]) {
(function(...args: unknown[]) {
expect(t.isArguments(args)).to.strictlyEqual(false);
}());


(function(arr: any[]): void {
(function(arr: unknown[]): void {
expect(t.isArguments(arr)).to.strictlyEqual(false);
}([]));

Expand Down
2 changes: 1 addition & 1 deletion tests/utils/is-null.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('isNull', () => {
expect(t.isNull(undefined)).to.strictlyEqual(false);
expect(t.isNull(null)).to.strictlyEqual(true);

const obj: any = { bar: 1, baz: false, bag: undefined, bah: null };
const obj: Record<string, unknown> = { bar: 1, baz: false, bag: undefined, bah: null };

expect(t.isNull(obj.foo)).to.strictlyEqual(false);
expect(t.isNull(obj.bar)).to.strictlyEqual(false);
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/is-undefined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('isUndefined', () => {
expect(t.isUndefined(undefined)).to.strictlyEqual(true);
expect(t.isUndefined(null)).to.strictlyEqual(false);

const obj: any = { bar: 1, baz: false, bag: undefined, bah: null };
const obj: Record<string, unknown> = { bar: 1, baz: false, bag: undefined, bah: null };

expect(t.isUndefined(obj.foo)).to.strictlyEqual(true);
expect(t.isUndefined(obj.bar)).to.strictlyEqual(false);
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/make-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('makeTemplate', () => {
expect(makeTemplate('Hello <%= name %>')).to.be.a('function');
});

function one(tmpl: string, expected: string, data: Record<string, any>): void {
function one(tmpl: string, expected: string, data: Record<string, unknown>): void {
const compiled = makeTemplate(tmpl);

expect(compiled(data)).to.eql(expected);
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/pick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('pick', () => {
it('can pick properties from the object\'s prototype', () => {
const objWithProto = { a: 1 } as { a: number; b: number };

(objWithProto as any).__proto__ = { b: 2 }; // eslint-disable-line no-proto
Object.setPrototypeOf(objWithProto, { b: 2 });

expect(objWithProto.b).to.strictlyEqual(2);
expect(pick(objWithProto, 'b')).to.eql({ b: 2 });
Expand Down

0 comments on commit 754599c

Please sign in to comment.