mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-19 11:12:36 +02:00
refactor: fix a lot of errors in type-aware linting (#7477)
This commit is contained in:
parent
222bf3c091
commit
bf1513a3e3
120 changed files with 407 additions and 364 deletions
|
@ -463,7 +463,7 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|||
expect(sidebarSlice).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('throws for unknown index link', async () => {
|
||||
it('throws for unknown index link', () => {
|
||||
const generateSidebar = () =>
|
||||
DefaultSidebarItemsGenerator({
|
||||
numberPrefixParser: DefaultNumberPrefixParser,
|
||||
|
@ -499,7 +499,7 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|||
},
|
||||
});
|
||||
|
||||
await expect(generateSidebar).rejects.toThrowErrorMatchingInlineSnapshot(`
|
||||
expect(() => generateSidebar()).toThrowErrorMatchingInlineSnapshot(`
|
||||
"Can't find any doc with ID foo.
|
||||
Available doc IDs:
|
||||
- intro"
|
||||
|
|
|
@ -26,7 +26,7 @@ describe('processSidebars', () => {
|
|||
function createStaticSidebarItemGenerator(
|
||||
sidebarSlice: SidebarItem[],
|
||||
): SidebarItemsGenerator {
|
||||
return jest.fn(async () => sidebarSlice);
|
||||
return jest.fn(() => sidebarSlice);
|
||||
}
|
||||
|
||||
const StaticGeneratedSidebarSlice: NormalizedSidebar = [
|
||||
|
@ -40,7 +40,7 @@ describe('processSidebars', () => {
|
|||
// @ts-expect-error: good enough for this test
|
||||
const version: VersionMetadata = {
|
||||
versionName: '1.0.0',
|
||||
versionPath: '/docs/1.0.0',
|
||||
path: '/docs/1.0.0',
|
||||
};
|
||||
|
||||
const params: SidebarProcessorParams = {
|
||||
|
|
|
@ -134,11 +134,11 @@ describe('createSidebarsUtils', () => {
|
|||
getFirstLink,
|
||||
} = createSidebarsUtils(sidebars);
|
||||
|
||||
it('getFirstDocIdOfFirstSidebar', async () => {
|
||||
it('getFirstDocIdOfFirstSidebar', () => {
|
||||
expect(getFirstDocIdOfFirstSidebar()).toBe('doc1');
|
||||
});
|
||||
|
||||
it('getSidebarNameByDocId', async () => {
|
||||
it('getSidebarNameByDocId', () => {
|
||||
expect(getSidebarNameByDocId('doc1')).toBe('sidebar1');
|
||||
expect(getSidebarNameByDocId('doc2')).toBe('sidebar1');
|
||||
expect(getSidebarNameByDocId('doc3')).toBe('sidebar2');
|
||||
|
@ -149,7 +149,7 @@ describe('createSidebarsUtils', () => {
|
|||
expect(getSidebarNameByDocId('unknown_id')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('getDocNavigation', async () => {
|
||||
it('getDocNavigation', () => {
|
||||
expect(getDocNavigation('doc1', 'doc1', undefined)).toEqual({
|
||||
sidebarName: 'sidebar1',
|
||||
previous: undefined,
|
||||
|
@ -229,7 +229,7 @@ describe('createSidebarsUtils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('getCategoryGeneratedIndexNavigation', async () => {
|
||||
it('getCategoryGeneratedIndexNavigation', () => {
|
||||
expect(
|
||||
getCategoryGeneratedIndexNavigation('/s3-subcategory-index-permalink'),
|
||||
).toMatchObject({
|
||||
|
@ -259,7 +259,7 @@ describe('createSidebarsUtils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('getCategoryGeneratedIndexList', async () => {
|
||||
it('getCategoryGeneratedIndexList', () => {
|
||||
expect(getCategoryGeneratedIndexList()).toMatchObject([
|
||||
{
|
||||
type: 'category',
|
||||
|
@ -301,7 +301,7 @@ describe('createSidebarsUtils', () => {
|
|||
});
|
||||
|
||||
describe('collectSidebarDocItems', () => {
|
||||
it('can collect docs', async () => {
|
||||
it('can collect docs', () => {
|
||||
const sidebar: Sidebar = [
|
||||
{
|
||||
type: 'category',
|
||||
|
@ -357,7 +357,7 @@ describe('collectSidebarDocItems', () => {
|
|||
});
|
||||
|
||||
describe('collectSidebarCategories', () => {
|
||||
it('can collect categories', async () => {
|
||||
it('can collect categories', () => {
|
||||
const sidebar: Sidebar = [
|
||||
{
|
||||
type: 'category',
|
||||
|
@ -415,7 +415,7 @@ describe('collectSidebarCategories', () => {
|
|||
});
|
||||
|
||||
describe('collectSidebarLinks', () => {
|
||||
it('can collect links', async () => {
|
||||
it('can collect links', () => {
|
||||
const sidebar: Sidebar = [
|
||||
{
|
||||
type: 'category',
|
||||
|
@ -453,7 +453,7 @@ describe('collectSidebarLinks', () => {
|
|||
});
|
||||
|
||||
describe('collectSidebarsDocIds', () => {
|
||||
it('can collect sidebars doc items', async () => {
|
||||
it('can collect sidebars doc items', () => {
|
||||
const sidebar1: Sidebar = [
|
||||
{
|
||||
type: 'category',
|
||||
|
@ -499,7 +499,7 @@ describe('collectSidebarsDocIds', () => {
|
|||
});
|
||||
|
||||
describe('transformSidebarItems', () => {
|
||||
it('can transform sidebar items', async () => {
|
||||
it('can transform sidebar items', () => {
|
||||
const sidebar: Sidebar = [
|
||||
{
|
||||
type: 'category',
|
||||
|
|
|
@ -9,7 +9,7 @@ import {validateSidebars, validateCategoryMetadataFile} from '../validation';
|
|||
import type {SidebarsConfig, CategoryMetadataFile} from '../types';
|
||||
|
||||
describe('validateSidebars', () => {
|
||||
it('throw for bad value', async () => {
|
||||
it('throw for bad value', () => {
|
||||
expect(() => validateSidebars({sidebar: [{type: 42}]}))
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
|
@ -21,12 +21,12 @@ describe('validateSidebars', () => {
|
|||
`);
|
||||
});
|
||||
|
||||
it('accept empty object', async () => {
|
||||
it('accept empty object', () => {
|
||||
const sidebars: SidebarsConfig = {};
|
||||
validateSidebars(sidebars);
|
||||
});
|
||||
|
||||
it('accept valid values', async () => {
|
||||
it('accept valid values', () => {
|
||||
const sidebars: SidebarsConfig = {
|
||||
sidebar1: [
|
||||
{type: 'doc', id: 'doc1'},
|
||||
|
@ -207,7 +207,7 @@ describe('validateSidebars', () => {
|
|||
).toThrowErrorMatchingInlineSnapshot(`"sidebar.forEach is not a function"`);
|
||||
});
|
||||
|
||||
it('sidebars item doc but id is not a string', async () => {
|
||||
it('sidebars item doc but id is not a string', () => {
|
||||
expect(() =>
|
||||
validateSidebars({
|
||||
docs: [
|
||||
|
@ -267,18 +267,18 @@ describe('validateSidebars', () => {
|
|||
describe('validateCategoryMetadataFile', () => {
|
||||
// TODO add more tests
|
||||
|
||||
it('throw for bad value', async () => {
|
||||
it('throw for bad value', () => {
|
||||
expect(() =>
|
||||
validateCategoryMetadataFile(42),
|
||||
).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`);
|
||||
});
|
||||
|
||||
it('accept empty object', async () => {
|
||||
it('accept empty object', () => {
|
||||
const content: CategoryMetadataFile = {};
|
||||
expect(validateCategoryMetadataFile(content)).toEqual(content);
|
||||
});
|
||||
|
||||
it('accept valid values', async () => {
|
||||
it('accept valid values', () => {
|
||||
const content: CategoryMetadataFile = {
|
||||
className: 'className',
|
||||
label: 'Category Label',
|
||||
|
@ -295,7 +295,7 @@ describe('validateCategoryMetadataFile', () => {
|
|||
expect(validateCategoryMetadataFile(content)).toEqual(content);
|
||||
});
|
||||
|
||||
it('rejects permalink', async () => {
|
||||
it('rejects permalink', () => {
|
||||
const content: CategoryMetadataFile = {
|
||||
className: 'className',
|
||||
label: 'Category Label',
|
||||
|
|
|
@ -46,7 +46,7 @@ type Dir = {
|
|||
};
|
||||
|
||||
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
||||
export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
|
||||
export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = ({
|
||||
numberPrefixParser,
|
||||
isCategoryIndex,
|
||||
docs: allDocs,
|
||||
|
|
|
@ -84,7 +84,7 @@ function postProcessSidebarItem(
|
|||
};
|
||||
}
|
||||
// A non-collapsible category can't be collapsed!
|
||||
if (category.collapsible === false) {
|
||||
if (!category.collapsible) {
|
||||
category.collapsed = false;
|
||||
}
|
||||
return category;
|
||||
|
|
|
@ -251,7 +251,7 @@ export type SidebarItemsGeneratorArgs = {
|
|||
};
|
||||
export type SidebarItemsGenerator = (
|
||||
generatorArgs: SidebarItemsGeneratorArgs,
|
||||
) => Promise<NormalizedSidebar>;
|
||||
) => NormalizedSidebar | Promise<NormalizedSidebar>;
|
||||
|
||||
export type SidebarItemsGeneratorOption = (
|
||||
generatorArgs: {
|
||||
|
@ -262,7 +262,7 @@ export type SidebarItemsGeneratorOption = (
|
|||
*/
|
||||
defaultSidebarItemsGenerator: SidebarItemsGenerator;
|
||||
} & SidebarItemsGeneratorArgs,
|
||||
) => Promise<NormalizedSidebarItem[]>;
|
||||
) => NormalizedSidebar | Promise<NormalizedSidebar>;
|
||||
|
||||
export type SidebarProcessorParams = {
|
||||
sidebarItemsGenerator: SidebarItemsGeneratorOption;
|
||||
|
|
|
@ -257,7 +257,7 @@ export function createSidebarsUtils(sidebars: Sidebars): SidebarsUtils {
|
|||
): boolean {
|
||||
return (
|
||||
item.type === 'category' &&
|
||||
item.link?.type === 'generated-index' &&
|
||||
item.link.type === 'generated-index' &&
|
||||
item.link.permalink === categoryGeneratedIndexPermalink
|
||||
);
|
||||
}
|
||||
|
|
|
@ -144,7 +144,7 @@ function validateSidebarItem(
|
|||
// manually
|
||||
Joi.assert(item, sidebarItemSchema);
|
||||
|
||||
if ((item as NormalizedSidebarItemCategory).type === 'category') {
|
||||
if ((item as NormalizedSidebarItem).type === 'category') {
|
||||
(item as NormalizedSidebarItemCategory).items.forEach(validateSidebarItem);
|
||||
}
|
||||
}
|
||||
|
@ -170,5 +170,9 @@ const categoryMetadataFileSchema = Joi.object<CategoryMetadataFile>({
|
|||
export function validateCategoryMetadataFile(
|
||||
unsafeContent: unknown,
|
||||
): CategoryMetadataFile {
|
||||
return Joi.attempt(unsafeContent, categoryMetadataFileSchema);
|
||||
const {error, value} = categoryMetadataFileSchema.validate(unsafeContent);
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue