Skip to content

Commit 5d1e98b

Browse files
authored
chore: add explicit-module-boundary-types lint rule (#9539)
1 parent dbeb5da commit 5d1e98b

File tree

114 files changed

+528
-377
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+528
-377
lines changed

.eslintrc.js

+18
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ module.exports = {
7373
'import/no-extraneous-dependencies': 0,
7474
},
7575
},
76+
{
77+
files: 'packages/**/*.ts',
78+
rules: {
79+
'@typescript-eslint/explicit-module-boundary-types': 2,
80+
},
81+
},
82+
{
83+
files: [
84+
'**/__tests__/**',
85+
'**/__mocks__/**',
86+
'packages/jest-jasmine2/src/jasmine/**/*',
87+
'packages/expect/src/jasmineUtils.ts',
88+
'**/vendor/**/*',
89+
],
90+
rules: {
91+
'@typescript-eslint/explicit-module-boundary-types': 0,
92+
},
93+
},
7694
{
7795
files: [
7896
'packages/jest-jasmine2/src/jasmine/**/*',

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"@types/jest": "24.0.2",
1818
"@types/node": "*",
1919
"@types/which": "^1.3.2",
20-
"@typescript-eslint/eslint-plugin": "^2.2.0",
21-
"@typescript-eslint/parser": "^2.2.0",
20+
"@typescript-eslint/eslint-plugin": "^2.19.0",
21+
"@typescript-eslint/parser": "^2.19.0",
2222
"ansi-regex": "^5.0.0",
2323
"ansi-styles": "^4.2.0",
2424
"babel-eslint": "^10.0.3",

packages/babel-plugin-jest-hoist/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ FUNCTIONS.deepUnmock = args => args.length === 1 && args[0].isStringLiteral();
155155
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = args =>
156156
args.length === 0;
157157

158-
export default () => {
158+
export default (): {visitor: Visitor} => {
159159
const shouldHoistExpression = (expr: NodePath): boolean => {
160160
if (!expr.isCallExpression()) {
161161
return false;

packages/diff-sequences/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ export default (
785785
bLength: number,
786786
isCommon: IsCommon,
787787
foundSubsequence: FoundSubsequence,
788-
) => {
788+
): void => {
789789
validateLength('aLength', aLength);
790790
validateLength('bLength', bLength);
791791
validateCallback('isCommon', isCommon);

packages/expect/src/asymmetricMatchers.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,23 @@ class StringMatching extends AsymmetricMatcher<RegExp> {
239239
}
240240
}
241241

242-
export const any = (expectedObject: any) => new Any(expectedObject);
243-
export const anything = () => new Anything();
244-
export const arrayContaining = (sample: Array<unknown>) =>
242+
export const any = (expectedObject: unknown): Any => new Any(expectedObject);
243+
export const anything = (): Anything => new Anything();
244+
export const arrayContaining = (sample: Array<unknown>): ArrayContaining =>
245245
new ArrayContaining(sample);
246-
export const arrayNotContaining = (sample: Array<unknown>) =>
246+
export const arrayNotContaining = (sample: Array<unknown>): ArrayContaining =>
247247
new ArrayContaining(sample, true);
248-
export const objectContaining = (sample: Record<string, any>) =>
249-
new ObjectContaining(sample);
250-
export const objectNotContaining = (sample: Record<string, any>) =>
251-
new ObjectContaining(sample, true);
252-
export const stringContaining = (expected: string) =>
248+
export const objectContaining = (
249+
sample: Record<string, any>,
250+
): ObjectContaining => new ObjectContaining(sample);
251+
export const objectNotContaining = (
252+
sample: Record<string, any>,
253+
): ObjectContaining => new ObjectContaining(sample, true);
254+
export const stringContaining = (expected: string): StringContaining =>
253255
new StringContaining(expected);
254-
export const stringNotContaining = (expected: string) =>
256+
export const stringNotContaining = (expected: string): StringContaining =>
255257
new StringContaining(expected, true);
256-
export const stringMatching = (expected: string | RegExp) =>
258+
export const stringMatching = (expected: string | RegExp): StringMatching =>
257259
new StringMatching(expected);
258-
export const stringNotMatching = (expected: string | RegExp) =>
260+
export const stringNotMatching = (expected: string | RegExp): StringMatching =>
259261
new StringMatching(expected, true);

packages/expect/src/jestMatchersObject.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
3131
});
3232
}
3333

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

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

40-
export const getMatchers = () => (global as any)[JEST_MATCHERS_OBJECT].matchers;
40+
export const getMatchers = (): MatchersObject =>
41+
(global as any)[JEST_MATCHERS_OBJECT].matchers;
4142

4243
export const setMatchers = (
4344
matchers: MatchersObject,
4445
isInternal: boolean,
4546
expect: Expect,
46-
) => {
47+
): void => {
4748
Object.keys(matchers).forEach(key => {
4849
const matcher = matchers[key];
4950
Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {

packages/expect/src/print.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,24 @@ export const printCloseTo = (
9292
export const printExpectedConstructorName = (
9393
label: string,
9494
expected: Function,
95-
) => printConstructorName(label, expected, false, true) + '\n';
95+
): string => printConstructorName(label, expected, false, true) + '\n';
9696

9797
export const printExpectedConstructorNameNot = (
9898
label: string,
9999
expected: Function,
100-
) => printConstructorName(label, expected, true, true) + '\n';
100+
): string => printConstructorName(label, expected, true, true) + '\n';
101101

102102
export const printReceivedConstructorName = (
103103
label: string,
104104
received: Function,
105-
) => printConstructorName(label, received, false, false) + '\n';
105+
): string => printConstructorName(label, received, false, false) + '\n';
106106

107107
// Do not call function if received is equal to expected.
108108
export const printReceivedConstructorNameNot = (
109109
label: string,
110110
received: Function,
111111
expected: Function,
112-
) =>
112+
): string =>
113113
typeof expected.name === 'string' &&
114114
expected.name.length !== 0 &&
115115
typeof received.name === 'string' &&

packages/expect/src/toThrowMatchers.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
printReceivedStringContainExpectedSubstring,
2828
} from './print';
2929
import {
30+
ExpectationResult,
3031
MatcherState,
3132
MatchersObject,
3233
RawMatcherFn,
@@ -75,7 +76,12 @@ export const createMatcher = (
7576
matcherName: string,
7677
fromPromise?: boolean,
7778
): RawMatcherFn =>
78-
function(this: MatcherState, received: Function, expected: any) {
79+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
80+
function(
81+
this: MatcherState,
82+
received: Function,
83+
expected: any,
84+
): ExpectationResult {
7985
const options = {
8086
isNot: this.isNot,
8187
promise: this.promise,

packages/expect/src/utils.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const hasGetterFromConstructor = (object: object, key: string) => {
4444
return descriptor !== undefined && typeof descriptor.get === 'function';
4545
};
4646

47-
export const hasOwnProperty = (object: object, key: string) =>
47+
export const hasOwnProperty = (object: object, key: string): boolean =>
4848
Object.prototype.hasOwnProperty.call(object, key) ||
4949
hasGetterFromConstructor(object, key);
5050

@@ -104,6 +104,7 @@ export const getPath = (
104104

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

151+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
150152
export const iterableEquality = (
151153
a: any,
152154
b: any,
153155
aStack: Array<any> = [],
154156
bStack: Array<any> = [],
155-
) => {
157+
): boolean | undefined => {
156158
if (
157159
typeof a !== 'object' ||
158160
typeof b !== 'object' ||
@@ -273,16 +275,17 @@ const isObjectWithKeys = (a: any) =>
273275
!(a instanceof Array) &&
274276
!(a instanceof Date);
275277

278+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
276279
export const subsetEquality = (
277280
object: any,
278281
subset: any,
279-
): undefined | boolean => {
282+
): boolean | undefined => {
280283
// subsetEquality needs to keep track of the references
281284
// it has already visited to avoid infinite loops in case
282285
// there are circular references in the subset passed to it.
283286
const subsetEqualityWithContext = (
284287
seenReferences: WeakMap<object, boolean> = new WeakMap(),
285-
) => (object: any, subset: any): undefined | boolean => {
288+
) => (object: any, subset: any): boolean | undefined => {
286289
if (!isObjectWithKeys(subset)) {
287290
return undefined;
288291
}
@@ -314,15 +317,19 @@ export const subsetEquality = (
314317
return subsetEqualityWithContext()(object, subset);
315318
};
316319

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

322326
return false;
323327
};
324328

325-
export const sparseArrayEquality = (a: unknown, b: unknown) => {
329+
export const sparseArrayEquality = (
330+
a: unknown,
331+
b: unknown,
332+
): boolean | undefined => {
326333
if (!Array.isArray(a) || !Array.isArray(b)) {
327334
return undefined;
328335
}
@@ -347,7 +354,7 @@ export const partition = <T>(
347354
};
348355

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

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

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

369-
export const isOneline = (expected: any, received: any): boolean =>
377+
export const isOneline = (expected: unknown, received: unknown): boolean =>
370378
typeof expected === 'string' &&
371379
typeof received === 'string' &&
372380
(!MULTILINE_REGEXP.test(expected) || !MULTILINE_REGEXP.test(received));

packages/jest-circus/src/globalErrorHandlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const injectGlobalErrorHandlers = (
2828
export const restoreGlobalErrorHandlers = (
2929
parentProcess: NodeJS.Process,
3030
originalErrorHandlers: Circus.GlobalErrorHandlers,
31-
) => {
31+
): void => {
3232
parentProcess.removeListener('uncaughtException', uncaught);
3333
parentProcess.removeListener('unhandledRejection', uncaught);
3434

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import globals from '..';
3434

3535
type Process = NodeJS.Process;
3636

37+
// TODO: hard to type
38+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
3739
export const initialize = ({
3840
config,
3941
environment,

packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
toThrowErrorMatchingSnapshot,
1616
} from 'jest-snapshot';
1717

18-
export default (config: {expand: boolean}) => {
18+
export default (config: {expand: boolean}): void => {
1919
global.expect = expect;
2020
expect.setState({
2121
expand: config.expand,

packages/jest-circus/src/utils.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ const hasEnabledTest = (describeBlock: Circus.DescribeBlock): boolean => {
7373
return hasOwnEnabledTests || describeBlock.children.some(hasEnabledTest);
7474
};
7575

76-
export const getAllHooksForDescribe = (describe: Circus.DescribeBlock) => {
77-
const result: {
78-
beforeAll: Array<Circus.Hook>;
79-
afterAll: Array<Circus.Hook>;
80-
} = {
76+
type DescribeHooks = {
77+
beforeAll: Array<Circus.Hook>;
78+
afterAll: Array<Circus.Hook>;
79+
};
80+
81+
export const getAllHooksForDescribe = (
82+
describe: Circus.DescribeBlock,
83+
): DescribeHooks => {
84+
const result: DescribeHooks = {
8185
afterAll: [],
8286
beforeAll: [],
8387
};
@@ -98,11 +102,13 @@ export const getAllHooksForDescribe = (describe: Circus.DescribeBlock) => {
98102
return result;
99103
};
100104

101-
export const getEachHooksForTest = (test: Circus.TestEntry) => {
102-
const result: {
103-
beforeEach: Array<Circus.Hook>;
104-
afterEach: Array<Circus.Hook>;
105-
} = {afterEach: [], beforeEach: []};
105+
type TestHooks = {
106+
beforeEach: Array<Circus.Hook>;
107+
afterEach: Array<Circus.Hook>;
108+
};
109+
110+
export const getEachHooksForTest = (test: Circus.TestEntry): TestHooks => {
111+
const result: TestHooks = {afterEach: [], beforeEach: []};
106112
let block: Circus.DescribeBlock | undefined | null = test.parent;
107113

108114
do {
@@ -298,7 +304,7 @@ const makeTestResults = (
298304

299305
// Return a string that identifies the test (concat of parent describe block
300306
// names + test title)
301-
export const getTestID = (test: Circus.TestEntry) => {
307+
export const getTestID = (test: Circus.TestEntry): string => {
302308
const titles = [];
303309
let parent: Circus.TestEntry | Circus.DescribeBlock | undefined = test;
304310
do {
@@ -341,7 +347,7 @@ export const addErrorToEachTestUnderDescribe = (
341347
describeBlock: Circus.DescribeBlock,
342348
error: Circus.Exception,
343349
asyncError: Circus.Exception,
344-
) => {
350+
): void => {
345351
for (const test of describeBlock.tests) {
346352
test.errors.push([error, asyncError]);
347353
}

packages/jest-cli/src/cli/args.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {Config} from '@jest/types';
99
import {isJSONString} from 'jest-config';
1010
import isCI = require('is-ci');
1111

12-
export const check = (argv: Config.Argv) => {
12+
export const check = (argv: Config.Argv): true => {
1313
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
1414
throw new Error(
1515
'Both --runInBand and --maxWorkers were specified, but these two ' +

packages/jest-cli/src/cli/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {sync as realpath} from 'realpath-native';
1919
import init from '../init';
2020
import * as args from './args';
2121

22-
export async function run(maybeArgv?: Array<string>, project?: Config.Path) {
22+
export async function run(
23+
maybeArgv?: Array<string>,
24+
project?: Config.Path,
25+
): Promise<void> {
2326
try {
2427
const argv: Config.Argv = buildArgv(maybeArgv);
2528

packages/jest-cli/src/init/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ type PromptsResults = {
3434

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

37-
export default async (rootDir: string = realpath(process.cwd())) => {
37+
export default async (
38+
rootDir: string = realpath(process.cwd()),
39+
): Promise<void> => {
3840
// prerequisite checks
3941
const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON);
4042

0 commit comments

Comments
 (0)