Skip to content

Commit

Permalink
fix(utils): [preleaveIfStatement] consequent only logic
Browse files Browse the repository at this point in the history
- if `node` only has a consequent, add to `trash` only if the consequent is empty

Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Mar 2, 2024
1 parent 5addfc8 commit 1fb494f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
12 changes: 2 additions & 10 deletions src/utils/__tests__/preleave-if-statement.functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,15 @@ describe('functional:utils/preleaveIfStatement', () => {
beforeAll(() => {
node = {
alternate: null,
consequent: {
expression: {
arguments: [{ name: 'result', type: 'Identifier' }],
callee: { name: 'ok', type: 'Identifier' },
optional: false,
type: 'CallExpression'
},
type: 'ExpressionStatement'
},
consequent: { body: [], type: 'BlockStatement' },
test,
type: 'IfStatement'
}

testSubject(node, context.trash)
})

it('should add node to trash', () => {
it('should add node to trash if isEmpty(node.consequent)', () => {
expect(context.trash.has(node)).to.be.true
})
})
Expand Down
8 changes: 5 additions & 3 deletions src/utils/is-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module estree-util-unassert/utils/isEmpty
*/

import type { EmptyArray } from '@flex-development/tutils'
import type { EmptyArray, Nilable } from '@flex-development/tutils'
import type { EmptyBlockStatement, EmptyStatement, Node } from 'estree'
import { is } from 'unist-util-is'

Expand All @@ -20,11 +20,13 @@ declare module 'estree' {
* @see {@linkcode EmptyStatement}
* @see {@linkcode Node}
*
* @param {Node} node - Node to check
* @param {Nilable<Node>} node - Node to check
* @return {node is EmptyBlockStatement | EmptyStatement} `true` if `node` is an
* empty block statement or empty statement, `false` otherwise
*/
const isEmpty = (node: Node): node is EmptyBlockStatement | EmptyStatement => {
const isEmpty = (
node: Nilable<Node>
): node is EmptyBlockStatement | EmptyStatement => {
return (
is(node, 'EmptyStatement') ||
is(node, 'BlockStatement') && !node.body.length
Expand Down
19 changes: 6 additions & 13 deletions src/utils/preleave-if-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
*/

import type { IfStatement, Node } from 'estree'
import { is } from 'unist-util-is'
import isEmpty from './is-empty'

/**
* Prepare to leave an {@linkcode IfStatement}.
*
* If `node` only has a consequent, `node` will be added to the `trash` to be
* removed on exit. `node` will also be added to the `trash` if its consequent
* is an empty statement (block or otherwise) and its alternate is an empty
* block statement. If the only empty child node in this case is the alternate,
* only the alternate will be removed.
* If `node` only has an empty consequent, `node` will be added to the `trash`
* to be removed on exit. `node` will also be added to the `trash` if both its
* consequent and alternate are types of empty statement. If the only empty
* child node is the alternate, only the alternate will be removed.
*
* @see {@linkcode IfStatement}
* @see {@linkcode Node}
Expand All @@ -24,13 +22,8 @@ import isEmpty from './is-empty'
* @return {void} Nothing
*/
const preleaveIfStatement = (node: IfStatement, trash: WeakSet<Node>): void => {
// trash node if it only has a consequent
!node.alternate && trash.add(node)

// 1. remove alternates that are empty block statements
// 2. add node to trash if consequent is empty
if (is(node.alternate, 'BlockStatement') && isEmpty(node.alternate)) {
delete node.alternate
if (!node.alternate || isEmpty(node.alternate)) {
node.alternate && delete node.alternate
isEmpty(node.consequent) && trash.add(node)
}

Expand Down

0 comments on commit 1fb494f

Please sign in to comment.