Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(v2): reduce number of ESLint warnings #4993

Merged
merged 16 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jest.config.js
jest.transform.js
website/
scripts
examples/

packages/docusaurus/lib/
packages/docusaurus-*/lib/*
Expand Down
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ module.exports = {
'no-unused-vars': OFF,
'no-nested-ternary': WARNING,
'@typescript-eslint/no-empty-function': OFF,
'@typescript-eslint/no-non-null-assertion': OFF, // Have to use type assertion anyways
'@typescript-eslint/no-unused-vars': [ERROR, {argsIgnorePattern: '^_'}],
'@typescript-eslint/ban-ts-comment': [
ERROR,
Expand All @@ -113,7 +114,7 @@ module.exports = {
'prefer-destructuring': WARNING,
yoda: WARNING,
'no-control-regex': WARNING,
'no-empty': WARNING,
'no-empty': [WARNING, {allowEmptyCatch: true}],
'no-prototype-builtins': WARNING,
'no-case-declarations': WARNING,
'no-undef': OFF,
Expand All @@ -130,6 +131,7 @@ module.exports = {
],
rules: {
'header/header': OFF,
'global-require': OFF,
},
},
{
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function doProcessDocMetadata({

// Strip number prefixes by default (01-MyFolder/01-MyDoc.md => MyFolder/MyDoc) by default,
// but allow to disable this behavior with frontmatterr
// eslint-disable-next-line camelcase
parse_number_prefixes = true,
} = frontMatter;

Expand All @@ -144,6 +145,7 @@ function doProcessDocMetadata({
// ex: myDoc -> .
const sourceDirName = path.dirname(source);

// eslint-disable-next-line camelcase
const {filename: unprefixedFileName, numberPrefix} = parse_number_prefixes
? options.numberPrefixParser(sourceFileNameWithoutExtension)
: {filename: sourceFileNameWithoutExtension, numberPrefix: undefined};
Expand Down Expand Up @@ -172,6 +174,7 @@ function doProcessDocMetadata({
return undefined;
}
// Eventually remove the number prefixes from intermediate directories
// eslint-disable-next-line camelcase
return parse_number_prefixes
? stripPathNumberPrefixes(sourceDirName, options.numberPrefixParser)
: sourceDirName;
Expand Down
20 changes: 18 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ function assertIsCategory(
);
}
// "collapsed" is an optional property
if (item.hasOwnProperty('collapsed') && typeof item.collapsed !== 'boolean') {
if (
typeof item.collapsed !== 'undefined' &&
typeof item.collapsed !== 'boolean'
) {
throw new Error(
`Error loading ${JSON.stringify(item)}: "collapsed" must be a boolean.`,
);
Expand Down Expand Up @@ -438,7 +441,20 @@ export function collectSidebarsDocIds(
});
}

export function createSidebarsUtils(sidebars: Sidebars) {
export function createSidebarsUtils(
sidebars: Sidebars,
): {
getFirstDocIdOfFirstSidebar: () => string | undefined;
getSidebarNameByDocId: (docId: string) => string | undefined;
getDocNavigation: (
docId: string,
) => {
sidebarName: string | undefined;
previousId: string | undefined;
nextId: string | undefined;
};
checkSidebarsDocIds: (validDocIds: string[], sidebarFilePath: string) => void;
} {
const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);

function getFirstDocIdOfFirstSidebar(): string | undefined {
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export type LastUpdateData = {
};

export type DocFrontMatter = {
// Front matter uses snake case
/* eslint-disable camelcase */
id?: string;
title?: string;
hide_title?: boolean;
Expand All @@ -195,6 +197,7 @@ export type DocFrontMatter = {
pagination_label?: string;
custom_edit_url?: string | null;
parse_number_prefixes?: boolean;
/* eslint-enable camelcase */
};

export type DocMetadataBase = LastUpdateData & {
Expand All @@ -208,7 +211,6 @@ export type DocMetadataBase = LastUpdateData & {
sourceDirName: string; // relative to the docs folder (can be ".")
slug: string;
permalink: string;
// eslint-disable-next-line camelcase
sidebarPosition?: number;
editUrl?: string | null;
frontMatter: DocFrontMatter & Record<string, unknown>;
Expand Down
12 changes: 7 additions & 5 deletions packages/docusaurus-plugin-content-docs/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,13 @@ function createVersionMetadata({
// retro-compatible values
const defaultVersionLabel =
versionName === CURRENT_VERSION_NAME ? 'Next' : versionName;
const defaultVersionPathPart = isLast
? ''
: versionName === CURRENT_VERSION_NAME
? 'next'
: versionName;
function getDefaultVersionPathPart() {
if (isLast) {
return '';
}
return versionName === CURRENT_VERSION_NAME ? 'next' : versionName;
}
const defaultVersionPathPart = getDefaultVersionPathPart();

const versionOptions: VersionOptions = options.versions[versionName] ?? {};

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-ideal-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default function (
options: {
emitFile: !isServer, // don't emit for server-side rendering
disable: !isProd,
// eslint-disable-next-line global-require
adapter: require('@docusaurus/responsive-loader/sharp'),
name: isProd
? 'assets/ideal-img/[name].[hash:hex:7].[width].[ext]'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const ThemedImage = (props: Props): JSX.Element => {

type SourceName = keyof Props['sources'];

const clientThemes: SourceName[] = isDarkTheme ? ['dark'] : ['light'];

const renderedSourceNames: SourceName[] = isClient
? isDarkTheme
? ['dark']
: ['light']
? clientThemes
: // We need to render both images on the server to avoid flash
// See https://github.com/facebook/docusaurus/pull/3730
['light', 'dark'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const ThemedImage = (props: Props): JSX.Element => {

type SourceName = keyof Props['sources'];

const clientThemes: SourceName[] = isDarkTheme ? ['dark'] : ['light'];

const renderedSourceNames: SourceName[] = isClient
? isDarkTheme
? ['dark']
: ['light']
? clientThemes
: // We need to render both images on the server to avoid flash
// See https://github.com/facebook/docusaurus/pull/3730
['light', 'dark'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const useTabGroupChoice = (): useTabGroupChoiceReturns => {
useEffect(() => {
try {
const localStorageChoices = {};
for (const storageKey of listStorageKeys()) {
listStorageKeys().forEach((storageKey) => {
if (storageKey.startsWith(TAB_CHOICE_PREFIX)) {
const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length);
localStorageChoices[groupId] = createStorageSlot(storageKey).get();
}
}
});
setChoices(localStorageChoices);
} catch (err) {
console.error(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function extractThemeCodeMessages() {
const {
globSourceCodeFilePaths,
extractAllSourceCodeFileTranslations,
// eslint-disable-next-line global-require
} = require('@docusaurus/core/lib/server/translations/translationsExtractor');

const filePaths = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import {useLocation} from '@docusaurus/router';
// Permits to obtain the url of the current page in another locale
// Useful to generate hreflang meta headers etc...
// See https://developers.google.com/search/docs/advanced/crawling/localized-versions
export function useAlternatePageUtils() {
export function useAlternatePageUtils(): {
createUrl: ({
locale,
fullyQualified,
}: {
locale: string;
fullyQualified: boolean;
}) => string;
} {
const {
siteConfig: {baseUrl, url},
i18n: {defaultLocale, currentLocale},
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-theme-common/src/utils/usePluralForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ function selectPluralMessage(
}
}

export function usePluralForm() {
export function usePluralForm(): {
selectMessage: (count: number, pluralMessages: string) => string;
} {
const localePluralForm = useLocalePluralForms();
return {
selectMessage: (count: number, pluralMessages: string): string => {
Expand Down
9 changes: 4 additions & 5 deletions packages/docusaurus-utils-common/src/applyTrailingSlash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default function applyTrailingSlash(
function removeTrailingSlash(str: string): string {
return str.endsWith('/') ? str.slice(0, -1) : str;
}
function handleTrailingSlash(str: string, trailing: boolean): string {
return trailing ? addTrailingSlash(str) : removeTrailingSlash(str);
}

// undefined = legacy retrocompatible behavior
if (typeof trailingSlash === 'undefined') {
Expand All @@ -32,10 +35,6 @@ export default function applyTrailingSlash(

// Never transform '/' to ''
const newPathname =
pathname === '/'
? '/'
: trailingSlash
? addTrailingSlash(pathname)
: removeTrailingSlash(pathname);
pathname === '/' ? '/' : handleTrailingSlash(pathname, trailingSlash);
return path.replace(pathname, newPathname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const logValidationBugReportHint = (): void => {
);
};

export function printWarning(warning?: Joi.ValidationError) {
export function printWarning(warning?: Joi.ValidationError): void {
if (warning) {
const warningMessages = warning.details
.map(({message}) => message)
Expand Down
12 changes: 6 additions & 6 deletions packages/docusaurus-utils/src/markdownParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ export function createExcerpt(fileString: string): string | undefined {
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
.replace(/^\#\s*([^#]*)\s*\#?/gm, '')
.replace(/^#\s*([^#]*)\s*#?/gm, '')
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
// Remove Markdown + ATX-style headers
.replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
.replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
// Remove emphasis and strikethroughs.
.replace(/([\*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
.replace(/([*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
// Remove images.
.replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
.replace(/!\[(.*?)\][[(].*?[\])]/g, '$1')
// Remove footnotes.
.replace(/\[\^.+?\](\: .*?$)?/g, '')
.replace(/\[\^.+?\](: .*?$)?/g, '')
// Remove inline links.
.replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
.replace(/\[(.*?)\][[(].*?[\])]/g, '$1')
// Remove inline code.
.replace(/`(.+?)`/g, '$1')
// Remove blockquotes.
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/commands/buildRemoteBranchUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function buildUrl(
organizationName: string,
projectName: string,
useSSH: boolean | undefined,
) {
): string {
return useSSH
? buildSshUrl(githubHost, organizationName, projectName, githubPort)
: buildHttpsUrl(
Expand Down
5 changes: 4 additions & 1 deletion packages/docusaurus/src/server/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
};
}

export function shouldWarnAboutNodeVersion(version: number, locales: string[]) {
export function shouldWarnAboutNodeVersion(
version: number,
locales: string[],
): boolean {
const isOnlyEnglish = locales.length === 1 && locales.includes('en');
const isOlderNodeVersion = version < 14;
return isOlderNodeVersion && !isOnlyEnglish;
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function loadSiteConfig({
}: {
siteDir: string;
customConfigFilePath?: string;
}) {
}): Promise<{siteConfig: DocusaurusConfig; siteConfigPath: string}> {
const siteConfigPathUnresolved =
customConfigFilePath ?? DEFAULT_CONFIG_FILE_NAME;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {applyTrailingSlash} from '@docusaurus/utils-common';
export default function applyRouteTrailingSlash(
route: RouteConfig,
trailingSlash: boolean | undefined,
) {
): RouteConfig {
return {
...route,
path: applyTrailingSlash(route.path, trailingSlash),
Expand Down