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

View file

@ -5,12 +5,12 @@
* 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 {getTranslationFiles, translateThemeConfig} from './translations';
import path from 'path';
import {createRequire} from 'module';
import type {AcceptedPlugin, Plugin as PostCssPlugin} from 'postcss';
import type {Plugin as PostCssPlugin} from 'postcss';
import rtlcss from 'rtlcss';
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
@ -132,7 +132,12 @@ export default function docusaurusThemeClassic(
},
getTranslationFiles: async () => getTranslationFiles({themeConfig}),
translateThemeConfig,
translateThemeConfig: (params) =>
translateThemeConfig({
themeConfig: params.themeConfig as ThemeConfig,
translationFiles: params.translationFiles,
}),
getDefaultCodeTranslationMessages() {
return readDefaultCodeTranslationMessages({
@ -178,7 +183,7 @@ export default function docusaurusThemeClassic(
};
},
configurePostCss(postCssOptions: {plugins: AcceptedPlugin[]}) {
configurePostCss(postCssOptions: PostCssOptions) {
if (direction === 'rtl') {
const resolvedInfimaFile = require.resolve(getInfimaCSSFile(direction));
const plugin: PostCssPlugin = {

View file

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

View file

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

View file

@ -235,6 +235,8 @@ ${logKeys(untranslatedKeys)}`),
}
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) {
const {baseFile, localesFiles} = await getCodeTranslationFiles(theme.name);
logSection(`Will update base file for ${theme.name}`);
@ -259,6 +261,7 @@ async function updateCodeTranslations() {
);
}
} else {
// eslint-disable-next-line no-restricted-syntax
for (const localeFile of localesFiles) {
logSection(
`Will update ${path.basename(

View file

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

View file

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