chore(v2): Fix more eslint errors (#2976)

This commit is contained in:
Sam Zhou 2020-06-21 03:09:00 -04:00 committed by GitHub
parent 3611c96f90
commit 6e43c9bd34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
81 changed files with 375 additions and 237 deletions

View file

@ -170,7 +170,8 @@ describe('simple site', () => {
});
// unrelated frontmatter is not part of metadata
expect(data['unrelated_frontmatter']).toBeUndefined();
// @ts-expect-error: It doesn't exist, so the test will show it's undefined.
expect(data.unrelated_frontmatter).toBeUndefined();
});
test('docs with last update time and author', async () => {

View file

@ -14,15 +14,15 @@ import {
VERSIONED_SIDEBARS_DIR,
} from './constants';
export function getVersionedDocsDir(siteDir: string) {
export function getVersionedDocsDir(siteDir: string): string {
return path.join(siteDir, VERSIONED_DOCS_DIR);
}
export function getVersionedSidebarsDir(siteDir: string) {
export function getVersionedSidebarsDir(siteDir: string): string {
return path.join(siteDir, VERSIONED_SIDEBARS_DIR);
}
export function getVersionsJSONFile(siteDir: string) {
export function getVersionsJSONFile(siteDir: string): string {
return path.join(siteDir, VERSIONS_JSON_FILE);
}
@ -41,6 +41,7 @@ export default function (siteDir: string): Env {
fs.readFileSync(versionsJSONFile, 'utf8'),
);
if (parsedVersions && parsedVersions.length > 0) {
// eslint-disable-next-line prefer-destructuring
versioning.latestVersion = parsedVersions[0];
versioning.enabled = true;
versioning.versions = parsedVersions;

View file

@ -378,9 +378,7 @@ Available document ids=
}),
);
return routes.sort((a, b) =>
a.path > b.path ? 1 : b.path > a.path ? -1 : 0,
);
return routes.sort((a, b) => a.path.localeCompare(b.path));
};
// This is the base route of the document root (for a doc given version)
@ -400,10 +398,10 @@ Available document ids=
// Important: the layout component should not end with /,
// as it conflicts with the home doc
// Workaround fix for https://github.com/facebook/docusaurus/issues/2917
const path = docsBaseRoute === '/' ? '' : docsBaseRoute;
const docsPath = docsBaseRoute === '/' ? '' : docsBaseRoute;
addRoute({
path,
path: docsPath,
exact: false, // allow matching /docs/* as well
component: docLayoutComponent, // main docs component (DocPage)
routes, // subroute for each doc
@ -466,7 +464,7 @@ Available document ids=
const routes = await genRoutes(Object.values(content.docsMetadata));
const docsBaseMetadata = createDocsBaseMetadata();
const docsBaseRoute = normalizeUrl([baseUrl, routeBasePath]);
return addBaseRoute(docsBaseRoute, docsBaseMetadata, routes);
await addBaseRoute(docsBaseRoute, docsBaseMetadata, routes);
}
},

View file

@ -41,13 +41,12 @@ function inferVersion(
.replace(/^version-/, '');
if (inferredVersion && versioning.versions.includes(inferredVersion)) {
return inferredVersion;
} else {
throw new Error(
`Can't infer version from folder=${dirName}
}
throw new Error(
`Can't infer version from folder=${dirName}
Expected versions:
- ${versioning.versions.join('- ')}`,
);
}
);
} else {
return 'next';
}

View file

@ -30,6 +30,7 @@ export default function createOrder(allSidebars: Sidebar = {}): Order {
case 'doc':
ids.push(item.id);
break;
default:
}
});
};

View file

@ -45,8 +45,12 @@ function normalizeCategoryShorthand(
/**
* Check that item contains only allowed keys.
*/
function assertItem(item: Object, keys: string[]): void {
function assertItem<K extends string>(
item: any,
keys: K[],
): asserts item is Record<K, any> {
const unknownKeys = Object.keys(item).filter(
// @ts-expect-error: key is always string
(key) => !keys.includes(key) && key !== 'type',
);
@ -59,7 +63,9 @@ function assertItem(item: Object, keys: string[]): void {
}
}
function assertIsCategory(item: any): asserts item is SidebarItemCategoryRaw {
function assertIsCategory(
item: unknown,
): asserts item is SidebarItemCategoryRaw {
assertItem(item, ['items', 'label', 'collapsed']);
if (typeof item.label !== 'string') {
throw new Error(
@ -79,7 +85,7 @@ function assertIsCategory(item: any): asserts item is SidebarItemCategoryRaw {
}
}
function assertIsDoc(item: any): asserts item is SidebarItemDoc {
function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
assertItem(item, ['id']);
if (typeof item.id !== 'string') {
throw new Error(
@ -88,7 +94,7 @@ function assertIsDoc(item: any): asserts item is SidebarItemDoc {
}
}
function assertIsLink(item: any): asserts item is SidebarItemLink {
function assertIsLink(item: unknown): asserts item is SidebarItemLink {
assertItem(item, ['href', 'label']);
if (typeof item.href !== 'string') {
throw new Error(
@ -175,7 +181,7 @@ export default function loadSidebars(sidebarPaths?: string[]): Sidebar {
return {} as Sidebar;
}
sidebarPaths.map((sidebarPath) => {
sidebarPaths.forEach((sidebarPath) => {
if (sidebarPath && fs.existsSync(sidebarPath)) {
const sidebar = importFresh(sidebarPath) as SidebarRaw;
Object.assign(allSidebars, sidebar);

View file

@ -5,13 +5,20 @@
* LICENSE file in the root directory of this source tree.
*/
let versions: string[] = [];
let versions: string[];
try {
// eslint-disable-next-line global-require
versions = require('@site/versions.json');
} catch (e) {}
} catch {
versions = [];
}
function useVersioning() {
function useVersioning(): {
versioningEnabled: boolean;
versions: string[];
latestVersion: string;
} {
return {
versioningEnabled: versions.length > 0,
versions,

View file

@ -65,7 +65,7 @@ export type SidebarItemRaw =
| SidebarItemCategoryRaw
| {
type: string;
[key: string]: any;
[key: string]: unknown;
};
export interface SidebarCategoryShorthandRaw {

View file

@ -15,6 +15,8 @@ import path from 'path';
import {Sidebar, PathOptions, SidebarItem} from './types';
import loadSidebars from './sidebars';
// Tests depend on non-default export for mocking.
// eslint-disable-next-line import/prefer-default-export
export function docsVersion(
version: string | null | undefined,
siteDir: string,