mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-18 19:46:57 +02:00
refactor: ensure all types are using index signature instead of Record (#6995)
* refactor: ensure all types are using index signature instead of Record * kick CI
This commit is contained in:
parent
e8800b9d49
commit
87592bca03
99 changed files with 339 additions and 307 deletions
|
@ -1535,10 +1535,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle
|
|||
"type": "autogenerated",
|
||||
},
|
||||
"numberPrefixParser": [Function],
|
||||
"options": {
|
||||
"sidebarCollapsed": true,
|
||||
"sidebarCollapsible": true,
|
||||
},
|
||||
"version": {
|
||||
"contentPath": "docs",
|
||||
"versionName": "current",
|
||||
|
|
|
@ -43,7 +43,7 @@ const createFakeDocFile = ({
|
|||
markdown = 'some markdown content',
|
||||
}: {
|
||||
source: string;
|
||||
frontMatter?: Record<string, string>;
|
||||
frontMatter?: {[key: string]: string};
|
||||
markdown?: string;
|
||||
}): DocFile => {
|
||||
const content = `---
|
||||
|
|
|
@ -13,11 +13,11 @@ function testField(params: {
|
|||
prefix: string;
|
||||
validFrontMatters: DocFrontMatter[];
|
||||
convertibleFrontMatter?: [
|
||||
ConvertibleFrontMatter: Record<string, unknown>,
|
||||
ConvertibleFrontMatter: {[key: string]: unknown},
|
||||
ConvertedFrontMatter: DocFrontMatter,
|
||||
][];
|
||||
invalidFrontMatters?: [
|
||||
InvalidFrontMatter: Record<string, unknown>,
|
||||
InvalidFrontMatter: {[key: string]: unknown},
|
||||
ErrorMessage: string,
|
||||
][];
|
||||
}) {
|
||||
|
|
|
@ -52,7 +52,7 @@ Available ids are:\n- ${version.docs.map((d) => d.unversionedId).join('\n- ')}`,
|
|||
|
||||
const createFakeActions = (contentDir: string) => {
|
||||
const routeConfigs: RouteConfig[] = [];
|
||||
const dataContainer: Record<string, unknown> = {};
|
||||
const dataContainer: {[key: string]: unknown} = {};
|
||||
const globalDataContainer: {pluginName?: {pluginId: unknown}} = {};
|
||||
|
||||
const actions = {
|
||||
|
|
|
@ -17,7 +17,7 @@ function getCategoryGeneratedIndexMetadata({
|
|||
}: {
|
||||
category: SidebarItemCategoryWithGeneratedIndex;
|
||||
sidebarsUtils: SidebarsUtils;
|
||||
docsById: Record<string, DocMetadataBase>;
|
||||
docsById: {[docId: string]: DocMetadataBase};
|
||||
}): CategoryGeneratedIndexMetadata {
|
||||
const {sidebarName, previous, next} =
|
||||
sidebarsUtils.getCategoryGeneratedIndexNavigation(category.link.permalink);
|
||||
|
|
|
@ -21,7 +21,7 @@ import _ from 'lodash';
|
|||
|
||||
describe('docsClientUtils', () => {
|
||||
it('getActivePlugin', () => {
|
||||
const data: Record<string, GlobalPluginData> = {
|
||||
const data: {[key: string]: GlobalPluginData} = {
|
||||
pluginIosId: {
|
||||
path: '/ios',
|
||||
versions: [],
|
||||
|
|
|
@ -23,11 +23,11 @@ import type {
|
|||
// ie the docs of that plugin are currently browsed
|
||||
// it is useful to support multiple docs plugin instances
|
||||
export function getActivePlugin(
|
||||
allPluginDatas: Record<string, GlobalPluginData>,
|
||||
allPluginData: {[pluginId: string]: GlobalPluginData},
|
||||
pathname: string,
|
||||
options: GetActivePluginOptions = {},
|
||||
): ActivePlugin | undefined {
|
||||
const activeEntry = Object.entries(allPluginDatas)
|
||||
const activeEntry = Object.entries(allPluginData)
|
||||
// Route sorting: '/android/foo' should match '/android' instead of '/'
|
||||
.sort((a, b) => b[1].path.localeCompare(a[1].path))
|
||||
.find(
|
||||
|
@ -46,7 +46,7 @@ export function getActivePlugin(
|
|||
if (!activePlugin && options.failfast) {
|
||||
throw new Error(
|
||||
`Can't find active docs plugin for "${pathname}" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: ${Object.values(
|
||||
allPluginDatas,
|
||||
allPluginData,
|
||||
)
|
||||
.map((plugin) => plugin.path)
|
||||
.join(', ')}`,
|
||||
|
|
|
@ -31,7 +31,7 @@ const StableEmptyObject = {};
|
|||
// Not using useAllPluginInstancesData() because in blog-only mode, docs hooks
|
||||
// are still used by the theme. We need a fail-safe fallback when the docs
|
||||
// plugin is not in use
|
||||
export const useAllDocsData = (): Record<string, GlobalPluginData> =>
|
||||
export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} =>
|
||||
useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject;
|
||||
|
||||
export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
declare module 'remark-admonitions' {
|
||||
type Options = Record<string, unknown>;
|
||||
type Options = {[key: string]: unknown};
|
||||
|
||||
const plugin: (options?: Options) => void;
|
||||
export = plugin;
|
||||
|
|
|
@ -424,7 +424,7 @@ export function getDocIds(doc: DocMetadataBase): [string, string] {
|
|||
// to "id")
|
||||
export function createDocsByIdIndex<
|
||||
Doc extends {id: string; unversionedId: string},
|
||||
>(docs: Doc[]): Record<string, Doc> {
|
||||
>(docs: Doc[]): {[docId: string]: Doc} {
|
||||
return Object.fromEntries(
|
||||
docs.flatMap((doc) => [
|
||||
[doc.unversionedId, doc],
|
||||
|
|
|
@ -41,8 +41,8 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
|
|||
...FrontMatterTOCHeadingLevels,
|
||||
}).unknown();
|
||||
|
||||
export function validateDocFrontMatter(
|
||||
frontMatter: Record<string, unknown>,
|
||||
): DocFrontMatter {
|
||||
export function validateDocFrontMatter(frontMatter: {
|
||||
[key: string]: unknown;
|
||||
}): DocFrontMatter {
|
||||
return validateFrontMatter(frontMatter, DocFrontMatterSchema);
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ function toGlobalDataGeneratedIndex(
|
|||
function toGlobalSidebars(
|
||||
sidebars: Sidebars,
|
||||
version: LoadedVersion,
|
||||
): Record<string, GlobalSidebar> {
|
||||
): {[sidebarId: string]: GlobalSidebar} {
|
||||
const {getFirstLink} = createSidebarsUtils(sidebars);
|
||||
return _.mapValues(sidebars, (sidebar, sidebarId) => {
|
||||
const firstLink = getFirstLink(sidebarId);
|
||||
|
|
|
@ -56,7 +56,6 @@ import type {
|
|||
PropTagsListPage,
|
||||
PluginOptions,
|
||||
} from '@docusaurus/plugin-content-docs';
|
||||
import type {GlobalPluginData} from '@docusaurus/plugin-content-docs/client';
|
||||
import {createSidebarsUtils} from './sidebars/utils';
|
||||
import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex';
|
||||
|
||||
|
@ -293,7 +292,7 @@ export default async function pluginContentDocs(
|
|||
// TODO tags should be a sub route of the version route
|
||||
await Promise.all(loadedVersions.map(createVersionTagsRoutes));
|
||||
|
||||
setGlobalData<GlobalPluginData>({
|
||||
setGlobalData({
|
||||
path: normalizeUrl([baseUrl, options.routeBasePath]),
|
||||
versions: loadedVersions.map(toGlobalDataVersion),
|
||||
breadcrumbs,
|
||||
|
|
|
@ -68,7 +68,7 @@ declare module '@docusaurus/plugin-content-docs' {
|
|||
};
|
||||
export type VersionsOptions = {
|
||||
lastVersion?: string;
|
||||
versions: Record<string, VersionOptions>;
|
||||
versions: {[versionName: string]: VersionOptions};
|
||||
onlyIncludeVersions?: string[];
|
||||
};
|
||||
export type SidebarOptions = {
|
||||
|
@ -89,7 +89,7 @@ declare module '@docusaurus/plugin-content-docs' {
|
|||
docTagDocListComponent: string;
|
||||
docTagsListComponent: string;
|
||||
docCategoryGeneratedIndexComponent: string;
|
||||
admonitions: Record<string, unknown>;
|
||||
admonitions: {[key: string]: unknown};
|
||||
disableVersioning: boolean;
|
||||
includeCurrentVersion: boolean;
|
||||
sidebarItemsGenerator: import('./sidebars/types').SidebarItemsGeneratorOption;
|
||||
|
@ -282,7 +282,7 @@ declare module '@docusaurus/plugin-content-docs/client' {
|
|||
export type ActiveDocContext = {
|
||||
activeVersion?: GlobalVersion;
|
||||
activeDoc?: GlobalDoc;
|
||||
alternateDocVersions: Record<string, GlobalDoc>;
|
||||
alternateDocVersions: {[versionName: string]: GlobalDoc};
|
||||
};
|
||||
export type GlobalDoc = {
|
||||
id: string;
|
||||
|
@ -297,7 +297,7 @@ declare module '@docusaurus/plugin-content-docs/client' {
|
|||
path: string;
|
||||
mainDocId: string; // home doc (if docs homepage configured), or first doc
|
||||
docs: GlobalDoc[];
|
||||
sidebars?: Record<string, GlobalSidebar>;
|
||||
sidebars?: {[sidebarId: string]: GlobalSidebar};
|
||||
};
|
||||
|
||||
export type GlobalSidebarLink = {
|
||||
|
@ -322,7 +322,7 @@ declare module '@docusaurus/plugin-content-docs/client' {
|
|||
};
|
||||
export type GetActivePluginOptions = {failfast?: boolean}; // use fail-fast option if you know for sure one plugin instance is active
|
||||
|
||||
export const useAllDocsData: () => Record<string, GlobalPluginData>;
|
||||
export const useAllDocsData: () => {[pluginId: string]: GlobalPluginData};
|
||||
export const useDocsData: (pluginId?: string) => GlobalPluginData;
|
||||
export const useActivePlugin: (
|
||||
options?: GetActivePluginOptions,
|
||||
|
|
|
@ -57,7 +57,7 @@ describe('processSidebars', () => {
|
|||
|
||||
async function testProcessSidebars(
|
||||
unprocessedSidebars: NormalizedSidebars,
|
||||
categoriesMetadata: Record<string, CategoryMetadataFile> = {},
|
||||
categoriesMetadata: {[filePath: string]: CategoryMetadataFile} = {},
|
||||
paramsOverrides: Partial<SidebarProcessorParams> = {},
|
||||
) {
|
||||
return processSidebars(unprocessedSidebars, categoriesMetadata, {
|
||||
|
@ -142,7 +142,6 @@ describe('processSidebars', () => {
|
|||
},
|
||||
numberPrefixParser: DefaultNumberPrefixParser,
|
||||
isCategoryIndex,
|
||||
options: params.sidebarOptions,
|
||||
});
|
||||
expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
|
||||
defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
|
||||
|
@ -154,7 +153,6 @@ describe('processSidebars', () => {
|
|||
},
|
||||
numberPrefixParser: DefaultNumberPrefixParser,
|
||||
isCategoryIndex,
|
||||
options: params.sidebarOptions,
|
||||
});
|
||||
expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
|
||||
defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
|
||||
|
@ -166,7 +164,6 @@ describe('processSidebars', () => {
|
|||
},
|
||||
numberPrefixParser: DefaultNumberPrefixParser,
|
||||
isCategoryIndex,
|
||||
options: params.sidebarOptions,
|
||||
});
|
||||
|
||||
expect(processedSidebar).toEqual({
|
||||
|
|
|
@ -680,7 +680,7 @@ describe('toNavigationLink', () => {
|
|||
return {...data, frontMatter: {}} as DocMetadataBase;
|
||||
}
|
||||
|
||||
const docsById: Record<string, DocMetadataBase> = {
|
||||
const docsById: {[docId: string]: DocMetadataBase} = {
|
||||
doc1: testDoc({
|
||||
title: 'Doc 1',
|
||||
permalink: '/doc1',
|
||||
|
|
|
@ -48,16 +48,10 @@ function toSidebarItemsGeneratorVersion(
|
|||
// post-processing checks
|
||||
async function processSidebar(
|
||||
unprocessedSidebar: NormalizedSidebar,
|
||||
categoriesMetadata: Record<string, CategoryMetadataFile>,
|
||||
categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
|
||||
params: SidebarProcessorParams,
|
||||
): Promise<ProcessedSidebar> {
|
||||
const {
|
||||
sidebarItemsGenerator,
|
||||
numberPrefixParser,
|
||||
docs,
|
||||
version,
|
||||
sidebarOptions,
|
||||
} = params;
|
||||
const {sidebarItemsGenerator, numberPrefixParser, docs, version} = params;
|
||||
|
||||
// Just a minor lazy transformation optimization
|
||||
const getSidebarItemsGeneratorDocsAndVersion = _.memoize(() => ({
|
||||
|
@ -74,7 +68,6 @@ async function processSidebar(
|
|||
defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
|
||||
isCategoryIndex,
|
||||
...getSidebarItemsGeneratorDocsAndVersion(),
|
||||
options: sidebarOptions,
|
||||
categoriesMetadata,
|
||||
});
|
||||
// Process again... weird but sidebar item generated might generate some
|
||||
|
@ -113,7 +106,7 @@ async function processSidebar(
|
|||
|
||||
export async function processSidebars(
|
||||
unprocessedSidebars: NormalizedSidebars,
|
||||
categoriesMetadata: Record<string, CategoryMetadataFile>,
|
||||
categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
|
||||
params: SidebarProcessorParams,
|
||||
): Promise<ProcessedSidebars> {
|
||||
const processedSidebars = await combinePromises(
|
||||
|
|
|
@ -15,11 +15,11 @@ import type {
|
|||
import type {Slugger} from '@docusaurus/utils';
|
||||
|
||||
// Makes all properties visible when hovering over the type
|
||||
type Expand<T extends Record<string, unknown>> = {[P in keyof T]: T[P]};
|
||||
type Expand<T extends {[x: string]: unknown}> = {[P in keyof T]: T[P]};
|
||||
|
||||
export type SidebarItemBase = {
|
||||
className?: string;
|
||||
customProps?: Record<string, unknown>;
|
||||
customProps?: {[key: string]: unknown};
|
||||
};
|
||||
|
||||
export type SidebarItemDoc = SidebarItemBase & {
|
||||
|
@ -216,7 +216,7 @@ export type CategoryMetadataFile = {
|
|||
collapsible?: boolean;
|
||||
className?: string;
|
||||
link?: SidebarItemCategoryLinkConfig | null;
|
||||
customProps?: Record<string, unknown>;
|
||||
customProps?: {[key: string]: unknown};
|
||||
|
||||
// TODO should we allow "items" here? how would this work? would an
|
||||
// "autogenerated" type be allowed?
|
||||
|
@ -247,8 +247,7 @@ export type SidebarItemsGeneratorArgs = {
|
|||
docs: SidebarItemsGeneratorDoc[];
|
||||
numberPrefixParser: NumberPrefixParser;
|
||||
isCategoryIndex: CategoryIndexMatcher;
|
||||
categoriesMetadata: Record<string, CategoryMetadataFile>;
|
||||
options: SidebarOptions;
|
||||
categoriesMetadata: {[filePath: string]: CategoryMetadataFile};
|
||||
};
|
||||
export type SidebarItemsGenerator = (
|
||||
generatorArgs: SidebarItemsGeneratorArgs,
|
||||
|
|
|
@ -107,15 +107,15 @@ export function collectSidebarNavigation(
|
|||
});
|
||||
}
|
||||
|
||||
export function collectSidebarsDocIds(
|
||||
sidebars: Sidebars,
|
||||
): Record<string, string[]> {
|
||||
export function collectSidebarsDocIds(sidebars: Sidebars): {
|
||||
[sidebarId: string]: string[];
|
||||
} {
|
||||
return _.mapValues(sidebars, collectSidebarDocIds);
|
||||
}
|
||||
|
||||
export function collectSidebarsNavigations(
|
||||
sidebars: Sidebars,
|
||||
): Record<string, SidebarNavigationItem[]> {
|
||||
export function collectSidebarsNavigations(sidebars: Sidebars): {
|
||||
[sidebarId: string]: SidebarNavigationItem[];
|
||||
} {
|
||||
return _.mapValues(sidebars, collectSidebarNavigation);
|
||||
}
|
||||
|
||||
|
@ -360,7 +360,7 @@ export function toDocNavigationLink(doc: DocMetadataBase): DocNavLink {
|
|||
|
||||
export function toNavigationLink(
|
||||
navigationItem: SidebarNavigationItem | undefined,
|
||||
docsById: Record<string, DocMetadataBase>,
|
||||
docsById: {[docId: string]: DocMetadataBase},
|
||||
): DocNavLink | undefined {
|
||||
function getDocById(docId: string) {
|
||||
const doc = docsById[docId];
|
||||
|
|
|
@ -149,9 +149,9 @@ function validateSidebarItem(
|
|||
}
|
||||
}
|
||||
|
||||
export function validateSidebars(
|
||||
sidebars: Record<string, unknown>,
|
||||
): asserts sidebars is NormalizedSidebars {
|
||||
export function validateSidebars(sidebars: {
|
||||
[sidebarId: string]: unknown;
|
||||
}): asserts sidebars is NormalizedSidebars {
|
||||
Object.values(sidebars as NormalizedSidebars).forEach((sidebar) => {
|
||||
sidebar.forEach(validateSidebarItem);
|
||||
});
|
||||
|
|
|
@ -269,7 +269,7 @@ function getVersionTranslationFiles(version: LoadedVersion): TranslationFiles {
|
|||
}
|
||||
function translateVersion(
|
||||
version: LoadedVersion,
|
||||
translationFiles: Record<string, TranslationFile>,
|
||||
translationFiles: {[fileName: string]: TranslationFile},
|
||||
): LoadedVersion {
|
||||
const versionTranslations =
|
||||
translationFiles[getVersionFileName(version.versionName)]!.content;
|
||||
|
@ -289,7 +289,7 @@ function getVersionsTranslationFiles(
|
|||
}
|
||||
function translateVersions(
|
||||
versions: LoadedVersion[],
|
||||
translationFiles: Record<string, TranslationFile>,
|
||||
translationFiles: {[fileName: string]: TranslationFile},
|
||||
): LoadedVersion[] {
|
||||
return versions.map((version) => translateVersion(version, translationFiles));
|
||||
}
|
||||
|
@ -303,7 +303,7 @@ export function translateLoadedContent(
|
|||
loadedContent: LoadedContent,
|
||||
translationFiles: TranslationFile[],
|
||||
): LoadedContent {
|
||||
const translationFilesMap: Record<string, TranslationFile> = _.keyBy(
|
||||
const translationFilesMap: {[fileName: string]: TranslationFile} = _.keyBy(
|
||||
translationFiles,
|
||||
(f) => f.path,
|
||||
);
|
||||
|
|
|
@ -59,7 +59,7 @@ export type DocFrontMatter = {
|
|||
sidebar_label?: string;
|
||||
sidebar_position?: number;
|
||||
sidebar_class_name?: string;
|
||||
sidebar_custom_props?: Record<string, unknown>;
|
||||
sidebar_custom_props?: {[key: string]: unknown};
|
||||
displayed_sidebar?: string | null;
|
||||
pagination_label?: string;
|
||||
custom_edit_url?: string | null;
|
||||
|
@ -83,7 +83,7 @@ export type DocMetadataBase = LastUpdateData & {
|
|||
sidebarPosition?: number;
|
||||
editUrl?: string | null;
|
||||
tags: Tag[];
|
||||
frontMatter: DocFrontMatter & Record<string, unknown>;
|
||||
frontMatter: DocFrontMatter & {[key: string]: unknown};
|
||||
};
|
||||
|
||||
export type DocNavLink = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue