Skip to content

Commit

Permalink
chore(v2): Fix more linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 committed Mar 17, 2021
1 parent 5e73c72 commit 71a5973
Show file tree
Hide file tree
Showing 27 changed files with 116 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
'jsx-a11y/no-noninteractive-element-interactions': WARNING,
'no-console': OFF,
'no-else-return': OFF,
'no-param-reassign': [WARNING, {props: false}],
'no-underscore-dangle': OFF,
curly: [WARNING, 'all'],
'react/jsx-closing-bracket-location': OFF, // Conflicts with Prettier.
Expand Down Expand Up @@ -103,7 +104,6 @@ module.exports = {
'import/no-extraneous-dependencies': ERROR,
'no-useless-escape': WARNING,
'prefer-template': WARNING,
'no-param-reassign': WARNING,
'no-template-curly-in-string': WARNING,
'array-callback-return': WARNING,
camelcase: WARNING,
Expand Down
1 change: 1 addition & 0 deletions __tests__/validate-package-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const readFile = util.promisify(fsCb.readFile);

type PackageJsonFile = {
file: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
content: any;
};

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.0.0-alpha.72",
"description": "Docusaurus Loader for MDX",
"main": "src/index.js",
"types": "src/index.d.ts",
"publishConfig": {
"access": "public"
},
Expand Down
19 changes: 19 additions & 0 deletions packages/docusaurus-mdx-loader/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

type RemarkOrRehypePlugin =
// eslint-disable-next-line @typescript-eslint/ban-types
[Function, Record<string, unknown>] | Function;

declare function docusaurusMdxLoader(fileString: string): string;

export interface RemarkAndRehypePluginOptions {
remarkPlugins: RemarkOrRehypePlugin[];
rehypePlugins: string[];
beforeDefaultRemarkPlugins: RemarkOrRehypePlugin[];
beforeDefaultRehypePlugins: RemarkOrRehypePlugin[];
}
2 changes: 2 additions & 0 deletions packages/docusaurus-mdx-loader/src/remark/toc/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

const toString = require('mdast-util-to-string');
const visit = require('unist-util-visit');
// Destructuring require tslib
// eslint-disable-next-line prefer-destructuring
const toValue = require('../utils').toValue;

/** @typedef {import('@docusaurus/types').TOCItem} TOC */
Expand Down
19 changes: 10 additions & 9 deletions packages/docusaurus-migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Color from 'color';

import {
ClassicPresetEntries,
SidebarEntry,
SidebarEntries,
VersionOneConfig,
VersionTwoConfig,
Expand Down Expand Up @@ -584,26 +585,23 @@ function migrateVersionedSidebar(
return;
}
const newSidebar = Object.entries(sidebarEntries).reduce(
(topLevel: {[key: string]: any}, value) => {
(topLevel: SidebarEntries, value) => {
const key = value[0].replace(versionRegex, '');
// eslint-disable-next-line no-param-reassign
topLevel[key] = Object.entries(value[1]).reduce(
(
acc: {[key: string]: Array<Record<string, unknown> | string>},
val,
) => {
acc[
val[0].replace(versionRegex, '')
] = (val[1] as Array<any>).map((item) => {
] = (val[1] as Array<SidebarEntry>).map((item) => {
if (typeof item === 'string') {
return item.replace(versionRegex, '');
}
return {
type: 'category',
label: item.label,
ids: item.ids.map((id: string) =>
id.replace(versionRegex, ''),
),
ids: item.ids.map((id) => id.replace(versionRegex, '')),
};
});
return acc;
Expand All @@ -618,14 +616,14 @@ function migrateVersionedSidebar(
});
sidebars.forEach((sidebar) => {
const newSidebar = Object.entries(sidebar.entries).reduce(
(acc: {[key: string]: any}, val) => {
(acc: SidebarEntries, val) => {
const key = `version-${sidebar.version}/${val[0]}`;
// eslint-disable-next-line prefer-destructuring
acc[key] = Object.entries(val[1]).map((value) => {
return {
type: 'category',
label: value[0],
items: (value[1] as Array<any>).map((sidebarItem) => {
items: (value[1] as Array<SidebarEntry>).map((sidebarItem) => {
if (typeof sidebarItem === 'string') {
return {
type: 'doc',
Expand Down Expand Up @@ -756,7 +754,10 @@ function migratePackageFile(
newDir: string,
): void {
const packageFile = importFresh(`${siteDir}/package.json`) as {
[key: string]: any;
scripts?: Record<string, string>;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
[otherKey: string]: unknown;
};
packageFile.scripts = {
...packageFile.scripts,
Expand Down
12 changes: 10 additions & 2 deletions packages/docusaurus-migrate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export type ClassicPresetEntries = {
theme: {[key: string]: unknown};
};

export type SidebarEntry =
| string
| {
type: string;
label: string;
ids: string[];
};

export type SidebarEntries = {
[key: string]:
| Record<string, unknown>
Expand Down Expand Up @@ -97,10 +105,10 @@ export type VersionOneConfig = {
organizationName?: string;
projectName?: string;
noIndex?: boolean;
headerLinks?: Array<any>;
headerLinks?: Array<{doc: string; href: string; label: string; page: string}>;
headerIcon?: string;
favicon?: string;
colors?: any;
colors?: {primaryColor: string};
copyright?: string;
editUrl?: string;
customDocsPath?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable @typescript-eslint/no-non-null-assertion */

import fs from 'fs-extra';
import path from 'path';
import pluginContentBlog from '../index';
Expand Down
13 changes: 2 additions & 11 deletions packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
import {
BrokenMarkdownLink,
ContentPaths,
Expand Down Expand Up @@ -33,7 +34,7 @@ export type EditUrlFunction = (editUrlParams: {
locale: string;
}) => string | undefined;

export interface PluginOptions {
export interface PluginOptions extends RemarkAndRehypePluginOptions {
id?: string;
path: string;
routeBasePath: string;
Expand All @@ -47,16 +48,6 @@ export interface PluginOptions {
blogDescription: string;
blogSidebarCount: number | 'ALL';
blogSidebarTitle: string;
remarkPlugins: ([Function, Record<string, unknown>] | Function)[];
beforeDefaultRehypePlugins: (
| [Function, Record<string, unknown>]
| Function
)[];
beforeDefaultRemarkPlugins: (
| [Function, Record<string, unknown>]
| Function
)[];
rehypePlugins: string[];
truncateMarker: RegExp;
showReadingTime: boolean;
feedOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable @typescript-eslint/no-non-null-assertion */

import path from 'path';
import {isMatch} from 'picomatch';
import commander from 'commander';
Expand Down Expand Up @@ -42,8 +44,8 @@ const defaultDocMetadata: Partial<DocMetadata> = {

const createFakeActions = (contentDir: string) => {
const routeConfigs: RouteConfig[] = [];
const dataContainer: any = {};
const globalDataContainer: any = {};
const dataContainer: Record<string, unknown> = {};
const globalDataContainer: {pluginName?: {pluginId: unknown}} = {};

const actions = {
addRoute: (config: RouteConfig) => {
Expand All @@ -53,7 +55,7 @@ const createFakeActions = (contentDir: string) => {
dataContainer[name] = content;
return path.join(contentDir, name);
},
setGlobalData: (data: any) => {
setGlobalData: (data: unknown) => {
globalDataContainer.pluginName = {pluginId: data};
},
};
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export function cliDocsVersionCommand(

// Since we are going to create `version-${version}` folder, we need to make
// sure it's a valid pathname.
if (/[<>:"\/\\|?*\x00-\x1F]/g.test(version)) {
// eslint-disable-next-line no-control-regex
if (/[<>:"|?*\x00-\x1F]/g.test(version)) {
throw new Error(
`${pluginIdLogPrefix}Invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const getActiveDocContext = (
data.versions.forEach((version) => {
version.docs.forEach((doc) => {
if (doc.id === docId) {
result[version.name!] = doc;
result[version.name] = doc;
}
});
});
Expand Down Expand Up @@ -157,7 +157,7 @@ export const getDocVersionSuggestions = (
const isNotOnLatestVersion = activeDocContext.activeVersion !== latestVersion;

const latestDocSuggestion: GlobalDoc | undefined = isNotOnLatestVersion
? activeDocContext?.alternateDocVersions[latestVersion.name!]
? activeDocContext?.alternateDocVersions[latestVersion.name]
: undefined;

const latestVersionSuggestion = isNotOnLatestVersion
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function normalizeCategoryShorthand(
function assertItem<K extends string>(
item: Record<string, unknown>,
keys: K[],
): asserts item is Record<K, any> {
): asserts item is Record<K, never> {
const unknownKeys = Object.keys(item).filter(
// @ts-expect-error: key is always string
(key) => !keys.includes(key as string) && key !== 'type',
Expand Down Expand Up @@ -272,9 +272,7 @@ export function collectSidebarsDocIds(
});
}

export function createSidebarsUtils(
sidebars: Sidebars,
): Record<string, Function> {
export function createSidebarsUtils(sidebars: Sidebars) {
const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);

function getFirstDocIdOfFirstSidebar(): string | undefined {
Expand Down
14 changes: 3 additions & 11 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// eslint-disable-next-line spaced-comment
/// <reference types="@docusaurus/module-type-aliases" />

import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
import {
BrokenMarkdownLink as IBrokenMarkdownLink,
ContentPaths,
Expand Down Expand Up @@ -72,21 +73,12 @@ export type VersionsOptions = {

export type PluginOptions = MetadataOptions &
PathOptions &
VersionsOptions & {
VersionsOptions &
RemarkAndRehypePluginOptions & {
id: string;
include: string[];
docLayoutComponent: string;
docItemComponent: string;
remarkPlugins: ([Function, Record<string, unknown>] | Function)[];
rehypePlugins: string[];
beforeDefaultRemarkPlugins: (
| [Function, Record<string, unknown>]
| Function
)[];
beforeDefaultRehypePlugins: (
| [Function, Record<string, unknown>]
| Function
)[];
admonitions: Record<string, unknown>;
disableVersioning: boolean;
excludeNextVersionDocs?: boolean;
Expand Down
14 changes: 3 additions & 11 deletions packages/docusaurus-plugin-content-pages/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

export interface PluginOptions {
import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';

export interface PluginOptions extends RemarkAndRehypePluginOptions {
id?: string;
path: string;
routeBasePath: string;
include: string[];
exclude: string[];
mdxPageComponent: string;
remarkPlugins: ([Function, Record<string, unknown>] | Function)[];
rehypePlugins: string[];
beforeDefaultRemarkPlugins: (
| [Function, Record<string, unknown>]
| Function
)[];
beforeDefaultRehypePlugins: (
| [Function, Record<string, unknown>]
| Function
)[];
admonitions: Record<string, unknown>;
}

Expand Down
23 changes: 11 additions & 12 deletions packages/docusaurus-theme-classic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {Plugin} from '@docusaurus/types';
import {DocusaurusContext, Plugin} from '@docusaurus/types';
import {ThemeConfig} from '@docusaurus/theme-common';
import {getTranslationFiles, translateThemeConfig} from './translations';
import path from 'path';
import Module from 'module';
import postcss from 'postcss';
import postcss, {Root as PostCssRoot} from 'postcss';
import rtlcss from 'rtlcss';
import {readDefaultCodeTranslationMessages} from '@docusaurus/utils';

Expand Down Expand Up @@ -73,14 +74,15 @@ type PluginOptions = {
};

export default function docusaurusThemeClassic(
context: any, // TODO: LoadContext is missing some of properties
context: DocusaurusContext, // TODO: LoadContext is missing some of properties
options: PluginOptions,
): Plugin<void> {
const {
siteConfig: {themeConfig},
siteConfig: {themeConfig: roughlyTypedThemeConfig},
i18n: {currentLocale, localeConfigs},
} = context;
const {colorMode, prism: {additionalLanguages = []} = {}} = themeConfig || {};
const themeConfig = (roughlyTypedThemeConfig || {}) as ThemeConfig;
const {colorMode, prism: {additionalLanguages = []} = {}} = themeConfig;
const {customCss} = options || {};
const {direction} = localeConfigs[currentLocale];

Expand Down Expand Up @@ -143,10 +145,7 @@ export default function docusaurusThemeClassic(

return {
stats: {
warningsFilter: [
// The TS def does not allow function for array item :(
useDocsWarningFilter as any,
],
warningsFilter: useDocsWarningFilter,
},
plugins: [
new ContextReplacementPlugin(
Expand All @@ -164,12 +163,12 @@ export default function docusaurusThemeClassic(
const resolvedInfimaFile = require.resolve(
getInfimaCSSFile(direction),
);
function isInfimaCSSFile(file) {
function isInfimaCSSFile(file?: string) {
return file === resolvedInfimaFile;
}

return function (root: any) {
const file = root?.source.input.file;
return function (root: PostCssRoot) {
const file = root?.source?.input.file;

// Skip Infima as we are using the its RTL version.
if (isInfimaCSSFile(file)) {
Expand Down
Loading

0 comments on commit 71a5973

Please sign in to comment.