Skip to content

Commit c948d5f

Browse files
committed
feature: @putout/plugin-putout: apply-exports: add
1 parent 3a0c527 commit c948d5f

File tree

17 files changed

+197
-34
lines changed

17 files changed

+197
-34
lines changed

packages/plugin-putout/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ npm i @putout/plugin-putout -D
2525
-[apply-create-test](#apply-create-test);
2626
-[apply-create-nested-directory](#apply-create-nested-directory);
2727
-[apply-declare](#apply-declare);
28+
-[apply-exports](#apply-exports);
2829
-[apply-exports-to-add-args](#apply-exports-to-add-args);
2930
-[apply-exports-to-match-files](#apply-exports-to-match-files);
3031
-[apply-exports-to-rename-files](#apply-exports-to-rename-files);
@@ -99,6 +100,7 @@ npm i @putout/plugin-putout -D
99100
"putout/apply-create-nested-directory": "on",
100101
"putout/apply-async-formatter": "on",
101102
"putout/apply-declare": "on",
103+
"putout/apply-exports": "on",
102104
"putout/apply-exports-to-add-args": "on",
103105
"putout/apply-exports-to-match-files": "on",
104106
"putout/apply-exports-to-rename-files": "on",
@@ -462,6 +464,53 @@ module.exports.declare = () => ({
462464
});
463465
```
464466
467+
## apply-exports
468+
469+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/cf35de5e80e8f7aad866358a50c5eded/0af6142fc9c9e71ac2a2aa96cb85613dd95c9fbf).
470+
Possible configuration:
471+
472+
```json
473+
{
474+
"rules": {
475+
"putout/apply-exports": {
476+
"addArgs": [
477+
"report",
478+
"fix",
479+
"scan"
480+
]
481+
}
482+
}
483+
}
484+
```
485+
486+
### ❌ Example of incorrect code
487+
488+
```js
489+
export default createRenameProperty([
490+
...v32,
491+
...v29,
492+
]);
493+
```
494+
495+
### ✅ Example of correct code
496+
497+
```js
498+
const {
499+
report,
500+
fix,
501+
scan,
502+
} = createRenameProperty([
503+
...v32,
504+
...v29,
505+
]);
506+
507+
export {
508+
report,
509+
fix,
510+
scan,
511+
};
512+
```
513+
465514
## apply-exports-to-add-args
466515
467516
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/cf35de5e80e8f7aad866358a50c5eded/0af6142fc9c9e71ac2a2aa96cb85613dd95c9fbf).

packages/plugin-putout/lib/apply-exports-to-add-args/fixture/apply-exports-to-add-args-fix.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
report,
44
fix,
55
traverse,
6-
} = addArgs(__args);
6+
} = addArgs({});
77
export {
88
report,
99
fix,
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default addArgs(__args)
1+
export default addArgs({})
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
export const report = () => `Apply exports to 'addArgs()'`;
1+
import {createApplyExports} from '../create-apply-exports.js';
22

3-
export const replace = () => ({
4-
'export default addArgs(__args)': `{
5-
const {report, fix, traverse} = addArgs(__args);
6-
export {
7-
report,
8-
fix,
9-
traverse,
10-
}
11-
}`,
3+
const {report, replace} = createApplyExports({
4+
addArgs: [
5+
'report',
6+
'fix',
7+
'traverse',
8+
],
129
});
10+
11+
export {
12+
report,
13+
replace,
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
export const report = () => `Apply 'exports' to 'matchFiles()'`;
1+
import {createApplyExports} from '../create-apply-exports.js';
22

3-
export const replace = () => ({
4-
'export default matchFiles(__args)': `{
5-
const {report, fix, scan} = matchFiles(__args);
6-
export {
7-
report,
8-
fix,
9-
scan,
10-
};
11-
}`,
3+
const {report, replace} = createApplyExports({
4+
matchFiles: [
5+
'report',
6+
'fix',
7+
'scan',
8+
],
129
});
10+
11+
export {
12+
report,
13+
replace,
14+
};

packages/plugin-putout/lib/apply-exports-to-match-files/index.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const test = createTest(import.meta.url, {
88
});
99

1010
test('putout: apply-exports-to-match-files: report', (t) => {
11-
t.report('apply-exports-to-match-files', `Apply 'exports' to 'matchFiles()'`);
11+
t.report('apply-exports-to-match-files', `Apply exports to 'matchFiles()'`);
1212
t.end();
1313
});
1414

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
export const report = () => `Apply 'exports' to 'renameFiles()`;
1+
import {createApplyExports} from '../create-apply-exports.js';
22

3-
export const replace = () => ({
4-
'export default renameFiles(__args)': `{
5-
const {report, fix, scan} = renameFiles(__args);
6-
export {
7-
report,
8-
fix,
9-
scan,
10-
};
11-
}`,
3+
const {report, replace} = createApplyExports({
4+
renameFiles: [
5+
'report',
6+
'fix',
7+
'scan',
8+
],
129
});
10+
11+
export {
12+
report,
13+
replace,
14+
};

packages/plugin-putout/lib/apply-exports-to-rename-files/index.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const test = createTest(import.meta.url, {
88
});
99

1010
test('putout: apply-exports-to-rename-files: report', (t) => {
11-
t.report('apply-exports-to-rename-files', `Apply 'exports' to 'renameFiles()`);
11+
t.report('apply-exports-to-rename-files', `Apply exports to 'renameFiles()'`);
1212
t.end();
1313
});
1414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
const {
3+
report,
4+
fix,
5+
scan,
6+
} = createRenameProperty([
7+
...v32,
8+
...v29,
9+
]);
10+
export {
11+
report,
12+
fix,
13+
scan,
14+
};
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default createRenameProperty([
2+
...v32,
3+
...v29,
4+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {createApplyExports} from '../create-apply-exports.js';
2+
3+
const {report, replace} = createApplyExports();
4+
5+
export {
6+
report,
7+
replace,
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['apply-exports', plugin],
7+
],
8+
});
9+
10+
test('putout: apply-exports: report', (t) => {
11+
t.reportWithOptions('apply-exports', `Apply exports to 'createRenameProperty()'`, {
12+
createRenameProperty: [
13+
'report',
14+
'fix',
15+
'scan',
16+
],
17+
});
18+
t.end();
19+
});
20+
21+
test('putout: apply-exports: transform', (t) => {
22+
t.transformWithOptions('apply-exports', {
23+
createRenameProperty: [
24+
'report',
25+
'fix',
26+
'scan',
27+
],
28+
});
29+
t.end();
30+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const {entries} = Object;
2+
3+
const report = (path) => {
4+
const namePath = path.get('declaration.callee');
5+
const {name} = namePath.node;
6+
7+
return `Apply exports to '${name}()'`;
8+
};
9+
10+
export const createApplyExports = (defaultOptions = {}) => {
11+
return {
12+
report,
13+
replace: createReplace(defaultOptions),
14+
};
15+
};
16+
17+
export const createReplace = (defaultOptions) => ({options}) => {
18+
const result = {};
19+
const all = {
20+
...defaultOptions,
21+
...options,
22+
};
23+
24+
for (const [name, exports] of entries(all)) {
25+
const from = `export default ${name}(__args)`;
26+
27+
result[from] = `{
28+
const {${exports.join(', ')}} = ${name}(__args);
29+
export {
30+
${exports.join(',\n')}
31+
}
32+
}`;
33+
}
34+
35+
return result;
36+
};

packages/plugin-putout/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as applyExports from './apply-exports/index.js';
12
import * as applyExportsToRenameFiles from './apply-exports-to-rename-files/index.js';
23
import * as applyExportsToMatchFiles from './apply-exports-to-match-files/index.js';
34
import * as applyProcessorsDestructuring from './apply-processors-destructuring/index.js';
@@ -136,4 +137,5 @@ export const rules = {
136137
'apply-report': applyReport,
137138
'apply-exports-to-match-files': applyExportsToMatchFiles,
138139
'apply-exports-to-rename-files': applyExportsToRenameFiles,
140+
'apply-exports': applyExports,
139141
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default createRenameProperty([
2+
...v32,
3+
...v29,
4+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default createRenameProperty([
2+
...v32,
3+
...v29,
4+
]);

packages/plugin-putout/test/putout.js

+5
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,8 @@ test('plugin-putout: transform: apply-exports-to-rename-files', (t) => {
334334
t.transform('apply-exports-to-rename-files');
335335
t.end();
336336
});
337+
338+
test('plugin-putout: transform: apply-exports', (t) => {
339+
t.transform('apply-exports');
340+
t.end();
341+
});

0 commit comments

Comments
 (0)