Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v2): correct some of type errors reported by eslint #4382

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
24 changes: 18 additions & 6 deletions packages/docusaurus-plugin-content-docs/src/theme/hooks/useDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import {
getActiveVersion,
getActiveDocContext,
getDocVersionSuggestions,
GetActivePluginOptions,
ActivePlugin,
ActiveDocContext,
DocVersionSuggestions,
GetActivePluginOptions,
} from '../../client/docsClientUtils';

export const useAllDocsData = (): Record<string, GlobalPluginData> =>
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-common/src/utils/generalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus-utils-validation/src/validationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -51,7 +51,7 @@ export function normalizePluginOptions<T extends {id?: string}>(
logValidationBugReportHint();
if (isValidationDisabledEscapeHatch) {
console.error(error);
return options;
return options as T;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unsafe but it happens only if we want to ignore validation

} else {
throw error;
}
Expand All @@ -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
Expand All @@ -76,7 +76,7 @@ export function normalizeThemeConfig<T>(
logValidationBugReportHint();
if (isValidationDisabledEscapeHatch) {
console.error(error);
return themeConfig;
return themeConfig as T;
} else {
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/client/flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {};
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus/src/commands/writeHeadingIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}

Expand Down Expand Up @@ -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> = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/webpack/plugins/WaitPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"rootDir": "src",
"outDir": "lib",
"noImplicitAny": false,
"jsx": "react",
"jsx": "react"
},
"exclude": ["node_modules", "**/__tests__/**/*", "**/lib/**/*", "src/client"]
}