Skip to content

Commit 3d7d816

Browse files
committed
feature: @putout/plugin-math: remove-unchanged-zero-declarations: exclude reference parents: CallExpression, ObjectProperty
1 parent 1f3cc54 commit 3d7d816

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
for (let index = 0; index < n; index++) {
2+
const tokenDelta = 0;
3+
const templateDelta = 0;
4+
5+
fn(tokenDelta);
6+
fn({
7+
templateDelta,
8+
});
9+
10+
for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
11+
const currentTokenIndex = index + templateIndex - tokenDelta;
12+
const templateToken = templateTokens[templateIndex - templateDelta];
13+
const currentToken = tokens[currentTokenIndex];
14+
15+
if (!compareAll(currentToken, templateToken)) {
16+
isEqual = false;
17+
break;
18+
}
19+
20+
isEqual = true;
21+
start = index;
22+
end = currentTokenIndex + tokenDelta;
23+
}
24+
25+
if (isEqual)
26+
return [true, start, ++end];
27+
}

packages/plugin-math/lib/remove-unchanged-zero-declarations/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const {operator, types} = require('putout');
44
const {
5+
isObjectProperty,
56
isNumericLiteral,
67
isExportNamedDeclaration,
8+
isCallExpression,
79
} = types;
810

911
const {remove} = operator;
@@ -38,6 +40,14 @@ module.exports.traverse = ({push}) => ({
3840
referencePaths,
3941
} = binding;
4042

43+
for (const {parent} of referencePaths) {
44+
if (isCallExpression(parent))
45+
return;
46+
47+
if (isObjectProperty(parent))
48+
return;
49+
}
50+
4151
if (constantViolations.length)
4252
return;
4353

packages/plugin-math/lib/remove-unchanged-zero-declarations/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ test('math: remove-unchanged-zero-declarations: no report: exported', (t) => {
2828
t.noReport('exported');
2929
t.end();
3030
});
31+
32+
test('math: remove-unchanged-zero-declarations: no report: call', (t) => {
33+
t.noReport('call');
34+
t.end();
35+
});

0 commit comments

Comments
 (0)