Skip to content

Commit d9b8c9a

Browse files
committed
feature: @putout/plugin-extract-keywords-from-variables: export
1 parent 1d21cae commit d9b8c9a

File tree

10 files changed

+76
-9
lines changed

10 files changed

+76
-9
lines changed

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,50 @@
22

33
const {types, operator} = require('putout');
44
const {
5+
isArrowFunctionExpression,
6+
isLiteral,
57
IfStatement,
68
ImportDefaultSpecifier,
79
ImportDeclaration,
810
ExportNamedDeclaration,
9-
isLiteral,
1011
VariableDeclarator,
1112
VariableDeclaration,
1213
isAssignmentExpression,
14+
isExportNamedDeclaration,
1315
} = types;
1416

1517
const {
1618
remove,
1719
replaceWith,
1820
isDeclarationKeyword,
1921
isConditionKeyword,
22+
isModuleDeclarationKeyword,
2023
} = operator;
2124

2225
const buildDeclaration = (type) => (nextPath, path) => {
2326
const {expression} = nextPath.node;
27+
let left;
28+
let right;
2429

2530
if (isAssignmentExpression(expression)) {
26-
const {left, right} = expression;
27-
replaceWith(nextPath, VariableDeclaration(type, [VariableDeclarator(left, right)]));
31+
({
32+
left,
33+
right,
34+
} = expression);
2835
} else {
29-
replaceWith(nextPath, VariableDeclaration(type, [VariableDeclarator(path.node.id, nextPath.node.expression)]));
36+
left = path.node.id;
37+
right = nextPath.node.expression;
3038
}
39+
40+
replaceWith(nextPath, VariableDeclaration(type, [VariableDeclarator(left, right)]));
41+
42+
const {name} = path.node.id;
43+
44+
if (isDeclarationKeyword(name))
45+
return;
46+
47+
if (isExportNamedDeclaration(path.parentPath.parentPath))
48+
replaceWith(nextPath, ExportNamedDeclaration(nextPath.node));
3149
};
3250

3351
const builders = {
@@ -50,7 +68,7 @@ module.exports.traverse = ({push}) => ({
5068
VariableDeclarator(path) {
5169
const {name} = path.node.id;
5270

53-
if (isDeclarationKeyword(name) || isConditionKeyword(name)) {
71+
if (isDeclarationKeyword(name) || isConditionKeyword(name) || isModuleDeclarationKeyword(name)) {
5472
const topPath = getTopPath(path);
5573
const nextPath = topPath.getNextSibling();
5674

@@ -73,13 +91,18 @@ module.exports.traverse = ({push}) => ({
7391

7492
const {kind} = path.parentPath.node;
7593

76-
if (kind === 'const' && !path.node.init) {
94+
if (!path.node.init) {
7795
const topPath = getTopPath(path);
7896
const nextPath = topPath.getNextSibling();
7997

80-
if (nextPath.isExpressionStatement() && isLiteral(nextPath.node.expression))
81-
return push({
82-
name: 'const',
98+
if (!nextPath.isExpressionStatement())
99+
return;
100+
101+
const {expression} = nextPath.node;
102+
103+
if (isLiteral(expression) || isArrowFunctionExpression(expression))
104+
push({
105+
name: kind,
83106
path,
84107
nextPath,
85108
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ test('putout: extract-keywords-from-variables: transform: if', (t) => {
2929
t.end();
3030
});
3131

32+
test('putout: extract-keywords-from-variables: transform: export', (t) => {
33+
t.transform('export');
34+
t.end();
35+
});
36+
37+
test('putout: extract-keywords-from-variables: transform: export-let', (t) => {
38+
t.transform('export-let');
39+
t.end();
40+
});
41+
42+
test('putout: extract-keywords-from-variables: transform: export-var', (t) => {
43+
t.transform('export-var');
44+
t.end();
45+
});
46+
47+
test('putout: extract-keywords-from-variables: transform: let-no-init', (t) => {
48+
t.transform('let-no-init');
49+
t.end();
50+
});
51+
3252
test('putout: extract-keywords-from-variables: report: const', (t) => {
3353
t.report('const', `Extract 'const' from variable`);
3454
t.end();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '';
2+
export const fix = () => {};
3+
export const include = () => ['JSXElement'];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '';
2+
export const fix = () => {};
3+
export let include = () => ['JSXElement'];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '',
2+
export const fix = () => {};
3+
export let include () => ['JSXElement'];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '';
2+
export const fix = () => {};
3+
export var include = () => ['JSXElement'];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '',
2+
export const fix = () => {};
3+
export var include () => ['JSXElement'];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '',
2+
export const fix = () => {};
3+
export const include () => ['JSXElement'];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '';
2+
export const fix = () => {};
3+
export let include;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const report = () => '',
2+
export const fix = () => {};
3+
export let include;

0 commit comments

Comments
 (0)