Skip to content

Commit 8fa4544

Browse files
committed
feature: @putout/plugin-types: convert-typeof-to-is-type: declared
1 parent 79f59e0 commit 8fa4544

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {isArray} = Array;
2+
3+
export const c = () => {
4+
const b = isArray(a) ? [] : {};
5+
const m = x[i];
6+
const value = object[key];
7+
8+
if (isArray(object)) {}
9+
10+
const isObject = isObject(value);
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {isArray} = Array;
2+
3+
export const c = () => {
4+
const b = isArray(a) ? [] : {};
5+
const m = x[i];
6+
const value = object[key];
7+
8+
if (isArray(object)) {}
9+
10+
const isObject = value instanceof Object;
11+
};

packages/plugin-types/lib/convert-typeof-to-is-type/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {operator} from 'putout';
22

3-
const {compare, getBindingPath} = operator;
3+
const {
4+
compare,
5+
getBindingPath,
6+
extract,
7+
} = operator;
48

59
const NAMES = {
610
function: 'isFn',
@@ -10,6 +14,7 @@ const NAMES = {
1014
undefined: 'isUndefined',
1115
symbol: 'isSymbol',
1216
bigint: 'isBigInt',
17+
Object: 'isObject',
1318
};
1419

1520
const EQUAL = 'typeof __a === "__b"';
@@ -53,10 +58,11 @@ export const report = () => `Use function to check type instead of 'typeof' or '
5358
export const match = () => ({
5459
[EQUAL]: check,
5560
[NOT_EQUAL]: check,
56-
[EQUAL_INSTANCE_OF]: check,
61+
5762
[NOT_EQUAL_INSTANCE_OF]: check,
5863
[EQUAL_UNDEFINED]: check,
5964
[NOT_EQUAL_UNDEFINED]: check,
65+
[EQUAL_INSTANCE_OF]: check,
6066
});
6167

6268
export const replace = () => ({
@@ -88,7 +94,7 @@ function check({__a, __b}, path) {
8894
if (path.parentPath.isFunction())
8995
return false;
9096

91-
if (__b && isBind(path, __b.value))
97+
if (__b && isBind(path, extract(__b)))
9298
return false;
9399

94100
return getBindingPath(path, __a);
@@ -103,7 +109,13 @@ function isBind(path, name) {
103109
return false;
104110

105111
if (bindingPath.isVariableDeclarator()) {
106-
const {body} = bindingPath.node.init;
112+
const initPath = bindingPath.get('init');
113+
114+
if (!initPath.isFunction())
115+
return true;
116+
117+
const {body} = initPath.node;
118+
107119
return !compare(body, fnBody);
108120
}
109121

packages/plugin-types/lib/convert-typeof-to-is-type/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createTest} from '@putout/test';
2+
import * as declareBeforeReference from '@putout/plugin-declare-before-reference';
23
import * as convertTypeofToIsType from './index.js';
34

45
const test = createTest(import.meta.url, {
@@ -81,3 +82,10 @@ test('plugin-convert-typeof-to-is-type: no transform: not-declared', (t) => {
8182
t.noTransform('not-declared');
8283
t.end();
8384
});
85+
86+
test('plugin-convert-typeof-to-is-type: no report: declare-before-reference', (t) => {
87+
t.noReport('declare-before-reference', [
88+
['declare-before-reference', declareBeforeReference],
89+
]);
90+
t.end();
91+
});

packages/plugin-types/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
],
3333
"devDependencies": {
3434
"@putout/eslint-flat": "^3.0.0",
35+
"@putout/plugin-declare-before-reference": "*",
3536
"@putout/plugin-putout": "*",
3637
"@putout/test": "^14.0.0",
3738
"c8": "^10.0.0",

0 commit comments

Comments
 (0)