mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 19:32:35 +02:00
refactor: reduce ESLint warnings / better typing (#5242)
* Fix code block children type Signed-off-by: Josh-Cena <sidachen2003@gmail.com> * Add return type Signed-off-by: Josh-Cena <sidachen2003@gmail.com> * Add types Signed-off-by: Josh-Cena <sidachen2003@gmail.com> * Fix return types Signed-off-by: Josh-Cena <sidachen2003@gmail.com> * Fix details type Signed-off-by: Josh-Cena <sidachen2003@gmail.com> * Fix type Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
parent
a59378ba87
commit
9e615eff02
9 changed files with 38 additions and 21 deletions
|
@ -50,7 +50,8 @@
|
||||||
"rtlcss": "^3.1.2"
|
"rtlcss": "^3.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@docusaurus/module-type-aliases": "2.0.0-beta.4"
|
"@docusaurus/module-type-aliases": "2.0.0-beta.4",
|
||||||
|
"@types/parse-numeric-range": "^0.0.1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "^16.8.4 || ^17.0.0",
|
"react": "^16.8.4 || ^17.0.0",
|
||||||
|
|
|
@ -39,11 +39,9 @@ function smoothScrollTopPolyfill(): CancelScrollTop {
|
||||||
}
|
}
|
||||||
rafRecursion();
|
rafRecursion();
|
||||||
|
|
||||||
return () => {
|
// Break the recursion
|
||||||
// Break the recursion
|
// Prevents the user from "fighting" against that recursion producing a weird UX
|
||||||
// Prevents the user from "fighting" against that recursion producing a weird UX
|
return () => raf && cancelAnimationFrame(raf);
|
||||||
raf && cancelAnimationFrame(raf);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UseSmoothScrollTopReturn = {
|
type UseSmoothScrollTopReturn = {
|
||||||
|
|
|
@ -62,7 +62,7 @@ const getHighlightDirectiveRegex = (
|
||||||
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
|
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
|
||||||
};
|
};
|
||||||
// select comment styles based on language
|
// select comment styles based on language
|
||||||
const highlightDirectiveRegex = (lang) => {
|
const highlightDirectiveRegex = (lang: string) => {
|
||||||
switch (lang) {
|
switch (lang) {
|
||||||
case 'js':
|
case 'js':
|
||||||
case 'javascript':
|
case 'javascript':
|
||||||
|
@ -119,11 +119,12 @@ export default function CodeBlock({
|
||||||
const prismTheme = usePrismTheme();
|
const prismTheme = usePrismTheme();
|
||||||
|
|
||||||
// In case interleaved Markdown (e.g. when using CodeBlock as standalone component).
|
// In case interleaved Markdown (e.g. when using CodeBlock as standalone component).
|
||||||
const content = Array.isArray(children) ? children.join('') : children;
|
const content = Array.isArray(children)
|
||||||
|
? children.join('')
|
||||||
|
: (children as string);
|
||||||
|
|
||||||
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
||||||
// Tested above
|
// Tested above
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)![1];
|
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)![1];
|
||||||
highlightLines = rangeParser(highlightLinesRange).filter((n) => n > 0);
|
highlightLines = rangeParser(highlightLinesRange).filter((n) => n > 0);
|
||||||
}
|
}
|
||||||
|
@ -145,7 +146,7 @@ export default function CodeBlock({
|
||||||
const directiveRegex = highlightDirectiveRegex(language);
|
const directiveRegex = highlightDirectiveRegex(language);
|
||||||
// go through line by line
|
// go through line by line
|
||||||
const lines = content.replace(/\n$/, '').split('\n');
|
const lines = content.replace(/\n$/, '').split('\n');
|
||||||
let blockStart;
|
let blockStart: number;
|
||||||
// loop through lines
|
// loop through lines
|
||||||
for (let index = 0; index < lines.length; ) {
|
for (let index = 0; index < lines.length; ) {
|
||||||
const line = lines[index];
|
const line = lines[index];
|
||||||
|
@ -169,7 +170,7 @@ export default function CodeBlock({
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'highlight-end':
|
case 'highlight-end':
|
||||||
range += `${blockStart}-${lineNumber - 1},`;
|
range += `${blockStart!}-${lineNumber - 1},`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -30,10 +30,10 @@ const MDXComponents: MDXComponentsObject = {
|
||||||
},
|
},
|
||||||
a: (props) => <Link {...props} />,
|
a: (props) => <Link {...props} />,
|
||||||
pre: (props) => {
|
pre: (props) => {
|
||||||
const {children} = props as {children: any};
|
const {children} = props;
|
||||||
|
|
||||||
// See comment for `code` above
|
// See comment for `code` above
|
||||||
if (isValidElement(children?.props?.children)) {
|
if (isValidElement(children) && isValidElement(children?.props?.children)) {
|
||||||
return children?.props.children;
|
return children?.props.children;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,11 +46,11 @@ const MDXComponents: MDXComponentsObject = {
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
details: (props): JSX.Element => {
|
details: (props): JSX.Element => {
|
||||||
const items = React.Children.toArray(props.children);
|
const items = React.Children.toArray(props.children) as ReactElement[];
|
||||||
// Split summary item from the rest to pass it as a separate prop to the Detais theme component
|
// Split summary item from the rest to pass it as a separate prop to the Detais theme component
|
||||||
const summary: ReactElement<
|
const summary: ReactElement<ComponentProps<'summary'>> = items.find(
|
||||||
ComponentProps<'summary'>
|
(item) => item?.props?.mdxType === 'summary',
|
||||||
> = (items as any[]).find((item) => item?.props?.mdxType === 'summary');
|
)!;
|
||||||
const children = <>{items.filter((item) => item !== summary)}</>;
|
const children = <>{items.filter((item) => item !== summary)}</>;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -16,7 +16,10 @@ const ACTIVE_LINK_CLASS_NAME = 'table-of-contents__link--active';
|
||||||
const TOP_OFFSET = 100;
|
const TOP_OFFSET = 100;
|
||||||
|
|
||||||
/* eslint-disable jsx-a11y/control-has-associated-label */
|
/* eslint-disable jsx-a11y/control-has-associated-label */
|
||||||
export function TOCHeadings({toc, isChild}: TOCHeadingsProps) {
|
export function TOCHeadings({
|
||||||
|
toc,
|
||||||
|
isChild,
|
||||||
|
}: TOCHeadingsProps): JSX.Element | null {
|
||||||
if (!toc.length) {
|
if (!toc.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,10 @@ import styles from './styles.module.css';
|
||||||
import {TOCHeadings} from '@theme/TOC';
|
import {TOCHeadings} from '@theme/TOC';
|
||||||
import type {TOCCollapsibleProps} from '@theme/TOCCollapsible';
|
import type {TOCCollapsibleProps} from '@theme/TOCCollapsible';
|
||||||
|
|
||||||
export default function TOCCollapsible({toc, className}: TOCCollapsibleProps) {
|
export default function TOCCollapsible({
|
||||||
|
toc,
|
||||||
|
className,
|
||||||
|
}: TOCCollapsibleProps): JSX.Element {
|
||||||
const {collapsed, toggleCollapsed} = useCollapsible({
|
const {collapsed, toggleCollapsed} = useCollapsible({
|
||||||
initialState: true,
|
initialState: true,
|
||||||
});
|
});
|
||||||
|
|
|
@ -65,8 +65,10 @@ declare module '@theme/BlogLayout' {
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module '@theme/CodeBlock' {
|
declare module '@theme/CodeBlock' {
|
||||||
|
import {ReactElement} from 'react';
|
||||||
|
|
||||||
export type Props = {
|
export type Props = {
|
||||||
readonly children: string;
|
readonly children: string | ReactElement;
|
||||||
readonly className?: string;
|
readonly className?: string;
|
||||||
readonly metastring?: string;
|
readonly metastring?: string;
|
||||||
readonly title?: string;
|
readonly title?: string;
|
||||||
|
|
|
@ -95,7 +95,11 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
|
||||||
|
|
||||||
const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
|
const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
|
||||||
|
|
||||||
export const AnnouncementBarProvider = ({children}: {children: ReactNode}) => {
|
export const AnnouncementBarProvider = ({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: ReactNode;
|
||||||
|
}): JSX.Element => {
|
||||||
const value = useAnnouncementBarContextValue();
|
const value = useAnnouncementBarContextValue();
|
||||||
return (
|
return (
|
||||||
<AnnouncementBarContext.Provider value={value}>
|
<AnnouncementBarContext.Provider value={value}>
|
||||||
|
|
|
@ -4277,6 +4277,11 @@
|
||||||
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
|
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
|
||||||
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
|
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
|
||||||
|
|
||||||
|
"@types/parse-numeric-range@^0.0.1":
|
||||||
|
version "0.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/parse-numeric-range/-/parse-numeric-range-0.0.1.tgz#1a807487565a753f486cb3ee4b2145937d49759d"
|
||||||
|
integrity sha512-nI3rPGKk8BxedokP2VilnW5JyZHYNjGCUDsAZ2JQgISgDflHNUO0wXMfGYP8CkihrKYDm5tilD52XfGhO/ZFCA==
|
||||||
|
|
||||||
"@types/parse5@^5.0.0":
|
"@types/parse5@^5.0.0":
|
||||||
version "5.0.3"
|
version "5.0.3"
|
||||||
resolved "https://registry.yarnpkg.com/@types/parse5/-/parse5-5.0.3.tgz#e7b5aebbac150f8b5fdd4a46e7f0bd8e65e19109"
|
resolved "https://registry.yarnpkg.com/@types/parse5/-/parse5-5.0.3.tgz#e7b5aebbac150f8b5fdd4a46e7f0bd8e65e19109"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue