Skip to content

Commit 1afaf73

Browse files
committed
feature: @putout/plugin-extract-keywords-from-variables: add
1 parent 1c69fa8 commit 1afaf73

File tree

16 files changed

+268
-0
lines changed

16 files changed

+268
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,7 @@ It has a lot of plugins divided by groups:
22682268
|--------|-------|
22692269
| [`@putout/plugin-extract-sequence-expressions`](/packages/plugin-extract-sequence-expressions#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-extract-sequence-expressions.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-extract-sequence-expressions) |
22702270
| [`@putout/plugin-extract-object-properties`](/packages/plugin-extract-object-properties#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-extract-object-properties.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-extract-object-properties) |
2271+
| [`@putout/plugin-extract-keywords-from-variables`](/packages/plugin-extract-keywords-from-variables#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-extract-keywords-from-variables.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-extract-keywords-from-variables) |
22712272

22722273
### Reusers
22732274

docs/syntax-errors.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,17 @@ a && (b = a);
284284
```
285285

286286
</details>
287+
288+
<details><summary>extract keywords from variables</summary>
289+
290+
```diff
291+
-export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle',
292+
+export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle';
293+
export const isTemplateTail = (a) => a?.type === 'TemplateTail';
294+
295+
-export const a = 1,
296+
+export const a = 1;
297+
const b = 5;
298+
```
299+
300+
</details>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": [
3+
"plugin:n/recommended",
4+
"plugin:putout/recommended"
5+
],
6+
"plugins": [
7+
"putout",
8+
"n"
9+
]
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
*.swp
3+
yarn-error.log
4+
5+
coverage
6+
.idea
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {run} from 'madrun';
2+
3+
export default {
4+
'test': () => `tape 'test/*.js'`,
5+
'watch:test': async () => `nodemon -w lib -w test -x ${await run('test')}`,
6+
'lint': () => `putout .`,
7+
'fresh:lint': () => run('lint', '--fresh'),
8+
'lint:fresh': () => run('lint', '--fresh'),
9+
'fix:lint': () => run('lint', '--fix'),
10+
'coverage': async () => `c8 ${await run('test')}`,
11+
'report': () => 'c8 report --reporter=lcov',
12+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.*
2+
test
3+
yarn-error.log
4+
5+
coverage
6+
*.config.*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"check-coverage": true,
3+
"all": true,
4+
"exclude": [
5+
"**/*.spec.*",
6+
"**/fixture",
7+
"test",
8+
".*.*",
9+
"**/*.config.*"
10+
],
11+
"branches": 100,
12+
"lines": 100,
13+
"functions": 100,
14+
"statements": 100
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) coderaiser
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# @putout/plugin-extract-keywords-from-variables [![NPM version][NPMIMGURL]][NPMURL]
2+
3+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-extract-keywords-from-variables.svg?style=flat&longCache=true
4+
[NPMURL]: https://npmjs.org/package/@putout/plugin-extract-keywords-from-variables "npm"
5+
6+
> The JavaScript exceptions "unexpected token" occur when the parser does not see a token it recognizes at the given position, so it cannot make sense of the structure of the program. This might be a simple typo.
7+
>
8+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token)
9+
10+
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to extract `keywords` from variables. Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/785d072fc20d0a3854f6ced093918b06/483b359357ebc04232f4a321bd3db627dc6a15cb).
11+
12+
## Install
13+
14+
```
15+
npm i @putout/plugin-extract-keywords-from-variables -D
16+
```
17+
18+
## Rule
19+
20+
```json
21+
{
22+
"rules": {
23+
"extract-keywords-from-variables": "on"
24+
}
25+
}
26+
```
27+
28+
```diff
29+
-export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle',
30+
+export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle';
31+
export const isTemplateTail = (a) => a?.type === 'TemplateTail';
32+
```
33+
34+
## License
35+
36+
MIT
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const {types, operator} = require('putout');
4+
const {remove, replaceWith} = operator;
5+
const {
6+
VariableDeclarator,
7+
VariableDeclaration,
8+
ExportNamedDeclaration,
9+
} = types;
10+
11+
const keywords = [
12+
'export',
13+
'const',
14+
];
15+
16+
module.exports.report = () => `Extract 'export' from variable`;
17+
18+
module.exports.fix = ({path, keywordPath}) => {
19+
if (keywordPath.node.id.name === 'export')
20+
replaceWith(path, ExportNamedDeclaration(path.node));
21+
22+
if (keywordPath.node.id.name === 'const')
23+
replaceWith(path, VariableDeclaration('const', [VariableDeclarator(path.node.expression.left, path.node.expression.right)]));
24+
25+
remove(keywordPath);
26+
};
27+
28+
module.exports.traverse = ({push}) => ({
29+
VariableDeclarator(path) {
30+
if (!keywords.includes(path.node.id.name))
31+
return;
32+
33+
const topPath = getTopPath(path);
34+
const nextPath = topPath.getNextSibling();
35+
36+
if (nextPath.isVariableDeclaration())
37+
return push({
38+
path: nextPath,
39+
keywordPath: path,
40+
});
41+
42+
if (nextPath.isExpressionStatement() && nextPath.get('expression').isAssignmentExpression())
43+
return push({
44+
path: nextPath,
45+
keywordPath: path,
46+
});
47+
},
48+
});
49+
50+
function getTopPath(path) {
51+
if (path.parentPath.parentPath.isExportDeclaration())
52+
return path.parentPath.parentPath;
53+
54+
return path.parentPath;
55+
}

0 commit comments

Comments
 (0)