Skip to content

Commit 4db0c62

Browse files
authored
refactor: enable a few TS flags (facebook#6852)
* refactor: enable a few TS flags * refactor * revert to working version * fix * better * change
1 parent 9f925a4 commit 4db0c62

71 files changed

Lines changed: 208 additions & 172 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/docusaurus-logger/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function interpolate(
2121
): string {
2222
let res = '';
2323
values.forEach((value, idx) => {
24-
const flag = msgs[idx].match(/[a-z]+=$/);
25-
res += msgs[idx].replace(/[a-z]+=$/, '');
24+
const flag = msgs[idx]!.match(/[a-z]+=$/);
25+
res += msgs[idx]!.replace(/[a-z]+=$/, '');
2626
const format = (() => {
2727
if (!flag) {
2828
return (a: string | number) => a;

packages/docusaurus-mdx-loader/src/remark/toc/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default function plugin(options: PluginOptions = {}): Transformer {
9494
const targetIndex = getOrCreateExistingTargetIndex(children, name);
9595

9696
if (headings && headings.length) {
97-
children[targetIndex].value = `export const ${name} = ${stringifyObject(
97+
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
9898
headings,
9999
)};`;
100100
}

packages/docusaurus-migrate/src/frontMatter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default function extractMetadata(content: string): Data {
3737

3838
// New line characters => to handle all operating systems.
3939
const lines = (both.header ?? '').split(/\r?\n/);
40-
for (let i = 0; i < lines.length - 1; i += 1) {
41-
const keyValue = lines[i].split(':');
40+
lines.slice(0, -1).forEach((line) => {
41+
const keyValue = line.split(':') as [string, ...string[]];
4242
const key = keyValue[0].trim();
4343
let value = keyValue.slice(1).join(':').trim();
4444
try {
@@ -47,7 +47,7 @@ export default function extractMetadata(content: string): Data {
4747
// Ignore the error as it means it's not a JSON value.
4848
}
4949
metadata[key] = value;
50-
}
50+
});
5151
return {metadata, rawContent: both.content};
5252
}
5353

packages/docusaurus-migrate/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ async function migrateVersionedSidebar(
535535
// Order matters: if a sidebar file doesn't exist, we have to use the
536536
// previous version's
537537
for (let i = 0; i < versions.length; i += 1) {
538-
const version = versions[i];
538+
const version = versions[i]!;
539539
let sidebarEntries: SidebarEntries;
540540
const sidebarPath = path.join(
541541
siteDir,
@@ -545,7 +545,7 @@ async function migrateVersionedSidebar(
545545
try {
546546
sidebarEntries = JSON.parse(await fs.readFile(sidebarPath, 'utf-8'));
547547
} catch {
548-
sidebars.push({version, entries: sidebars[i - 1].entries});
548+
sidebars.push({version, entries: sidebars[i - 1]!.entries});
549549
return;
550550
}
551551
const newSidebar = Object.entries(sidebarEntries).reduce(

packages/docusaurus-migrate/src/transform.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const property = (key: string, value: ArrowFunctionExpression) =>
3333

3434
const processCallExpression = (node: ASTPath<VariableDeclarator>) => {
3535
const args = (node?.value?.init as CallExpression)?.arguments[0];
36+
if (!args) {
37+
return;
38+
}
3639
if (args.type === 'Literal') {
3740
if (
3841
typeof args.value === 'string' &&
@@ -71,6 +74,9 @@ const processMemberExpression = (node: ASTPath<VariableDeclarator>) => {
7174
return;
7275
}
7376
const args = object.arguments[0];
77+
if (!args) {
78+
return;
79+
}
7480
if (args.type === 'Literal') {
7581
if (args.value === '../../core/CompLibrary.js') {
7682
const newDeclarator = jscodeshift.variableDeclarator(
@@ -117,7 +123,7 @@ export default function transformer(file: string): string {
117123
}
118124
});
119125
if (r[r.length - 1]) {
120-
jscodeshift(r[r.length - 1].parent).insertAfter(
126+
jscodeshift(r[r.length - 1]!.parent).insertAfter(
121127
jscodeshift.importDeclaration(
122128
[jscodeshift.importDefaultSpecifier(jscodeshift.identifier('Layout'))],
123129
jscodeshift.literal('@theme/Layout'),

packages/docusaurus-plugin-content-blog/src/blogUtils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ export function parseBlogFileName(
139139
if (dateFilenameMatch) {
140140
const {folder, text, date: dateString} = dateFilenameMatch.groups!;
141141
// Always treat dates as UTC by adding the `Z`
142-
const date = new Date(`${dateString}Z`);
143-
const slugDate = dateString.replace(/-/g, '/');
144-
const slug = `/${slugDate}/${folder}${text}`;
145-
return {date, text, slug};
142+
const date = new Date(`${dateString!}Z`);
143+
const slugDate = dateString!.replace(/-/g, '/');
144+
const slug = `/${slugDate}/${folder!}${text!}`;
145+
return {date, text: text!, slug};
146146
}
147147
const text = blogSourceRelative.replace(/(?:\/index)?\.mdx?$/, '');
148148
const slug = `/${text}`;

packages/docusaurus-plugin-content-blog/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export default async function pluginContentBlog(
307307
({
308308
content: {
309309
__import: true,
310-
path: blogItemsToMetadata[postID].source,
310+
path: blogItemsToMetadata[postID]!.source,
311311
query: {
312312
truncated: true,
313313
},
@@ -359,7 +359,7 @@ export default async function pluginContentBlog(
359359
modules: {
360360
sidebar: aliasedSource(sidebarProp),
361361
items: items.map((postID) => {
362-
const blogPostMetadata = blogItemsToMetadata[postID];
362+
const blogPostMetadata = blogItemsToMetadata[postID]!;
363363
return {
364364
content: {
365365
__import: true,

packages/docusaurus-plugin-content-blog/src/translations.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ function translateListPage(
1919
items,
2020
metadata: {
2121
...metadata,
22-
blogTitle: translations.title.message,
23-
blogDescription: translations.description.message,
22+
blogTitle: translations.title?.message ?? page.metadata.blogTitle,
23+
blogDescription:
24+
translations.description?.message ?? page.metadata.blogDescription,
2425
},
2526
};
2627
});
@@ -52,10 +53,14 @@ export function translateContent(
5253
content: BlogContent,
5354
translationFiles: TranslationFiles,
5455
): BlogContent {
55-
const [{content: optionsTranslations}] = translationFiles;
56+
if (translationFiles.length === 0) {
57+
return content;
58+
}
59+
const {content: optionsTranslations} = translationFiles[0]!;
5660
return {
5761
...content,
58-
blogSidebarTitle: optionsTranslations['sidebar.title'].message,
62+
blogSidebarTitle:
63+
optionsTranslations['sidebar.title']?.message ?? content.blogSidebarTitle,
5964
blogListPaginated: translateListPage(
6065
content.blogListPaginated,
6166
optionsTranslations,

packages/docusaurus-plugin-content-docs/src/docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export function getMainDocId({
363363
doc.unversionedId === firstDocIdOfFirstSidebar,
364364
)!;
365365
}
366-
return docs[0];
366+
return docs[0]!;
367367
}
368368

369369
return getMainDoc().unversionedId;

packages/docusaurus-plugin-content-docs/src/sidebars/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async function readCategoriesMetadata(contentPath: string) {
4949
const categoryToFile = _.groupBy(categoryFiles, path.dirname);
5050
return combinePromises(
5151
_.mapValues(categoryToFile, async (files, folder) => {
52-
const [filePath] = files;
52+
const filePath = files[0]!;
5353
if (files.length > 1) {
5454
logger.warn`There are more than one category metadata files for path=${folder}: ${files.join(
5555
', ',

0 commit comments

Comments
 (0)