Skip to content

Commit

Permalink
chore: add explicit-module-boundary-types lint rule (#9539)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 9, 2020
1 parent dbeb5da commit 5d1e98b
Show file tree
Hide file tree
Showing 114 changed files with 528 additions and 377 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Expand Up @@ -73,6 +73,24 @@ module.exports = {
'import/no-extraneous-dependencies': 0,
},
},
{
files: 'packages/**/*.ts',
rules: {
'@typescript-eslint/explicit-module-boundary-types': 2,
},
},
{
files: [
'**/__tests__/**',
'**/__mocks__/**',
'packages/jest-jasmine2/src/jasmine/**/*',
'packages/expect/src/jasmineUtils.ts',
'**/vendor/**/*',
],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 0,
},
},
{
files: [
'packages/jest-jasmine2/src/jasmine/**/*',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -17,8 +17,8 @@
"@types/jest": "24.0.2",
"@types/node": "*",
"@types/which": "^1.3.2",
"@typescript-eslint/eslint-plugin": "^2.2.0",
"@typescript-eslint/parser": "^2.2.0",
"@typescript-eslint/eslint-plugin": "^2.19.0",
"@typescript-eslint/parser": "^2.19.0",
"ansi-regex": "^5.0.0",
"ansi-styles": "^4.2.0",
"babel-eslint": "^10.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-jest-hoist/src/index.ts
Expand Up @@ -155,7 +155,7 @@ FUNCTIONS.deepUnmock = args => args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = args =>
args.length === 0;

export default () => {
export default (): {visitor: Visitor} => {
const shouldHoistExpression = (expr: NodePath): boolean => {
if (!expr.isCallExpression()) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/diff-sequences/src/index.ts
Expand Up @@ -785,7 +785,7 @@ export default (
bLength: number,
isCommon: IsCommon,
foundSubsequence: FoundSubsequence,
) => {
): void => {
validateLength('aLength', aLength);
validateLength('bLength', bLength);
validateCallback('isCommon', isCommon);
Expand Down
26 changes: 14 additions & 12 deletions packages/expect/src/asymmetricMatchers.ts
Expand Up @@ -239,21 +239,23 @@ 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 any = (expectedObject: unknown): Any => new Any(expectedObject);
export const anything = (): Anything => new Anything();
export const arrayContaining = (sample: Array<unknown>): ArrayContaining =>
new ArrayContaining(sample);
export const arrayNotContaining = (sample: Array<unknown>) =>
export const arrayNotContaining = (sample: Array<unknown>): ArrayContaining =>
new ArrayContaining(sample, true);
export const objectContaining = (sample: Record<string, any>) =>
new ObjectContaining(sample);
export const objectNotContaining = (sample: Record<string, any>) =>
new ObjectContaining(sample, true);
export const stringContaining = (expected: string) =>
export const objectContaining = (
sample: Record<string, any>,
): ObjectContaining => new ObjectContaining(sample);
export const objectNotContaining = (
sample: Record<string, any>,
): ObjectContaining => new ObjectContaining(sample, true);
export const stringContaining = (expected: string): StringContaining =>
new StringContaining(expected);
export const stringNotContaining = (expected: string) =>
export const stringNotContaining = (expected: string): StringContaining =>
new StringContaining(expected, true);
export const stringMatching = (expected: string | RegExp) =>
export const stringMatching = (expected: string | RegExp): StringMatching =>
new StringMatching(expected);
export const stringNotMatching = (expected: string | RegExp) =>
export const stringNotMatching = (expected: string | RegExp): StringMatching =>
new StringMatching(expected, true);
9 changes: 5 additions & 4 deletions packages/expect/src/jestMatchersObject.ts
Expand Up @@ -31,19 +31,20 @@ if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
});
}

export const getState = () => (global as any)[JEST_MATCHERS_OBJECT].state;
export const getState = (): any => (global as any)[JEST_MATCHERS_OBJECT].state;

export const setState = (state: object) => {
export const setState = (state: object): void => {
Object.assign((global as any)[JEST_MATCHERS_OBJECT].state, state);
};

export const getMatchers = () => (global as any)[JEST_MATCHERS_OBJECT].matchers;
export const getMatchers = (): MatchersObject =>
(global as any)[JEST_MATCHERS_OBJECT].matchers;

export const setMatchers = (
matchers: MatchersObject,
isInternal: boolean,
expect: Expect,
) => {
): void => {
Object.keys(matchers).forEach(key => {
const matcher = matchers[key];
Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
Expand Down
8 changes: 4 additions & 4 deletions packages/expect/src/print.ts
Expand Up @@ -92,24 +92,24 @@ export const printCloseTo = (
export const printExpectedConstructorName = (
label: string,
expected: Function,
) => printConstructorName(label, expected, false, true) + '\n';
): string => printConstructorName(label, expected, false, true) + '\n';

export const printExpectedConstructorNameNot = (
label: string,
expected: Function,
) => printConstructorName(label, expected, true, true) + '\n';
): string => printConstructorName(label, expected, true, true) + '\n';

export const printReceivedConstructorName = (
label: string,
received: Function,
) => printConstructorName(label, received, false, false) + '\n';
): string => printConstructorName(label, received, false, false) + '\n';

// Do not call function if received is equal to expected.
export const printReceivedConstructorNameNot = (
label: string,
received: Function,
expected: Function,
) =>
): string =>
typeof expected.name === 'string' &&
expected.name.length !== 0 &&
typeof received.name === 'string' &&
Expand Down
8 changes: 7 additions & 1 deletion packages/expect/src/toThrowMatchers.ts
Expand Up @@ -27,6 +27,7 @@ import {
printReceivedStringContainExpectedSubstring,
} from './print';
import {
ExpectationResult,
MatcherState,
MatchersObject,
RawMatcherFn,
Expand Down Expand Up @@ -75,7 +76,12 @@ export const createMatcher = (
matcherName: string,
fromPromise?: boolean,
): RawMatcherFn =>
function(this: MatcherState, received: Function, expected: any) {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function(
this: MatcherState,
received: Function,
expected: any,
): ExpectationResult {
const options = {
isNot: this.isNot,
promise: this.promise,
Expand Down
26 changes: 17 additions & 9 deletions packages/expect/src/utils.ts
Expand Up @@ -44,7 +44,7 @@ const hasGetterFromConstructor = (object: object, key: string) => {
return descriptor !== undefined && typeof descriptor.get === 'function';
};

export const hasOwnProperty = (object: object, key: string) =>
export const hasOwnProperty = (object: object, key: string): boolean =>
Object.prototype.hasOwnProperty.call(object, key) ||
hasGetterFromConstructor(object, key);

Expand Down Expand Up @@ -104,6 +104,7 @@ export const getPath = (

// Strip properties from object that are not present in the subset. Useful for
// printing the diff for toMatchObject() without adding unrelated noise.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const getObjectSubset = (
object: any,
subset: any,
Expand Down Expand Up @@ -147,12 +148,13 @@ const IteratorSymbol = Symbol.iterator;
const hasIterator = (object: any) =>
!!(object != null && object[IteratorSymbol]);

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const iterableEquality = (
a: any,
b: any,
aStack: Array<any> = [],
bStack: Array<any> = [],
) => {
): boolean | undefined => {
if (
typeof a !== 'object' ||
typeof b !== 'object' ||
Expand Down Expand Up @@ -273,16 +275,17 @@ const isObjectWithKeys = (a: any) =>
!(a instanceof Array) &&
!(a instanceof Date);

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const subsetEquality = (
object: any,
subset: any,
): undefined | boolean => {
): boolean | undefined => {
// subsetEquality needs to keep track of the references
// it has already visited to avoid infinite loops in case
// there are circular references in the subset passed to it.
const subsetEqualityWithContext = (
seenReferences: WeakMap<object, boolean> = new WeakMap(),
) => (object: any, subset: any): undefined | boolean => {
) => (object: any, subset: any): boolean | undefined => {
if (!isObjectWithKeys(subset)) {
return undefined;
}
Expand Down Expand Up @@ -314,15 +317,19 @@ export const subsetEquality = (
return subsetEqualityWithContext()(object, subset);
};

export const typeEquality = (a: any, b: any) => {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const typeEquality = (a: any, b: any): boolean | undefined => {
if (a == null || b == null || a.constructor === b.constructor) {
return undefined;
}

return false;
};

export const sparseArrayEquality = (a: unknown, b: unknown) => {
export const sparseArrayEquality = (
a: unknown,
b: unknown,
): boolean | undefined => {
if (!Array.isArray(a) || !Array.isArray(b)) {
return undefined;
}
Expand All @@ -347,7 +354,7 @@ export const partition = <T>(
};

// Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/src/Angular.js#L685-L693
export const isError = (value: unknown) => {
export const isError = (value: unknown): value is Error => {
switch (Object.prototype.toString.call(value)) {
case '[object Error]':
return true;
Expand All @@ -360,13 +367,14 @@ export const isError = (value: unknown) => {
}
};

export function emptyObject(obj: any) {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function emptyObject(obj: any): boolean {
return obj && typeof obj === 'object' ? !Object.keys(obj).length : false;
}

const MULTILINE_REGEXP = /[\r\n]/;

export const isOneline = (expected: any, received: any): boolean =>
export const isOneline = (expected: unknown, received: unknown): boolean =>
typeof expected === 'string' &&
typeof received === 'string' &&
(!MULTILINE_REGEXP.test(expected) || !MULTILINE_REGEXP.test(received));
2 changes: 1 addition & 1 deletion packages/jest-circus/src/globalErrorHandlers.ts
Expand Up @@ -28,7 +28,7 @@ export const injectGlobalErrorHandlers = (
export const restoreGlobalErrorHandlers = (
parentProcess: NodeJS.Process,
originalErrorHandlers: Circus.GlobalErrorHandlers,
) => {
): void => {
parentProcess.removeListener('uncaughtException', uncaught);
parentProcess.removeListener('unhandledRejection', uncaught);

Expand Down
Expand Up @@ -34,6 +34,8 @@ import globals from '..';

type Process = NodeJS.Process;

// TODO: hard to type
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const initialize = ({
config,
environment,
Expand Down
Expand Up @@ -15,7 +15,7 @@ import {
toThrowErrorMatchingSnapshot,
} from 'jest-snapshot';

export default (config: {expand: boolean}) => {
export default (config: {expand: boolean}): void => {
global.expect = expect;
expect.setState({
expand: config.expand,
Expand Down
30 changes: 18 additions & 12 deletions packages/jest-circus/src/utils.ts
Expand Up @@ -73,11 +73,15 @@ const hasEnabledTest = (describeBlock: Circus.DescribeBlock): boolean => {
return hasOwnEnabledTests || describeBlock.children.some(hasEnabledTest);
};

export const getAllHooksForDescribe = (describe: Circus.DescribeBlock) => {
const result: {
beforeAll: Array<Circus.Hook>;
afterAll: Array<Circus.Hook>;
} = {
type DescribeHooks = {
beforeAll: Array<Circus.Hook>;
afterAll: Array<Circus.Hook>;
};

export const getAllHooksForDescribe = (
describe: Circus.DescribeBlock,
): DescribeHooks => {
const result: DescribeHooks = {
afterAll: [],
beforeAll: [],
};
Expand All @@ -98,11 +102,13 @@ export const getAllHooksForDescribe = (describe: Circus.DescribeBlock) => {
return result;
};

export const getEachHooksForTest = (test: Circus.TestEntry) => {
const result: {
beforeEach: Array<Circus.Hook>;
afterEach: Array<Circus.Hook>;
} = {afterEach: [], beforeEach: []};
type TestHooks = {
beforeEach: Array<Circus.Hook>;
afterEach: Array<Circus.Hook>;
};

export const getEachHooksForTest = (test: Circus.TestEntry): TestHooks => {
const result: TestHooks = {afterEach: [], beforeEach: []};
let block: Circus.DescribeBlock | undefined | null = test.parent;

do {
Expand Down Expand Up @@ -298,7 +304,7 @@ const makeTestResults = (

// Return a string that identifies the test (concat of parent describe block
// names + test title)
export const getTestID = (test: Circus.TestEntry) => {
export const getTestID = (test: Circus.TestEntry): string => {
const titles = [];
let parent: Circus.TestEntry | Circus.DescribeBlock | undefined = test;
do {
Expand Down Expand Up @@ -341,7 +347,7 @@ export const addErrorToEachTestUnderDescribe = (
describeBlock: Circus.DescribeBlock,
error: Circus.Exception,
asyncError: Circus.Exception,
) => {
): void => {
for (const test of describeBlock.tests) {
test.errors.push([error, asyncError]);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/cli/args.ts
Expand Up @@ -9,7 +9,7 @@ import {Config} from '@jest/types';
import {isJSONString} from 'jest-config';
import isCI = require('is-ci');

export const check = (argv: Config.Argv) => {
export const check = (argv: Config.Argv): true => {
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
throw new Error(
'Both --runInBand and --maxWorkers were specified, but these two ' +
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-cli/src/cli/index.ts
Expand Up @@ -19,7 +19,10 @@ import {sync as realpath} from 'realpath-native';
import init from '../init';
import * as args from './args';

export async function run(maybeArgv?: Array<string>, project?: Config.Path) {
export async function run(
maybeArgv?: Array<string>,
project?: Config.Path,
): Promise<void> {
try {
const argv: Config.Argv = buildArgv(maybeArgv);

Expand Down
4 changes: 3 additions & 1 deletion packages/jest-cli/src/init/index.ts
Expand Up @@ -34,7 +34,9 @@ type PromptsResults = {

const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext;

export default async (rootDir: string = realpath(process.cwd())) => {
export default async (
rootDir: string = realpath(process.cwd()),
): Promise<void> => {
// prerequisite checks
const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON);

Expand Down

0 comments on commit 5d1e98b

Please sign in to comment.