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:
Joshua Chen 2022-03-25 18:06:30 +08:00 committed by GitHub
parent e8800b9d49
commit 87592bca03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
99 changed files with 339 additions and 307 deletions

View file

@ -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({

View file

@ -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',

View file

@ -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(

View file

@ -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,

View file

@ -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];

View file

@ -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);
});