Skip to content

Commit

Permalink
chore(v2): Fix more eslint errors (#2976)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored Jun 21, 2020
1 parent 3611c96 commit 6e43c9b
Show file tree
Hide file tree
Showing 81 changed files with 374 additions and 236 deletions.
14 changes: 4 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,23 @@ module.exports = {
],
'no-unused-vars': OFF,
'@typescript-eslint/no-unused-vars': [ERROR, {argsIgnorePattern: '^_'}],
'@typescript-eslint/ban-ts-comment': [
ERROR,
{'ts-expect-error': 'allow-with-description'},
],

// TODO re-enable some these as errors
// context: https://github.com/facebook/docusaurus/pull/2949
'@typescript-eslint/ban-ts-comment': WARNING,
'@typescript-eslint/ban-types': WARNING,
'import/prefer-default-export': WARNING,
'import/no-extraneous-dependencies': WARNING,
'no-useless-escape': WARNING,
'prefer-template': WARNING,
'no-shadow': WARNING,
'no-param-reassign': WARNING,
'no-else-return': WARNING,
'no-template-curly-in-string': WARNING,
'array-callback-return': WARNING,
camelcase: WARNING,
'no-nested-ternary': WARNING,
'object-shorthand': WARNING,
'no-restricted-syntax': WARNING,
'no-unused-expressions': WARNING,
'consistent-return': WARNING,
'no-useless-return': WARNING,
'@typescript-eslint/no-empty-function': WARNING,
'global-require': WARNING,
'prefer-destructuring': WARNING,
Expand All @@ -114,8 +110,6 @@ module.exports = {
'no-empty': WARNING,
'no-prototype-builtins': WARNING,
'no-case-declarations': WARNING,
'default-case': WARNING,
'dot-notation': WARNING,
},
overrides: [
{
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
"@babel/preset-typescript": "^7.9.0",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"@types/express": "^4.17.2",
"@types/fs-extra": "^8.0.1",
"@types/inquirer": "^6.5.0",
Expand All @@ -54,13 +52,18 @@
"@types/lodash.pick": "^4.4.6",
"@types/lodash.pickby": "^4.6.6",
"@types/node": "^13.11.0",
"@types/react": "^16.9.13",
"@types/react": "^16.9.38",
"@types/react-dev-utils": "^9.0.1",
"@types/react-helmet": "^6.0.0",
"@types/react-loadable": "^5.5.3",
"@types/react-router-config": "^5.0.1",
"@types/semver": "^7.1.0",
"@types/shelljs": "^0.8.6",
"@types/webpack": "^4.41.0",
"@types/webpack-dev-server": "^3.9.0",
"@types/webpack-merge": "^4.1.5",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"babel-eslint": "^10.0.3",
"concurrently": "^5.2.0",
"enzyme": "^3.10.0",
Expand All @@ -85,7 +88,7 @@
"rimraf": "^3.0.2",
"serve": "^11.3.2",
"stylelint": "^13.2.1",
"typescript": "^3.7.2"
"typescript": "^3.9.5"
},
"peerDependencies": {
"stylelint-copyright": "^2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-init/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const chalk = require('chalk');
const semver = require('semver');
const path = require('path');
const program = require('commander');
const {init} = require('../lib');
const {default: init} = require('../lib');
const requiredVersion = require('../package.json').engines.node;

if (!semver.satisfies(process.version, requiredVersion)) {
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus-init/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ function isValidGitRepoUrl(gitRepoUrl: string): boolean {
return ['https://', 'git@'].some((item) => gitRepoUrl.startsWith(item));
}

async function updatePkg(pkgPath: string, obj: any): Promise<void> {
async function updatePkg(
pkgPath: string,
obj: Record<string, unknown>,
): Promise<void> {
const content = await fs.readFile(pkgPath, 'utf-8');
const pkg = JSON.parse(content);
const newPkg = Object.assign(pkg, obj);

await fs.outputFile(pkgPath, JSON.stringify(newPkg, null, 2));
}

export async function init(
export default async function init(
rootDir: string,
siteName?: string,
reqTemplate?: string,
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-plugin-client-redirects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
"dependencies": {
"@docusaurus/types": "^2.0.0-alpha.58",
"@docusaurus/utils": "^2.0.0-alpha.58",
"chalk": "^3.0.0",
"eta": "^1.1.1",
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"lodash": "^4.17.15",
"yup": "^0.29.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('collectRedirects', () => {
{
createRedirects: (routePath) => {
if (routePath === '/') {
return [[`/fromPath`]] as any;
return ([[`/fromPath`]] as unknown) as string;
}
return undefined;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,26 @@ describe('normalizePluginOptions', () => {
test('should reject bad fromExtensions user inputs', () => {
expect(() =>
normalizePluginOptions({
fromExtensions: [null, undefined, 123, true] as any,
fromExtensions: ([null, undefined, 123, true] as unknown) as string[],
}),
).toThrowErrorMatchingSnapshot();
});

test('should reject bad toExtensions user inputs', () => {
expect(() =>
normalizePluginOptions({
toExtensions: [null, undefined, 123, true] as any,
toExtensions: ([null, undefined, 123, true] as unknown) as string[],
}),
).toThrowErrorMatchingSnapshot();
});

test('should reject bad createRedirects user inputs', () => {
expect(() =>
normalizePluginOptions({
createRedirects: ['bad', 'value'] as any,
createRedirects: ([
'bad',
'value',
] as unknown) as CreateRedirectsFnOption,
}),
).toThrowErrorMatchingSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function validateCollectedRedirects(
.map((redirect) => {
try {
validateRedirect(redirect);
return undefined;
} catch (e) {
return e.message;
}
Expand Down Expand Up @@ -132,12 +133,11 @@ function createRedirectsOptionRedirects(
function optionToRedirects(option: RedirectOption): RedirectMetadata[] {
if (typeof option.from === 'string') {
return [{from: option.from, to: option.to}];
} else {
return option.from.map((from) => ({
from,
to: option.to,
}));
}
return option.from.map((from) => ({
from,
to: option.to,
}));
}

return flatten(redirectsOption.map(optionToRedirects));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {RedirectMetadata} from './types';
export const PathnameValidator = Yup.string().test({
name: 'isValidPathname',
message:
// Yup requires this format.
// eslint-disable-next-line no-template-curly-in-string
'${path} is not a valid pathname. Pathname should start with / and not contain any domain or query string',
test: isValidPathname,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('blogFeed', () => {
routeBasePath: 'blog',
include: ['*.md', '*.mdx'],
feedOptions: {
type: feedType as any,
type: feedType,
copyright: 'Copyright',
},
} as PluginOptions,
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('blogFeed', () => {
routeBasePath: 'blog',
include: ['*r*.md', '*.mdx'], // skip no-date.md - it won't play nice with snapshots
feedOptions: {
type: feedType as any,
type: feedType,
copyright: 'Copyright',
},
} as PluginOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export async function generateBlogFeed(
blogPosts.forEach((post) => {
const {
id,
metadata: {title, permalink, date, description},
metadata: {title: metadataTitle, permalink, date, description},
} = post;
feed.addItem({
title,
title: metadataTitle,
id,
link: normalizeUrl([siteUrl, permalink]),
date,
Expand Down
22 changes: 12 additions & 10 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ export default function pluginContentBlog(
label: tag,
permalink,
};
} else {
return tag;
}
return tag;
});
});

Expand Down Expand Up @@ -244,7 +243,7 @@ export default function pluginContentBlog(
`~blog/${path.relative(dataDir, source)}`;
const {addRoute, createData} = actions;
const {
blogPosts,
blogPosts: loadedBlogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
Expand All @@ -254,7 +253,7 @@ export default function pluginContentBlog(

// Create routes for blog entries.
await Promise.all(
blogPosts.map(async (blogPost) => {
loadedBlogPosts.map(async (blogPost) => {
const {id, metadata} = blogPost;
await createData(
// Note that this created data path must be in sync with
Expand Down Expand Up @@ -292,12 +291,11 @@ export default function pluginContentBlog(
exact: true,
modules: {
items: items.map((postID) => {
const metadata = blogItemsToMetadata[postID];
// To tell routes.js this is an import and not a nested object to recurse.
return {
content: {
__import: true,
path: metadata.source,
path: blogItemsToMetadata[postID].source,
query: {
truncated: true,
},
Expand Down Expand Up @@ -481,22 +479,26 @@ export default function pluginContentBlog(
};
const headTags: HtmlTags = [];

feedTypes.map((feedType) => {
feedTypes.forEach((feedType) => {
const feedConfig = feedsConfig[feedType] || {};

if (!feedsConfig) {
return;
}

const {type, path, title} = feedConfig;
const {type, path: feedConfigPath, title: feedConfigTitle} = feedConfig;

headTags.push({
tagName: 'link',
attributes: {
rel: 'alternate',
type,
href: normalizeUrl([baseUrl, options.routeBasePath, path]),
title,
href: normalizeUrl([
baseUrl,
options.routeBasePath,
feedConfigPath,
]),
title: feedConfigTitle,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/docusaurus-plugin-content-docs/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
},

Expand Down
9 changes: 4 additions & 5 deletions packages/docusaurus-plugin-content-docs/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-docs/src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function createOrder(allSidebars: Sidebar = {}): Order {
case 'doc':
ids.push(item.id);
break;
default:
}
});
};
Expand Down
Loading

0 comments on commit 6e43c9b

Please sign in to comment.