Skip to content

Commit

Permalink
feature: @putout/plugin-typescript: apply-type-guards: add (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 25, 2023
1 parent d8892e9 commit 864436d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/plugin-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ npm i putout @putout/plugin-typescript -D
"rules": {
"typescript/apply-as-type-assertion": "on",
"typescript/apply-utility-types": "on",
"typescript/apply-type-guards": "on",
"typescript/convert-generic-to-shorthand": "on",
"typescript/remove-duplicates-from-union": "on",
"typescript/remove-duplicates-interface-keys": "on",
Expand Down Expand Up @@ -69,6 +70,27 @@ type SuperType1 = {
type SuperType1 = Partial<Type>;
```

### apply-type-guards

> It just so happens that TypeScript has something called a `type guard`.
> A `type guard` is some expression that performs a runtime check that guarantees the type in some scope.
>
> (c) [typescript.org](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)
Check out in 🐊[**Putout Editor](https://putout.cloudcmd.io/#/gist/5ac4459242197c4820b331f19d3681eb/4b66175c486ee8e865aee9645bf4e5fffc664e01).

#### ❌ Example of incorrect code

```ts
const isNumber = (a) => typeof a === 'number';
```

#### ✅ Example of correct code

```ts
const isNumber = (a): a is number => typeof a === 'number';
```

### convert-generic-to-shorthand

> There is no difference at all. `Type[]` is the shorthand syntax for an `array` of `Type`. `Array<Type>` is the generic syntax. They are completely equivalent.
Expand Down
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';
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'
28 changes: 28 additions & 0 deletions packages/plugin-typescript/lib/apply-type-guards/index.js
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 packages/plugin-typescript/lib/apply-type-guards/index.spec.js
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();
});
1 change: 1 addition & 0 deletions packages/plugin-typescript/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const getRule = (a) => ({

module.exports.rules = {
...getRule('apply-as-type-assertion'),
...getRule('apply-type-guards'),
...getRule('apply-utility-types'),
...getRule('convert-generic-to-shorthand'),
...getRule('remove-duplicates-from-union'),
Expand Down
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 packages/plugin-typescript/test/fixture/apply-type-guards.ts
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'
5 changes: 5 additions & 0 deletions packages/plugin-typescript/test/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ test('putout: plugin: typescript: transform: apply-as-type-assertion', (t) => {
t.end();
});

test('putout: plugin: typescript: transform: apply-type-guards', (t) => {
t.transform('apply-type-guards');
t.end();
});

test('putout: plugin: typescript: transform: apply-utility-types', (t) => {
t.transform('apply-utility-types');
t.end();
Expand Down

0 comments on commit 864436d

Please sign in to comment.