Skip to content

Commit

Permalink
refactor: minor ESLint improvements (#5981)
Browse files Browse the repository at this point in the history
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
  • Loading branch information
Josh-Cena and slorber committed Dec 3, 2021
1 parent cfae5d0 commit bfd7fd9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 46 deletions.
10 changes: 2 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -149,6 +142,7 @@ module.exports = {
allowSingleExtends: true,
},
],
'@typescript-eslint/method-signature-style': ERROR,
'no-restricted-imports': [
ERROR,
{
Expand Down
13 changes: 9 additions & 4 deletions packages/docusaurus-theme-classic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-classic/src/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-theme-translations/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-theme-translations/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ describe('theme-translations package', () => {
await fs
.readdirSync(baseMessagesDirPath)
.reduce(async (messages, baseMessagesFile) => {
messages = {
const newMessages = {
...(await messages),
...JSON.parse(
await fs.readFile(
path.join(baseMessagesDirPath, baseMessagesFile),
),
),
};
return messages;
return newMessages;
}, {}),
(_, key) => !key.endsWith('___DESCRIPTION'),
);
Expand Down
56 changes: 28 additions & 28 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -239,64 +239,64 @@ 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;
};
// 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> & {
Expand All @@ -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 & {
Expand Down

0 comments on commit bfd7fd9

Please sign in to comment.