Skip to content

Commit 1fb494f

Browse files
committed
fix(utils): [preleaveIfStatement] consequent only logic
- if `node` only has a consequent, add to `trash` only if the consequent is empty Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 5addfc8 commit 1fb494f

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

src/utils/__tests__/preleave-if-statement.functional.spec.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,15 @@ describe('functional:utils/preleaveIfStatement', () => {
2929
beforeAll(() => {
3030
node = {
3131
alternate: null,
32-
consequent: {
33-
expression: {
34-
arguments: [{ name: 'result', type: 'Identifier' }],
35-
callee: { name: 'ok', type: 'Identifier' },
36-
optional: false,
37-
type: 'CallExpression'
38-
},
39-
type: 'ExpressionStatement'
40-
},
32+
consequent: { body: [], type: 'BlockStatement' },
4133
test,
4234
type: 'IfStatement'
4335
}
4436

4537
testSubject(node, context.trash)
4638
})
4739

48-
it('should add node to trash', () => {
40+
it('should add node to trash if isEmpty(node.consequent)', () => {
4941
expect(context.trash.has(node)).to.be.true
5042
})
5143
})

src/utils/is-empty.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module estree-util-unassert/utils/isEmpty
44
*/
55

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

@@ -20,11 +20,13 @@ declare module 'estree' {
2020
* @see {@linkcode EmptyStatement}
2121
* @see {@linkcode Node}
2222
*
23-
* @param {Node} node - Node to check
23+
* @param {Nilable<Node>} node - Node to check
2424
* @return {node is EmptyBlockStatement | EmptyStatement} `true` if `node` is an
2525
* empty block statement or empty statement, `false` otherwise
2626
*/
27-
const isEmpty = (node: Node): node is EmptyBlockStatement | EmptyStatement => {
27+
const isEmpty = (
28+
node: Nilable<Node>
29+
): node is EmptyBlockStatement | EmptyStatement => {
2830
return (
2931
is(node, 'EmptyStatement') ||
3032
is(node, 'BlockStatement') && !node.body.length

src/utils/preleave-if-statement.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
*/
55

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

109
/**
1110
* Prepare to leave an {@linkcode IfStatement}.
1211
*
13-
* If `node` only has a consequent, `node` will be added to the `trash` to be
14-
* removed on exit. `node` will also be added to the `trash` if its consequent
15-
* is an empty statement (block or otherwise) and its alternate is an empty
16-
* block statement. If the only empty child node in this case is the alternate,
17-
* only the alternate will be removed.
12+
* If `node` only has an empty consequent, `node` will be added to the `trash`
13+
* to be removed on exit. `node` will also be added to the `trash` if both its
14+
* consequent and alternate are types of empty statement. If the only empty
15+
* child node is the alternate, only the alternate will be removed.
1816
*
1917
* @see {@linkcode IfStatement}
2018
* @see {@linkcode Node}
@@ -24,13 +22,8 @@ import isEmpty from './is-empty'
2422
* @return {void} Nothing
2523
*/
2624
const preleaveIfStatement = (node: IfStatement, trash: WeakSet<Node>): void => {
27-
// trash node if it only has a consequent
28-
!node.alternate && trash.add(node)
29-
30-
// 1. remove alternates that are empty block statements
31-
// 2. add node to trash if consequent is empty
32-
if (is(node.alternate, 'BlockStatement') && isEmpty(node.alternate)) {
33-
delete node.alternate
25+
if (!node.alternate || isEmpty(node.alternate)) {
26+
node.alternate && delete node.alternate
3427
isEmpty(node.consequent) && trash.add(node)
3528
}
3629

0 commit comments

Comments
 (0)