Skip to content

Commit 9202702

Browse files
committed
feature: @putout/plugin-labels: remove-unused: exclude when previous ReturnStatement without args
1 parent 8f8efd1 commit 9202702

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
return
2+
{
3+
a: 5
4+
}

packages/plugin-labels/lib/remove-unused/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
const {operator} = require('putout');
3+
const {operator, types} = require('putout');
4+
const {isReturnStatement} = types;
45
const {traverse, replaceWith} = operator;
56

67
module.exports.report = ({name}) => `Label '${name}' is defined but never used`;
@@ -14,6 +15,9 @@ module.exports.traverse = ({push}) => ({
1415
const {label} = path.node;
1516
const {name} = label;
1617

18+
if (isPrevReturnWithoutArg(path))
19+
return;
20+
1721
if (!usedLabel({path, name}))
1822
push({
1923
path,
@@ -45,3 +49,12 @@ function usedLabel({path, name}) {
4549

4650
return used;
4751
}
52+
53+
function isPrevReturnWithoutArg({parentPath}) {
54+
const retPath = parentPath.getPrevSibling();
55+
56+
if (!isReturnStatement(retPath))
57+
return false;
58+
59+
return !retPath.node.argument;
60+
}

packages/plugin-labels/lib/remove-unused/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ test('putout: remove-unused: transform: wrong', (t) => {
2323
t.transform('wrong');
2424
t.end();
2525
});
26+
27+
test('putout: remove-unused: no report: return', (t) => {
28+
t.noReport('return');
29+
t.end();
30+
});

0 commit comments

Comments
 (0)