refactor: minor ESLint improvements (#5981)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
Joshua Chen 2021-12-04 00:38:29 +08:00 committed by GitHub
parent cfae5d0933
commit bfd7fd9d8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 46 deletions

View file

@ -49,14 +49,7 @@ module.exports = {
'import/no-unresolved': [ 'import/no-unresolved': [
ERROR, ERROR,
{ {
ignore: [ ignore: ['^@theme', '^@docusaurus', '^@generated', '^@site'],
'^@theme',
'^@docusaurus',
'^@generated',
'^@site',
'unist',
'mdast',
],
}, },
], ],
'import/extensions': OFF, 'import/extensions': OFF,
@ -149,6 +142,7 @@ module.exports = {
allowSingleExtends: true, allowSingleExtends: true,
}, },
], ],
'@typescript-eslint/method-signature-style': ERROR,
'no-restricted-imports': [ 'no-restricted-imports': [
ERROR, ERROR,
{ {

View file

@ -5,12 +5,12 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import {DocusaurusContext, Plugin} from '@docusaurus/types'; import {DocusaurusContext, Plugin, PostCssOptions} from '@docusaurus/types';
import type {ThemeConfig} from '@docusaurus/theme-common'; import type {ThemeConfig} from '@docusaurus/theme-common';
import {getTranslationFiles, translateThemeConfig} from './translations'; import {getTranslationFiles, translateThemeConfig} from './translations';
import path from 'path'; import path from 'path';
import {createRequire} from 'module'; import {createRequire} from 'module';
import type {AcceptedPlugin, Plugin as PostCssPlugin} from 'postcss'; import type {Plugin as PostCssPlugin} from 'postcss';
import rtlcss from 'rtlcss'; import rtlcss from 'rtlcss';
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations'; import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
@ -132,7 +132,12 @@ export default function docusaurusThemeClassic(
}, },
getTranslationFiles: async () => getTranslationFiles({themeConfig}), getTranslationFiles: async () => getTranslationFiles({themeConfig}),
translateThemeConfig,
translateThemeConfig: (params) =>
translateThemeConfig({
themeConfig: params.themeConfig as ThemeConfig,
translationFiles: params.translationFiles,
}),
getDefaultCodeTranslationMessages() { getDefaultCodeTranslationMessages() {
return readDefaultCodeTranslationMessages({ return readDefaultCodeTranslationMessages({
@ -178,7 +183,7 @@ export default function docusaurusThemeClassic(
}; };
}, },
configurePostCss(postCssOptions: {plugins: AcceptedPlugin[]}) { configurePostCss(postCssOptions: PostCssOptions) {
if (direction === 'rtl') { if (direction === 'rtl') {
const resolvedInfimaFile = require.resolve(getInfimaCSSFile(direction)); const resolvedInfimaFile = require.resolve(getInfimaCSSFile(direction));
const plugin: PostCssPlugin = { const plugin: PostCssPlugin = {

View file

@ -151,6 +151,8 @@ export function translateThemeConfig({
themeConfig, themeConfig,
translationFiles, translationFiles,
}: { }: {
// Why partial? To make TS correctly figure out the contravariance in parameter.
// In practice it's always normalized
themeConfig: ThemeConfig; themeConfig: ThemeConfig;
translationFiles: TranslationFile[]; translationFiles: TranslationFile[];
}): ThemeConfig { }): ThemeConfig {

View file

@ -12,10 +12,8 @@ declare module '@docusaurus/theme-search-algolia' {
declare module '@theme/hooks/useSearchQuery' { declare module '@theme/hooks/useSearchQuery' {
export interface SearchQuery { export interface SearchQuery {
searchQuery: string; searchQuery: string;
setSearchQuery: (newSearchQuery: string) => void;
setSearchQuery(newSearchQuery: string): void; generateSearchPageLink: (targetSearchQuery: string) => string;
generateSearchPageLink(targetSearchQuery: string): string;
} }
export default function useSearchQuery(): SearchQuery; export default function useSearchQuery(): SearchQuery;

View file

@ -235,6 +235,8 @@ ${logKeys(untranslatedKeys)}`),
} }
async function updateCodeTranslations() { async function updateCodeTranslations() {
// Order is important. The log messages must be in the same order as execution
// eslint-disable-next-line no-restricted-syntax
for (const theme of Themes) { for (const theme of Themes) {
const {baseFile, localesFiles} = await getCodeTranslationFiles(theme.name); const {baseFile, localesFiles} = await getCodeTranslationFiles(theme.name);
logSection(`Will update base file for ${theme.name}`); logSection(`Will update base file for ${theme.name}`);
@ -259,6 +261,7 @@ async function updateCodeTranslations() {
); );
} }
} else { } else {
// eslint-disable-next-line no-restricted-syntax
for (const localeFile of localesFiles) { for (const localeFile of localesFiles) {
logSection( logSection(
`Will update ${path.basename( `Will update ${path.basename(

View file

@ -21,7 +21,7 @@ describe('theme-translations package', () => {
await fs await fs
.readdirSync(baseMessagesDirPath) .readdirSync(baseMessagesDirPath)
.reduce(async (messages, baseMessagesFile) => { .reduce(async (messages, baseMessagesFile) => {
messages = { const newMessages = {
...(await messages), ...(await messages),
...JSON.parse( ...JSON.parse(
await fs.readFile( await fs.readFile(
@ -29,7 +29,7 @@ describe('theme-translations package', () => {
), ),
), ),
}; };
return messages; return newMessages;
}, {}), }, {}),
(_, key) => !key.endsWith('___DESCRIPTION'), (_, key) => !key.endsWith('___DESCRIPTION'),
); );

View file

@ -220,10 +220,10 @@ export interface Props extends LoadContext, InjectedHtmlTags {
} }
export interface PluginContentLoadedActions { export interface PluginContentLoadedActions {
addRoute(config: RouteConfig): void; addRoute: (config: RouteConfig) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
createData(name: string, data: any): Promise<string>; createData: (name: string, data: any) => Promise<string>;
setGlobalData<T = unknown>(data: T): void; setGlobalData: <T = unknown>(data: T) => void;
} }
export type AllContent = Record< export type AllContent = Record<
@ -239,32 +239,32 @@ export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
export interface Plugin<Content = unknown> { export interface Plugin<Content = unknown> {
name: string; name: string;
loadContent?(): Promise<Content>; loadContent?: () => Promise<Content>;
contentLoaded?({ contentLoaded?: ({
content, content,
actions, actions,
}: { }: {
content: Content; // the content loaded by this plugin instance content: Content; // the content loaded by this plugin instance
allContent: AllContent; // content loaded by ALL the plugins allContent: AllContent; // content loaded by ALL the plugins
actions: PluginContentLoadedActions; actions: PluginContentLoadedActions;
}): Promise<void>; }) => Promise<void>;
routesLoaded?(routes: RouteConfig[]): void; // TODO remove soon, deprecated (alpha-60) routesLoaded?: (routes: RouteConfig[]) => void; // TODO remove soon, deprecated (alpha-60)
postBuild?(props: Props): void; postBuild?: (props: Props) => void;
postStart?(props: Props): void; postStart?: (props: Props) => void;
// TODO refactor the configureWebpack API surface: use an object instead of multiple params (requires breaking change) // TODO refactor the configureWebpack API surface: use an object instead of multiple params (requires breaking change)
configureWebpack?( configureWebpack?: (
config: Configuration, config: Configuration,
isServer: boolean, isServer: boolean,
utils: ConfigureWebpackUtils, utils: ConfigureWebpackUtils,
content: Content, content: Content,
): Configuration & {mergeStrategy?: ConfigureWebpackFnMergeStrategy}; ) => Configuration & {mergeStrategy?: ConfigureWebpackFnMergeStrategy};
configurePostCss?(options: PostCssOptions): PostCssOptions; configurePostCss?: (options: PostCssOptions) => PostCssOptions;
getThemePath?(): string; getThemePath?: () => string;
getTypeScriptThemePath?(): string; getTypeScriptThemePath?: () => string;
getPathsToWatch?(): string[]; getPathsToWatch?: () => string[];
getClientModules?(): string[]; getClientModules?: () => string[];
extendCli?(cli: Command): void; extendCli?: (cli: Command) => void;
injectHtmlTags?({content}: {content: Content}): { injectHtmlTags?: ({content}: {content: Content}) => {
headTags?: HtmlTags; headTags?: HtmlTags;
preBodyTags?: HtmlTags; preBodyTags?: HtmlTags;
postBodyTags?: HtmlTags; postBodyTags?: HtmlTags;
@ -272,31 +272,31 @@ export interface Plugin<Content = unknown> {
// TODO before/afterDevServer implementation // TODO before/afterDevServer implementation
// translations // translations
getTranslationFiles?({ getTranslationFiles?: ({
content, content,
}: { }: {
content: Content; content: Content;
}): Promise<TranslationFiles>; }) => Promise<TranslationFiles>;
getDefaultCodeTranslationMessages?(): Promise< getDefaultCodeTranslationMessages?: () => Promise<
Record< Record<
string, // id string, // id
string // message string // message
> >
>; >;
translateContent?({ translateContent?: ({
content, content,
translationFiles, translationFiles,
}: { }: {
content: Content; // the content loaded by this plugin instance content: Content; // the content loaded by this plugin instance
translationFiles: TranslationFiles; translationFiles: TranslationFiles;
}): Content; }) => Content;
translateThemeConfig?({ translateThemeConfig?: ({
themeConfig, themeConfig,
translationFiles, translationFiles,
}: { }: {
themeConfig: ThemeConfig; themeConfig: ThemeConfig;
translationFiles: TranslationFiles; translationFiles: TranslationFiles;
}): ThemeConfig; }) => ThemeConfig;
} }
export type InitializedPlugin<Content = unknown> = Plugin<Content> & { export type InitializedPlugin<Content = unknown> = Plugin<Content> & {
@ -310,9 +310,9 @@ export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & {
export type PluginModule = { export type PluginModule = {
<T, X>(context: LoadContext, options: T): Plugin<X>; <T, X>(context: LoadContext, options: T): Plugin<X>;
validateOptions?<T>(data: OptionValidationContext<T>): T; validateOptions?: <T>(data: OptionValidationContext<T>) => T;
validateThemeConfig?<T>(data: ThemeConfigValidationContext<T>): T; validateThemeConfig?: <T>(data: ThemeConfigValidationContext<T>) => T;
getSwizzleComponentList?(): string[]; getSwizzleComponentList?: () => string[];
}; };
export type ImportedPluginModule = PluginModule & { export type ImportedPluginModule = PluginModule & {