Skip to content

Commit 5e5371d

Browse files
committed
feature: @putout/plugin-remove-useless-variables: declaration: no loc support
1 parent 9070145 commit 5e5371d

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import a from 'a';
2+
3+
const noop = () => {};
4+
const b = 0;
5+
6+
export const fn = (a, b) => {
7+
if (c)
8+
return noop;
9+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import a from 'a';
2+
const b = 0;
3+
4+
export const fn = (a, b) => {
5+
if (c)
6+
return () => {};
7+
};

packages/plugin-remove-useless-variables/lib/declaration/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ function isTakesOneLine(binding) {
119119
if (!binding.path.node.init)
120120
return false;
121121

122-
const {start, end} = binding.path.node.init.loc;
122+
const {loc} = binding.path.node.init;
123+
124+
if (!loc)
125+
return false;
126+
127+
const {start, end} = loc;
123128

124129
return start.line === end.line;
125130
}

packages/plugin-remove-useless-variables/lib/declaration/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {createTest} from '@putout/test';
2+
import * as declare from '@putout/plugin-declare';
3+
import * as maybe from '@putout/plugin-maybe';
24
import * as plugin from './index.js';
35

46
const test = createTest(import.meta.url, {
@@ -103,3 +105,11 @@ test('remove useless variables: declaration: no transform: re-assign-member', (t
103105
t.noTransform('re-assign-member');
104106
t.end();
105107
});
108+
109+
test('remove useless variables: declaration: transform: no-loc', (t) => {
110+
t.transform('no-loc', {
111+
declare,
112+
maybe,
113+
});
114+
t.end();
115+
});

packages/plugin-remove-useless-variables/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
],
3737
"devDependencies": {
3838
"@putout/eslint-flat": "^3.0.0",
39+
"@putout/plugin-declare": "*",
40+
"@putout/plugin-maybe": "*",
3941
"@putout/plugin-remove-useless-array-from": "*",
4042
"@putout/plugin-reuse-duplicate-init": "*",
4143
"@putout/test": "^14.0.0",

0 commit comments

Comments
 (0)