Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyAlford committed Jul 7, 2024
1 parent 5a6a7b6 commit 226d185
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
15 changes: 7 additions & 8 deletions source/components/JsxParser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global JSX */
import * as Acorn from 'acorn'
import * as AcornJSX from 'acorn-jsx'
import React, { Fragment, ComponentType, ExoticComponent } from 'react'
Expand All @@ -24,9 +23,9 @@ export type TProps = {
jsx?: string,
onError?: (error: Error) => void,
showWarnings?: boolean,
renderError?: (props: { error: string }) => JSX.Element | null,
renderError?: (props: { error: string }) => React.ReactNode | null,
renderInWrapper?: boolean,
renderUnrecognized?: (tagName: string) => JSX.Element | null,
renderUnrecognized?: (tagName: string) => React.ReactNode | null,
}
type Scope = Record<string, any>

Expand Down Expand Up @@ -54,7 +53,7 @@ export default class JsxParser extends React.Component<TProps> {

private ParsedChildren: ParsedTree = null

#parseJSX = (jsx: string): JSX.Element | JSX.Element[] | null => {
#parseJSX = (jsx: string): React.ReactNode | React.ReactNode[] | null => {
const parser = Acorn.Parser.extend(AcornJSX.default({
autoCloseVoidElements: this.props.autoCloseVoidElements,
}))
Expand Down Expand Up @@ -225,7 +224,7 @@ export default class JsxParser extends React.Component<TProps> {
#parseElement = (
element: AcornJSX.JSXElement | AcornJSX.JSXFragment,
scope?: Scope,
): JSX.Element | JSX.Element[] | null => {
): React.ReactNode | React.ReactNode[] | null => {
const { allowUnknownElements, components, componentsOnly, onError } = this.props
const { children: childNodes = [] } = element
const openingTag = element.type === 'JSXElement'
Expand All @@ -242,7 +241,7 @@ export default class JsxParser extends React.Component<TProps> {
.map(tag => tag.trim().toLowerCase()).filter(Boolean)

if (/^(html|head|body)$/i.test(name)) {
return childNodes.map(c => this.#parseElement(c, scope)) as JSX.Element[]
return childNodes.map(c => this.#parseElement(c, scope)) as React.ReactNode[]
}
const tagName = name.trim().toLowerCase()
if (blacklistedTags.indexOf(tagName) !== -1) {
Expand Down Expand Up @@ -332,7 +331,7 @@ export default class JsxParser extends React.Component<TProps> {
return React.createElement(component || lowerName, props, children)
}

render = (): React.ReactNode => {
render() {
const jsx = (this.props.jsx || '').trim().replace(/<!DOCTYPE([^>]*)>/g, '')
this.ParsedChildren = this.#parseJSX(jsx)
const className = [...new Set(['jsx-parser', ...String(this.props.className).split(' ')])]
Expand All @@ -342,7 +341,7 @@ export default class JsxParser extends React.Component<TProps> {
return (
this.props.renderInWrapper
? <div className={className}>{this.ParsedChildren}</div>
: <>{this.ParsedChildren}</>
: this.ParsedChildren
)
}
}
Expand Down
13 changes: 6 additions & 7 deletions source/helpers/parseStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ type Style = string | Partial<CSSStyleDeclaration>
*/
export const parseStyle = (style: Style): Partial<CSSStyleDeclaration> | undefined => {
switch (typeof style) {
case 'string':
return style.split(';').filter(r => r)
.reduce((map, rule) => {
case 'string':
return style.split(';').filter(r => r).reduce((map, rule) => {
const name = rule.slice(0, rule.indexOf(':')).trim()
const value = rule.slice(rule.indexOf(':') + 1).trim()

Expand All @@ -20,10 +19,10 @@ export const parseStyle = (style: Style): Partial<CSSStyleDeclaration> | undefin
[camelCase(name)]: value,
}
}, {})
case 'object':
return style
case 'object':
return style

default:
return undefined
default:
return undefined
}
}

0 comments on commit 226d185

Please sign in to comment.