Skip to content

Commit

Permalink
use typescript-eslint array-type rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Feb 24, 2019
1 parent 5d48312 commit 38fce95
Show file tree
Hide file tree
Showing 63 changed files with 361 additions and 388 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint/eslint-plugin'],
rules: {
'@typescript-eslint/array-type': ['error', 'array'],
'@typescript-eslint/no-unused-vars': [
'error',
{argsIgnorePattern: '^_'},
Expand Down
13 changes: 6 additions & 7 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ const IDVisitor = {
};

const FUNCTIONS: {
[key: string]: (args: Array<NodePath>) => boolean;
[key: string]: (args: NodePath[]) => boolean;
} = Object.create(null);

FUNCTIONS.mock = (args: Array<NodePath>) => {
FUNCTIONS.mock = (args: NodePath[]) => {
if (args.length === 1) {
return args[0].isStringLiteral() || args[0].isLiteral();
} else if (args.length === 2 || args.length === 3) {
Expand Down Expand Up @@ -146,13 +146,12 @@ FUNCTIONS.mock = (args: Array<NodePath>) => {
return false;
};

FUNCTIONS.unmock = (args: Array<NodePath>) =>
FUNCTIONS.unmock = (args: NodePath[]) =>
args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.deepUnmock = (args: Array<NodePath>) =>
FUNCTIONS.deepUnmock = (args: NodePath[]) =>
args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = (
args: Array<NodePath>,
) => args.length === 0;
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = (args: NodePath[]) =>
args.length === 0;

export = () => {
const shouldHoistExpression = (expr: NodePath): boolean => {
Expand Down
21 changes: 9 additions & 12 deletions packages/diff-sequences/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('invalid arg', () => {
});

// Return length of longest common subsequence according to Object.is method.
const countCommonObjectIs = (a: Array<any>, b: Array<any>): number => {
const countCommonObjectIs = (a: any[], b: any[]): number => {
let n = 0;
diff(
a.length,
Expand All @@ -75,7 +75,7 @@ const countCommonObjectIs = (a: Array<any>, b: Array<any>): number => {
};

// Return length of longest common subsequence according to === operator.
const countCommonStrictEquality = (a: Array<any>, b: Array<any>): number => {
const countCommonStrictEquality = (a: any[], b: any[]): number => {
let n = 0;
diff(
a.length,
Expand Down Expand Up @@ -133,8 +133,8 @@ const assertEnd = (name: string, val: number, end: number) => {
};

const assertCommonItems = (
a: Array<any> | string,
b: Array<any> | string,
a: any[] | string,
b: any[] | string,
nCommon: number,
aCommon: number,
bCommon: number,
Expand Down Expand Up @@ -191,10 +191,7 @@ const countDifferences = (
};

// Return array of items in a longest common subsequence of array-like objects.
const findCommonItems = (
a: Array<any> | string,
b: Array<any> | string,
): Array<any> => {
const findCommonItems = (a: any[] | string, b: any[] | string): any[] => {
const aLength = a.length;
const bLength = b.length;
const isCommon = (aIndex: number, bIndex: number) => {
Expand Down Expand Up @@ -231,9 +228,9 @@ const findCommonItems = (

// Assert that array-like objects have the expected common items.
const expectCommonItems = (
a: Array<any> | string,
b: Array<any> | string,
expected: Array<any>,
a: any[] | string,
b: any[] | string,
expected: any[],
) => {
expect(findCommonItems(a, b)).toEqual(expected);

Expand Down Expand Up @@ -730,7 +727,7 @@ const assertCommonSubstring = (
};

// Return array of substrings in a longest common subsequence of strings.
const findCommonSubstrings = (a: string, b: string): Array<string> => {
const findCommonSubstrings = (a: string, b: string): string[] => {
const array = [];
diff(
a.length,
Expand Down
4 changes: 2 additions & 2 deletions packages/diff-sequences/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type Callbacks = {
// and also updates the index arrays in place to cut memory in half.
// kF = 2 * iF - d
// kR = d - 2 * iR
type Indexes = Array<number>;
type Indexes = number[];

// Division of index intervals in sequences a and b at the middle change.
// Invariant: intervals do not have common items at the start or end.
Expand Down Expand Up @@ -657,7 +657,7 @@ const findSubsequences = (
bStart: number,
bEnd: number,
transposed: boolean,
callbacks: Array<Callbacks>,
callbacks: Callbacks[],
aIndexesF: Indexes, // temporary memory, not input nor output
aIndexesR: Indexes, // temporary memory, not input nor output
division: Division, // temporary memory, not input nor output
Expand Down
10 changes: 5 additions & 5 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ class Anything extends AsymmetricMatcher<void> {
}
}

class ArrayContaining extends AsymmetricMatcher<Array<unknown>> {
constructor(sample: Array<unknown>, inverse: boolean = false) {
class ArrayContaining extends AsymmetricMatcher<unknown[]> {
constructor(sample: unknown[], inverse: boolean = false) {
super(sample);
this.inverse = inverse;
}

asymmetricMatch(other: Array<unknown>) {
asymmetricMatch(other: unknown[]) {
if (!Array.isArray(this.sample)) {
throw new Error(
`You must provide an array to ${this.toString()}, not '` +
Expand Down Expand Up @@ -241,9 +241,9 @@ class StringMatching extends AsymmetricMatcher<RegExp> {

export const any = (expectedObject: any) => new Any(expectedObject);
export const anything = () => new Anything();
export const arrayContaining = (sample: Array<unknown>) =>
export const arrayContaining = (sample: unknown[]) =>
new ArrayContaining(sample);
export const arrayNotContaining = (sample: Array<unknown>) =>
export const arrayNotContaining = (sample: unknown[]) =>
new ArrayContaining(sample, true);
export const objectContaining = (sample: Object) =>
new ObjectContaining(sample);
Expand Down
4 changes: 2 additions & 2 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const getPromiseMatcher = (name: string, matcher: any) => {
return null;
};

const expect: any = (actual: any, ...rest: Array<any>): ExpectationObject => {
const expect: any = (actual: any, ...rest: any[]): ExpectationObject => {
if (rest.length !== 0) {
throw new Error('Expect takes at most one argument.');
}
Expand Down Expand Up @@ -384,7 +384,7 @@ function assertions(expected: number) {
getState().expectedAssertionsNumber = expected;
getState().expectedAssertionsNumberError = error;
}
function hasAssertions(...args: Array<any>) {
function hasAssertions(...args: any[]) {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, hasAssertions);
Expand Down
4 changes: 2 additions & 2 deletions packages/expect/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
import {equals} from './jasmineUtils';

type ContainIterable =
| Array<unknown>
| unknown[]
| Set<unknown>
| NodeListOf<any>
| DOMTokenList
Expand Down Expand Up @@ -565,7 +565,7 @@ const matchers: MatchersObject = {
toHaveProperty(
this: MatcherState,
object: object,
keyPath: string | Array<string>,
keyPath: string | string[],
value?: unknown,
) {
const valuePassed = arguments.length === 3;
Expand Down
16 changes: 6 additions & 10 deletions packages/expect/src/spyMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const createToReturnTimesMatcher = (matcherName: string) => (

const createToBeCalledWithMatcher = (matcherName: string) => (
received: any,
...expected: Array<unknown>
...expected: unknown[]
): SyncExpectationResult => {
ensureMock(received, matcherName);

Expand Down Expand Up @@ -253,7 +253,7 @@ const createToReturnWithMatcher = (matcherName: string) => (

const createLastCalledWithMatcher = (matcherName: string) => (
received: any,
...expected: Array<unknown>
...expected: unknown[]
): SyncExpectationResult => {
ensureMock(received, matcherName);

Expand Down Expand Up @@ -332,7 +332,7 @@ const createLastReturnedMatcher = (matcherName: string) => (
const createNthCalledWithMatcher = (matcherName: string) => (
received: any,
nth: number,
...expected: Array<unknown>
...expected: unknown[]
): SyncExpectationResult => {
ensureMock(received, matcherName);

Expand Down Expand Up @@ -517,11 +517,7 @@ const getPrintedReturnValues = (calls: any[], limit: number): string => {
return result.join('\n\n ');
};

const formatReceivedCalls = (
calls: Array<any>,
limit: number,
options: any,
) => {
const formatReceivedCalls = (calls: any[], limit: number, options: any) => {
if (calls.length) {
const but = options && options.sameSentence ? 'but' : 'But';
const count = calls.length - limit;
Expand All @@ -540,7 +536,7 @@ const formatReceivedCalls = (
};

const formatMismatchedCalls = (
calls: Array<any>,
calls: any[],
expected: any,
limit: number,
): string => {
Expand All @@ -560,7 +556,7 @@ const formatMismatchedCalls = (
};

const formatMismatchedReturnValues = (
returnValues: Array<any>,
returnValues: any[],
expected: any,
limit: number,
): string => {
Expand Down
10 changes: 5 additions & 5 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export type MatcherState = {
equals: (
a: unknown,
b: unknown,
customTesters?: Array<Tester>,
customTesters?: Tester[],
strictCheck?: boolean,
) => boolean;
expand?: boolean;
expectedAssertionsNumber?: number;
isExpectingAssertions?: boolean;
isNot: boolean;
promise: string;
suppressedErrors: Array<Error>;
suppressedErrors: Error[];
testPath?: Config.Path;
utils: typeof jestMatcherUtils & {
iterableEquality: Tester;
Expand All @@ -59,18 +59,18 @@ export type Expect = {
addSnapshotSerializer(arg0: any): void;
assertions(arg0: number): void;
extend(arg0: any): void;
extractExpectedAssertionsErrors: () => Array<{
extractExpectedAssertionsErrors: () => {
actual: string | number;
error: Error;
expected: string;
}>;
}[];
getState(): MatcherState;
hasAssertions(): void;
setState(arg0: any): void;

any(expectedObject: any): AsymmetricMatcher;
anything(): AsymmetricMatcher;
arrayContaining(sample: Array<any>): AsymmetricMatcher;
arrayContaining(sample: any[]): AsymmetricMatcher;
objectContaining(sample: Object): AsymmetricMatcher;
stringContaining(expected: string): AsymmetricMatcher;
stringMatching(expected: string | RegExp): AsymmetricMatcher;
Expand Down
10 changes: 5 additions & 5 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
type GetPath = {
hasEndProp?: boolean;
lastTraversedObject: unknown;
traversedPath: Array<string>;
traversedPath: string[];
value?: unknown;
};

Expand Down Expand Up @@ -49,7 +49,7 @@ export const hasOwnProperty = (object: object, key: string) =>

export const getPath = (
object: object,
propertyPath: string | Array<string>,
propertyPath: string | string[],
): GetPath => {
if (!Array.isArray(propertyPath)) {
propertyPath = (propertyPath as string).split('.');
Expand Down Expand Up @@ -262,10 +262,10 @@ export const sparseArrayEquality = (a: unknown, b: unknown) => {
};

export const partition = <T>(
items: Array<T>,
items: T[],
predicate: (arg: T) => boolean,
): [Array<T>, Array<T>] => {
const result: [Array<T>, Array<T>] = [[], []];
): [T[], T[]] => {
const result: [T[], T[]] = [[], []];

items.forEach(item => result[predicate(item) ? 0 : 1].push(item));

Expand Down
7 changes: 3 additions & 4 deletions packages/jest-changed-files/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {Config} from '@jest/types';
import {SCMAdapter} from './types';

const findChangedFilesUsingCommand = async (
args: Array<string>,
args: string[],
cwd: Config.Path,
): Promise<Array<Config.Path>> => {
): Promise<Config.Path[]> => {
const result = await execa('git', args, {cwd});

return result.stdout
Expand All @@ -29,8 +29,7 @@ const adapter: SCMAdapter = {
const changedSince: string | undefined =
options && (options.withAncestor ? 'HEAD^' : options.changedSince);

const includePaths: Array<Config.Path> =
(options && options.includePaths) || [];
const includePaths: Config.Path[] = (options && options.includePaths) || [];

if (options && options.lastCommit) {
return findChangedFilesUsingCommand(
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-changed-files/src/hg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const env = {...process.env, HGPLAIN: '1'};

const adapter: SCMAdapter = {
findChangedFiles: async (cwd, options) => {
const includePaths: Array<Config.Path> =
(options && options.includePaths) || [];
const includePaths: Config.Path[] = (options && options.includePaths) || [];

const args = ['status', '-amnu'];
if (options && options.withAncestor) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-changed-files/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Options = {
lastCommit?: boolean;
withAncestor?: boolean;
changedSince?: string;
includePaths?: Array<Config.Path>;
includePaths?: Config.Path[];
};

type ChangedFiles = Set<Config.Path>;
Expand All @@ -25,6 +25,6 @@ export type SCMAdapter = {
findChangedFiles: (
cwd: Config.Path,
options: Options,
) => Promise<Array<Config.Path>>;
) => Promise<Config.Path[]>;
getRoot: (cwd: Config.Path) => Promise<Config.Path | null>;
};
2 changes: 1 addition & 1 deletion packages/jest-circus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const test: Global.It = (() => {
const only = (testName: TestName, fn: TestFn, timeout?: number): void =>
_addTest(testName, 'only', fn, test.only, timeout);

test.todo = (testName: TestName, ...rest: Array<any>): void => {
test.todo = (testName: TestName, ...rest: any[]): void => {
if (rest.length > 0 || typeof testName !== 'string') {
throw new ErrorWithStack(
'Todo must be called with only a description.',
Expand Down
Loading

0 comments on commit 38fce95

Please sign in to comment.