Skip to content

Commit 9f8c82e

Browse files
committed
feature: @putout/plugin-exptract-keywrods-from-variables: ArrowFunctionExpression
1 parent d2e034a commit 9f8c82e

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

packages/plugin-extract-keywords-from-variables/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export const isTemplateTail = (a) => a?.type === 'TemplateTail';
3232

3333
-const a 5;
3434
+const a = 5;
35+
36+
-export const packContent = (content) {
37+
+export const packContent = (content) => {
38+
console.log(a);
39+
}
3540
```
3641

3742
## License

packages/plugin-extract-keywords-from-variables/lib/extract-keywords-from-variables.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const {types, operator} = require('putout');
44
const {
5+
isExportDeclaration,
6+
ArrowFunctionExpression,
57
isArrowFunctionExpression,
68
isLiteral,
79
IfStatement,
@@ -14,9 +16,11 @@ const {
1416
isExportNamedDeclaration,
1517
isIdentifier,
1618
isMemberExpression,
19+
isBlockStatement,
1720
} = types;
1821

1922
const {
23+
removeParens,
2024
remove,
2125
replaceWith,
2226
isDeclarationKeyword,
@@ -31,7 +35,13 @@ const buildDeclaration = (type) => (nextPath, path) => {
3135
let left;
3236
let right;
3337

34-
if (isAssignmentExpression(expression)) {
38+
if (isBlockStatement(nextPath)) {
39+
left = path.node.id;
40+
const {node: init} = removeParens(path.get('init'));
41+
const params = [init];
42+
43+
right = ArrowFunctionExpression(params, nextPath.node);
44+
} else if (isAssignmentExpression(expression)) {
3545
({
3646
left,
3747
right,
@@ -110,6 +120,19 @@ module.exports.traverse = ({push}) => ({
110120
path,
111121
nextPath,
112122
});
123+
124+
return;
125+
}
126+
127+
if (isExportDeclaration(path.parentPath.parentPath)) {
128+
const nextPath = path.parentPath.parentPath.getNextSibling();
129+
130+
if (isBlockStatement(nextPath))
131+
push({
132+
name: kind,
133+
path,
134+
nextPath,
135+
});
113136
}
114137
},
115138
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
const a = (x) => x + 1;
22
fn(a);
3+
4+
export const packContent = (content) => {
5+
console.log(a);
6+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
const a (x) => x + 1;
22
fn(a);
3+
4+
export const packContent = (content) {
5+
console.log(a);
6+
}

0 commit comments

Comments
 (0)