refactor: fix a lot of errors in type-aware linting (#7477)

This commit is contained in:
Joshua Chen 2022-05-24 15:40:26 +08:00 committed by GitHub
parent 222bf3c091
commit bf1513a3e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
120 changed files with 407 additions and 364 deletions

View file

@ -21,7 +21,7 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
/** As provided by the content plugin. */
readonly metadata: Metadata;
/** A list of TOC items (headings). */
readonly toc: readonly TOCItem[];
readonly toc?: readonly TOCItem[];
/** First h1 title before any content. */
readonly contentTitle: string | undefined;
/**

View file

@ -55,7 +55,7 @@ export type MDXOptions = {
beforeDefaultRehypePlugins: MDXPlugin[];
};
export type Options = MDXOptions & {
export type Options = Partial<MDXOptions> & {
staticDirs: string[];
siteDir: string;
isMDXPartial?: (filePath: string) => boolean;
@ -138,7 +138,7 @@ export async function mdxLoader(
): Promise<void> {
const callback = this.async();
const filePath = this.resourcePath;
const reqOptions = this.getOptions() ?? {};
const reqOptions = this.getOptions();
const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);

View file

@ -17,7 +17,7 @@ export default function plugin(): Transformer {
return (root) => {
const slugs = createSlugger();
visit(root, 'heading', (headingNode: Heading) => {
const data = headingNode.data || (headingNode.data = {});
const data = headingNode.data ?? (headingNode.data = {});
const properties = (data.hProperties || (data.hProperties = {})) as {
id: string;
};
@ -36,7 +36,7 @@ export default function plugin(): Transformer {
// Support explicit heading IDs
const parsedHeading = parseMarkdownHeadingId(heading);
id = parsedHeading.id || slugs.slug(heading);
id = parsedHeading.id ?? slugs.slug(heading);
if (parsedHeading.id) {
// When there's an id, it is always in the last child node

View file

@ -110,8 +110,9 @@ async function processLinkNode(node: Link, context: Context) {
if (!node.url) {
// Try to improve error feedback
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
const title = node.title || (node.children[0] as Literal)?.value || '?';
const line = node?.position?.start?.line || '?';
const title =
node.title ?? (node.children[0] as Literal | undefined)?.value ?? '?';
const line = node.position?.start.line ?? '?';
throw new Error(
`Markdown link URL is mandatory in "${toMessageRelativeFilePath(
context.filePath,

View file

@ -15,7 +15,7 @@ export function stringifyContent(node: Parent): string {
}
export function toValue(node: PhrasingContent | Heading): string {
switch (node?.type) {
switch (node.type) {
case 'text':
return escapeHtml(node.value);
case 'heading':