Skip to content

Commit

Permalink
refactor: remove unnecessary as statement
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 21, 2023
1 parent f54dedc commit 3739401
Showing 1 changed file with 43 additions and 50 deletions.
93 changes: 43 additions & 50 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,60 +377,53 @@ runAsWorker(

function handleChildren(node: Nodes) {
return 'children' in node
? (node.children as Nodes[]).reduce<JSXElement['children']>(
(acc, child) => {
processed.add(child)

if (
child.data &&
'estree' in child.data &&
child.data.estree
) {
const { estree } = child.data

assert(estree.body.length <= 1)

const statement = estree.body[0]

isExpressionStatement(statement)

const expression = statement?.expression

if (child.type === 'mdxTextExpression') {
const {
start: { offset: start },
end: { offset: end },
} = node.position

const expressionContainer: JSXExpressionContainer = {
...normalizeNode(start, end),
type: 'JSXExpressionContainer',
expression: expression || {
...normalizeNode(start + 1, end - 1),
type: 'JSXEmptyExpression',
},
}
acc.push(expressionContainer)
} else if (expression) {
acc.push(expression as JSXElement['children'][number])
}
? node.children.reduce<JSXElement['children']>((acc, child) => {
processed.add(child)

comments.push(...estree.comments)
} else {
const expression = handleNode(child) as Arrayable<
JSXElement['children']
>
if (Array.isArray(expression)) {
acc.push(...expression)
} else if (expression) {
acc.push(expression)
if (child.data && 'estree' in child.data && child.data.estree) {
const { estree } = child.data

assert(estree.body.length <= 1)

const statement = estree.body[0]

isExpressionStatement(statement)

const expression = statement?.expression

if (child.type === 'mdxTextExpression') {
const {
start: { offset: start },
end: { offset: end },
} = node.position

const expressionContainer: JSXExpressionContainer = {
...normalizeNode(start, end),
type: 'JSXExpressionContainer',
expression: expression || {
...normalizeNode(start + 1, end - 1),
type: 'JSXEmptyExpression',
},
}
acc.push(expressionContainer)
} else if (expression) {
acc.push(expression as JSXElement['children'][number])
}

return acc
},
[],
)
comments.push(...estree.comments)
} else {
const expression = handleNode(child) as Arrayable<
JSXElement['children']
>
if (Array.isArray(expression)) {
acc.push(...expression)
} else if (expression) {
acc.push(expression)
}
}

return acc
}, [])
: []
}

Expand Down

0 comments on commit 3739401

Please sign in to comment.