-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: @putout/plugin-typescript: apply-type-guards: add (#165)
- Loading branch information
1 parent
d8892e9
commit 864436d
Showing
9 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/plugin-typescript/lib/apply-type-guards/fixture/apply-type-guards-fix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const isNumber = (a): a is number => typeof 'number'; | ||
const isString = (a): a is string => typeof 'string'; |
2 changes: 2 additions & 0 deletions
2
packages/plugin-typescript/lib/apply-type-guards/fixture/apply-type-guards.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const isNumber = (a): a is number => typeof a === 'number' | ||
const isString = (a) => typeof a === 'string' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const { | ||
types, | ||
operator, | ||
template, | ||
} = require('putout'); | ||
|
||
const {replaceWith} = operator; | ||
const {Identifier} = types; | ||
|
||
const create = template('(__a): __a is __c => typeof "__b"', { | ||
placeholderPattern: /__/, | ||
}); | ||
|
||
module.exports.report = () => `Use 'type guards'`; | ||
|
||
module.exports.replace = () => ({ | ||
'(__a) => typeof __a === "__b"': ({__a, __b}, path) => { | ||
replaceWith(path, create({ | ||
__a, | ||
__b, | ||
__c: Identifier(__b.value), | ||
})); | ||
|
||
return path; | ||
}, | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/plugin-typescript/lib/apply-type-guards/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const {createTest} = require('@putout/test'); | ||
const applyUtilityTypes = require('.'); | ||
|
||
const test = createTest(__dirname, { | ||
printer: 'putout', | ||
plugins: [ | ||
['apply-guards', applyUtilityTypes], | ||
], | ||
}); | ||
|
||
test('plugin-apply-guards: transform: report', (t) => { | ||
t.report('apply-type-guards', `Use 'type guards'`); | ||
t.end(); | ||
}); | ||
|
||
test('plugin-apply-guards: transform: apply-type-guards', (t) => { | ||
t.transform('apply-type-guards'); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/plugin-typescript/test/fixture/apply-type-guards-fix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const isNumber = (a): a is number => typeof 'number'; | ||
const isString = (a): a is string => typeof 'string'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const isNumber = (a): a is number => typeof a === 'number' | ||
const isString = (a) => typeof a === 'string' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters