Skip to content

Commit

Permalink
TS: Fix strict issues in src/jsutils
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed May 27, 2021
1 parent bf47ef8 commit 946d06a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/jsutils/__tests__/identityFunc-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { identityFunc } from '../identityFunc';

describe('identityFunc', () => {
it('returns the first argument it receives', () => {
// @ts-expect-error FIXME: TS Conversion
// @ts-expect-error (Expects an argument)
expect(identityFunc()).to.equal(undefined);
expect(identityFunc(undefined)).to.equal(undefined);
expect(identityFunc(null)).to.equal(null);
Expand Down
21 changes: 14 additions & 7 deletions src/jsutils/__tests__/inspect-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ describe('inspect', () => {
'{ self: [Circular], deepSelf: { self: [Circular] } }',
);

const array = [];
const array: any = [];
array[0] = array;
array[1] = [array];

expect(inspect(array)).to.equal('[[Circular], [[Circular]]]');

const mixed = { array: [] };
const mixed: any = { array: [] };
mixed.array[0] = mixed;

expect(inspect(mixed)).to.equal('{ array: [[Circular]] }');
Expand All @@ -165,14 +165,21 @@ describe('inspect', () => {

expect(inspect([[new Foo()]])).to.equal('[[[Foo]]]');

Foo.prototype[Symbol.toStringTag] = 'Bar';
expect(inspect([[new Foo()]])).to.equal('[[[Bar]]]');
class Foo2 {
foo: string;

[Symbol.toStringTag] = 'Bar';

constructor() {
this.foo = 'bar';
}
}
expect(inspect([[new Foo2()]])).to.equal('[[[Bar]]]');

// eslint-disable-next-line func-names
const objectWithoutClassName = new (function () {
// eslint-disable-next-line @typescript-eslint/no-invalid-this
const objectWithoutClassName = new (function (this: any) {
this.foo = 1;
})();
} as any)();
expect(inspect([[objectWithoutClassName]])).to.equal('[[[Object]]]');
});
});
7 changes: 3 additions & 4 deletions src/jsutils/didYouMean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ export function didYouMean(
firstArg: string | ReadonlyArray<string>,
secondArg?: ReadonlyArray<string>,
) {
const [subMessage, suggestionsArg] =
typeof firstArg === 'string'
? [firstArg, secondArg]
: [undefined, firstArg];
const [subMessage, suggestionsArg] = secondArg
? [firstArg as string, secondArg]
: [undefined, firstArg as ReadonlyArray<string>];

let message = ' Did you mean ';
if (subMessage) {
Expand Down
16 changes: 9 additions & 7 deletions src/jsutils/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function formatValue(value: unknown, seenValues: Array<unknown>): string {
}

function formatObjectValue(
value: Object,
value: object | null,
previouslySeenValues: Array<unknown>,
): string {
if (value === null) {
Expand All @@ -35,10 +35,8 @@ function formatObjectValue(

const seenValues = [...previouslySeenValues, value];

// @ts-expect-error FIXME: TS Conversion
if (typeof value.toJSON === 'function') {
// @ts-expect-error FIXME: TS Conversion
const jsonValue = (value.toJSON as () => unknown)();
if (isJSONable(value)) {
const jsonValue = value.toJSON();

// check for infinite recursion
if (jsonValue !== value) {
Expand All @@ -53,7 +51,11 @@ function formatObjectValue(
return formatObject(value, seenValues);
}

function formatObject(object: Object, seenValues: Array<unknown>): string {
function isJSONable(value: any): value is { toJSON: () => unknown } {
return typeof value.toJSON === 'function';
}

function formatObject(object: object, seenValues: Array<unknown>): string {
const entries = Object.entries(object);
if (entries.length === 0) {
return '{}';
Expand Down Expand Up @@ -98,7 +100,7 @@ function formatArray(
return '[' + items.join(', ') + ']';
}

function getObjectTag(object: Object): string {
function getObjectTag(object: object): string {
const tag = Object.prototype.toString
.call(object)
.replace(/^\[object /, '')
Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/isAsyncIterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* implementing a `Symbol.asyncIterator` method.
*/
export function isAsyncIterable(
maybeAsyncIterable: unknown,
maybeAsyncIterable: any,
): maybeAsyncIterable is AsyncIterable<unknown> {
return typeof maybeAsyncIterable?.[Symbol.asyncIterator] === 'function';
}
2 changes: 1 addition & 1 deletion src/jsutils/isIterableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* isIterableObject({ length: 1, 0: 'Alpha' }) // false
*/
export function isIterableObject(
maybeIterable: unknown,
maybeIterable: any,
): maybeIterable is Iterable<unknown> {
return (
typeof maybeIterable === 'object' &&
Expand Down
5 changes: 2 additions & 3 deletions src/jsutils/isPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Returns true if the value acts like a Promise, i.e. has a "then" function,
* otherwise returns false.
*/
export function isPromise(value: unknown): value is Promise<unknown> {
// eslint-disable-next-line @typescript-eslint/dot-notation
return typeof value?.['then'] === 'function';
export function isPromise(value: any): value is Promise<unknown> {
return typeof value?.then === 'function';
}
2 changes: 1 addition & 1 deletion src/jsutils/memoize3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function memoize3<
A3 extends object,
R,
>(fn: (a1: A1, a2: A2, a3: A3) => R): (a1: A1, a2: A2, a3: A3) => R {
let cache0;
let cache0: WeakMap<A1, WeakMap<A2, WeakMap<A3, R>>>;

return function memoized(a1, a2, a3) {
if (cache0 === undefined) {
Expand Down

0 comments on commit 946d06a

Please sign in to comment.