Skip to content

Commit f8ec6ba

Browse files
committed
feature: @putout/plugin-printer: remove-useless-tuples-from-type-checker: comparison
1 parent f434a24 commit f8ec6ba

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const isInsideArrayTupleOfThree = createTypeChecker([
2+
'-: parentPath.parentPath -> !ArrayExpression',
3+
['+: parentPath.parentPath.node.elements.length', '=', 3],
4+
]);

packages/plugin-printer/lib/remove-useless-tuples-from-type-checker/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ export const traverse = ({push}) => ({
4040
},
4141
});
4242

43-
const isTwoElementTuple = (a) => {
43+
const isCountElementsTuple = (count) => (a) => {
4444
if (!isArrayExpression(a))
4545
return false;
4646

47-
return a.node.elements.length === 2;
47+
const {length} = a.node.elements;
48+
49+
return length === count;
4850
};
4951

5052
function isConsistent(elements) {
@@ -58,5 +60,8 @@ function isConsistent(elements) {
5860
if (elements.length === identifiersCount)
5961
return true;
6062

61-
return elements.filter(isTwoElementTuple).length;
63+
if (elements.filter(isCountElementsTuple(3)).length)
64+
return true;
65+
66+
return elements.filter(isCountElementsTuple(2)).length;
6267
}

packages/plugin-printer/lib/remove-useless-tuples-from-type-checker/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ test('printer: remove-useless-tuples-from-type-checker: transform: one-element-t
2121
t.transform('one-element-tuple');
2222
t.end();
2323
});
24+
25+
test('printer: remove-useless-tuples-from-type-checker: no report: comparison', (t) => {
26+
t.noReport('comparison');
27+
t.end();
28+
});

0 commit comments

Comments
 (0)