Skip to content

Commit 0859c02

Browse files
committed
feature: @putout/plugin-eslint: remove-useless-match-to-flat: add
1 parent aa23cc1 commit 0859c02

File tree

9 files changed

+97
-0
lines changed

9 files changed

+97
-0
lines changed

packages/plugin-eslint/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ npm i @putout/plugin-eslint -D
3838
-[remove-overrides-with-empty-rules](#remove-overrides-with-empty-rules);
3939
-[remove-useless-slice](#remove-useless-slice);
4040
-[remove-useless-properties](#remove-useless-properties);
41+
-[remove-useless-match-to-flat](#remove-useless-match-to-flat);
4142
-[remove-parser-options](#remove-parser-options);
4243
-[remove-suffix-config](#remove-suffix-config);
4344
-[remove-create-eslint-config-with-one-argument](#remove-create-eslint-config-with-one-argument);
@@ -71,6 +72,7 @@ npm i @putout/plugin-eslint -D
7172
"eslint/remove-overrides-with-empty-rules": "on",
7273
"eslint/remove-useless-slice": "on",
7374
"eslint/remove-useless-properties": "on",
75+
"eslint/remove-useless-match-to-flat": "on",
7476
"eslint/remove-parser-options": "on",
7577
"eslint/remove-suffix-config": "on",
7678
"eslint/remove-create-eslint-config-with-one-argument": "on",
@@ -635,6 +637,27 @@ module.exports = [
635637
module.exports = safeAlign;
636638
```
637639
640+
## remove-useless-match-to-flat
641+
642+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/bab49b08d12e779729265c05423bb856/a0b80b8e52f5319c40174773f36d8afbffb1c8b9).
643+
644+
### ❌ Example of incorrect code
645+
646+
```js
647+
import {safeAlign} from 'eslint-plugin-putout';
648+
649+
export let match;
650+
export default createESLintConfig([safeAlign, matchToFlat(match)]);
651+
```
652+
653+
## ✅ Example of correct code
654+
655+
```js
656+
import {safeAlign} from 'eslint-plugin-putout';
657+
658+
export default safeAlign;
659+
```
660+
638661
## convert-export-match-to-declaration
639662
640663
Fixes [apply-match-to-flat](#apply-match-to-flat).

packages/plugin-eslint/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const removeParserOptions = require('./remove-parser-options');
2525
const removeSpreadFromCreateEslintConfig = require('./remove-spread-from-create-eslint-config');
2626
const removeSuffixConfig = require('./remove-suffix-config');
2727
const removeCreateEslintConfigWithOneArgument = require('./remove-create-eslint-config-with-one-argument');
28+
const removeUselessMatchToFlat = require('./remove-useless-match-to-flat');
2829

2930
module.exports.rules = {
3031
'add-putout': addPutout,
@@ -52,4 +53,5 @@ module.exports.rules = {
5253
'remove-spread-from-create-eslint-config': removeSpreadFromCreateEslintConfig,
5354
'remove-suffix-config': removeSuffixConfig,
5455
'remove-create-eslint-config-with-one-argument': removeCreateEslintConfigWithOneArgument,
56+
'remove-useless-match-to-flat': removeUselessMatchToFlat,
5557
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {safeAlign} from 'eslint-plugin-putout/config';
2+
3+
export default createESLintConfig([safeAlign]);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {safeAlign} from 'eslint-plugin-putout/config';
2+
3+
export let match;
4+
export default createESLintConfig([safeAlign, matchToFlat(match)]);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const {operator} = require('putout');
4+
const {compare, remove} = operator;
5+
6+
module.exports.report = () => `Avoid useless 'matchToFlat()'`;
7+
8+
const EXPORT_LET_MATCH = 'export let match';
9+
10+
module.exports.match = () => ({
11+
'matchToFlat(match)': (vars, path) => getExportLetMatch(path),
12+
});
13+
14+
module.exports.replace = () => ({
15+
'matchToFlat(match)': (vars, path) => {
16+
const letMatch = getExportLetMatch(path);
17+
remove(letMatch);
18+
19+
return '';
20+
},
21+
});
22+
23+
function getExportLetMatch(path) {
24+
const programScope = path.scope.getProgramParent();
25+
const programPath = programScope.path;
26+
27+
for (const current of programPath.get('body'))
28+
if (compare(current, EXPORT_LET_MATCH))
29+
return current;
30+
31+
return null;
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['remove-useless-match-to-flat', plugin],
9+
],
10+
});
11+
12+
test('eslint: remove-useless-match-to-flat: report', (t) => {
13+
t.report('remove-useless-match-to-flat', `Avoid useless 'matchToFlat()'`);
14+
t.end();
15+
});
16+
17+
test('eslint: remove-useless-match-to-flat: transform', (t) => {
18+
t.transform('remove-useless-match-to-flat');
19+
t.end();
20+
});

packages/plugin-eslint/test/eslint.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ test('plugin-eslint: transform: remove-create-eslint-config-with-one-argument',
138138
t.transform('remove-create-eslint-config-with-one-argument');
139139
t.end();
140140
});
141+
142+
test('plugin-eslint: transform: remove-useless-match-to-flat', (t) => {
143+
t.transform('remove-useless-match-to-flat');
144+
t.end();
145+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {safeAlign} from 'eslint-plugin-putout';
2+
import {createESLintConfig} from '@putout/eslint-flat';
3+
4+
export default safeAlign;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {safeAlign} from 'eslint-plugin-putout/config';
2+
3+
export let match;
4+
export default createESLintConfig([safeAlign, matchToFlat(match)]);

0 commit comments

Comments
 (0)