Skip to content

Commit

Permalink
chore: tighten ESLint config (#6931)
Browse files Browse the repository at this point in the history
* chore: tighten ESLint config

* more refactor

* refactor push

* fix
  • Loading branch information
Josh-Cena authored and slorber committed Mar 18, 2022
1 parent 788da9d commit b19cab5
Show file tree
Hide file tree
Showing 54 changed files with 177 additions and 193 deletions.
27 changes: 18 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = {
'no-await-in-loop': OFF,
'no-case-declarations': WARNING,
'no-console': OFF,
'no-continue': OFF,
'no-control-regex': WARNING,
'no-else-return': [WARNING, {allowElseIf: true}],
'no-empty': [WARNING, {allowEmptyCatch: true}],
Expand Down Expand Up @@ -275,6 +276,7 @@ module.exports = {
},
],
'@typescript-eslint/no-inferrable-types': OFF,
'@typescript-eslint/no-namespace': [WARNING, {allowDeclarations: true}],
'no-use-before-define': OFF,
'@typescript-eslint/no-use-before-define': [
ERROR,
Expand All @@ -286,14 +288,11 @@ module.exports = {
'no-shadow': OFF,
'@typescript-eslint/no-shadow': ERROR,
'no-unused-vars': OFF,
'@typescript-eslint/no-unused-vars': [
ERROR,
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
ignoreRestSiblings: true,
},
],
// We don't provide any escape hatches for this rule. Rest siblings and
// function placeholder params are always ignored, and any other unused
// locals must be justified with a disable comment.
'@typescript-eslint/no-unused-vars': [ERROR, {ignoreRestSiblings: true}],
'@typescript-eslint/prefer-optional-chain': ERROR,
},
overrides: [
{
Expand Down Expand Up @@ -340,7 +339,17 @@ module.exports = {
},
},
{
files: ['*.test.ts', '*.test.tsx'],
// Internal files where extraneous deps don't matter much at long as
// they run
files: [
'*.test.ts',
'*.test.tsx',
'admin/**',
'jest/**',
'website/**',
'packages/docusaurus-theme-translations/update.mjs',
'packages/docusaurus-theme-translations/src/utils.ts',
],
rules: {
'import/no-extraneous-dependencies': OFF,
},
Expand Down
15 changes: 13 additions & 2 deletions __tests__/validate-package-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ import fs from 'fs-extra';

type PackageJsonFile = {
file: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
content: any;
content: {
name?: string;
private?: boolean;
version?: string;
repository?: {
type?: string;
url?: string;
directory?: string;
};
publishConfig?: {
access?: string;
};
};
};

async function getPackagesJsonFiles(): Promise<PackageJsonFile[]> {
Expand Down
2 changes: 1 addition & 1 deletion admin/new.docusaurus.io/functions/codesandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import type {Handler} from '@netlify/functions';

import {createPlaygroundResponse} from '../functionUtils/playgroundUtils';

export const handler: Handler = async function handler(_event, _context) {
export const handler: Handler = async function handler() {
return createPlaygroundResponse('codesandbox');
};
2 changes: 1 addition & 1 deletion admin/new.docusaurus.io/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
createPlaygroundDocumentationResponse,
} from '../functionUtils/playgroundUtils';

export const handler: Handler = async (event, _context) => {
export const handler: Handler = async (event) => {
const playgroundName = readPlaygroundName(event);
return playgroundName
? createPlaygroundResponse(playgroundName)
Expand Down
2 changes: 1 addition & 1 deletion admin/new.docusaurus.io/functions/stackblitz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import type {Handler} from '@netlify/functions';

import {createPlaygroundResponse} from '../functionUtils/playgroundUtils';

export const handler: Handler = async function handler(_event, _context) {
export const handler: Handler = async function handler() {
return createPlaygroundResponse('stackblitz');
};
2 changes: 0 additions & 2 deletions admin/scripts/generateExamples.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable import/no-extraneous-dependencies */

import fs from 'fs-extra';
import shell from 'shelljs';

Expand Down
2 changes: 0 additions & 2 deletions admin/scripts/image-resize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable import/no-extraneous-dependencies */

import sharp from 'sharp';
import fs from 'fs-extra';
import path from 'path';
Expand Down
1 change: 0 additions & 1 deletion jest/snapshotPathNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/no-extraneous-dependencies */

// Forked from https://github.com/tribou/jest-serializer-path/blob/master/lib/index.js
// Added some project-specific handlers
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-mdx-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default async function mdxLoader(

// MDX partials are MDX files starting with _ or in a folder starting with _
// Partial are not expected to have associated metadata files or front matter
const isMDXPartial = options.isMDXPartial && options.isMDXPartial(filePath);
const isMDXPartial = options.isMDXPartial?.(filePath);
if (isMDXPartial && hasFrontMatter) {
const errorMessage = `Docusaurus MDX partial files should not contain FrontMatter.
Those partial files use the _ prefix as a convention by default, but this is configurable.
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-mdx-loader/src/remark/toc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function plugin(options: PluginOptions = {}): Transformer {
return (root) => {
const headings: TOCItem[] = [];

visit(root, 'heading', (child: Heading, _index, parent) => {
visit(root, 'heading', (child: Heading, index, parent) => {
const value = toString(child);

// depth:1 headings are titles and not included in the TOC
Expand All @@ -93,7 +93,7 @@ export default function plugin(options: PluginOptions = {}): Transformer {
const {children} = root as Parent<Literal>;
const targetIndex = getOrCreateExistingTargetIndex(children, name);

if (headings && headings.length) {
if (headings.length) {
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
headings,
)};`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {Code, Parent} from 'mdast';
// See https://github.com/facebook/docusaurus/pull/4278
export default function plugin(this: Processor): Transformer {
return (root) => {
visit(root, 'code', (node: Code, _index, parent) => {
visit(root, 'code', (node: Code, index, parent) => {
if (node.lang === 'mdx-code-block') {
const newChildren = (this.parse(node.value) as Parent).children;

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function walk(dir: string): Promise<string[]> {
for (const file of list) {
const fullPath = `${dir}/${file}`;
const stat = await fs.stat(fullPath);
if (stat && stat.isDirectory()) {
if (stat.isDirectory()) {
results.push(...(await walk(fullPath)));
} else {
results.push(fullPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ describe('normalizePluginOptions', () => {
});

it('overrides all default options with valid user options', () => {
const createRedirects: Options['createRedirects'] = (
_routePath: string,
) => [];
const createRedirects: Options['createRedirects'] = () => [];
expect(
testValidate({
fromExtensions: ['exe', 'zip'],
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-client-redirects/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const UserOptionsSchema = Joi.object<PluginOptions>({
redirects: Joi.array()
.items(RedirectPluginOptionValidation)
.default(DEFAULT_OPTIONS.redirects),
createRedirects: Joi.function().arity(1),
createRedirects: Joi.function().maxArity(1),
}).default(DEFAULT_OPTIONS);

export function validateOptions({
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function generateBlogFeed({
const {url: siteUrl, baseUrl, title, favicon} = siteConfig;
const blogBaseUrl = normalizeUrl([siteUrl, baseUrl, routeBasePath]);

const updated = blogPosts[0] && blogPosts[0].metadata.date;
const updated = blogPosts[0]?.metadata.date;

const feed = new Feed({
id: blogBaseUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export default function markdownLoader(
finalContent = truncate(finalContent, markdownLoaderOptions.truncateMarker);
}

return callback && callback(null, finalContent);
return callback?.(null, finalContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,7 @@ describe('site with custom sidebar items generator', () => {
}

it('sidebarItemsGenerator is called with appropriate data', async () => {
const customSidebarItemsGeneratorMock = jest.fn(
async (_arg: SidebarItemsGeneratorOptionArgs) => [],
);
const customSidebarItemsGeneratorMock = jest.fn(async () => []);
const {siteDir} = await loadSite(customSidebarItemsGeneratorMock);

const generatorArg: SidebarItemsGeneratorOptionArgs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ export default function markdownLoader(
const fileString = source;
const callback = this.async();
const options = this.getOptions();
return (
callback && callback(null, linkify(fileString, this.resourcePath, options))
);
return callback?.(null, linkify(fileString, this.resourcePath, options));
}
38 changes: 18 additions & 20 deletions packages/docusaurus-plugin-content-docs/src/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,24 @@ function getSidebarTranslationFileContent(
},
]);

if (category.link) {
if (category.link.type === 'generated-index') {
if (category.link.title) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`,
{
message: category.link.title,
description: `The generated-index page title for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
if (category.link.description) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`,
{
message: category.link.description,
description: `The generated-index page description for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
if (category.link?.type === 'generated-index') {
if (category.link.title) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`,
{
message: category.link.title,
description: `The generated-index page title for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
if (category.link.description) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`,
{
message: category.link.description,
description: `The generated-index page description for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export default function markdownLoader(
// TODO provide additional md processing here? like interlinking pages?
// fileString = linkify(fileString)

return callback && callback(null, fileString);
return callback?.(null, fileString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ function PluginContent({
<div>
{Object.entries(pluginContent)
// filter plugin instances with no content
.filter(
([_pluginId, pluginInstanceContent]) => !!pluginInstanceContent,
)
.filter(([, pluginInstanceContent]) => !!pluginInstanceContent)
.map(([pluginId, pluginInstanceContent]) => (
<PluginInstanceContent
key={pluginId}
Expand All @@ -61,7 +59,7 @@ export default function DebugContent({allContent}: Props): JSX.Element {
<div>
{Object.entries(allContent)
// filter plugins with no content
.filter(([_pluginName, pluginContent]) =>
.filter(([, pluginContent]) =>
Object.values(pluginContent).some(
(instanceContent) => !!instanceContent,
),
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-pwa/src/registerSw.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function isStandaloneDisplayMode() {
const OfflineModeActivationStrategiesImplementations = {
always: () => true,
mobile: () => window.innerWidth <= MAX_MOBILE_WIDTH,
saveData: () => !!(navigator.connection && navigator.connection.saveData),
saveData: () => !!navigator.connection?.saveData,
appInstalled: async () => {
const installedEventFired = await isAppInstalledEventFired();
const installedRelatedApps = await isAppInstalledRelatedApps();
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-pwa/src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function getPossibleURLs(url) {
});
}

const type = event.data && event.data.type;
const type = event.data?.type;

if (type === 'SKIP_WAITING') {
self.skipWaiting();
Expand Down
9 changes: 3 additions & 6 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,8 @@ declare module '@theme/IconExternalLink' {
}

declare module '@theme/TagsListByLetter' {
export type TagsListItem = Readonly<{
name: string;
permalink: string;
count: number;
}>;
import type {TagsListItem} from '@docusaurus/theme-common';

export interface Props {
readonly tags: readonly TagsListItem[];
}
Expand All @@ -853,7 +850,7 @@ declare module '@theme/TagsListInline' {
}

declare module '@theme/Tag' {
import type {TagsListItem} from '@theme/TagsListByLetter';
import type {TagsListItem} from '@docusaurus/theme-common';
import type {Optional} from 'utility-types';

export interface Props extends Optional<TagsListItem, 'count'> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ function DocSidebarItemHtml({
)}
key={index}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: value,
}}
dangerouslySetInnerHTML={{__html: value}}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export default function FooterCopyright({copyright}: Props): JSX.Element {
className="footer__copyright"
// Developer provided the HTML, so assume it's safe.
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: copyright,
}}
dangerouslySetInnerHTML={{__html: copyright}}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ function ColumnLinkItem({item}: {item: ColumnItemType}) {
className="footer__item"
// Developer provided the HTML, so assume it's safe.
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: item.html,
}}
dangerouslySetInnerHTML={{__html: item.html}}
/>
) : (
<li key={item.href || item.to} className="footer__item">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ function SimpleLinkItem({item}: {item: Props['links'][number]}) {
className="footer__link-item"
// Developer provided the HTML, so assume it's safe.
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: item.html,
}}
dangerouslySetInnerHTML={{__html: item.html}}
/>
) : (
<LinkItem item={item} />
Expand Down
Loading

0 comments on commit b19cab5

Please sign in to comment.