refactor: fix all eslint warnings (#6502)

This commit is contained in:
Joshua Chen 2022-01-29 13:21:40 +08:00 committed by GitHub
parent c1e3801ee7
commit 4f2b09fe32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 10 deletions

View file

@ -14,6 +14,7 @@ import remarkStringify from 'remark-stringify';
import htmlTags from 'html-tags'; import htmlTags from 'html-tags';
import toText from 'hast-util-to-string'; import toText from 'hast-util-to-string';
import type {Code, InlineCode} from 'mdast'; import type {Code, InlineCode} from 'mdast';
import type {Element, Text} from 'hast';
const tags = htmlTags.reduce((acc: {[key: string]: boolean}, tag) => { const tags = htmlTags.reduce((acc: {[key: string]: boolean}, tag) => {
acc[tag] = true; acc[tag] = true;
@ -34,12 +35,14 @@ export default function sanitizeMD(code: string): string {
.stringify(markdownTree); .stringify(markdownTree);
const htmlTree = unified().use(parse).parse(markdownString); const htmlTree = unified().use(parse).parse(markdownString);
visit(htmlTree, 'element', (node: any) => {
visit(htmlTree, 'element', (node: Element) => {
if (!tags[node.tagName as string]) { if (!tags[node.tagName as string]) {
node.type = 'text'; (node as Element | Text).type = 'text';
node.value = node.tagName + toText(node); (node as Element & Partial<Omit<Text, 'type'>>).value =
delete node.children; node.tagName + toText(node);
delete node.tagName; delete (node as Partial<Element>).children;
delete (node as Partial<Element>).tagName;
} }
}); });
return toJsx(htmlTree) return toJsx(htmlTree)

View file

@ -12,6 +12,9 @@ import jscodeshift, {
type Collection, type Collection,
type TemplateElement, type TemplateElement,
VariableDeclarator, VariableDeclarator,
type CallExpression,
type MemberExpression,
type Identifier,
} from 'jscodeshift'; } from 'jscodeshift';
const empty = () => const empty = () =>
@ -29,9 +32,12 @@ const property = (key: string, value: ArrowFunctionExpression) =>
jscodeshift.objectProperty(jscodeshift.identifier(key), value); jscodeshift.objectProperty(jscodeshift.identifier(key), value);
const processCallExpression = (node: ASTPath<VariableDeclarator>) => { const processCallExpression = (node: ASTPath<VariableDeclarator>) => {
const args = (node?.value?.init as any)?.arguments[0]; const args = (node?.value?.init as CallExpression)?.arguments[0];
if (args.type === 'Literal') { if (args.type === 'Literal') {
if (args.value.includes('../../core/CompLibrary')) { if (
typeof args.value === 'string' &&
args.value.includes('../../core/CompLibrary')
) {
const newDeclarator = jscodeshift.variableDeclarator( const newDeclarator = jscodeshift.variableDeclarator(
node.value.id, node.value.id,
jscodeshift.objectExpression([ jscodeshift.objectExpression([
@ -60,7 +66,11 @@ const processCallExpression = (node: ASTPath<VariableDeclarator>) => {
}; };
const processMemberExpression = (node: ASTPath<VariableDeclarator>) => { const processMemberExpression = (node: ASTPath<VariableDeclarator>) => {
const args = (node?.value?.init as any)?.object?.arguments[0]; const object = (node?.value?.init as MemberExpression)?.object;
if (!(object.type === 'CallExpression')) {
return;
}
const args = object.arguments[0];
if (args.type === 'Literal') { if (args.type === 'Literal') {
if (args.value === '../../core/CompLibrary.js') { if (args.value === '../../core/CompLibrary.js') {
const newDeclarator = jscodeshift.variableDeclarator( const newDeclarator = jscodeshift.variableDeclarator(
@ -72,7 +82,7 @@ const processMemberExpression = (node: ASTPath<VariableDeclarator>) => {
]), ]),
); );
jscodeshift(node).replaceWith(newDeclarator); jscodeshift(node).replaceWith(newDeclarator);
} else if (args.value.match(/server/)) { } else if (typeof args.value === 'string' && args.value.match(/server/)) {
const newDeclarator = jscodeshift.variableDeclarator( const newDeclarator = jscodeshift.variableDeclarator(
node.value.id, node.value.id,
empty(), empty(),
@ -146,7 +156,7 @@ export default function transformer(file: string): string {
[ [
jscodeshift.jsxElement( jscodeshift.jsxElement(
jscodeshift.jsxOpeningElement( jscodeshift.jsxOpeningElement(
jscodeshift.jsxIdentifier((p.value.right as any).name), jscodeshift.jsxIdentifier((p.value.right as Identifier).name),
[ [
jscodeshift.jsxSpreadAttribute( jscodeshift.jsxSpreadAttribute(
jscodeshift.identifier('props'), jscodeshift.identifier('props'),

View file

@ -46,6 +46,7 @@ function Details({summary, children, ...props}: DetailsProps): JSX.Element {
const [open, setOpen] = useState(props.open); const [open, setOpen] = useState(props.open);
return ( return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions
<details <details
{...props} {...props}
ref={detailsRef} ref={detailsRef}