Skip to content

Commit d730980

Browse files
committed
feature(@putout/plugin-madrun) convert-run-to-cut-env: add ability to convert body to array
1 parent fd2a2f0 commit d730980

4 files changed

Lines changed: 48 additions & 18 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
'test': () => [env, 'npm test'],
3+
'coverage': async () => [env, `c8 ${await cutEnv('test')}`],
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
'test': () => [env, 'npm test'],
3+
'coverage': async () => `c8 ${await run('test')}`,
4+
}
Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

33
const {types, operator} = require('putout');
4-
const {isArrayExpression} = types;
4+
const {
5+
isArrayExpression,
6+
ArrayExpression,
7+
} = types;
58
const {
69
getExportDefault,
710
getProperty,
@@ -10,25 +13,39 @@ const {
1013
module.exports.report = () => `Use 'cutEnv()' instead of 'run()'`;
1114

1215
module.exports.replace = () => ({
13-
'run(__a)': 'cutEnv(__a)',
14-
});
15-
16-
module.exports.match = () => ({
17-
'run(__a)': ({__a}, path) => {
18-
const exportDefault = getExportDefault(path);
19-
20-
if (!exportDefault)
21-
return false;
22-
23-
const declarationPath = exportDefault.get('declaration');
24-
const property = getProperty(declarationPath, __a.value);
16+
'run(__a)': (vars, path) => {
17+
const {body} = path.scope.block;
2518

26-
if (!property)
27-
return false;
19+
if (!isArrayExpression(body)) {
20+
const tuple = getTuple(vars, path);
21+
path.scope.block.body = ArrayExpression([tuple.elements[0], body]);
22+
}
2823

29-
const {body} = property.node.value;
30-
31-
return isArrayExpression(body);
24+
return 'cutEnv(__a)';
3225
},
3326
});
3427

28+
module.exports.match = () => ({
29+
'run(__a)': getTuple,
30+
});
31+
32+
function getTuple({__a}, path) {
33+
const exportDefault = getExportDefault(path);
34+
35+
if (!exportDefault)
36+
return null;
37+
38+
const declarationPath = exportDefault.get('declaration');
39+
const property = getProperty(declarationPath, __a.value);
40+
41+
if (!property)
42+
return null;
43+
44+
const {body} = property.node.value;
45+
46+
if (!isArrayExpression(body))
47+
return null;
48+
49+
return body;
50+
}
51+

packages/plugin-madrun/lib/convert-run-to-cut-env/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ test('madrun: convert-run-to-cut-env: transform', (t) => {
1717
t.end();
1818
});
1919

20+
test('madrun: convert-run-to-cut-env: transform: no-env', (t) => {
21+
t.transform('no-env');
22+
t.end();
23+
});
24+
2025
test('madrun: convert-run-to-cut-env: no transform: no export default', (t) => {
2126
t.noTransformCode(`export const hello = 'world'; run('hello')`);
2227
t.end();

0 commit comments

Comments
 (0)