Skip to content

Commit bcd07e4

Browse files
committed
feature: @putout/plugin-printer: add-missing-tuples-to-type-checker: type name
1 parent 8d790b2 commit bcd07e4

5 files changed

Lines changed: 47 additions & 21 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
2+
"rules": {
3+
"printer": "on"
4+
},
25
"match": {
36
"declare/index.js": {
47
"putout/apply-namespace-specifier": "off"
58
},
69
"*.md{js}": {
7-
"putout": "off"
10+
"putout": "off",
11+
"printer": "off"
812
}
9-
}
13+
},
14+
"plugins": ["printer"]
1015
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const beforeIf = createTypeChecker([
2+
['+: -> StringLiteral'],
3+
['- : ->', isBlockStatement],
4+
['-', isBlockStatement],
5+
]);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const beforeIf = createTypeChecker([
2+
'StringLiteral',
3+
['- : ->', isBlockStatement],
4+
['-', isBlockStatement],
5+
]);

packages/plugin-printer/lib/add-missing-tuples-to-type-checker/index.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@ const {
66
isArrayExpression,
77
isIdentifier,
88
isCallExpression,
9+
isStringLiteral,
910
arrayExpression,
1011
stringLiteral,
1112
} = types;
1213

1314
const {replaceWith} = operator;
14-
const instrument = Boolean(process.env.PUTOUT_INSTRUMENT);
15+
16+
const hasResult = (value) => /^[+-]/.test(value);
17+
const typeOptions = {
18+
instrument: Boolean(process.env.PUTOUT_INSTRUMENT),
19+
};
20+
21+
const isDsl = createTypeChecker([
22+
['-: -> !StringLiteral'],
23+
['+: node.value ->', hasResult],
24+
], typeOptions);
25+
26+
const isSimple = createTypeChecker([
27+
['+: -> Identifier'],
28+
['+: -> CallExpression'],
29+
['-: -> !StringLiteral'],
30+
['+: node.value -> !', hasResult],
31+
], typeOptions);
1532

1633
export const report = (path) => {
1734
return `Add missing tuple around: ${path}`;
@@ -25,6 +42,13 @@ export const fix = (path) => {
2542
return;
2643
}
2744

45+
const {value} = node;
46+
47+
if (isStringLiteral(path) && !/^[+-]/.test(value)) {
48+
replaceWith(path, arrayExpression([stringLiteral(`+: -> ${value}`)]));
49+
return;
50+
}
51+
2852
replaceWith(path, arrayExpression([node]));
2953
};
3054

@@ -50,24 +74,6 @@ export const traverse = ({push}) => ({
5074
},
5175
});
5276

53-
const hasResult = (value) => /^[+-]/.test(value);
54-
55-
const isDsl = createTypeChecker([
56-
['-: -> !StringLiteral'],
57-
['+: node.value ->', hasResult],
58-
], {
59-
instrument,
60-
});
61-
62-
const isSimple = createTypeChecker([
63-
'Identifier',
64-
'CallExpression',
65-
['-: -> !StringLiteral'],
66-
['+: node.value ->!', hasResult],
67-
], {
68-
instrument,
69-
});
70-
7177
function isConsistent(elements) {
7278
const arraysCount = elements.filter(isArrayExpression).length;
7379

packages/plugin-printer/lib/add-missing-tuples-to-type-checker/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ test('printer: add-missing-tuples-to-type-checker: transform: call', (t) => {
2727
t.end();
2828
});
2929

30+
test('printer: add-missing-tuples-to-type-checker: transform: type', (t) => {
31+
t.transform('type');
32+
t.end();
33+
});
34+
3035
test('printer: add-missing-tuples-to-type-checker: no report: not-type-checker', (t) => {
3136
t.noReport('not-type-checker');
3237
t.end();

0 commit comments

Comments
 (0)