Просмотр исходного кода

fix lint errors not auto-fixable by biome

Futa Arai 10 месяцев назад
Родитель
Сommit
35e4d07da8

+ 2 - 2
packages/remark-lsx/src/client/components/Lsx.tsx

@@ -58,7 +58,7 @@ const LsxSubstance = React.memo(
     const hasError = error != null;
     const errorMessage = error?.message;
 
-    const Error = useCallback((): JSX.Element => {
+    const ErrorMessage = useCallback((): JSX.Element => {
       if (!hasError) {
         return <></>;
       }
@@ -150,7 +150,7 @@ const LsxSubstance = React.memo(
 
     return (
       <div className={`lsx ${styles.lsx}`}>
-        <Error />
+        <ErrorMessage />
         <Loading />
         {contents}
         <LoadMore />

+ 3 - 3
packages/remark-lsx/src/client/components/lsx-context.ts

@@ -12,9 +12,9 @@ export class LsxContext {
     this.pagePath = pagePath;
 
     // remove undefined keys
-    Object.keys(options).forEach(
-      (key) => options[key] === undefined && delete options[key],
-    );
+    for (const key in options) {
+      options[key] === undefined && delete options[key];
+    }
 
     this.options = options;
   }

+ 7 - 10
packages/remark-lsx/src/client/services/renderer/lsx.ts

@@ -1,5 +1,3 @@
-import assert from 'assert';
-
 import {
   addTrailingSlash,
   hasHeadingSlash,
@@ -45,7 +43,8 @@ export const remarkPlugin: Plugin = () => (tree) => {
         return;
       }
 
-      const data = node.data ?? (node.data = {});
+      const data = node.data ?? {};
+      node.data = data;
       const attributes = (node.attributes as DirectiveAttributes) || {};
 
       // set 'prefix' attribute if the first attribute is only value
@@ -94,11 +93,9 @@ const pathResolver = (href: string, basePath: string): string => {
 };
 
 export const rehypePlugin: Plugin<[LsxRehypePluginParams]> = (options = {}) => {
-  assert.notStrictEqual(
-    options.pagePath,
-    null,
-    "lsx rehype plugin requires 'pagePath' option",
-  );
+  if (options.pagePath == null) {
+    throw new Error("lsx rehype plugin requires 'pagePath' option");
+  }
 
   return (tree) => {
     if (options.pagePath == null) {
@@ -108,7 +105,7 @@ export const rehypePlugin: Plugin<[LsxRehypePluginParams]> = (options = {}) => {
     const basePagePath = options.pagePath;
     const elements = selectAll('lsx', tree as HastNode);
 
-    elements.forEach((lsxElem) => {
+    for (const lsxElem of elements) {
       if (lsxElem.properties == null) {
         return;
       }
@@ -133,7 +130,7 @@ export const rehypePlugin: Plugin<[LsxRehypePluginParams]> = (options = {}) => {
 
       // resolve relative path
       lsxElem.properties.prefix = decodeURI(pathResolver(prefix, basePagePath));
-    });
+    }
   };
 };
 

+ 1 - 1
packages/remark-lsx/src/client/utils/page-node.spec.ts

@@ -8,7 +8,7 @@ import { generatePageNodeTree } from './page-node';
 
 function omitPageData(pageNode: PageNode): Omit<PageNode, 'page'> {
   const obj = Object.assign({}, pageNode);
-  delete obj.page;
+  obj.page = undefined;
 
   // omit data in children
   obj.children = obj.children.map((child) => omitPageData(child));

+ 18 - 8
packages/remark-lsx/src/client/utils/page-node.ts

@@ -1,5 +1,3 @@
-import * as url from 'url';
-
 import type { IPageHasId } from '@growi/core';
 import type { ParseRangeResult } from '@growi/core/dist/remark-plugins';
 import { removeTrailingSlash } from '@growi/core/dist/utils/path-utils';
@@ -7,8 +5,20 @@ import { removeTrailingSlash } from '@growi/core/dist/utils/path-utils';
 import type { PageNode } from '../../interfaces/page-node';
 import { getDepthOfPath } from '../../utils/depth-utils';
 
+// url.resolve is legacy, use WHATWG URL API instead
+// c.f) https://nodejs.org/api/url.html#url_url_resolve_from_to
+function resolve(from, to) {
+  const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
+  if (resolvedUrl.protocol === 'resolve:') {
+    // `from` is a relative URL.
+    const { pathname, search, hash } = resolvedUrl;
+    return pathname + search + hash;
+  }
+  return resolvedUrl.toString();
+}
+
 function getParentPath(path: string) {
-  return removeTrailingSlash(decodeURIComponent(url.resolve(path, './')));
+  return removeTrailingSlash(decodeURIComponent(resolve(path, './')));
 }
 
 /**
@@ -76,7 +86,7 @@ export function generatePageNodeTree(
 ): PageNode[] {
   const pathToNodeMap: Record<string, PageNode> = {};
 
-  pages.forEach((page) => {
+  for (const page of pages) {
     const node = generatePageNode(
       pathToNodeMap,
       rootPagePath,
@@ -86,22 +96,22 @@ export function generatePageNodeTree(
 
     // exclude rootPagePath itself
     if (node == null) {
-      return;
+      continue;
     }
 
     // set the Page substance
     node.page = page;
-  });
+  }
 
   // return root objects
   const rootNodes: PageNode[] = [];
-  Object.keys(pathToNodeMap).forEach((pagePath) => {
+  for (const pagePath in pathToNodeMap) {
     const parentPath = getParentPath(pagePath);
 
     // pick up what parent doesn't exist
     if (parentPath === '/' || !(parentPath in pathToNodeMap)) {
       rootNodes.push(pathToNodeMap[pagePath]);
     }
-  });
+  }
   return rootNodes;
 }

+ 3 - 3
packages/remark-lsx/src/server/index.ts

@@ -22,9 +22,9 @@ const lsxValidator = [
       try {
         const jsonData: LsxApiOptions = JSON.parse(options);
 
-        Object.keys(jsonData).forEach((key) => {
+        for (const key in jsonData) {
           jsonData[key] = filterXSS.process(jsonData[key]);
-        });
+        }
 
         return jsonData;
       } catch (err) {
@@ -48,7 +48,7 @@ const paramValidator = (req: Request, res: Response, next: NextFunction) => {
   res.status(400).json({ errors: errs.map((err) => err.message) });
 };
 
-// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
+// biome-ignore lint/suspicious/noExplicitAny: ignore
 const middleware = (crowi: any, app: any): void => {
   const loginRequired = crowi.require('../middlewares/login-required')(
     crowi,

+ 1 - 1
packages/remark-lsx/src/server/routes/list-pages/generate-base-query.ts

@@ -18,7 +18,7 @@ export const generateBaseQuery = async (
   user: IUser,
 ): Promise<PageQueryBuilder> => {
   const Page = model<IPageHasId>('Page');
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  // biome-ignore lint/suspicious/noExplicitAny: ignore
   const PageAny = Page as any;
 
   const baseQuery = Page.find();

+ 2 - 2
packages/remark-lsx/src/server/routes/list-pages/index.ts

@@ -34,7 +34,7 @@ function addFilterCondition(
 
   const pagePathForRegexp = escapeStringRegexp(addTrailingSlash(pagePath));
 
-  let filterPath;
+  let filterPath: RegExp;
   try {
     if (optionsFilter.charAt(0) === '^') {
       // move '^' to the first of path
@@ -88,7 +88,7 @@ export const listPages = async (
   const builder = await generateBaseQuery(params.pagePath, user);
 
   // count viewers of `/`
-  let toppageViewersCount;
+  let toppageViewersCount: number;
   try {
     toppageViewersCount = await getToppageViewersCount();
   } catch (error) {