Skip to content

Commit 96d22a0

Browse files
committed
feature: @putout/plugin-typescript: apply-type-guards: function
1 parent 2fd49bb commit 96d22a0

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const isFn = (a): a is Function => typeof a === 'function';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const isFn = (a) => typeof a === 'function'

packages/plugin-typescript/lib/apply-type-guards/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
template,
55
} from 'putout';
66

7-
const {replaceWith} = operator;
87
const {identifier} = types;
8+
const {replaceWith} = operator;
99

1010
const create = template('(__a): __a is __c => typeof __a === "__b"', {
1111
placeholderPattern: /__/,
@@ -19,12 +19,21 @@ export const match = () => ({
1919

2020
export const replace = () => ({
2121
'(__a) => typeof __a === "__b"': ({__a, __b}, path) => {
22+
const __c = parseGuard(__b);
23+
2224
replaceWith(path, create({
2325
__a,
2426
__b,
25-
__c: identifier(__b.value),
27+
__c,
2628
}));
2729

2830
return path;
2931
},
3032
});
33+
34+
function parseGuard({value}) {
35+
if (value === 'function')
36+
return identifier('Function');
37+
38+
return identifier(value);
39+
}

packages/plugin-typescript/lib/apply-type-guards/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ test('plugin-apply-guards: no report: exists', (t) => {
2121
t.noReport('exists');
2222
t.end();
2323
});
24+
25+
test('plugin-apply-guards: transform: fn', (t) => {
26+
t.transform('fn');
27+
t.end();
28+
});

0 commit comments

Comments
 (0)