Skip to content

Commit f6ae237

Browse files
committed
feature: @putout/plugin-eslint: remove-parser-options: add
1 parent a905dc1 commit f6ae237

File tree

11 files changed

+155
-4
lines changed

11 files changed

+155
-4
lines changed

packages/plugin-eslint/README.md

Lines changed: 38 additions & 4 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-parser-options](#remove-parser-options);
4142

4243
## Config
4344

@@ -52,21 +53,22 @@ npm i @putout/plugin-eslint -D
5253
}],
5354
"eslint/apply-safe-align": "on",
5455
"eslint/apply-match-to-flat": "on",
56+
"eslint/declare": "on",
5557
"eslint/move-putout-to-end-of-extends": "on",
5658
"eslint/convert-export-match-to-decleration": "on",
5759
"eslint/convert-files-to-array": "on",
5860
"eslint/convert-ide-to-safe": "on",
5961
"eslint/convert-require-to-import": "on",
6062
"eslint/convert-node-to-n": "on",
61-
"eslint/declare": "on",
63+
"eslint/convert-plugins-array-to-object": "on",
64+
"eslint/convert-rc-to-flat": "off",
6265
"eslint/remove-no-missing": "on",
6366
"eslint/remove-no-unpublished-require": "on",
6467
"eslint/remove-no-unsupported-features": "on",
6568
"eslint/remove-overrides-with-empty-rules": "on",
6669
"eslint/remove-useless-slice": "on",
6770
"eslint/remove-useless-properties": "on",
68-
"eslint/convert-plugins-array-to-object": "on",
69-
"eslint/convert-rc-to-flat": "off"
71+
"eslint/remove-parser-options": "on"
7072
}
7173
}
7274
```
@@ -458,6 +460,38 @@ And ofcourse remove only elements with empty `rules`:
458460
};
459461
```
460462
463+
## remove-parser-options
464+
465+
> In flat config files, the `globals`, and `parserOptions` are consolidated under the `languageOptions` key;
466+
>
467+
> (c) [eslint.org](https://eslint.org/docs/latest/use/configure/migration-guide#configuring-language-options)
468+
469+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d6b1cf2681ec6829f11aa2a5f0b60538/199de7e426b1ebeb7f0b242778dfce3fd345b2ae)
470+
471+
### ❌ Example of incorrect code
472+
473+
```js
474+
const ruleTester = new RuleTester({
475+
languageOptions: {
476+
parserOptions: {
477+
ecmaVersion: 2025,
478+
sourceType: 'module',
479+
},
480+
},
481+
});
482+
```
483+
484+
### ✅ Example of correct code
485+
486+
```js
487+
const ruleTester = new RuleTester({
488+
languageOptions: {
489+
ecmaVersion: 2025,
490+
sourceType: 'module',
491+
},
492+
});
493+
```
494+
461495
## convert-node-to-n
462496
463497
`eslint-plugin-node` [is no longer supported](https://github.com/mysticatea/eslint-plugin-node/issues/300). Better to use [`eslint-plugin-n`](https://github.com/weiran-zsd/eslint-plugin-node).
@@ -516,7 +550,7 @@ export default x.slice();
516550
module.exports = x.slice();
517551
```
518552
519-
## ✅ Example of correct code
553+
### ✅ Example of correct code
520554
521555
```js
522556
export default x;

packages/plugin-eslint/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const convertPluginsArrayToObject = require('./convert-plugins-array-to-object')
2121
const removeUselessProperties = require('./remove-useless-properties');
2222
const applyIgnores = require('./apply-ignores');
2323
const applyCreateEslintConfig = require('./apply-create-eslint-config');
24+
const removeParserOptions = require('./remove-parser-options');
2425

2526
module.exports.rules = {
2627
'add-putout': addPutout,
@@ -44,4 +45,5 @@ module.exports.rules = {
4445
'remove-useless-properties': removeUselessProperties,
4546
'apply-ignores': ['off', applyIgnores],
4647
'apply-create-eslint-config': applyCreateEslintConfig,
48+
'remove-parser-options': removeParserOptions,
4749
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = createESLintConfig([
2+
safeAlign, {
3+
files: ['*.md{js}'],
4+
languageOptions: {
5+
parserOptions: {
6+
babelOptions: {
7+
sourceType: 'script',
8+
},
9+
},
10+
},
11+
},
12+
]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
parserOptions: {
3+
ecmaVersion: 2025,
4+
sourceType: 'module',
5+
},
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const ruleTester = new RuleTester({
2+
languageOptions: {
3+
ecmaVersion: 2025,
4+
sourceType: 'module',
5+
},
6+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const ruleTester = new RuleTester({
2+
languageOptions: {
3+
parserOptions: {
4+
ecmaVersion: 2025,
5+
sourceType: 'module',
6+
},
7+
},
8+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const {operator} = require('putout');
4+
const {
5+
traverseProperties,
6+
replaceWith,
7+
} = operator;
8+
9+
module.exports.report = () => `Avoid "parserOptions" in FlatConfig`;
10+
11+
module.exports.fix = (path) => {
12+
replaceWith(path.parentPath, path.node.value);
13+
};
14+
15+
module.exports.traverse = ({push}) => ({
16+
__object(path) {
17+
const [languageOptionsPath] = traverseProperties(path, 'languageOptions');
18+
19+
if (!languageOptionsPath)
20+
return;
21+
22+
const [parserOptionsPath] = traverseProperties(languageOptionsPath, 'parserOptions');
23+
24+
if (!parserOptionsPath)
25+
return;
26+
27+
const [babelOptionsPath] = traverseProperties(parserOptionsPath, 'babelOptions');
28+
29+
if (babelOptionsPath)
30+
return;
31+
32+
push(parserOptionsPath);
33+
},
34+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['remove-parser-options', plugin],
9+
],
10+
});
11+
12+
test('eslint: remove-parser-options: report', (t) => {
13+
t.report('remove-parser-options', `Avoid "parserOptions" in FlatConfig`);
14+
t.end();
15+
});
16+
17+
test('eslint: remove-parser-options: transform', (t) => {
18+
t.transform('remove-parser-options');
19+
t.end();
20+
});
21+
22+
test('eslint: remove-parser-options: no report: no-language-options', (t) => {
23+
t.noReport('no-language-options');
24+
t.end();
25+
});
26+
27+
test('eslint: remove-parser-options: no report: babel-options', (t) => {
28+
t.noReport('babel-options');
29+
t.end();
30+
});

packages/plugin-eslint/test/eslint.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,8 @@ test('plugin-eslint: transform: apply-create-eslint-config', (t) => {
118118
t.transform('apply-create-eslint-config');
119119
t.end();
120120
});
121+
122+
test('plugin-eslint: transform: remove-parser-options', (t) => {
123+
t.transform('remove-parser-options');
124+
t.end();
125+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const ruleTester = new RuleTester({
2+
languageOptions: {
3+
ecmaVersion: 2025,
4+
sourceType: 'module',
5+
},
6+
});

0 commit comments

Comments
 (0)