Skip to content

Commit

Permalink
chore: fix ESLint warnings, restrict export all syntax (#6605)
Browse files Browse the repository at this point in the history
* chore: fix ESLint warnings, forbid export all syntax

* fix...

* reorder
  • Loading branch information
Josh-Cena authored Feb 4, 2022
1 parent 3fd99ad commit 45f6f8b
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 148 deletions.
29 changes: 26 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ module.exports = {
'no-param-reassign': [WARNING, {props: false}],
'no-prototype-builtins': WARNING,
'no-restricted-exports': OFF,
'no-useless-escape': WARNING,
'no-template-curly-in-string': WARNING,
'no-restricted-imports': [
ERROR,
{
Expand All @@ -97,8 +95,33 @@ module.exports = {
],
},
],
'no-restricted-syntax': WARNING,
'no-restricted-syntax': [
WARNING,
// Copied from airbnb, removed for...of statement, added export all
{
selector: 'ForInStatement',
message:
'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.',
},
{
selector: 'LabeledStatement',
message:
'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
},
{
selector: 'WithStatement',
message:
'`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
},
{
selector: 'ExportAllDeclaration',
message:
"Export all does't work well if imported in ESM due to how they are transpiled, and they can also lead to unexpected exposure of internal methods.",
},
],
'no-template-curly-in-string': WARNING,
'no-unused-expressions': [WARNING, {allowTaggedTemplates: true}],
'no-useless-escape': WARNING,
'prefer-destructuring': WARNING,
'prefer-named-capture-group': WARNING,
'prefer-template': WARNING,
Expand Down
1 change: 0 additions & 1 deletion admin/scripts/generateExamples.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ const templates = (
await fs.readdir('./packages/create-docusaurus/templates')
).filter((name) => !excludes.includes(name));
console.log(`Will generate examples for templates: ${templates.join(',')}`);
// eslint-disable-next-line no-restricted-syntax
for (const template of templates) {
await generateTemplateExample(template);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ declare module '@docusaurus/Translate' {
}

declare module '@docusaurus/router' {
// eslint-disable-next-line import/no-extraneous-dependencies
// eslint-disable-next-line import/no-extraneous-dependencies, no-restricted-syntax
export * from 'react-router-dom';
}
declare module '@docusaurus/history' {
// eslint-disable-next-line import/no-extraneous-dependencies
// eslint-disable-next-line import/no-extraneous-dependencies, no-restricted-syntax
export * from 'history';
}

Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export interface BlogContent {

export interface BlogTags {
// TODO, the key is the tag slug/permalink
// This is due to legacy frontmatter: tags: [{label: "xyz", permalink: "/1"}, {label: "xyz", permalink: "/2"}
// This is due to legacy frontmatter: tags:
// [{label: "xyz", permalink: "/1"}, {label: "xyz", permalink: "/2"}]
// Soon we should forbid declaring permalink through frontmatter
[tagKey: string]: BlogTag;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/index.js",
"exports": {
"./client": "./lib/client/index.js",
"./server": "./lib/server/index.js",
"./server": "./lib/server-export.js",
".": "./lib/index.js"
},
"types": "src/plugin-content-docs.d.ts",
Expand Down
108 changes: 0 additions & 108 deletions packages/docusaurus-plugin-content-docs/src/client/globalDataHooks.ts

This file was deleted.

98 changes: 97 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,100 @@
* LICENSE file in the root directory of this source tree.
*/

export * from './globalDataHooks';
import {useLocation} from '@docusaurus/router';
import useGlobalData, {usePluginData} from '@docusaurus/useGlobalData';

import {
getActivePlugin,
getLatestVersion,
getActiveVersion,
getActiveDocContext,
getDocVersionSuggestions,
} from './docsClientUtils';
import type {
GlobalPluginData,
GlobalVersion,
ActivePlugin,
ActiveDocContext,
DocVersionSuggestions,
GetActivePluginOptions,
} from '@docusaurus/plugin-content-docs/client';

// Important to use a constant object to avoid React useEffect executions etc.
// see https://github.com/facebook/docusaurus/issues/5089
const StableEmptyObject = {};

// Not using useAllPluginInstancesData() because in blog-only mode, docs hooks
// are still used by the theme. We need a fail-safe fallback when the docs
// plugin is not in use
export const useAllDocsData = (): Record<string, GlobalPluginData> =>
useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject;

export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData;

// TODO this feature should be provided by docusaurus core
export const useActivePlugin = (
options: GetActivePluginOptions = {},
): ActivePlugin | undefined => {
const data = useAllDocsData();
const {pathname} = useLocation();
return getActivePlugin(data, pathname, options);
};

export const useActivePluginAndVersion = (
options: GetActivePluginOptions = {},
):
| undefined
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} => {
const activePlugin = useActivePlugin(options);
const {pathname} = useLocation();
if (activePlugin) {
const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
return {
activePlugin,
activeVersion,
};
}
return undefined;
};

// versions are returned ordered (most recent first)
export const useVersions = (pluginId: string | undefined): GlobalVersion[] => {
const data = useDocsData(pluginId);
return data.versions;
};

export const useLatestVersion = (
pluginId: string | undefined,
): GlobalVersion => {
const data = useDocsData(pluginId);
return getLatestVersion(data);
};

// Note: return undefined on doc-unrelated pages,
// because there's no version currently considered as active
export const useActiveVersion = (
pluginId: string | undefined,
): GlobalVersion | undefined => {
const data = useDocsData(pluginId);
const {pathname} = useLocation();
return getActiveVersion(data, pathname);
};

export const useActiveDocContext = (
pluginId: string | undefined,
): ActiveDocContext => {
const data = useDocsData(pluginId);
const {pathname} = useLocation();
return getActiveDocContext(data, pathname);
};

// Useful to say "hey, you are not on the latest docs version, please switch"
export const useDocVersionSuggestions = (
pluginId: string | undefined,
): DocVersionSuggestions => {
const data = useDocsData(pluginId);
const {pathname} = useLocation();
return getDocVersionSuggestions(data, pathname);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/

// APIs available to Node.js
export * from '../constants';
export {
CURRENT_VERSION_NAME,
VERSIONED_DOCS_DIR,
VERSIONED_SIDEBARS_DIR,
VERSIONS_JSON_FILE,
} from './constants';

export {
filterVersions,
Expand All @@ -16,4 +21,4 @@ export {
getVersionsFilePath,
readVersionsFile,
readVersionNames,
} from '../versions';
} from './versions';
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ Available document ids are:
label: string;
}
| undefined {
// eslint-disable-next-line no-restricted-syntax
for (const item of sidebar) {
if (item.type === 'doc') {
return {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-classic/src/theme/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
// If you swizzled this, it is your responsibility to provide an implementation
// Tip: swizzle the SearchBar from the Algolia theme for inspiration:
// npm run swizzle @docusaurus/theme-search-algolia SearchBar

export {default} from '@docusaurus/Noop';
2 changes: 0 additions & 2 deletions packages/docusaurus-theme-common/src/utils/docsUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export function findSidebarCategory(
sidebar: PropSidebar,
predicate: (category: PropSidebarItemCategory) => boolean,
): PropSidebarItemCategory | undefined {
// eslint-disable-next-line no-restricted-syntax
for (const item of sidebar) {
if (item.type === 'category') {
if (predicate(item)) {
Expand All @@ -119,7 +118,6 @@ export function findFirstCategoryLink(
return item.href;
}

// eslint-disable-next-line no-restricted-syntax
for (const subItem of item.items) {
if (subItem.type === 'link') {
return subItem.href;
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-live-codeblock/src/deps.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare module '@philpl/buble' {
import type {TransformOptions as OriginalTransformOptions} from 'buble';
// eslint-disable-next-line import/no-extraneous-dependencies
// eslint-disable-next-line import/no-extraneous-dependencies, no-restricted-syntax
export * from 'buble';
export const features: string[];
export interface TransformOptions extends OriginalTransformOptions {
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-theme-translations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export async function readDefaultCodeTranslationMessages({

// Return the content of the first file that match
// fr_FR.json => fr.json => nothing
// eslint-disable-next-line no-restricted-syntax
for (const localeToTry of localesToTry) {
const filePath = path.resolve(dirPath, localeToTry, `${name}.json`);

Expand Down
3 changes: 0 additions & 3 deletions packages/docusaurus-theme-translations/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ async function updateCodeTranslations() {
const stats = {};
let messageCount = 0;
const {2: newLocale} = process.argv;
// Order is important. The log messages must be in the same order as execution
// eslint-disable-next-line no-restricted-syntax
for (const theme of Themes) {
const {baseFile, localesFiles} = await getCodeTranslationFiles(theme.name);
logger.info`Will update base file for name=${theme.name}\n`;
Expand All @@ -289,7 +287,6 @@ async function updateCodeTranslations() {
)} was already created!`;
}
} else {
// eslint-disable-next-line no-restricted-syntax
for (const localeFile of localesFiles) {
const localeName = path.basename(path.dirname(localeFile));
const pluginName = path.basename(localeFile, path.extname(localeFile));
Expand Down
Loading

0 comments on commit 45f6f8b

Please sign in to comment.