mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 08:37:25 +02:00
refactor(v2): correct some of type errors reported by eslint (#4382)
* fix: correct some of type errors reported by eslint * fix: remove unnecessary import
This commit is contained in:
parent
a39c62f644
commit
bfe52cdae3
10 changed files with 32 additions and 21 deletions
|
@ -20,7 +20,7 @@ export type RedirectFileMetadata = {
|
|||
fileContent: string;
|
||||
};
|
||||
|
||||
export function createToUrl(baseUrl: string, to: string) {
|
||||
export function createToUrl(baseUrl: string, to: string): string {
|
||||
return normalizeUrl([baseUrl, to]);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,10 @@ import {
|
|||
getActiveVersion,
|
||||
getActiveDocContext,
|
||||
getDocVersionSuggestions,
|
||||
GetActivePluginOptions,
|
||||
ActivePlugin,
|
||||
ActiveDocContext,
|
||||
DocVersionSuggestions,
|
||||
GetActivePluginOptions,
|
||||
} from '../../client/docsClientUtils';
|
||||
|
||||
export const useAllDocsData = (): Record<string, GlobalPluginData> =>
|
||||
|
@ -28,7 +30,9 @@ export const useAllDocsData = (): Record<string, GlobalPluginData> =>
|
|||
export const useDocsData = (pluginId: string | undefined) =>
|
||||
usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData;
|
||||
|
||||
export const useActivePlugin = (options: GetActivePluginOptions = {}) => {
|
||||
export const useActivePlugin = (
|
||||
options: GetActivePluginOptions = {},
|
||||
): ActivePlugin | undefined => {
|
||||
const data = useAllDocsData();
|
||||
const {pathname} = useLocation();
|
||||
return getActivePlugin(data, pathname, options);
|
||||
|
@ -57,27 +61,35 @@ export const useVersions = (pluginId: string | undefined): GlobalVersion[] => {
|
|||
return data.versions;
|
||||
};
|
||||
|
||||
export const useLatestVersion = (pluginId: string | undefined) => {
|
||||
export const useLatestVersion = (
|
||||
pluginId: string | undefined,
|
||||
): GlobalVersion => {
|
||||
const data = useDocsData(pluginId);
|
||||
return getLatestVersion(data);
|
||||
};
|
||||
|
||||
// Note: return undefined on doc-unrelated pages,
|
||||
// because there's no version currently considered as active
|
||||
export const useActiveVersion = (pluginId: string | undefined) => {
|
||||
export const useActiveVersion = (
|
||||
pluginId: string | undefined,
|
||||
): GlobalVersion | undefined => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getActiveVersion(data, pathname);
|
||||
};
|
||||
|
||||
export const useActiveDocContext = (pluginId: string | undefined) => {
|
||||
export const useActiveDocContext = (
|
||||
pluginId: string | undefined,
|
||||
): ActiveDocContext => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getActiveDocContext(data, pathname);
|
||||
};
|
||||
|
||||
// Useful to say "hey, you are not on the latest docs version, please switch"
|
||||
export const useDocVersionSuggestions = (pluginId: string | undefined) => {
|
||||
export const useDocVersionSuggestions = (
|
||||
pluginId: string | undefined,
|
||||
): DocVersionSuggestions => {
|
||||
const data = useDocsData(pluginId);
|
||||
const {pathname} = useLocation();
|
||||
return getDocVersionSuggestions(data, pathname);
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
|
||||
export const useTitleFormatter = (title?: string | undefined) => {
|
||||
export const useTitleFormatter = (title?: string | undefined): string => {
|
||||
const {siteConfig = {}} = useDocusaurusContext();
|
||||
const {title: siteTitle, titleDelimiter = '|'} = siteConfig;
|
||||
return title && title.trim().length
|
||||
|
|
|
@ -28,7 +28,6 @@ function createTestHelpers({
|
|||
|
||||
function testFail(value: unknown) {
|
||||
expect(() => Joi.attempt(value, schema)).toThrowErrorMatchingSnapshot(
|
||||
// @ts-expect-error: seems ok at runtime, but bad typedef
|
||||
`for value=${JSON.stringify(value)}`,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ export const logValidationBugReportHint = (): void => {
|
|||
export function normalizePluginOptions<T extends {id?: string}>(
|
||||
schema: Joi.ObjectSchema<T>,
|
||||
options: unknown,
|
||||
) {
|
||||
): T {
|
||||
// All plugins can be provided an "id" option (multi-instance support)
|
||||
// we add schema validation automatically
|
||||
const finalSchema = schema.append({
|
||||
|
@ -51,7 +51,7 @@ export function normalizePluginOptions<T extends {id?: string}>(
|
|||
logValidationBugReportHint();
|
||||
if (isValidationDisabledEscapeHatch) {
|
||||
console.error(error);
|
||||
return options;
|
||||
return options as T;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ export function normalizePluginOptions<T extends {id?: string}>(
|
|||
export function normalizeThemeConfig<T>(
|
||||
schema: Joi.ObjectSchema<T>,
|
||||
themeConfig: unknown,
|
||||
) {
|
||||
): T {
|
||||
// A theme should only validate his "slice" of the full themeConfig,
|
||||
// not the whole object, so we allow unknown attributes
|
||||
// otherwise one theme would fail validating the data of another theme
|
||||
|
@ -76,7 +76,7 @@ export function normalizeThemeConfig<T>(
|
|||
logValidationBugReportHint();
|
||||
if (isValidationDisabledEscapeHatch) {
|
||||
console.error(error);
|
||||
return themeConfig;
|
||||
return themeConfig as T;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
// Too dynamic
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
function flat(target: unknown): Record<string, any> {
|
||||
const delimiter = '.';
|
||||
const output: Record<string, any> = {};
|
||||
|
|
|
@ -15,11 +15,11 @@ import initPlugins from '../server/plugins/init';
|
|||
import {flatten} from 'lodash';
|
||||
import {parseMarkdownHeadingId} from '@docusaurus/utils';
|
||||
|
||||
export function unwrapMarkdownLinks(line) {
|
||||
export function unwrapMarkdownLinks(line: string): string {
|
||||
return line.replace(/\[([^\]]+)\]\([^)]+\)/g, (match, p1) => p1);
|
||||
}
|
||||
|
||||
function addHeadingId(line, slugger) {
|
||||
function addHeadingId(line: string, slugger: GithubSlugger): string {
|
||||
let headingLevel = 0;
|
||||
while (line.charAt(headingLevel) === '#') {
|
||||
headingLevel += 1;
|
||||
|
@ -35,7 +35,7 @@ function addHeadingId(line, slugger) {
|
|||
export function transformMarkdownHeadingLine(
|
||||
line: string,
|
||||
slugger: GithubSlugger,
|
||||
) {
|
||||
): string {
|
||||
if (!line.startsWith('#')) {
|
||||
throw new Error(`Line is not a markdown heading: ${line}`);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ function isTranslatableSourceCodePath(filePath: string): boolean {
|
|||
return TranslatableSourceCodeExtension.has(nodePath.extname(filePath));
|
||||
}
|
||||
|
||||
function getSiteSourceCodeFilePaths(siteDir): string[] {
|
||||
function getSiteSourceCodeFilePaths(siteDir: string): string[] {
|
||||
return [nodePath.join(siteDir, SRC_DIR_NAME)];
|
||||
}
|
||||
|
||||
|
@ -180,7 +180,7 @@ function extractSourceCodeAstTranslations(
|
|||
return `File=${sourceCodeFilePath} at line=${node.loc?.start.line}`;
|
||||
}
|
||||
function generateCode(node: Node) {
|
||||
return generate(node as any).code;
|
||||
return generate(node).code;
|
||||
}
|
||||
|
||||
const translations: Record<string, TranslationMessage> = {};
|
||||
|
|
|
@ -21,7 +21,7 @@ export default class WaitPlugin {
|
|||
this.filepath = options.filepath;
|
||||
}
|
||||
|
||||
apply(compiler: Compiler) {
|
||||
apply(compiler: Compiler): void {
|
||||
// Before finishing the compilation step
|
||||
compiler.hooks.make.tapAsync('WaitPlugin', (compilation, callback) => {
|
||||
// To prevent 'waitFile' error on waiting non-existing directory
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
"rootDir": "src",
|
||||
"outDir": "lib",
|
||||
"noImplicitAny": false,
|
||||
"jsx": "react",
|
||||
"jsx": "react"
|
||||
},
|
||||
"exclude": ["node_modules", "**/__tests__/**/*", "**/lib/**/*", "src/client"]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue