Skip to content

Commit f8ae94a

Browse files
committed
feature: @putout/operator-regexp: types
1 parent 08a620e commit f8ae94a

7 files changed

Lines changed: 62 additions & 12 deletions

File tree

packages/operator-regexp/.madrun.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {run} from 'madrun';
22

33
export default {
4-
'wisdom': () => run(['lint', 'coverage']),
4+
'wisdom': () => run(['lint', 'coverage', 'test:dts']),
55
'test': () => `tape 'test/*.js' 'lib/**/*.spec.js'`,
6+
'test:dts': () => 'check-dts test/*.ts',
67
'watch:test': async () => `nodemon -w lib -x "${await run('test')}"`,
78
'lint': () => 'putout .',
89
'fresh:lint': () => run('lint', '--fresh'),

packages/operator-regexp/.nycrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"exclude": [
55
"**/*.spec.*",
66
"**/*.config.*",
7+
"**/*.ts",
78
"**/fixture",
89
".*.*",
910
"test"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function isSimpleRegExp(regexp: RegExp): boolean;
2+
3+
export function getStringFromRegExp(node: {
4+
pattern: string;
5+
}): string;
6+
7+
type RegExpTransformer = {
8+
report: (path?: unknown) => string;
9+
traverse: (api: {
10+
push: (path?: unknown) => void;
11+
}) => Record<string, (path?: unknown) => void>;
12+
fix: (path?: unknown) => void;
13+
};
14+
15+
export function transformRegExp(str: string, regExpTransformer: RegExpTransformer): [string, {
16+
message: string;
17+
position: {
18+
column: number;
19+
offset: number;
20+
line: number;
21+
};
22+
}[]];
23+

packages/operator-regexp/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"url": "git+https://github.com/coderaiser/putout.git"
1515
},
1616
"scripts": {
17+
"wisdom": "madrun wisdom",
1718
"test": "madrun test",
19+
"test:dts": "madrun test:dts",
1820
"watch:test": "madrun watch:test",
1921
"lint": "madrun lint",
2022
"fresh:lint": "madrun fresh:lint",
@@ -35,6 +37,7 @@
3537
],
3638
"devDependencies": {
3739
"@putout/test": "^15.0.0",
40+
"check-dts": "^1.0.0",
3841
"eslint": "^10.0.0",
3942
"eslint-plugin-putout": "^31.0.0",
4043
"madrun": "^13.0.0",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
isSimpleRegExp,
3+
getStringFromRegExp,
4+
transformRegExp,
5+
} from '../lib/regexp.js';
6+
7+
const noop = () => {};
8+
// THROWS Argument of type 'number' is not assignable to parameter of type 'RegExp'
9+
isSimpleRegExp(5);
10+
// THROWS Expected 1 arguments, but got 0
11+
isSimpleRegExp();
12+
// THROWS Argument of type 'number' is not assignable to parameter of type '{ pattern: string; }'
13+
getStringFromRegExp(5);
14+
// THROWS Expected 1 arguments, but got 0
15+
getStringFromRegExp();
16+
// THROWS Expected 2 arguments, but got 0
17+
transformRegExp();
18+
// THROWS Argument of type 'number' is not assignable to parameter of type 'string'
19+
transformRegExp(5, {
20+
report: () => '',
21+
fix: noop,
22+
traverse: ({push}) => ({
23+
RegExp(path) {
24+
push(path);
25+
},
26+
}),
27+
});

packages/putout/test/operator.errors.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,34 @@ const {
1212
findFile,
1313
matchFiles,
1414
findFileUp,
15+
isSimpleRegExp,
1516
} = operator;
1617

1718
// THROWS Expected 2 arguments, but got 1.
1819
replaceWith(1);
19-
2020
// THROWS Expected 2-4 arguments, but got 1.
2121
compare('hello');
22-
2322
// THROWS Expected 2 arguments, but got 1.
2423
traverse(1);
25-
2624
// THROWS Argument of type 'number' is not assignable to parameter of type 'string'
2725
toJS(5);
28-
2926
// THROWS Expected 2 arguments, but got 1
3027
hasTagName(5);
31-
3228
// THROWS Expected 2 arguments, but got 1.
3329
replaceWith(1);
34-
3530
// THROWS Argument of type 'number' is not assignable to parameter of type 'AddArgsOptions'
3631
addArgs(5);
37-
3832
// THROWS Argument of type 'number' is not assignable to parameter of type 'DeclareOptions'
3933
declare(5);
40-
4134
// THROWS Argument of type 'number' is not assignable to parameter of type 'string'
4235
isKeyword(5);
43-
4436
// THROWS Argument of type 'number' is not assignable to parameter of type 'NodePath_Final'.
4537
findFile(5, 'hello');
46-
4738
// THROWS Expected 1 arguments, but got 2.
4839
matchFiles(5, 'hello');
49-
5040
// THROWS Expected 2 arguments, but got 1
5141
findFileUp(5);
42+
// THROWS Argument of type 'number' is not assignable to parameter of type 'RegExp'
43+
isSimpleRegExp(5);
44+
// THROWS Expected 1 arguments, but got 0
45+
isSimpleRegExp();

packages/putout/types/operator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export * from '@putout/operator-ignore';
99
export * from '@putout/operator-add-args';
1010
export * from '@putout/operator-filesystem';
1111
export * from '@putout/operator-match-files';
12+
export * from '@putout/operator-regexp';
1213
export * from '@putout/operator-find-file-up';
1314

0 commit comments

Comments
 (0)