Skip to content

Commit c9ee6e4

Browse files
authored
refactor: import jest as global; unify import style of some modules (#6898)
* refactor: import jest as global * fix react
1 parent e97dc0d commit c9ee6e4

File tree

59 files changed

+170
-132
lines changed

Some content is hidden

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

59 files changed

+170
-132
lines changed

.eslintrc.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ module.exports = {
166166
// selector:
167167
// @ 'ExportDefaultDeclaration > Identifier, ExportNamedDeclaration[source=null] > ExportSpecifier',
168168
// message: 'Export in one statement'
169-
// }
169+
// },
170+
...['path', 'fs-extra', 'webpack', 'lodash'].map((m) => ({
171+
selector: `ImportDeclaration[importKind=value]:has(Literal[value=${m}]) > ImportSpecifier[importKind=value]`,
172+
message:
173+
'Default-import this, both for readability and interoperability with ESM',
174+
})),
170175
],
171176
'no-template-curly-in-string': WARNING,
172177
'no-unused-expressions': [WARNING, {allowTaggedTemplates: true}],
@@ -312,5 +317,11 @@ module.exports = {
312317
'@typescript-eslint/explicit-module-boundary-types': OFF,
313318
},
314319
},
320+
{
321+
files: ['*.test.ts', '*.test.tsx'],
322+
rules: {
323+
'import/no-extraneous-dependencies': OFF,
324+
},
325+
},
315326
],
316327
};

__tests__/validate-package-json.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
/* eslint-disable import/no-extraneous-dependencies */
87

98
import {Globby} from '@docusaurus/utils';
109
import fs from 'fs-extra';

packages/docusaurus-logger/src/__tests__/index.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import logger from '../index';
910

1011
describe('formatters', () => {

packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/index.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import remark from 'remark';
1011
import mdx from 'remark-mdx';

packages/docusaurus-mdx-loader/src/remark/unwrapMdxCodeBlocks/__tests__/index.test.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {join} from 'path';
8+
import path from 'path';
99
import remark from 'remark';
1010
import mdx from 'remark-mdx';
1111
import vfile from 'to-vfile';
1212
import plugin from '..';
1313

14-
const processFixture = async (name) => {
15-
const path = join(__dirname, '__fixtures__', name);
16-
const file = await vfile.read(path);
14+
const processFixture = async (name: string) => {
15+
const file = await vfile.read(path.join(__dirname, '__fixtures__', name));
1716
const result = await remark().use(mdx).use(plugin).process(file);
1817
return result.toString();
1918
};
2019

21-
const processFixtureAST = async (name) => {
22-
const path = join(__dirname, '__fixtures__', name);
23-
const file = await vfile.read(path);
20+
const processFixtureAST = async (name: string) => {
21+
const file = await vfile.read(path.join(__dirname, '__fixtures__', name));
2422
return remark().use(mdx).use(plugin).parse(file);
2523
};
2624

packages/docusaurus-migrate/src/__tests__/migration.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import {migrateDocusaurusProject} from '../index';
910
import path from 'path';
1011
import fs from 'fs-extra';

packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import fs from 'fs-extra';
1011
import {createBlogFeedFiles} from '../feed';

packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import pluginContentBlog from '../index';
1011
import type {DocusaurusConfig, LoadContext, I18n} from '@docusaurus/types';

packages/docusaurus-plugin-content-blog/src/__tests__/linkify.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import fs from 'fs-extra';
910
import path from 'path';
1011
import {linkify, type LinkifyParams, getSourceToPermalink} from '../blogUtils';

packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import {cliDocsVersionCommand} from '../cli';
1011
import type {

packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import {loadContext} from '@docusaurus/core/src/server/index';
1011
import {

packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import {isMatch} from 'picomatch';
1011
import commander from 'commander';
@@ -29,7 +30,7 @@ import type {
2930
} from '../sidebars/types';
3031
import {toSidebarsProp} from '../props';
3132

32-
import {validate} from 'webpack';
33+
import webpack from 'webpack';
3334
import {DefaultSidebarItemsGenerator} from '../sidebars/generator';
3435
import {DisabledSidebars} from '../sidebars';
3536

@@ -311,7 +312,7 @@ describe('simple website', () => {
311312
undefined,
312313
content,
313314
);
314-
const errors = validate(config);
315+
const errors = webpack.validate(config);
315316
expect(errors).toBeUndefined();
316317
});
317318

packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import fs from 'fs-extra';
910
import path from 'path';
1011
import shell from 'shelljs';

packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import path from 'path';
910
import {
1011
getVersionsFilePath,

packages/docusaurus-plugin-content-docs/src/markdown/__tests__/linkify.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import fs from 'fs-extra';
910
import path from 'path';
1011
import {linkify} from '../linkify';

packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import {DefaultSidebarItemsGenerator} from '../generator';
910
import type {SidebarItemsGenerator} from '../types';
1011
import {DefaultNumberPrefixParser} from '../../numberPrefix';

packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import {processSidebars} from '../processor';
910
import type {
1011
SidebarItem,

packages/docusaurus-theme-classic/src/theme/MDXComponents/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77

88
import React, {
9-
type ComponentProps,
109
isValidElement,
10+
type ComponentProps,
1111
type ReactElement,
1212
} from 'react';
1313
import Head from '@docusaurus/Head';
@@ -52,7 +52,7 @@ const MDXComponents: MDXComponentsObject = {
5252
const shouldBeInline = React.Children.toArray(props.children).every(
5353
(el) =>
5454
(typeof el === 'string' && !el.includes('\n')) ||
55-
(React.isValidElement(el) && inlineElements.includes(el.props.mdxType)),
55+
(isValidElement(el) && inlineElements.includes(el.props.mdxType)),
5656
);
5757

5858
return shouldBeInline ? <code {...props} /> : <CodeBlock {...props} />;

packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import React, {
99
useState,
1010
cloneElement,
11-
Children,
1211
isValidElement,
1312
type ReactElement,
1413
} from 'react';
@@ -40,7 +39,7 @@ function TabsComponent(props: Props): JSX.Element {
4039
groupId,
4140
className,
4241
} = props;
43-
const children = Children.map(props.children, (child) => {
42+
const children = React.Children.map(props.children, (child) => {
4443
if (isValidElement(child) && isTabItem(child)) {
4544
return child;
4645
}

packages/docusaurus-theme-common/src/components/Collapsible/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import React, {
1111
useEffect,
1212
useRef,
1313
useCallback,
14+
useLayoutEffect,
1415
type RefObject,
1516
type Dispatch,
1617
type SetStateAction,
1718
type ReactNode,
18-
useLayoutEffect,
1919
} from 'react';
2020

2121
const DefaultAnimationEasing = 'ease-in-out';

packages/docusaurus-theme-common/src/components/Details/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
*/
77

88
import React, {
9-
type ComponentProps,
10-
type ReactElement,
119
useRef,
1210
useState,
11+
type ComponentProps,
12+
type ReactElement,
1313
} from 'react';
1414
import useIsBrowser from '@docusaurus/useIsBrowser';
1515
import clsx from 'clsx';

packages/docusaurus-theme-common/src/utils/announcementBarUtils.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import React, {
1010
useEffect,
1111
useCallback,
1212
useMemo,
13-
type ReactNode,
1413
useContext,
15-
createContext,
14+
type ReactNode,
1615
} from 'react';
1716
import useIsBrowser from '@docusaurus/useIsBrowser';
1817
import {createStorageSlot} from './storageUtils';
@@ -96,7 +95,9 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
9695
);
9796
};
9897

99-
const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
98+
const AnnouncementBarContext = React.createContext<AnnouncementBarAPI | null>(
99+
null,
100+
);
100101

101102
export function AnnouncementBarProvider({
102103
children,

packages/docusaurus-theme-common/src/utils/colorModeUtils.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React, {
1111
useEffect,
1212
useContext,
1313
useMemo,
14+
useRef,
1415
type ReactNode,
1516
} from 'react';
1617
import {ReactContextError} from './reactUtils';
@@ -96,7 +97,7 @@ function useColorModeContextValue(): ColorModeContextValue {
9697
// be reset to dark when exiting print mode, disregarding user settings. When
9798
// the listener fires only because of a print/screen switch, we don't change
9899
// color mode. See https://github.com/facebook/docusaurus/pull/6490
99-
const previousMediaIsPrint = React.useRef(false);
100+
const previousMediaIsPrint = useRef(false);
100101

101102
useEffect(() => {
102103
if (disableSwitch && !respectPrefersColorScheme) {

packages/docusaurus-theme-common/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
*/
77

88
import React, {
9-
createContext,
10-
type ReactNode,
119
useContext,
1210
useEffect,
1311
useMemo,
1412
useState,
13+
type ReactNode,
1514
} from 'react';
1615
import {useThemeConfig, type DocsVersionPersistence} from '../useThemeConfig';
1716
import {isDocsPluginEnabled} from '../docsUtils';
@@ -131,7 +130,9 @@ function useContextValue() {
131130

132131
type DocsPreferredVersionContextValue = ReturnType<typeof useContextValue>;
133132

134-
const Context = createContext<DocsPreferredVersionContextValue | null>(null);
133+
const Context = React.createContext<DocsPreferredVersionContextValue | null>(
134+
null,
135+
);
135136

136137
export function DocsPreferredVersionContextProvider({
137138
children,

packages/docusaurus-theme-common/src/utils/docsUtils.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React, {createContext, type ReactNode, useContext} from 'react';
8+
import React, {type ReactNode, useContext} from 'react';
99
import {
1010
useActivePlugin,
1111
useAllDocsData,
@@ -29,7 +29,7 @@ export const isDocsPluginEnabled: boolean = !!useAllDocsData;
2929
// Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
3030
const EmptyContextValue: unique symbol = Symbol('EmptyContext');
3131

32-
const DocsVersionContext = createContext<
32+
const DocsVersionContext = React.createContext<
3333
PropVersionMetadata | typeof EmptyContextValue
3434
>(EmptyContextValue);
3535

@@ -69,7 +69,7 @@ export function useDocById(id: string | undefined): PropVersionDoc | undefined {
6969
return doc;
7070
}
7171

72-
const DocsSidebarContext = createContext<
72+
const DocsSidebarContext = React.createContext<
7373
PropSidebar | null | typeof EmptyContextValue
7474
>(EmptyContextValue);
7575

packages/docusaurus-theme-common/src/utils/mobileSecondaryMenu.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
import React, {
99
useState,
10-
type ReactNode,
1110
useContext,
12-
createContext,
1311
useEffect,
14-
type ComponentType,
1512
useMemo,
13+
type ReactNode,
14+
type ComponentType,
1615
} from 'react';
1716
import {ReactContextError} from './reactUtils';
1817

@@ -46,7 +45,7 @@ function useContextValue() {
4645

4746
type ContextValue = ReturnType<typeof useContextValue>;
4847

49-
const Context = createContext<ContextValue | null>(null);
48+
const Context = React.createContext<ContextValue | null>(null);
5049

5150
export function MobileSecondaryMenuProvider({
5251
children,

packages/docusaurus-theme-common/src/utils/scrollUtils.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
*/
77

88
import React, {
9-
createContext,
10-
type ReactNode,
119
useCallback,
1210
useContext,
1311
useEffect,
1412
useLayoutEffect,
1513
useMemo,
1614
useRef,
15+
type ReactNode,
1716
} from 'react';
1817
import {useDynamicCallback, ReactContextError} from './reactUtils';
1918
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
@@ -57,7 +56,7 @@ function useScrollControllerContextValue(): ScrollController {
5756
);
5857
}
5958

60-
const ScrollMonitorContext = createContext<ScrollController | undefined>(
59+
const ScrollMonitorContext = React.createContext<ScrollController | undefined>(
6160
undefined,
6261
);
6362

0 commit comments

Comments
 (0)